-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatcher_test.go
125 lines (116 loc) · 2.98 KB
/
matcher_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2015, Ben Morgan. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package matcher
import (
"path/filepath"
"testing"
)
func TestMatch(fw *testing.T) {
type io struct {
Path string
Match bool
}
type test struct {
Pattern string
IO []io
}
tests := []test{
test{"", []io{
{"", false},
{"/", false},
{"/home", false},
{"local", false},
}},
test{" \t", []io{
{"", false},
{"/", false},
{"/home", false},
{"local", false},
}},
test{"foo", []io{
{"foo", true},
{"bar/foo", true},
{"foobar", false},
}},
test{"bar/*", []io{
{"foo", false},
{"bar/foo", true},
{"bar", false},
}},
test{"Documentation/*.html", []io{
{"Documentation/git.html", true},
{"Documentation/xyz/git.html", false},
{"tools/Documentation/perf.html", false},
}},
}
for _, t := range tests {
for _, p := range t.IO {
if m := match(t.Pattern, p.Path); m != p.Match {
fw.Errorf("match(%q, %q) = %v, expected %v", t.Pattern, p.Path, m, p.Match)
}
}
}
}
func TestMatcher(fw *testing.T) {
// Inside tests directory
var tests = map[string]bool{
"bar": true,
"brain": false,
"brain/bar": false,
"brain/foo": true,
"brain/yahoo": false,
"dead": false,
"dead/bad": false,
"dead/bad/shubarf": true,
"dead/bad/somefoo": true,
"dead/good": false,
"dead/good/1": false,
"dead/good/2": true,
"dead/good/3": true,
"dead/good/4": true,
"dead/good/5": true,
"dead/good/6": false,
"dead/good/7": false,
"dead/good/8": false,
"dead/good/9": false,
"dead/good/cache": false,
"dead/good/hit": false,
"dead/good/match.conf": true,
"dead/good/yala": false,
"dead/match.conf": true,
"dead/never": false,
"dead/ok": true,
"dead/somewhere": false,
"dead/ugly": false,
"dead/ugly/bar": true,
"dead/ugly/foo": true,
"dead/ugly/foobar": true,
"foo": true,
"jack": false,
"lucy": false,
"match.conf": true,
}
m := New("match.conf")
m.ErrHandler = func(err error) error {
fw.Fatalf("Error: %s", err)
return nil
}
err := m.Add("match.conf")
if err != nil {
fw.Fatalf("Adding glob %q failed: %s", "match.conf", err)
}
for k, v := range tests {
b := filepath.Base(k)
w, err := m.NewWorker(filepath.Join("tests", filepath.Dir(k)))
if err != nil {
fw.Fatalf("Creating new Worker failed: %s", err)
}
if u := w.Matches(b); u != v {
path, _ := filepath.Abs(filepath.Join("tests", filepath.Dir(k)))
fw.Errorf("w.Matches(%q) = %v, expected %v", b, u, v)
fw.Errorf(" Variables (dir,file) = (%q,%q)", path, b)
fw.Errorf(" Available globs are:\n%s\n%s", w.global, w.local)
}
}
}