From a22bd4576ede80c32ab0dda164c9e17c29fb5ccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sun, 17 Sep 2023 18:55:39 -0300 Subject: [PATCH] Add tests to the extension module --- src/extension.rs | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/extension.rs b/src/extension.rs index d602ec7f0..0f7170b81 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -33,7 +33,8 @@ pub const PRETTY_SUPPORTED_EXTENSIONS: &str = "tar, zip, bz, bz2, gz, lz4, xz, l pub const PRETTY_SUPPORTED_ALIASES: &str = "tgz, tbz, tlz4, txz, tzlma, tsz, tzst"; /// A wrapper around `CompressionFormat` that allows combinations like `tgz` -#[derive(Debug, Clone, Eq)] +#[derive(Debug, Clone)] +#[cfg_attr(test, derive(PartialEq))] #[non_exhaustive] pub struct Extension { /// One extension like "tgz" can be made of multiple CompressionFormats ([Tar, Gz]) @@ -42,13 +43,6 @@ pub struct Extension { display_text: String, } -// The display_text should be ignored when comparing extensions -impl PartialEq for Extension { - fn eq(&self, other: &Self) -> bool { - self.compression_formats == other.compression_formats - } -} - impl Extension { /// # Panics: /// Will panic if `formats` is empty @@ -262,6 +256,37 @@ mod tests { assert_eq!(formats, vec![Tar, Gzip]); } + #[test] + fn test_separate_known_extensions_from_name() { + assert_eq!( + separate_known_extensions_from_name("file".as_ref()), + ("file".as_ref(), vec![]) + ); + assert_eq!( + separate_known_extensions_from_name("tar".as_ref()), + ("tar".as_ref(), vec![]) + ); + assert_eq!( + separate_known_extensions_from_name(".tar".as_ref()), + (".tar".as_ref(), vec![]) + ); + assert_eq!( + separate_known_extensions_from_name("file.tar".as_ref()), + ("file".as_ref(), vec![Extension::new(&[Tar], "tar")]) + ); + assert_eq!( + separate_known_extensions_from_name("file.tar.gz".as_ref()), + ( + "file".as_ref(), + vec![Extension::new(&[Tar], "tar"), Extension::new(&[Gzip], "gz")] + ) + ); + assert_eq!( + separate_known_extensions_from_name(".tar.gz".as_ref()), + (".tar".as_ref(), vec![Extension::new(&[Gzip], "gz")]) + ); + } + #[test] fn builds_suggestion_correctly() { assert_eq!(build_archive_file_suggestion(Path::new("linux.png"), ".tar"), None);