File: search_strings.rs

package info (click to toggle)
rust-broot 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,928 kB
  • sloc: makefile: 20
file content (108 lines) | stat: -rw-r--r-- 2,872 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! This checks some edge cases of pattern searches, especially composite patterns.
//! Don't hesitate to suggest more tests for clarification or to prevent regressions.
use {
    broot::{
        command::CommandParts,
        pattern::*,
    },
};

fn build_pattern(s: &str) -> Pattern {
    let cp = CommandParts::from(s);
    let search_modes = SearchModeMap::default();
    cp.pattern.print_tree();
    Pattern::new(
        &cp.pattern,
        &search_modes,
        0, // we don't do content search here
    ).unwrap()
}

fn check(
    pattern: &str,
    haystack: &str,
    expected: bool,
) {
    println!("applying pattern {:?} on {:?}", pattern, haystack);
    let pattern = build_pattern(pattern);
    //dbg!(&pattern);
    let found = pattern.search_string(haystack).is_some();
    assert_eq!(found, expected);
}

#[test]
fn simple_fuzzy() {
    check("toto", "toto", true);
    check("toto", "Toto", true);
    check("toto", "ToTuTo", true);
    check("tota", "ToTuTo", false);
}

#[test]
fn simple_exact() {
    check("e/toto", "toto", true);
    check("e/toto", "Toto", false);
    check("e/toto", "Tototo", true);
}

#[test]
fn simple_regex() {
    check("/toto", "toto", true);
    check("/to{3,5}to", "toto", false);
    check("/to{3,5}to", "tooooto", true);
    check("/to{3,5}to", "toooooooto", false);
    check("/to{3,5}to", "tooOoto", false);
    check("/to{3,5}to/i", "tooOoto", true);
}

#[test]
fn one_operator() {
    check("a&b", "a", false);
    check("a&b", "ab", true);
    check("a|b", "ab", true);
    check("a|b", "b", true);
    check("a|b", "c", false);
}

#[test]
fn negation() {
    check("!ab", "a", true);
    check("!ab", "aB", false);
    check("a&!b", "aB", false);
    check("a&!b", "aA", true);
    check("!a&!b", "ccc", true);
    check("!a|!b", "ccc", true);
    check("!a|!b", "cac", true);
    check("!a|!b", "cbc", true);
    check("!a|!b", "cbac", false);
}

// remember: it's left to right
#[test]
fn multiple_operators_no_parenthesis() {
    check("ab|ac|ad", "ab", true);
    check("ab|ac|ad", "ac", true);
    check("ab|ac|ad", "ad", true);
    check("ab|ac|ad|af|ag|er", "ad", true);
    check("ab&ac&ad", "ad", false);
    check("ab&ac&ad", "abcd", true);
    check("ab|ac|ad|ae", "ad", true);
    check("ab|ac|ad&ae", "ad", false);
    check("ab|ac|ad&ae", "axcd", false);
    check("ab|ac|ad&ae", "abe", true);
    check("ab|ac&ad|ae", "abd", true);
    check("ab|ac&ad|ae", "abc", false);
}

#[test]
fn multiple_operators_with_parenthesis() {
    check("ab|(ac|ad)", "ab", true);
    check("(ab|ac)|ad", "ac", true);
    check("ab|(ac|ad)|ae", "ad", true);
    check("ab|ac|(ad&ae)", "ac", true);
    check("ab|ac|(ad&ae)", "ad", false);
    check("(ab|ac)&(ad|ae)", "ad", false);
    check("!(ab|ac)&(ad|ae)", "ad", true);
    check("ab|(ac&ad|ae)", "abc", true);
    check("(ab|ac)&(ad|ae)", "abd", true);
}