# Tests of reading globs and matching text against them: each wildcard, sets, # `**`, braces, escapes, hidden names, case, roots, directories, the mistakes a # glob can hold, and `escape`. import std::io::glob; import std::testing; # Whether a glob matches text, read with the options, which by default tell # cases apart on every system. def fits(text: strview, path: strview, settings: glob::options = glob::options { folds_case = false }): bool { val made := glob::pattern::parse(text, settings) else (_) { return false; }; return made.matches(path); } # Whether reading a glob fails with a kind of mistake, at a byte. def fails_with(text: strview, kind: glob::error_kind, offset: u64): bool { match glob::pattern::parse(text) { value(&_) => return false; error(e) => return e.kind == kind && e.offset == offset; } return false; } @test def a_star_matches_any_run_within_a_name(t: &mut tester): void { t.expects(fits("*.c", "a.c") && fits("*.c", "abc.c") && fits("*", "x")); t.expects(!fits("*.c", "a.h") && !fits("*.c", "dir/a.c")); t.expects(fits("a*", "a") && fits("*a*b*c", "xaybzc") && fits("*a*b*c", "abc")); t.expects(!fits("*a*b*c", "acb") && !fits("a*b", "a/b")); # Stars take back what they took, but only the last seen needs to. t.expects(!fits("a*a*a*a*b", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); t.expects(fits("*?", "a") && !fits("*?", "")); # More than two stars, or two within a name, are one. t.expects(fits("a***b", "axyzb") && fits("a**b", "axyzb") && !fits("a**b", "ax/yb")); } @test def a_question_mark_matches_one_character(t: &mut tester): void { t.expects(fits("?.c", "a.c") && !fits("?.c", "ab.c") && !fits("?.c", ".c")); # A character is a code point, of however many bytes. t.expects(fits("?", "é") && fits("?", "日") && fits("?", "😀") && !fits("??", "é")); t.expects(fits("a?c", "a日c") && fits("*?本", "日本")); # A star takes whole characters, never half of one. t.expects(!fits("*\xA9", "é") && !fits("*\xA9", "\xC3\xC3\xA9") && fits("*\xA9", "a\xA9")); # A byte that starts none is one of its own. t.expects(fits("?", "\xFF") && fits("a?b", "a\xFFb") && !fits("?", "\xFF\xFF")); } @test def a_set_matches_one_character_of_its_own(t: &mut tester): void { t.expects(fits("[abc].c", "b.c") && !fits("[abc].c", "d.c")); t.expects(fits("[a-c]x", "bx") && !fits("[a-c]x", "dx")); t.expects(fits("[!a-c]x", "dx") && !fits("[!a-c]x", "ax") && fits("[^a]x", "bx")); # A `]` first is one, as is a `-` first or last. t.expects(fits("[]]x", "]x") && fits("[!]]x", "ax") && !fits("[!]]x", "]x")); t.expects(fits("[a-]x", "-x") && fits("[-a]x", "-x") && !fits("[a-]x", "bx")); # Escapes and characters of more than a byte. t.expects(fits("[\\]]", "]") && fits("[\\!a]", "!")); t.expects(fits("[é-ë]", "ê") && !fits("[é-ë]", "e") && fits("[日本]", "本")); # A set never matches the `.` starting a hidden name. t.expects(!fits("[.]x", ".x") && fits("[.]x", ".x", glob::options { hidden = true, folds_case = false })); } @test def two_stars_match_any_number_of_names(t: &mut tester): void { t.expects(fits("src/**/*.sakura", "src/a.sakura") && fits("src/**/*.sakura", "src/a/b/c.sakura")); t.expects(!fits("src/**/*.sakura", "lib/a.sakura") && !fits("src/**/*.sakura", "src/.git/a.sakura")); t.expects(fits("**", "a") && fits("**", "a/b/c") && fits("**/x", "x") && fits("**/x", "a/b/x")); t.expects(fits("a/**", "a") && fits("a/**", "a/b/c") && !fits("a/**", "b")); t.expects(fits("a/**/b/**/c", "a/b/c") && fits("a/**/b/**/c", "a/x/b/y/b/z/c") && !fits("a/**/b/**/c", "a/c")); t.expects(fits("**/**/x", "x") && fits("**/**/x", "a/b/x")); # `.` alone, or a root alone, is matched only by a glob naming it. t.expects(!fits("**", ".") && !fits("**", "") && fits(".", ".") && fits("/", "/") && !fits("/**", "/")); } @test def braces_make_globs_of_their_own(t: &mut tester): void { t.expects(fits("{src,test}/*.sakura", "src/a.sakura") && fits("{src,test}/*.sakura", "test/a.sakura")); t.expects(!fits("{src,test}/*.sakura", "lib/a.sakura")); t.expects(fits("a{b,{c,d}}e", "abe") && fits("a{b,{c,d}}e", "ace") && fits("a{b,{c,d}}e", "ade")); t.expects(fits("{a,b}{c,d}", "bd") && fits("{,x}y", "y") && fits("{,x}y", "xy")); t.expects(fits("{a/b,c}/d", "a/b/d") && fits("{a/b,c}/d", "c/d") && !fits("{a/b,c}/d", "a/d")); t.expects(fits("x{a}", "xa") && fits("{[,]}", ",") && fits("{a\\,b,c}", "a,b")); } @test def escapes_match_what_they_escape(t: &mut tester): void { t.expects(fits("\\*", "*") && !fits("\\*", "a") && fits("a\\?b", "a?b") && !fits("a\\?b", "axb")); t.expects(fits("\\{a,b\\}", "{a,b}") && fits("\\[a]", "[a]") && fits("a\\\\b", "a\\b")); # An escaped `/` still separates names. t.expects(fits("a\\/b", "a/b")); } @test def hidden_names_are_matched_only_by_a_dot(t: &mut tester): void { val hidden := glob::options { hidden = true, folds_case = false }; t.expects(fits(".*", ".git") && !fits("*", ".git") && fits(".git/*", ".git/config")); t.expects(!fits("**/x", ".a/x") && fits("**/x", ".a/x", hidden) && fits("*", ".git", hidden)); # Neither `.` nor `..` is ever a wildcard's. t.expects(!fits("*", "..", hidden) && !fits("?", ".", hidden) && !fits("**/x", "../x", hidden)); t.expects(fits("..", "..") && fits("../*.c", "../a.c")); } @test def letters_match_in_either_case_where_asked(t: &mut tester): void { val folded := glob::options { folds_case = true }; t.expects(!fits("*.TXT", "a.txt") && fits("*.TXT", "a.txt", folded)); t.expects(fits("[A-C]", "b", folded) && fits("[!a-c]", "D", folded) && !fits("[!a-c]", "B", folded)); t.expects(fits("SRC/*", "src/a", folded) && !fits("SRC/*", "src/a")); t.expects(fits("É", "É", folded) && !fits("é", "É", folded)); } @test def roots_and_directories_are_matched_too(t: &mut tester): void { t.expects(fits("/usr/*", "/usr/lib") && !fits("/usr/*", "usr/lib") && !fits("*", "/usr")); t.expects(fits("*/", "src/") && !fits("*/", "src") && fits("src", "src/")); t.expects(fits("a//b", "a/b") && fits("./a/*", "a/x") && fits("a/*", "a/./x") && fits("a/b", "a//b")); val made := glob::pattern::parse("*/") else (_) { return; }; t.expects(made.directories && made.text == "*/"); } @test def a_glob_without_wildcards_is_literal(t: &mut tester): void { for text in ["src/main.sakura", "a\\*b", "/usr/lib", "."] { val made := glob::pattern::parse(text) else (_) { t.expects(false); continue; }; t.expects(made.is_literal); } for text in ["src/*.c", "{a,b}", "a?", "[ab]", "**"] { val made := glob::pattern::parse(text) else (_) { t.expects(false); continue; }; t.expects(!made.is_literal); } } @test def mistakes_are_found_where_they_are(t: &mut tester): void { t.expects(fails_with("", glob::error_kind::empty, 0)); t.expects(fails_with("a\\", glob::error_kind::trailing_escape, 1)); t.expects(fails_with("x[ab", glob::error_kind::unclosed_set, 1)); t.expects(fails_with("[]", glob::error_kind::unclosed_set, 0)); t.expects(fails_with("{a,{b}", glob::error_kind::unclosed_brace, 0)); t.expects(fails_with("a}", glob::error_kind::unopened_brace, 1)); t.expects(fails_with("{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}", glob::error_kind::too_many_choices, 0)); t.expects(!fails_with("{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}", glob::error_kind::too_many_choices, 0)); # A brace or escape within a set is the set's. t.expects(fits("[{]", "{") && fits("[}]", "}")); match glob::pattern::parse("a}") { value(&_) => t.expects(false); error(e) => t.expects(e.describe() == "A `}` has no `{` opening it. (at byte 1)"); } } @test def escaped_text_matches_only_itself(t: &mut tester): void { val escaped := glob::escape("notes[1].txt"); t.expects(escaped == "notes\\[1\\].txt"); for text in ["notes[1].txt", "a*b?c", "{x,y}", "back\\slash", "]!^-"] { val made := glob::pattern::parse(glob::escape(text)) else (_) { t.expects(false); continue; }; t.expects(made.is_literal); when os == windows { continue; } t.expects(made.matches(text)); } }