rcom/tests/file_filter.rs

144 lines
4.5 KiB
Rust

use std::ffi::OsString;
use rcom::domain::{Entry, EntryKind, ListingSnapshot, fold_query, matches_folded};
fn file(name: impl Into<OsString>) -> Entry {
Entry::new(name, EntryKind::File)
}
fn names(entries: &[Entry]) -> Vec<String> {
entries
.iter()
.map(|entry| entry.name.to_string_lossy().into_owned())
.collect()
}
#[test]
fn snapshot_keeps_the_complete_canonical_order_and_parallel_folded_names() {
let snapshot = ListingSnapshot::new(vec![
file("zeta"),
Entry::new("Beta", EntryKind::Directory),
file("ALPHA"),
file(".hidden"),
]);
assert_eq!(
names(&snapshot.entries),
["Beta", ".hidden", "ALPHA", "zeta"]
);
assert_eq!(snapshot.folded_names, ["beta", ".hidden", "alpha", "zeta"]);
assert_eq!(snapshot.entries.len(), snapshot.folded_names.len());
assert_eq!(snapshot, snapshot.clone());
}
#[test]
fn projection_applies_hidden_policy_before_the_query() {
let snapshot = ListingSnapshot::new(vec![
file("notes"),
file(".readme-secret"),
file("README.md"),
Entry::new("readme-dir", EntryKind::Directory),
file(".hidden"),
]);
let without_hidden = snapshot.project(false, &fold_query("README"));
assert_eq!(
names(&without_hidden.eligible),
["readme-dir", "notes", "README.md"]
);
assert_eq!(names(&without_hidden.visible), ["readme-dir", "README.md"]);
let with_hidden = snapshot.project(true, &fold_query("readme"));
assert_eq!(
names(&with_hidden.visible),
["readme-dir", ".readme-secret", "README.md"]
);
let no_matches = snapshot.project(false, &fold_query("absent"));
assert_eq!(no_matches.eligible, without_hidden.eligible);
assert!(no_matches.visible.is_empty());
}
#[test]
fn empty_query_makes_visible_equal_to_eligible() {
let snapshot = ListingSnapshot::new(vec![file("visible"), file(".hidden")]);
for show_hidden in [false, true] {
let projection = snapshot.project(show_hidden, "");
assert_eq!(projection.visible, projection.eligible);
}
assert!(matches_folded("anything", ""));
}
#[test]
fn query_is_a_literal_substring_not_a_regular_expression() {
let snapshot = ListingSnapshot::new(vec![
file("report[1].txt"),
file("report1.txt"),
file("literal.*.txt"),
]);
let character_class = snapshot.project(true, &fold_query("[1]."));
assert_eq!(names(&character_class.visible), ["report[1].txt"]);
let repetition = snapshot.project(true, &fold_query(".*"));
assert_eq!(names(&repetition.visible), ["literal.*.txt"]);
}
#[test]
fn unicode_lowercase_does_not_promise_full_case_folding() {
let snapshot =
ListingSnapshot::new(vec![file("Straße"), file("STRASSE"), file("Σ"), file("ς")]);
let sharp_s = snapshot.project(true, &fold_query("SS"));
assert_eq!(names(&sharp_s.visible), ["STRASSE"]);
assert_eq!(fold_query("Straße"), "straße");
let capital_sigma = snapshot.project(true, &fold_query("Σ"));
assert_eq!(names(&capital_sigma.visible), ["Σ"]);
let final_sigma = snapshot.project(true, &fold_query("ς"));
assert_eq!(names(&final_sigma.visible), ["ς"]);
}
#[cfg(unix)]
#[test]
fn non_utf8_names_match_lossy_display_text_without_changing_the_entry() {
use std::os::unix::ffi::{OsStrExt, OsStringExt};
let raw = OsString::from_vec(vec![b'F', 0x80, b'O', b'.', b'T', b'X', b'T']);
let snapshot = ListingSnapshot::new(vec![file(raw.clone())]);
assert_eq!(snapshot.folded_names, ["f\u{fffd}o.txt"]);
let projection = snapshot.project(true, &fold_query("F\u{fffd}O"));
assert_eq!(projection.visible.len(), 1);
assert_eq!(
snapshot.entries[0].name.as_os_str().as_bytes(),
raw.as_bytes()
);
assert_eq!(
projection.visible[0].name.as_os_str().as_bytes(),
raw.as_bytes()
);
}
#[test]
fn twenty_thousand_entries_support_repeated_precomputed_matching() {
let entries = (0..20_000)
.map(|index| file(format!("Item-{index:05}-suffix")))
.collect();
let snapshot = ListingSnapshot::new(entries);
let folded_names = snapshot.folded_names.clone();
let query = fold_query("ITEM-0199");
let expected = (1_990..=1_999)
.map(|index| format!("Item-{index:05}-suffix"))
.collect::<Vec<_>>();
for _ in 0..16 {
let projection = snapshot.project(true, &query);
assert_eq!(projection.eligible.len(), 20_000);
assert_eq!(names(&projection.visible), expected);
}
assert_eq!(snapshot.folded_names, folded_names);
}