-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathspec_test.go
261 lines (252 loc) · 6.59 KB
/
spec_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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* Copyright 2021 Comcast Cable Communications Management, LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package core
import (
"context"
"errors"
"strings"
"testing"
)
func mockPatternParser(val interface{}, err error) func(string, interface{}) (interface{}, error) {
return func(_ string, _ interface{}) (interface{}, error) {
return val, err
}
}
func TestParsePatterns(t *testing.T) {
goodNodes := map[string]*Node{
"yay": {
Branches: &Branches{
Branches: []*Branch{
{Pattern: "something nice"},
{Pattern: "something else"},
{Pattern: "something??"},
},
},
},
"next thing": {
Branches: &Branches{
Branches: []*Branch{{Pattern: 5}},
},
},
}
emptyNodes := map[string]*Node{
"nowhere": {},
"a little further": {
Branches: &Branches{},
},
"getting there": {
Branches: &Branches{
Branches: []*Branch{
nil,
nil,
{Pattern: nil},
},
},
},
}
testErr := errors.New("test parser error")
tests := []struct {
description string
parser func(string, interface{}) (interface{}, error)
nodes map[string]*Node
expectedNodes map[string]*Node
expectedErr error
}{
{
description: "Success",
parser: mockPatternParser("a", nil),
nodes: goodNodes,
expectedNodes: map[string]*Node{
"yay": {
Branches: &Branches{
Branches: []*Branch{{Pattern: "a"},
{Pattern: "a"}, {Pattern: "a"},
},
},
},
"next thing": {
Branches: &Branches{
Branches: []*Branch{{Pattern: "a"}},
},
},
},
},
{
description: "Success with default parser",
nodes: goodNodes,
expectedNodes: goodNodes,
},
{
description: "Success with no nodes",
parser: mockPatternParser("c", nil),
},
{
description: "Success with no branches",
parser: mockPatternParser("d", nil),
nodes: emptyNodes,
expectedNodes: emptyNodes,
},
{
description: "Parse failure",
parser: mockPatternParser("e", testErr),
nodes: goodNodes,
expectedErr: testErr,
},
{
description: "Canonicalize failure",
parser: mockPatternParser([]interface{}{func() string { return ":(" }}, nil),
nodes: goodNodes,
expectedErr: errors.New("unsupported type"),
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
s := &Spec{
PatternParser: tc.parser,
Nodes: tc.nodes,
}
err := s.ParsePatterns(context.Background())
if s.PatternParser == nil {
t.Error("pattern parser shouldn't be nil")
}
if err == nil || tc.expectedErr == nil {
if err != tc.expectedErr {
t.Errorf("expected %v error but received %v",
tc.expectedErr, err)
}
} else if !strings.Contains(err.Error(), tc.expectedErr.Error()) {
t.Errorf("error %s doesn't include expected string %s",
err, tc.expectedErr)
}
// if we expected an error, leave before the nightmare begins.
if tc.expectedErr != nil {
return
}
// start checking nodes' branches' patterns...
if len(tc.expectedNodes) != len(s.Nodes) {
t.Fatalf("nodes don't match; expected %v but received %v",
tc.expectedNodes, s.Nodes)
}
for k, n := range tc.expectedNodes {
if n == nil {
if s.Nodes[k] != nil {
t.Fatalf("nodes don't match; expected %v but received %v",
tc.expectedNodes, s.Nodes)
}
continue
}
if n.Branches == nil {
if s.Nodes[k].Branches != nil {
t.Fatalf("nodes don't match; expected %v but received %v",
tc.expectedNodes, s.Nodes)
}
continue
}
if s.Nodes[k] == nil || s.Nodes[k].Branches == nil {
t.Fatalf("nodes don't match; expected %v but received %v",
tc.expectedNodes, s.Nodes)
}
expectedBranches := n.Branches.Branches
sBranches := s.Nodes[k].Branches.Branches
if len(expectedBranches) != len(sBranches) {
t.Fatalf("nodes don't match; expected %v but received %v",
tc.expectedNodes, s.Nodes)
}
for j, b := range expectedBranches {
if b == nil {
if sBranches[j] != nil {
t.Fatalf("nodes don't match; expected %v but received %v",
tc.expectedNodes, s.Nodes)
}
continue
}
if sBranches[j] == nil {
t.Fatalf("nodes don't match; expected %v but received %v",
tc.expectedNodes, s.Nodes)
}
if b.Pattern != sBranches[j].Pattern {
t.Fatalf("nodes don't match; expected %v but received %v",
tc.expectedNodes, s.Nodes)
}
}
}
})
}
}
func TestCompile(t *testing.T) {
s := &Spec{}
err := s.Compile(context.Background(), nil, false)
if err != nil {
t.Fatalf("expected no error but received %v", err)
}
}
func TestDefaultPatternParser(t *testing.T) {
tests := []struct {
description string
syntax string
val interface{}
expectedResult interface{}
expectedErr error
}{
{
description: "Default error",
syntax: "clearly invalid",
val: "testing123",
expectedErr: errors.New("unsupposed pattern syntax"),
},
{
description: "None syntax success",
syntax: "none",
val: struct{}{},
expectedResult: struct{}{},
},
{
description: "Empty syntax success",
},
{
description: "JSON syntax success",
syntax: "json",
val: `"test"`,
expectedResult: "test",
},
{
description: "JSON syntax non-string success",
syntax: "json",
},
{
description: "JSON unmarshal error",
syntax: "json",
val: "",
expectedErr: errors.New("unexpected end of JSON input"),
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
r, err := DefaultPatternParser(tc.syntax, tc.val)
if tc.expectedResult != r {
t.Errorf("expected %v pattern but received %v",
tc.expectedResult, r)
}
if err == nil || tc.expectedErr == nil {
if err != tc.expectedErr {
t.Errorf("expected %v error but received %v",
tc.expectedErr, err)
}
return
}
if !strings.Contains(err.Error(), tc.expectedErr.Error()) {
t.Errorf("error %s doesn't include expected string %s",
err, tc.expectedErr)
}
})
}
}