-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqualify_test.go
101 lines (82 loc) · 2.01 KB
/
qualify_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
package qualify
import (
"testing"
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
"github.com/bsm/gomega/types"
)
var _ = DescribeTable("Qualifier",
func(fact *mockFact, m types.GomegaMatcher) {
q := seedBuilder().Compile()
Expect(q.Qualify(nil, fact)).To(m)
},
Entry("match: simple", &mockFact{
Country: "gbr",
Attrs: []int{33, 55},
}, ConsistOf(7)),
Entry("match: combo", &mockFact{
Country: "gbr",
OS: "ios",
}, ConsistOf(8)),
Entry("match: multi", &mockFact{
Country: "gbr",
Attrs: []int{33, 34, 55},
OS: "ios",
}, ConsistOf(7, 8)),
Entry("no match: one-of missing", &mockFact{
Country: "usa",
}, BeEmpty()),
Entry("no match: one-of partially missing", &mockFact{
Country: "gbr",
Attrs: []int{33, 34},
}, BeEmpty()),
Entry("no match: none-of violation", &mockFact{
Country: "fra",
Attrs: []int{33, 55},
OS: "android",
}, BeEmpty()),
)
func TestSuite(t *testing.T) {
RegisterFailHandler(Fail)
BeforeEach(func() {
strDict = make(StrDict)
})
RunSpecs(t, "qualify")
}
// --------------------------------------------------------------------
const (
FieldCountry Field = iota
FieldBrowser
FieldOS
FieldAttrs
)
var strDict StrDict
type mockFact struct {
Country string
Browser string
OS string
Attrs []int
}
func (m *mockFact) AppendFieldValues(x []int, f Field) []int {
switch f {
case FieldCountry:
return strDict.Lookup(x, m.Country)
case FieldBrowser:
return strDict.Lookup(x, m.Browser)
case FieldOS:
return strDict.Lookup(x, m.OS)
case FieldAttrs:
return append(x, m.Attrs...)
}
return x
}
func seedBuilder() *Builder {
b := NewBuilder()
b.Require(7, FieldCountry, OneOf(strDict.Add("gbr"), strDict.Add("irl")))
b.Require(7, FieldBrowser, NoneOf(strDict.Add("safari")))
b.Require(7, FieldAttrs, OneOf(33, 34))
b.Require(7, FieldAttrs, OneOf(55, 56))
b.Require(8, FieldCountry, NoneOf(strDict.Add("ger"), strDict.Add("fra")))
b.Require(8, FieldOS, OneOf(strDict.Add("android"), strDict.Add("ios")))
return b
}