-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmatcher_test.go
67 lines (63 loc) · 2.57 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
package herd
import (
"regexp"
"testing"
)
type testcase struct {
a MatchAttribute
v interface{}
m bool
}
func TestMatchAttribute(t *testing.T) {
testcases := []testcase{
// Basic equality
{a: MatchAttribute{Value: 1}, v: 1, m: true},
{a: MatchAttribute{Value: 1}, v: 0, m: false},
{a: MatchAttribute{Value: "1"}, v: "1", m: true},
{a: MatchAttribute{Value: "1"}, v: "0", m: false},
{a: MatchAttribute{Value: true}, v: true, m: true},
{a: MatchAttribute{Value: true}, v: false, m: false},
{a: MatchAttribute{Value: false}, v: nil, m: false},
// Unequal types
{a: MatchAttribute{Value: "1"}, v: 1, m: false},
{a: MatchAttribute{Value: "true"}, v: true, m: false},
{a: MatchAttribute{Value: false}, v: nil, m: false},
// Integer niceness
{a: MatchAttribute{Value: int64(1)}, v: int(1), m: true},
// String fuzziness
{a: MatchAttribute{Value: "1", FuzzyTyping: true}, v: 1, m: true},
{a: MatchAttribute{Value: "1", FuzzyTyping: true}, v: 0, m: false},
{a: MatchAttribute{Value: "1", FuzzyTyping: true}, v: int32(1), m: true},
{a: MatchAttribute{Value: "1", FuzzyTyping: true}, v: uint16(1), m: true},
{a: MatchAttribute{Value: "true", FuzzyTyping: true}, v: true, m: true},
{a: MatchAttribute{Value: "nil", FuzzyTyping: true}, v: nil, m: true},
// Regular expressions
{a: MatchAttribute{Value: regexp.MustCompile("hello"), Regex: true}, v: "hello", m: true},
{a: MatchAttribute{Value: regexp.MustCompile("hello"), Regex: true}, v: "hello world", m: true},
{a: MatchAttribute{Value: regexp.MustCompile("hello$"), Regex: true}, v: "hello world", m: false},
// Slices
{a: MatchAttribute{Value: 1}, v: []int{2, 3, 1, 4}, m: true},
{a: MatchAttribute{Value: 1}, v: []int{2, 3, 4}, m: false},
{a: MatchAttribute{Value: 1}, v: []int{1}, m: true},
{a: MatchAttribute{Value: 1}, v: []int{}, m: false},
}
for i, c := range testcases {
c.a.Name = "v"
if m := c.a.Match(c.v); m != c.m {
if c.m {
t.Errorf("(%d) expected %v (%T) to match %v (%T), but they did not match", i, c.a, c.a.Value, c.v, c.v)
} else {
t.Errorf("(%d) expected %v (%T) to not match %v (%T), but they did match", i, c.a, c.a.Value, c.v, c.v)
}
}
// Test the negation as well
a := MatchAttribute{Name: "v", FuzzyTyping: c.a.FuzzyTyping, Value: c.a.Value, Regex: c.a.Regex, Negate: !c.a.Negate}
if m := a.Match(c.v); m != !c.m {
if !c.m {
t.Errorf("(%d) expected %v (%T) to match %v (%T), but they did not match", i, a, a.Value, c.v, c.v)
} else {
t.Errorf("(%d) expected %v (%T) to not match %v (%T), but they did match", i, a, a.Value, c.v, c.v)
}
}
}
}