Add unit tests for unsorted flag

This commit is contained in:
Michael 2022-02-20 17:25:35 +00:00 committed by Abin Simon
parent 1e578bae4d
commit 60c7c4ac19
2 changed files with 64 additions and 0 deletions

View file

@ -266,6 +266,16 @@ mod test_sort_column {
); );
} }
#[test]
fn test_from_arg_matches_unsorted() {
let argv = vec!["lsd", "--unsorted"];
let matches = app::build().get_matches_from_safe(argv).unwrap();
assert_eq!(
Some(SortColumn::None),
SortColumn::from_arg_matches(&matches)
);
}
#[test] #[test]
fn test_from_arg_matches_sort() { fn test_from_arg_matches_sort() {
let argv = vec!["lsd", "--sort", "time"]; let argv = vec!["lsd", "--sort", "time"];
@ -295,6 +305,13 @@ mod test_sort_column {
Some(SortColumn::Version), Some(SortColumn::Version),
SortColumn::from_arg_matches(&matches) SortColumn::from_arg_matches(&matches)
); );
let argv = vec!["lsd", "--sort", "none"];
let matches = app::build().get_matches_from_safe(argv).unwrap();
assert_eq!(
Some(SortColumn::None),
SortColumn::from_arg_matches(&matches)
);
} }
#[test] #[test]

View file

@ -296,4 +296,51 @@ mod tests {
let sorter = assemble_sorters(&flags); let sorter = assemble_sorters(&flags);
assert_eq!(by_meta(&sorter, &meta_b, &meta_c), Ordering::Less); assert_eq!(by_meta(&sorter, &meta_b, &meta_c), Ordering::Less);
} }
#[test]
fn test_sort_assemble_sorters_unsorted() {
let tmp_dir = tempdir().expect("failed to create temp dir");
// Create the file with rs extension;
let path_a = tmp_dir.path().join("aaa.aa");
File::create(&path_a).expect("failed to create file");
let meta_a = Meta::from_path(&path_a, false).expect("failed to get meta");
// Create the file with rs extension;
let path_b = tmp_dir.path().join("aaa");
create_dir(&path_b).expect("failed to create dir");
let meta_b = Meta::from_path(&path_b, false).expect("failed to get meta");
// Create the file with js extension;
let path_c = tmp_dir.path().join("zzz.zz");
File::create(&path_c).expect("failed to create file");
let meta_c = Meta::from_path(&path_c, false).expect("failed to get meta");
// Create the file with txt extension;
let path_d = tmp_dir.path().join("zzz");
create_dir(&path_d).expect("failed to create dir");
let meta_d = Meta::from_path(&path_d, false).expect("failed to get meta");
let mut flags = Flags::default();
flags.sorting.column = SortColumn::None;
// Sort by extension
let sorter = assemble_sorters(&flags);
assert_eq!(by_meta(&sorter, &meta_a, &meta_b), Ordering::Equal);
let sorter = assemble_sorters(&flags);
assert_eq!(by_meta(&sorter, &meta_a, &meta_c), Ordering::Equal);
let sorter = assemble_sorters(&flags);
assert_eq!(by_meta(&sorter, &meta_a, &meta_d), Ordering::Equal);
let sorter = assemble_sorters(&flags);
assert_eq!(by_meta(&sorter, &meta_b, &meta_c), Ordering::Equal);
let sorter = assemble_sorters(&flags);
assert_eq!(by_meta(&sorter, &meta_b, &meta_d), Ordering::Equal);
let sorter = assemble_sorters(&flags);
assert_eq!(by_meta(&sorter, &meta_c, &meta_d), Ordering::Equal);
}
} }