-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprefix_test.go
87 lines (79 loc) · 1.76 KB
/
prefix_test.go
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
// nolint: gocritic
package fileglob
import (
"testing"
"github.com/gobwas/glob/syntax/ast"
"github.com/gobwas/glob/syntax/lexer"
"github.com/matryer/is"
)
func TestStaticPrefix(t *testing.T) {
t.Parallel()
testCases := []struct {
pattern string
prefix string
}{
{"/foo/b*ar/baz", "/foo"},
{"foo/bar", "foo/bar"},
{"/foo/bar/{b,p}az", "/foo/bar"},
{"*/foo", "."},
{"./", "."},
{"fo\\*o/bar/b*z", "fo*o/bar"},
{"/\\{foo\\}/bar", "/{foo}/bar"},
{"C:/Path/To/Some/File", "C:/Path/To/Some/File"},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.pattern, func(t *testing.T) {
t.Parallel()
is := is.New(t)
prefix, err := staticPrefix(testCase.pattern)
is.NoErr(err)
is.Equal(testCase.prefix, prefix)
})
}
}
func TestContainsMatchers(t *testing.T) {
t.Parallel()
testCases := []struct {
pattern string
containsMatchers bool
}{
{"/a/*/b", true},
{"\\{a\\}/\\*/", false},
{"a/b/c", false},
{"", false},
{"\\*/\\?", false},
{"*/\\?", true},
{"\\{*\\}", true},
{"{a,b}/c", true},
{"\\{\\\\[a-z]", true},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.pattern, func(t *testing.T) {
t.Parallel()
is := is.New(t)
_, err := ast.Parse(lexer.NewLexer(testCase.pattern))
is.NoErr(err)
is.Equal(testCase.containsMatchers, ContainsMatchers(testCase.pattern))
})
}
}
func TestValidPattern(t *testing.T) {
t.Parallel()
testCases := []struct {
pattern string
valid bool
}{
{"/a/*/b", true},
{"{a[", false},
{"[*]", true},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.pattern, func(t *testing.T) {
t.Parallel()
is.New(t).Equal(testCase.valid, ValidPattern(testCase.pattern) == nil)
})
}
}