1
1
package au.com.dius.pact.core.matchers
2
2
3
+ import au.com.dius.pact.core.model.matchingrules.MatchingRuleCategory
3
4
import au.com.dius.pact.core.support.json.JsonValue
5
+ import groovy.transform.TupleConstructor
6
+ import org.w3c.dom.Document
7
+ import org.w3c.dom.NamedNodeMap
8
+ import org.w3c.dom.Node
9
+ import org.w3c.dom.NodeList
10
+ import org.w3c.dom.UserDataHandler
4
11
import spock.lang.Specification
5
12
6
- @SuppressWarnings (' LineLength' )
13
+ import static au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher.NumberType.NUMBER
14
+
15
+ @SuppressWarnings ([' LineLength' , ' SpaceAroundOperator' , ' GetterMethodCouldBeProperty' ])
7
16
class MatcherExecutorKtSpec extends Specification {
8
17
9
18
def ' match regex' () {
@@ -28,4 +37,176 @@ class MatcherExecutorKtSpec extends Specification {
28
37
' 04.5.7' | [new HeaderMismatch (' test' , ' ' , ' 04.5.7' , " '04.5.7' is not a valid semantic version" )]
29
38
new JsonValue.StringValue (' 4.5.7' ) | []
30
39
}
40
+
41
+ def ' matching numbers' () {
42
+ given :
43
+ def mismatchFactory = { a , b , c , d -> new HeaderMismatch (' test' , ' ' , actual. toString(), c) } as MismatchFactory
44
+
45
+ expect :
46
+ MatcherExecutorKt . matchNumber(NUMBER , [' $' ], expected, actual, mismatchFactory, null )? [0 ]?. mismatch == result
47
+
48
+ where :
49
+
50
+ expected | actual | result
51
+ null | null | ' Expected null (Null) to be a number'
52
+ null | ' 4.5' | " Expected '4.5' (String) to be a null value"
53
+ 100 | null | ' Expected null (Null) to be a number'
54
+ 100 | ' 4.5' | " Expected '4.5' (String) to be a number"
55
+ 100 | 4.5 | null
56
+ 100 | 4 | null
57
+ 100 | new JsonValue.StringValue (' 4.5.7.8' ) | " Expected '4.5.7.8' (String) to be a number"
58
+ 100 | new JsonValue.Integer (200 ) | null
59
+ 100 | new JsonValue.Decimal (200.10 ) | null
60
+ 100 | false | ' Expected false (Boolean) to be a number'
61
+ 100 | [100 ] | ' Expected [100] (Array) to be a number'
62
+ 100 | [a : 200.3 , b : 200 , c : 300 ] | ' Expected {a=200.3, b=200, c=300} (LinkedHashMap) to be a number'
63
+ 100 | 2.300g | null
64
+ 100 | 2.300d | null
65
+ 100 | new TestNode (' not a number' ) | ' Expected TestNode(not a number) (TestNode) to be a number'
66
+ 100 | new TestNode (' 22.33.44' ) | ' Expected TestNode(22.33.44) (TestNode) to be a number'
67
+ 100 | new TestNode (' 22.33' ) | null
68
+ }
69
+
70
+ def ' matching numbers with coercion enabled' () {
71
+ given :
72
+ def mismatchFactory = { a , b , c , d -> new HeaderMismatch (' test' , ' ' , actual. toString(), c) } as MismatchFactory
73
+ def context = new MatchingContext (new MatchingRuleCategory (' test' ), false , [:], true )
74
+
75
+ expect :
76
+ MatcherExecutorKt . matchNumber(NUMBER , [' $' ], expected, actual, mismatchFactory, context)? [0 ]?. mismatch == result
77
+
78
+ where :
79
+
80
+ expected | actual | result
81
+ 100 | ' 4.5' | null
82
+ 100 | 4.5 | null
83
+ 100 | 4 | null
84
+ 100 | new JsonValue.StringValue (' 4.5.7.8' ) | " Expected '4.5.7.8' (String) to be a number"
85
+ 100 | new JsonValue.StringValue (' 4.5' ) | null
86
+ 100 | new JsonValue.Integer (200 ) | null
87
+ 100 | new JsonValue.Decimal (200.10 ) | null
88
+ }
89
+
90
+ @SuppressWarnings (' UnnecessaryCast' )
91
+ def ' matching integer values' () {
92
+ expect :
93
+ MatcherExecutorKt . matchInteger(value, null ) == result
94
+
95
+ where :
96
+
97
+ value | result
98
+ ' 100' | false
99
+ ' 100x' | false
100
+ 100 | true
101
+ 100.0 | false
102
+ 100i | true
103
+ 100l | true
104
+ 100 as BigInteger | true
105
+ 100g | true
106
+ BigInteger . ZERO | true
107
+ BigDecimal . ZERO | true
108
+ }
109
+
110
+ @SuppressWarnings (' UnnecessaryCast' )
111
+ def ' matching integer values with coercion enabled' () {
112
+ given :
113
+ def context = new MatchingContext (new MatchingRuleCategory (' test' ), false , [:], true )
114
+
115
+ expect :
116
+ MatcherExecutorKt . matchInteger(value, context) == result
117
+
118
+ where :
119
+
120
+ value | result
121
+ ' 100' | true
122
+ ' 100x' | false
123
+ ' x100' | false
124
+ 100 | true
125
+ }
126
+
127
+ @SuppressWarnings (' UnnecessaryCast' )
128
+ def ' matching decimal number values' () {
129
+ expect :
130
+ MatcherExecutorKt . matchDecimal(value, null ) == result
131
+
132
+ where :
133
+
134
+ value | result
135
+ new JsonValue.Decimal (' 0' . chars) | true
136
+ ' 100' | false
137
+ ' 100.0' | false
138
+ 100 | false
139
+ 100.0 | true
140
+ 100.0f | true
141
+ 100.0d | true
142
+ 100i | false
143
+ 100l | false
144
+ 100 as BigInteger | false
145
+ BigInteger . ZERO | false
146
+ BigDecimal . ZERO | true
147
+ }
148
+
149
+ @SuppressWarnings (' UnnecessaryCast' )
150
+ def ' matching decimal number values with coercion enabled' () {
151
+ given :
152
+ def context = new MatchingContext (new MatchingRuleCategory (' test' ), false , [:], true )
153
+
154
+ expect :
155
+ MatcherExecutorKt . matchDecimal(value, context) == result
156
+
157
+ where :
158
+
159
+ value | result
160
+ new JsonValue.Decimal (' 0' . chars) | true
161
+ ' 100' | false
162
+ ' 100.0' | true
163
+ ' 100.0x' | false
164
+ ' x100.0' | false
165
+ }
166
+
167
+ @TupleConstructor
168
+ @SuppressWarnings ([' EmptyMethod' , ' UnusedMethodParameter' ])
169
+ static class TestNode implements Node {
170
+ String value
171
+
172
+ String toString () { " TestNode($value )" }
173
+
174
+ String getNodeName () { ' TestNode' }
175
+ String getNodeValue () { value }
176
+ void setNodeValue (String nodeValue ) { }
177
+ short getNodeType () { 0 }
178
+ Node getParentNode () { null }
179
+ NodeList getChildNodes () { null }
180
+ Node getFirstChild () { null }
181
+ Node getLastChild () { null }
182
+ Node getPreviousSibling () { null }
183
+ Node getNextSibling () { null }
184
+ NamedNodeMap getAttributes () { null }
185
+ Document getOwnerDocument () { null }
186
+ Node insertBefore (Node newChild , Node refChild ) { null }
187
+ Node replaceChild (Node newChild , Node oldChild ) { null }
188
+ Node removeChild (Node oldChild ) { null }
189
+ Node appendChild (Node newChild ) { null }
190
+ boolean hasChildNodes () { false }
191
+ Node cloneNode (boolean deep ) { null }
192
+ void normalize () { }
193
+ boolean isSupported (String feature , String version ) { false }
194
+ String getNamespaceURI () { null }
195
+ String getPrefix () { null }
196
+ void setPrefix (String prefix ) { }
197
+ String getLocalName () { null }
198
+ boolean hasAttributes () { false }
199
+ String getBaseURI () { null }
200
+ short compareDocumentPosition (Node other ) { 0 }
201
+ String getTextContent () { null }
202
+ void setTextContent (String textContent ) { }
203
+ boolean isSameNode (Node other ) { false }
204
+ String lookupPrefix (String namespaceURI ) { null }
205
+ boolean isDefaultNamespace (String namespaceURI ) { false }
206
+ String lookupNamespaceURI (String prefix ) { null }
207
+ boolean isEqualNode (Node arg ) { false }
208
+ Object getFeature (String feature , String version ) { null }
209
+ Object setUserData (String key , Object data , UserDataHandler handler ) { null }
210
+ Object getUserData (String key ) { null }
211
+ }
31
212
}
0 commit comments