-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher_test.go
198 lines (168 loc) · 4.5 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package matcher
import (
"fmt"
"os"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestMain(m *testing.M) {
RegisterDefaults()
code := m.Run()
os.Exit(code)
}
func TestErrorHandling(t *testing.T) {
Convey("Given a non-struct", t, func() {
v := "invalid"
Convey("When a match is attempted, an error is returned", func() {
clauses := []*Clause{&Clause{Field: "foo", Operator: "eq", Value: "bar"}}
_, err := Matches(v, clauses)
So(err, ShouldBeError)
})
})
Convey("Given a struct", t, func() {
v := struct {
Name string `matcher:"name"`
Age int `matcher:"age"`
Embedded struct {
Foo int
Bar float64
} `matcher:"embedded"`
}{"Alice", 32, struct {
Foo int
Bar float64
}{32, 3.151459}}
Convey("When a non-existent field is matched against, an error is returned", func() {
clauses := []*Clause{&Clause{Field: "invalid", Operator: "eq", Value: "foo"}}
_, err := Matches(v, clauses)
So(err, ShouldBeError)
})
Convey("When attempting to match with an invalid operator, an error is returned", func() {
clauses := []*Clause{&Clause{Field: "name", Operator: "foo", Value: "foo"}}
_, err := Matches(v, clauses)
So(err, ShouldBeError)
})
Convey("When a clause's value type doesn't match the struct, an error is returned", func() {
clauses := []*Clause{&Clause{Field: "name", Operator: "eq", Value: 3.14159}}
_, err := Matches(v, clauses)
So(err, ShouldBeError)
})
Convey("When a comparison is attempted against an unknown type, an error is returned", func() {
clauses := []*Clause{&Clause{Field: "embedded", Operator: "eq", Value: 3.14159}}
_, err := Matches(v, clauses)
So(err, ShouldBeError)
})
})
}
func TestMatching(t *testing.T) {
Convey("Given a struct", t, func() {
v := struct {
Name string `matcher:"name"`
Age int `matcher:"age"`
Unused bool
Skipped bool `matcher:"-"`
}{"Alice", 32, false, false}
Convey("When no clauses are provided, the struct should match", func() {
clauses := []*Clause{}
matched, err := Matches(v, clauses)
So(err, ShouldBeNil)
So(matched, ShouldBeTrue)
})
Convey("When all clauses match, the struct should match", func() {
clauses := []*Clause{&Clause{Field: "name", Operator: "eq", Value: "Alice"},
&Clause{Field: "age", Operator: "eq", Value: 32}}
matched, err := Matches(v, clauses)
So(err, ShouldBeNil)
So(matched, ShouldBeTrue)
})
Convey("If one clause doesn't match, the struct shouldn't match", func() {
clauses := []*Clause{&Clause{Field: "name", Operator: "eq", Value: "Alice"},
&Clause{Field: "age", Operator: "eq", Value: 40}}
matched, err := Matches(v, clauses)
So(err, ShouldBeNil)
So(matched, ShouldBeFalse)
})
})
}
func TestTypeMatching(t *testing.T) {
type Person struct {
Name string `matcher:"name"`
Age int `matcher:"age"`
Height float64 `matcher:"height"`
Birthday time.Time `matcher:"birthday"`
}
type TypeWrapper struct {
Name string
Field string
Base interface{}
Data map[string]interface{}
}
baseTime, _ := time.Parse(time.RFC3339, "2000-01-01T00:00:00Z")
p := Person{
Name: "foo",
Age: 1,
Height: 1.5,
Birthday: baseTime,
}
operators := []string{"gt", "lt", "eq", "ne"}
types := []*TypeWrapper{
{
Name: "string",
Field: "name",
Base: "foo",
Data: map[string]interface{}{
"gt": "e",
"lt": "g",
"eq": "foo",
"ne": "bar",
},
},
{
Name: "int",
Field: "age",
Base: 1,
Data: map[string]interface{}{
"gt": 0,
"lt": 2,
"eq": 1,
"ne": 3,
},
},
{
Name: "float",
Field: "height",
Base: 1.5,
Data: map[string]interface{}{
"gt": 1.0,
"lt": 2.0,
"eq": 1.5,
"ne": 3.0,
},
},
{
Name: "time",
Field: "birthday",
Base: baseTime,
Data: map[string]interface{}{
"gt": baseTime.Add(time.Duration(-1) * time.Second),
"lt": baseTime.Add(time.Duration(1) * time.Second),
"eq": baseTime,
"ne": baseTime.Add(time.Duration(1) * time.Hour),
},
},
}
Convey("Given a struct", t, func() {
for _, typeTest := range types {
for _, op := range operators {
msg := fmt.Sprintf("When compared, %s %v is %s %v",
typeTest.Name, typeTest.Base, op, typeTest.Data[op])
Convey(msg, func() {
clauses := []*Clause{&Clause{Field: typeTest.Field, Operator: op, Value: typeTest.Data[op]}}
matched, err := Matches(p, clauses)
So(err, ShouldBeNil)
So(matched, ShouldBeTrue)
})
}
}
})
}