summaryrefslogtreecommitdiffstats
path: root/vendor/gix-glob/tests/search/pattern.rs
blob: 0313b55648351c70ae6c6ed9c42f0a5b31c9c1af (plain)
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
mod list {
    use std::path::Path;

    use gix_glob::{
        pattern::Case,
        search::{
            pattern::{List, Mapping},
            Pattern,
        },
    };

    #[derive(Clone, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Default)]
    struct Dummy;

    impl Pattern for Dummy {
        type Value = ();

        fn bytes_to_patterns(_bytes: &[u8], _source: &Path) -> Vec<Mapping<Self::Value>> {
            vec![]
        }

        fn may_use_glob_pattern(_pattern: &gix_glob::Pattern) -> bool {
            unreachable!("won't be called")
        }
    }

    #[test]
    fn from_bytes_base() {
        {
            let list = List::<Dummy>::from_bytes(&[], "a/b/source", None);
            assert_eq!(list.base, None, "no root always means no-base, i.e. globals lists");
            assert_eq!(
                list.source.as_deref(),
                Some(Path::new("a/b/source")),
                "source is verbatim"
            );
        }

        {
            let cwd = std::env::current_dir().expect("cwd available");
            let list = List::<Dummy>::from_bytes(&[], cwd.join("a/b/source"), Some(cwd.as_path()));
            assert_eq!(
                list.base.as_ref().expect("set"),
                "a/b/",
                "bases are always relative, needs properly set root"
            );
            assert_eq!(
                list.source.as_deref(),
                Some(cwd.join("a/b/source").as_path()),
                "source is verbatim"
            );
        }

        {
            let list = List::<Dummy>::from_bytes(&[], "a/b/source", Some(Path::new("c/")));
            assert_eq!(
                list.base, None,
                "if root doesn't contain source, it silently skips it as base"
            );
            assert_eq!(
                list.source.as_deref(),
                Some(Path::new("a/b/source")),
                "source is always verbatim"
            );
        }
    }

    #[test]
    fn strip_base_handle_recompute_basename_pos() {
        let list = List::<Dummy>::from_bytes(&[], "a/b/source", Some(Path::new("")));
        assert_eq!(
            list.base.as_ref().expect("set"),
            "a/b/",
            "bases are computed if `root` is set, and always uses slashes"
        );
        let res = list.strip_base_handle_recompute_basename_pos("a/b/file".into(), Some(4), Case::Sensitive);
        assert_eq!(
            res,
            Some(("file".into(), None)),
            "files don't have a basename position anymore"
        );

        let res = list.strip_base_handle_recompute_basename_pos("a/B/c/File".into(), Some(6), Case::Fold);
        assert_eq!(
            res,
            Some(("c/File".into(), Some(2))),
            "otherwise the basename is recomputed, case folding is effective"
        );
    }
}