Debian Codegraph @

match_test.go

raw symbols SCIP
1 package main
2
3 import (
4 "regexp"
5 "testing"
6 )
7
8 func TestMatchers(t *testing.T) {
9 var (
10 glob = &globMatcher{glob: "foo*"}
11 globInv = &globMatcher{glob: "foo*", inverse: true}
12
13 regex = newRegexMatcher(regexp.MustCompile("foo.*"), false)
14 regexInv = newRegexMatcher(regexp.MustCompile("foo.*"), true)
15
16 multi = multiMatcher{
17 newRegexMatcher(regexp.MustCompile("foo"), false),
18 newRegexMatcher(regexp.MustCompile(`\.go$`), false),
19 newRegexMatcher(regexp.MustCompile("foobar"), true),
20 }
21 )
22 for _, tt := range []struct {
23 m Matcher
24 s string
25 want bool
26 }{
27 {glob, "foo", true},
28 {glob, "foobar", true},
29 {glob, "bar", false},
30 {globInv, "foo", false},
31 {globInv, "foobar", false},
32 {globInv, "bar", true},
33
34 {regex, "foo", true},
35 {regex, "foobar", true},
36 {regex, "bar", false},
37 {regexInv, "foo", false},
38 {regexInv, "foobar", false},
39 {regexInv, "bar", true},
40
41 {multi, "foo.go", true},
42 {multi, "foo/bar.go", true},
43 {multi, "foobar/blah.go", false},
44 } {
45 if got := tt.m.Match(tt.s); got != tt.want {
46 t.Errorf("(%v).Match(%q): got %t; want %t",
47 tt.m, tt.s, got, tt.want)
48 }
49 }
50 }
51
52 func TestExcludePrefix(t *testing.T) {
53 m := newRegexMatcher(regexp.MustCompile("foo"), false)
54 if m.ExcludePrefix("bar") {
55 t.Error("m.ExcludePrefix gave true for a non-inverted matcher")
56 }
57
58 for _, tt := range []struct {
59 re string
60 prefix string
61 want bool
62 }{
63 {"foo", "foo", true},
64 {"((foo{3,4})|abc*)+|foo", "foo", true},
65 {"foo$", "foo", false},
66 {`foo\b`, "foo", false},
67 {`(foo\b)|(baz$)`, "foo", false},
68 } {
69 m := newRegexMatcher(regexp.MustCompile(tt.re), true)
70 if got := m.ExcludePrefix(tt.prefix); got != tt.want {
71 t.Errorf("(%v).ExcludePrefix(%q): got %t; want %t",
72 m, tt.prefix, got, tt.want)
73 }
74 }
75 }
76
77 func TestDefaultExcludes(t *testing.T) {
78 for _, tt := range []struct {
79 name string
80 want bool
81 }{
82 {".git/HEAD", false},
83 {"foo.git", true},
84 {"foo/bar.git", true},
85 {"foo/bar/.git/HEAD", false},
86 {"foo~", false},
87 {"foo/bar~", false},
88 {"~foo", true},
89 {"foo~bar", true},
90 {"foo.swp", false},
91 {"foo.swp.bar", true},
92 {"foo/bar.swp", false},
93 {"foo.#123", false},
94 {"foo#123", true},
95 {"foo/bar.#123", false},
96 {"#foo#", false},
97 {"foo/#bar#", false},
98 {".DS_Store", false},
99 {"foo/.DS_Store", false},
100 } {
101 if got := defaultExcludeMatcher.Match(tt.name); got != tt.want {
102 if got {
103 t.Errorf("%q was excluded by the default excludes matcher", tt.name)
104 } else {
105 t.Errorf("%q was not excluded by the default excludes matcher", tt.name)
106 }
107 }
108 }
109 }