summaryrefslogtreecommitdiff
path: root/helix-term/src/ui/fuzzy_match/test.rs
blob: 3f90ef6813ecec9242d54ff5b9c61a1eea7dc769 (plain) (blame)
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
use crate::ui::fuzzy_match::FuzzyQuery;
use crate::ui::fuzzy_match::Matcher;

fn run_test<'a>(query: &str, items: &'a [&'a str]) -> Vec<String> {
    let query = FuzzyQuery::new(query);
    let matcher = Matcher::default();
    items
        .iter()
        .filter_map(|item| {
            let (_, indicies) = query.fuzzy_indicies(item, &matcher)?;
            let matched_string = indicies
                .iter()
                .map(|&pos| item.chars().nth(pos).unwrap())
                .collect();
            Some(matched_string)
        })
        .collect()
}

#[test]
fn match_single_value() {
    let matches = run_test("foo", &["foobar", "foo", "bar"]);
    assert_eq!(matches, &["foo", "foo"])
}

#[test]
fn match_multiple_values() {
    let matches = run_test(
        "foo bar",
        &["foo bar", "foo   bar", "bar foo", "bar", "foo"],
    );
    assert_eq!(matches, &["foobar", "foobar", "barfoo"])
}

#[test]
fn space_escape() {
    let matches = run_test(r"foo\ bar", &["bar foo", "foo bar", "foobar"]);
    assert_eq!(matches, &["foo bar"])
}

#[test]
fn trim() {
    let matches = run_test(r" foo bar ", &["bar foo", "foo bar", "foobar"]);
    assert_eq!(matches, &["barfoo", "foobar", "foobar"]);
    let matches = run_test(r" foo bar\ ", &["bar foo", "foo bar", "foobar"]);
    assert_eq!(matches, &["bar foo"])
}