-
-
Notifications
You must be signed in to change notification settings - Fork 473
/
MatcherExecutorSpec.groovy
executable file
·305 lines (263 loc) · 14 KB
/
MatcherExecutorSpec.groovy
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package au.com.dius.pact.core.matchers
import au.com.dius.pact.core.model.matchingrules.DateMatcher
import au.com.dius.pact.core.model.matchingrules.EqualsMatcher
import au.com.dius.pact.core.model.matchingrules.IncludeMatcher
import au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher
import au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher
import au.com.dius.pact.core.model.matchingrules.MinTypeMatcher
import au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher
import au.com.dius.pact.core.model.matchingrules.RegexMatcher
import au.com.dius.pact.core.model.matchingrules.TimeMatcher
import au.com.dius.pact.core.model.matchingrules.TimestampMatcher
import au.com.dius.pact.core.model.matchingrules.TypeMatcher
import au.com.dius.pact.core.support.json.JsonParser
import au.com.dius.pact.core.support.json.JsonValue
import spock.lang.Specification
import spock.lang.Unroll
import static au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher.NumberType.DECIMAL
import static au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher.NumberType.INTEGER
import static au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher.NumberType.NUMBER
@SuppressWarnings(['UnnecessaryBooleanExpression', 'CyclomaticComplexity'])
class MatcherExecutorSpec extends Specification {
MismatchFactory mismatchFactory
def path
static xml(String xml) {
XmlBodyMatcher.INSTANCE.parse(xml)
}
static json(String json) {
JsonParser.INSTANCE.parseString(json)
}
def setup() {
mismatchFactory = [create: { p0, p1, p2, p3 -> new PathMismatch('', '', p2) } ] as MismatchFactory
path = ['/']
}
@Unroll
def 'equals matcher matches using equals'() {
expect:
MatcherExecutorKt.domatch(EqualsMatcher.INSTANCE, path, expected, actual, mismatchFactory).empty == mustBeEmpty
where:
expected | actual || mustBeEmpty
'100' | '100' || true
100 | '100' || false
100 | 100 || true
new JsonValue.Integer('100'.chars) | new JsonValue.Integer('100'.chars) || true
null | null || true
'100' | null || false
null | 100 || false
JsonValue.Null.INSTANCE | null || true
null | JsonValue.Null.INSTANCE || true
JsonValue.Null.INSTANCE | JsonValue.Null.INSTANCE || true
xml('<a/>') | xml('<a/>') || true
xml('<a/>') | xml('<b/>') || false
xml('<e xmlns="a"/>') | xml('<a:e xmlns:a="a"/>') || true
xml('<a:e xmlns:a="a"/>') | xml('<b:e xmlns:b="a"/>') || true
xml('<e xmlns="a"/>') | xml('<e xmlns="b"/>') || false
json('"hello"') | json('"hello"') || true
}
@Unroll
def 'regex matcher matches using the provided regex'() {
expect:
MatcherExecutorKt.domatch(new RegexMatcher(regex), path, expected, actual, mismatchFactory).empty == mustBeEmpty
where:
expected | actual | regex || mustBeEmpty
'Harry' | 'Happy' | 'Ha[a-z]*' || true
'Harry' | null | 'Ha[a-z]*' || false
'100' | 20123 | '\\d+' || true
'100' | new JsonValue.Integer('20123'.chars) | '\\d+' || true
}
@Unroll
def 'type matcher matches on types'() {
expect:
MatcherExecutorKt.domatch(TypeMatcher.INSTANCE, path, expected, actual, mismatchFactory).empty == mustBeEmpty
where:
expected | actual || mustBeEmpty
'Harry' | 'Some other string' || true
100 | 200.3 || true
true | false || true
null | null || true
'200' | 200 || false
200 | null || false
[100, 200, 300] | [200.3] || true
[a: 100] | [a: 200.3, b: 200, c: 300] || true
JsonValue.Null.INSTANCE | null || true
null | JsonValue.Null.INSTANCE || true
JsonValue.Null.INSTANCE | JsonValue.Null.INSTANCE || true
xml('<a/>') | xml('<a/>') || true
xml('<a/>') | xml('<b/>') || false
xml('<e xmlns="a"/>') | xml('<a:e xmlns:a="a"/>') || true
xml('<a:e xmlns:a="a"/>') | xml('<b:e xmlns:b="a"/>') || true
xml('<e xmlns="a"/>') | xml('<e xmlns="b"/>') || false
json('"hello"') | json('"hello"') || true
json('100') | json('200') || true
}
@Unroll
def 'number type matcher matches on types'() {
expect:
MatcherExecutorKt.domatch(new NumberTypeMatcher(numberType), path, expected, actual, mismatchFactory).empty ==
mustBeEmpty
where:
numberType | expected | actual || mustBeEmpty
INTEGER | 100 | 'Some other string' || false
DECIMAL | 100.0 | 'Some other string' || false
NUMBER | 100 | 'Some other string' || false
INTEGER | 100 | 200.3 || false
NUMBER | 100 | 200.3 || true
DECIMAL | 100.0 | 200.3 || true
INTEGER | 100 | 200 || true
INTEGER | 100 | new JsonValue.Integer('200'.chars) || true
NUMBER | 100 | 200 || true
DECIMAL | 100.0 | 200 || false
INTEGER | 100 | false || false
DECIMAL | 100.0 | false || false
NUMBER | 100 | false || false
INTEGER | 100 | null || false
DECIMAL | 100.0 | null || false
NUMBER | 100 | null || false
INTEGER | 100 | [200.3] || false
DECIMAL | 100.0 | [200.3] || false
NUMBER | 100 | [200.3] || false
INTEGER | 100 | [a: 200.3, b: 200, c: 300] || false
DECIMAL | 100.0 | [a: 200.3, b: 200, c: 300] || false
NUMBER | 100 | [a: 200.3, b: 200, c: 300] || false
}
@Unroll
@SuppressWarnings('LineLength')
def 'timestamp matcher'() {
expect:
MatcherExecutorKt.domatch(matcher, path, expected, actual, mismatchFactory).empty == mustBeEmpty
where:
expected | actual | pattern || mustBeEmpty
'2014-01-01 14:00:00+10:00' | '2013-12-01 14:00:00+10:00' | null || true
'2014-01-01 14:00:00+10:00' | 'I\'m a timestamp!' | null || false
'2014-01-01 14:00:00+10:00' | '2013#12#01#14#00#00' | "yyyy'#'MM'#'dd'#'HH'#'mm'#'ss" || true
'2014-01-01 14:00:00+10:00' | null | null || false
'2014-01-01T10:00+10:00[Australia/Melbourne]' | '2020-01-01T10:00+01:00[Europe/Warsaw]' | "yyyy-MM-dd'T'HH:mmXXX'['zzz']'" || true
'2019-11-25T13:45:00+02:00' | '2019-11-25T11:45:00Z' | "yyyy-MM-dd'T'HH:mm:ssX" || true
'2019-11-25T13:45:00+02:00' | '2019-11-25T11:45:00Z' | "yyyy-MM-dd'T'HH:mm:ssZZ" || true
'2019-11-25T13:45:00+02:00' | '2019-11-25T11:45Z' | "yyyy-MM-dd'T'HH:mmZZ" || true
'2019-11-25T13:45:00+02:00' | '2019-11-25T11Z' | "yyyy-MM-dd'T'HHZZ" || true
'2019-11-25T13:45:00+0200' | '2019-11-25T11:45:00Z' | "yyyy-MM-dd'T'HH:mm:ssZ" || true
'2019-11-25T13:45:00+0200' | '2019-11-25T11:45Z' | "yyyy-MM-dd'T'HH:mmZ" || true
'2019-11-25T13:45:00+0200' | '2019-11-25T11Z' | "yyyy-MM-dd'T'HHZ" || true
'2019-11-25T13:45:00+0200' | '2019-11-25T11:45:00Z' | "yyyy-MM-dd'T'HH:mm:ss'Z'" || true
'2019-11-25T13:45:00:000000+0200' | '2019-11-25T11:19:00.000000Z' | "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'" || true
'2019-11-25T13:45:00:000+0200' | '2019-11-25T11:19:00.000Z' | "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" || true
// This is in order to keep backwards compatibility with version < 4.1.1
'2019-11-25T13:45:00:000000+0200' | '2019-11-25T11:19:00.000000Z' | "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" || true
matcher = pattern ? new TimestampMatcher(pattern) : new TimestampMatcher()
}
@Unroll
def 'time matcher'() {
expect:
MatcherExecutorKt.domatch(matcher, path, expected, actual, mismatchFactory).empty == mustBeEmpty
where:
expected | actual | pattern || mustBeEmpty
'14:00:00' | '14:00:00' | null || true
'00:00' | '14:01:02' | 'mm:ss' || false
'00:00:14' | '05:10:14' | 'ss:mm:HH' || true
'14:00:00+10:00' | null | null || false
matcher = pattern ? new TimeMatcher(pattern) : new TimeMatcher()
}
@Unroll
def 'date matcher'() {
expect:
MatcherExecutorKt.domatch(matcher, path, expected, actual, mismatchFactory).empty == mustBeEmpty
where:
expected | actual | pattern || mustBeEmpty
'01-01-1970' | '14-01-2000' | null || true
'01-01-1970' | '01011970' | 'dd-MM-yyyy' || false
'12/30/1970' | '01/14/2001' | 'MM/dd/yyyy' || true
'2014-01-01' | null | null || false
matcher = pattern ? new DateMatcher(pattern) : new DateMatcher()
}
@Unroll
def 'include matcher matches if the expected is included in the actual'() {
expect:
MatcherExecutorKt.domatch(matcher, path, expected, actual, mismatchFactory).empty == mustBeEmpty
where:
expected | actual || mustBeEmpty
'Harry' | 'Harry' || true
'Harry' | 'HarryBob' || true
'Harry' | 'BobHarry' || true
'Harry' | 'BobHarryGeorge' || true
'Harry' | 'Tom' || false
'Harry' | null || false
'100' | 2010023 || true
matcher = new IncludeMatcher(expected)
}
def 'equality matching produces a message on mismatch'() {
given:
def factory = Mock MismatchFactory
when:
MatcherExecutorKt.matchEquality path, 'foo', 'bar', factory
then:
1 * factory.create(_, _, "Expected 'bar' to equal 'foo'", _)
0 * _
}
@Unroll
def 'list type matcher matches on array sizes - #matcher'() {
expect:
MatcherExecutorKt.domatch(matcher, path, expected, actual, mismatchFactory).empty == mustBeEmpty
where:
matcher | expected | actual || mustBeEmpty
TypeMatcher.INSTANCE | [0] | [1] || true
new MinTypeMatcher(1) | [0] | [1] || true
new MinTypeMatcher(2) | [0, 1] | [1] || false
new MaxTypeMatcher(2) | [0] | [1] || true
new MaxTypeMatcher(1) | [0] | [1, 1] || false
new MinMaxTypeMatcher(1, 2) | [0] | [1] || true
new MinMaxTypeMatcher(2, 3) | [0, 1] | [1] || false
new MinMaxTypeMatcher(1, 2) | [0, 1] | [1, 1] || true
new MinMaxTypeMatcher(1, 2) | [0] | [1, 1, 2] || false
}
@Unroll
@SuppressWarnings('UnnecessaryCast')
def 'matching integer values'() {
expect:
MatcherExecutorKt.matchInteger(value) == result
where:
value | result
'100' | false
100 | true
100.0 | false
100 as int | true
100 as long | true
100 as BigInteger | true
100 as BigDecimal | true
BigInteger.ZERO | true
BigDecimal.ZERO | true
}
@Unroll
@SuppressWarnings('UnnecessaryCast')
def 'matching decimal number values'() {
expect:
MatcherExecutorKt.matchDecimal(value) == result
where:
value | result
new JsonValue.Decimal('0'.chars) | true
'100' | false
100 | false
100.0 | true
100.0 as float | true
100.0 as double | true
100 as int | false
100 as long | false
100 as BigInteger | false
BigInteger.ZERO | false
BigDecimal.ZERO | true
}
@Unroll
def 'display #value as #display'() {
expect:
MatcherExecutorKt.valueOf(value) == display
where:
value || display
null || 'null'
'foo' || "'foo'"
55 || '55'
xml('<foo/>') || '<foo>'
xml('<foo><bar/></foo>') || '<foo>'
xml('<foo xmlns="a"/>') || '<{a}foo>'
xml('<a>text</a>').firstChild || "'text'"
}
}