-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathladapfilter_test.go
95 lines (84 loc) · 1.59 KB
/
ladapfilter_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
package ldapfilter
import (
"reflect"
"testing"
)
func TestFilterAnd(t *testing.T) {
input := Input{
"foo": {"bar", "baz"},
}
filter := AndFilter{}
filter.Append(&AndFilter{})
res := filter.Match(input)
assert(t, true, res)
}
func TestFilterOr(t *testing.T) {
input := Input{
"foo": {"bar", "baz"},
}
filter := &OrFilter{
Children: []Filter{
&AndFilter{},
&AndFilter{},
},
}
res := filter.Match(input)
assert(t, true, res)
}
func TestFilterEquality(t *testing.T) {
filter := EqualityFilter{
Key: "foo",
Value: "baz",
}
t.Run("should match", func(t *testing.T) {
input := Input{
"foo": {"bar", "baz"},
}
res := filter.Match(input)
assert(t, true, res)
})
t.Run("should not match", func(t *testing.T) {
input := Input{
"foo": {"bar"},
}
res := filter.Match(input)
assert(t, false, res)
})
}
func TestFilter(t *testing.T) {
testData := []struct {
expr string
ok bool
inp Input
}{
{
expr: "(name=Jon)",
inp: Input{"name": {"Jon"}},
ok: true,
},
{
expr: "|(name=Jon)(name=Foo)",
inp: Input{"name": {"Jon"}},
ok: true,
},
{
expr: "|(name=Jon)(alt=Foo)",
inp: Input{"alt": {"Foo"}},
ok: true,
},
}
for _, test := range testData {
t.Run("", func(t *testing.T) {
filter, err := NewParser(test.expr).Parse()
assert(t, nil, err)
assert(t, test.ok, filter.Match(test.inp))
})
}
}
// helper
func assert(t *testing.T, want interface{}, have interface{}) {
t.Helper()
if !reflect.DeepEqual(want, have) {
t.Errorf("Assertion failed for %s\n\twant:\t%+v\n\thave:\t%+v", t.Name(), want, have)
}
}