-
Notifications
You must be signed in to change notification settings - Fork 1
/
predicates_test.go
202 lines (163 loc) · 5.31 KB
/
predicates_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
199
200
201
202
package yit
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
"gopkg.in/yaml.v3"
)
var _ = Describe("Predicates", func() {
Describe("WithKind", func() {
predicate := WithKind(yaml.ScalarNode)
It("returns true when nodes match the supplied Kind", func() {
Expect(predicate(&yaml.Node{Kind: yaml.ScalarNode})).To(BeTrue())
})
It("returns false when nodes don't match the supplied Kind", func() {
Expect(predicate(&yaml.Node{Kind: yaml.MappingNode})).To(BeFalse())
})
})
DescribeTable("Truths",
func(op func(p ...Predicate) Predicate, a, b, expected bool) {
actual := op(
func(node *yaml.Node) bool {
return a
}, func(node *yaml.Node) bool {
return b
},
)
Expect(actual(nil)).To(Equal(expected))
},
Entry("T | T = T", Union, true, true, true),
Entry("T | F = T", Union, true, false, true),
Entry("F | T = T", Union, false, true, true),
Entry("F | F = F", Union, false, false, false),
Entry("T & T = T", Intersect, true, true, true),
Entry("T & F = F", Intersect, true, false, false),
Entry("F & T = F", Intersect, false, true, false),
Entry("F & F = F", Intersect, false, false, false),
)
Describe("WithShortTag", func() {
predicate := WithShortTag("booooo")
It("returns true when nodes match the tag", func() {
Expect(predicate(&yaml.Node{Tag: "booooo"})).To(BeTrue())
})
It("returns false when nodes do not match the tag", func() {
Expect(predicate(&yaml.Node{Tag: "not boooo"})).To(BeFalse())
})
})
Describe("WithMapKey", func() {
predicate := WithMapKey("a")
It("returns true when the map has a specific key", func() {
node := &yaml.Node{Kind: yaml.MappingNode, Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "a"},
{Kind: yaml.ScalarNode, Value: "b"},
}}
result := predicate(node)
Expect(result).To(BeTrue())
})
It("returns false when the map doesn't have a specific key", func() {
node := &yaml.Node{Kind: yaml.MappingNode, Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "c"},
{Kind: yaml.ScalarNode, Value: "d"},
}}
result := predicate(node)
Expect(result).To(BeFalse())
})
It("returns false when the node isn't a map", func() {
node := &yaml.Node{Kind: yaml.SequenceNode, Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "a"},
}}
result := predicate(node)
Expect(result).To(BeFalse())
})
})
Describe("WithMapValues", func() {
predicate := WithMapValue("b")
It("returns true when the map has a specific key", func() {
node := &yaml.Node{Kind: yaml.MappingNode, Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "a"},
{Kind: yaml.ScalarNode, Value: "b"},
}}
result := predicate(node)
Expect(result).To(BeTrue())
})
It("returns false when the map doesn't have a specific key", func() {
node := &yaml.Node{Kind: yaml.MappingNode, Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "c"},
{Kind: yaml.ScalarNode, Value: "d"},
}}
result := predicate(node)
Expect(result).To(BeFalse())
})
It("returns false when the node isn't a map", func() {
node := &yaml.Node{Kind: yaml.SequenceNode, Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "b"},
}}
result := predicate(node)
Expect(result).To(BeFalse())
})
})
Describe("WithMapKeyValue", func() {
predicate := WithMapKeyValue(
// Key Predicate
WithStringValue("a"),
// Value Predicate
WithStringValue("b"),
)
It("returns true when the map has a specific key value pair", func() {
node := &yaml.Node{Kind: yaml.MappingNode, Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "a"},
{Kind: yaml.ScalarNode, Value: "b"},
}}
result := predicate(node)
Expect(result).To(BeTrue())
})
It("returns false when the map doesn't have a specific key value pair", func() {
node := &yaml.Node{Kind: yaml.MappingNode, Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "a"},
{Kind: yaml.ScalarNode, Value: "c"},
}}
result := predicate(node)
Expect(result).To(BeFalse())
})
It("returns false when the node isn't a map", func() {
node := &yaml.Node{Kind: yaml.SequenceNode, Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "a"},
{Kind: yaml.ScalarNode, Value: "b"},
}}
result := predicate(node)
Expect(result).To(BeFalse())
})
})
Describe("WithPrefix", func() {
predicate := WithPrefix("pre")
It("returns true when the node's value has a prefix", func() {
node := &yaml.Node{Value: "prefix"}
result := predicate(node)
Expect(result).To(BeTrue())
})
It("returns false when the node's value does not have a prefix", func() {
node := &yaml.Node{Value: "postfix"}
result := predicate(node)
Expect(result).To(BeFalse())
})
})
Describe("WithSuffix", func() {
predicate := WithSuffix("fix")
It("returns true when the node's value has a prefix", func() {
node := &yaml.Node{Value: "prefix"}
result := predicate(node)
Expect(result).To(BeTrue())
})
It("returns false when the node's value does not have a prefix", func() {
node := &yaml.Node{Value: "fixpost"}
result := predicate(node)
Expect(result).To(BeFalse())
})
})
Describe("Negate", func() {
It("reverses the result of a predicate", func() {
Expect(Negate(All)(nil)).To(BeFalse())
Expect(Negate(None)(nil)).To(BeTrue())
})
})
})