test: sort order of the folder views
A small library target (src/lib.rs) exposes the pure item and sort modules to tests/sort.rs: natural names, case ties, look names, each key in both directions, folders first and group buckets. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
parent
42aa599066
commit
95365b7754
2 changed files with 180 additions and 0 deletions
9
crates/tunal/src/lib.rs
Normal file
9
crates/tunal/src/lib.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
//! The pure parts of the app crate as a library, for the integration tests in `tests/`.
|
||||
//! The binary (`main.rs`) builds these same files as its own modules.
|
||||
|
||||
pub mod icons;
|
||||
|
||||
pub mod folder {
|
||||
pub mod item;
|
||||
pub mod sort;
|
||||
}
|
||||
171
crates/tunal/tests/sort.rs
Normal file
171
crates/tunal/tests/sort.rs
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
//! Row order of the folder views: natural names, each sort key, both directions, folders
|
||||
//! first, and the group buckets.
|
||||
|
||||
use std::cmp::Ordering;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
use tunal::folder::item::{Item, Look};
|
||||
use tunal::folder::sort::{self, GroupBy, Order, SortKey};
|
||||
use tunal_core::{Entry, Kind, mime};
|
||||
|
||||
fn item(name: &str, dir: bool, size: u64, age_days: u64) -> Item {
|
||||
let kind = if dir { Kind::Dir } else { Kind::File };
|
||||
Item::new(Entry {
|
||||
name: name.as_bytes().to_vec(),
|
||||
display_name: name.to_owned(),
|
||||
mime: mime::mime_type(name.as_bytes(), &kind),
|
||||
kind,
|
||||
size,
|
||||
mtime: Some(SystemTime::now() - Duration::from_secs(age_days * 86_400)),
|
||||
mode: 0o644,
|
||||
uid: 0,
|
||||
gid: 0,
|
||||
hidden: name.starts_with('.'),
|
||||
})
|
||||
}
|
||||
|
||||
fn order(key: SortKey, descending: bool, folders_first: bool) -> Order {
|
||||
Order {
|
||||
key,
|
||||
descending,
|
||||
folders_first,
|
||||
}
|
||||
}
|
||||
|
||||
/// Names of `items` sorted under `order`.
|
||||
fn sorted(items: &[Item], order: Order) -> Vec<&str> {
|
||||
let mut refs: Vec<&Item> = items.iter().collect();
|
||||
refs.sort_by(|a, b| sort::compare(a, b, &order, None));
|
||||
refs.iter().map(|i| i.entry.display_name.as_str()).collect()
|
||||
}
|
||||
|
||||
fn sample() -> Vec<Item> {
|
||||
vec![
|
||||
item("b.txt", false, 300, 1),
|
||||
item("Docs", true, 0, 9),
|
||||
item("a10.png", false, 100, 3),
|
||||
item("a2.mp3", false, 900, 40),
|
||||
item("apps", true, 0, 2),
|
||||
]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn natural_order_reads_digit_runs_as_numbers() {
|
||||
assert_eq!(sort::natural_cmp("file2", "file10"), Ordering::Less);
|
||||
assert_eq!(sort::natural_cmp("file10", "file2"), Ordering::Greater);
|
||||
assert_eq!(sort::natural_cmp("a", "a1"), Ordering::Less);
|
||||
assert_eq!(sort::natural_cmp("x9y", "x10y"), Ordering::Less);
|
||||
// Same value: fewer leading zeros first.
|
||||
assert_eq!(sort::natural_cmp("v7", "v007"), Ordering::Less);
|
||||
assert_eq!(sort::natural_cmp("same", "same"), Ordering::Equal);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn names_ignore_case_then_break_ties_exactly() {
|
||||
let items = vec![
|
||||
item("beta", false, 0, 0),
|
||||
item("Alpha", false, 0, 0),
|
||||
item("alpha", false, 0, 0),
|
||||
item("Beta2", false, 0, 0),
|
||||
];
|
||||
// Lower-cased natural order; equal keys fall back to the exact names (uppercase first).
|
||||
assert_eq!(
|
||||
sorted(&items, order(SortKey::Name, false, false)),
|
||||
["Alpha", "alpha", "beta", "Beta2"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_look_name_sorts_in_place_of_the_real_one() {
|
||||
let renamed = item("zzz", true, 0, 0).with_look(Some(Look {
|
||||
name: Some("Aardvark".into()),
|
||||
..Look::default()
|
||||
}));
|
||||
let items = vec![item("mmm", true, 0, 0), renamed];
|
||||
assert_eq!(
|
||||
sorted(&items, order(SortKey::Name, false, false)),
|
||||
["zzz", "mmm"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn name_ascending_and_descending_with_folders_first() {
|
||||
let items = sample();
|
||||
assert_eq!(
|
||||
sorted(&items, order(SortKey::Name, false, true)),
|
||||
["apps", "Docs", "a2.mp3", "a10.png", "b.txt"]
|
||||
);
|
||||
// Descending reverses the names but folders stay first.
|
||||
assert_eq!(
|
||||
sorted(&items, order(SortKey::Name, true, true)),
|
||||
["Docs", "apps", "b.txt", "a10.png", "a2.mp3"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn without_folders_first_folders_mix_in() {
|
||||
let items = sample();
|
||||
assert_eq!(
|
||||
sorted(&items, order(SortKey::Name, false, false)),
|
||||
["a2.mp3", "a10.png", "apps", "b.txt", "Docs"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn size_counts_folders_as_empty() {
|
||||
let items = sample();
|
||||
assert_eq!(
|
||||
sorted(&items, order(SortKey::Size, false, true)),
|
||||
["apps", "Docs", "a10.png", "b.txt", "a2.mp3"]
|
||||
);
|
||||
assert_eq!(
|
||||
sorted(&items, order(SortKey::Size, true, false)),
|
||||
["a2.mp3", "b.txt", "a10.png", "Docs", "apps"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn modified_oldest_first_and_newest_first() {
|
||||
let items = sample();
|
||||
assert_eq!(
|
||||
sorted(&items, order(SortKey::Modified, false, false)),
|
||||
["a2.mp3", "Docs", "a10.png", "apps", "b.txt"]
|
||||
);
|
||||
assert_eq!(
|
||||
sorted(&items, order(SortKey::Modified, true, true)),
|
||||
["apps", "Docs", "b.txt", "a10.png", "a2.mp3"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn type_orders_by_category_then_name() {
|
||||
let items = sample();
|
||||
// Folders, images, audio, text: the category order.
|
||||
assert_eq!(
|
||||
sorted(&items, order(SortKey::Type, false, false)),
|
||||
["apps", "Docs", "a10.png", "a2.mp3", "b.txt"]
|
||||
);
|
||||
assert_eq!(
|
||||
sorted(&items, order(SortKey::Type, true, false)),
|
||||
["b.txt", "a2.mp3", "a10.png", "Docs", "apps"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn groups_put_folders_and_sizes_in_buckets() {
|
||||
let calendar = sort::Calendar::now();
|
||||
let folder = item("Docs", true, 0, 0);
|
||||
let empty = item("e.txt", false, 0, 0);
|
||||
let big = item("big.iso", false, 2 << 30, 0);
|
||||
assert_eq!(
|
||||
sort::group(&folder, GroupBy::Size, &calendar),
|
||||
(0, "Folders")
|
||||
);
|
||||
assert_eq!(sort::group(&empty, GroupBy::Size, &calendar).1, "Empty");
|
||||
assert_eq!(
|
||||
sort::group(&big, GroupBy::Size, &calendar).1,
|
||||
"Huge (1 GiB and more)"
|
||||
);
|
||||
assert_eq!(sort::group(&empty, GroupBy::Modified, &calendar).1, "Today");
|
||||
assert_eq!(sort::group(&big, GroupBy::None, &calendar), (0, ""));
|
||||
}
|
||||
Loading…
Reference in a new issue