Skip to content

Commit

Permalink
Add tests to the extension module
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospb19 committed Mar 7, 2024
1 parent 3ca479c commit a22bd45
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a22bd45

Please sign in to comment.