hamcrest4qunit 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,142 @@
1
+ h4q.throwsError = (m, scope, args) ->
2
+
3
+ m = equalTo m if m? and not (m instanceof Matcher)
4
+
5
+ new Matcher
6
+ errorMatcher: m
7
+ scope: scope
8
+ arguments: args
9
+ noArgs: false
10
+ withoutArgs: ->
11
+ @noArgs = true
12
+ this
13
+ withArgs: ->
14
+ @arguments = h4q.argumentsToArray arguments
15
+ this
16
+ withScope: (scope) ->
17
+ @scope = scope
18
+ this
19
+ _matches: (v, msg) ->
20
+ unless v? and typeof v is "function"
21
+ msg.appendValue(v).appendText("is not a function")
22
+ false
23
+
24
+ else
25
+ res = null
26
+ try
27
+ v.apply @scope, if @noArgs then [] else @arguments
28
+ catch e
29
+ if @errorMatcher
30
+ errorMsg = new Description()
31
+ res = @errorMatcher.matches e, errorMsg
32
+
33
+ if res?
34
+ msg.appendText("an exception was thrown and").appendText errorMsg
35
+ else
36
+ msg.appendText("an exception was thrown but").appendText errorMsg
37
+ msg.diff = errorMsg.diff
38
+ else
39
+ msg.appendValue(e).appendText "was thrown"
40
+ res = true
41
+
42
+ unless res?
43
+ res = false
44
+ msg.appendValue(v).appendText "doesnt thrown"
45
+ if @errorMatcher
46
+ msg.appendDescriptionOf @errorMatcher
47
+ else
48
+ msg.appendText "any exception"
49
+ res
50
+
51
+ _describeTo: (msg) ->
52
+ msg.appendText "a Function which throws"
53
+
54
+ if @errorMatcher?
55
+ msg.appendDescriptionOf @errorMatcher
56
+ else
57
+ msg.appendText "an exception"
58
+
59
+ if @scope? or @arguments? or @noArgs
60
+ msg.appendText "when called"
61
+
62
+ if @scope?
63
+ msg.appendText("with scope").appendValue @scope
64
+
65
+ if @scope? and (@arguments? or @noArgs)
66
+ msg.appendText "and"
67
+
68
+ if not @noArgs and @arguments? and @arguments.length > 0
69
+ msg.appendText("with arguments").appendValue @arguments
70
+ else if @noArgs
71
+ msg.appendText "without arguments"
72
+
73
+ h4q.returns = (m, s, a) ->
74
+
75
+ m = equalTo m if m? and not (m instanceof Matcher)
76
+
77
+ return new Matcher
78
+ returnsMatcher: m
79
+ arguments: a
80
+ noArgs: false
81
+ scope: s
82
+ withoutArgs: ->
83
+ @noArgs = true
84
+ this
85
+ withArgs: ->
86
+ @arguments = h4q.argumentsToArray arguments
87
+ this
88
+ withScope: (scope) ->
89
+ @scope = scope
90
+ this
91
+ _matches: (v, msg) ->
92
+ unless v? and typeof v is "function"
93
+ msg.appendValue(v).appendText "is not a Function"
94
+ return false
95
+ else
96
+ res = v.apply @scope, if @noArgs then [] else @arguments
97
+
98
+ if @returnsMatcher
99
+ mmsg = new Description()
100
+ mres = @returnsMatcher.matches res, mmsg
101
+
102
+ msg.appendText("was")
103
+ .appendValue(v)
104
+ .appendText("of which returns")
105
+ .appendText(mmsg)
106
+ msg.diff = mmsg.diff
107
+ return mres
108
+ else
109
+ if res?
110
+ msg.appendText("was")
111
+ .appendValue(v)
112
+ .appendText("of which returns was")
113
+ .appendValue(res)
114
+ return true
115
+ else
116
+ msg.appendText("was")
117
+ .appendValue(v)
118
+ .appendText("which returned nothing")
119
+ return false
120
+
121
+ _describeTo: (msg) ->
122
+ msg.appendText "a Function that returns"
123
+
124
+ if @returnsMatcher?
125
+ msg.appendDescriptionOf @returnsMatcher
126
+ else
127
+ msg.appendText "anything"
128
+
129
+ if @scope? or @arguments? or @noArgs
130
+ msg.appendText "when called"
131
+
132
+ if @scope?
133
+ msg.appendText("with scope").appendValue @scope
134
+
135
+ if @scope and (@arguments? or @noArgs)
136
+ msg.appendText "and"
137
+
138
+ if not @noArgs and @arguments? and @arguments.length > 0
139
+ msg.appendText("with arguments").appendValue @arguments
140
+ else if @noArgs
141
+ msg.appendText "without arguments"
142
+
@@ -0,0 +1,177 @@
1
+ h4q.nanValue = ->
2
+ new Matcher
3
+ _matches: (v, msg) ->
4
+ msg.appendText("was").appendValue v
5
+ isNaN v
6
+ _describeTo: (msg) ->
7
+ msg.appendValue NaN
8
+
9
+ h4q.notNanValue = ->
10
+ new Matcher
11
+ _matches: (v,msg) ->
12
+ msg.appendText("was").appendValue v
13
+ not isNaN v
14
+ _describeTo: (msg) ->
15
+ msg.appendText("not").appendValue NaN
16
+
17
+ h4q.between = (a, b) ->
18
+ if isNaN a then throw new Error message: "a must be a valid number"
19
+ if isNaN b then throw new Error message: "b must be a valid number"
20
+ if a >= b then throw new Error message: "b must be a greater number than a"
21
+
22
+ new Matcher
23
+ a: a
24
+ b: b
25
+ included: false
26
+ inclusive: ->
27
+ @included = true
28
+ this
29
+
30
+ _matches: (v, msg) ->
31
+ if @included
32
+ if v >= @a and v <= @b
33
+ msg.appendText("was").appendValue(v)
34
+ true
35
+ else
36
+ if v < @a
37
+ msg.appendValue(v)
38
+ .appendText("was lower than the min value")
39
+ .appendValue(@a)
40
+ else
41
+ msg.appendValue(v)
42
+ .appendText("was greater than the max value")
43
+ .appendValue(@b)
44
+ false
45
+ else
46
+ if v > @a and v < @b
47
+ msg.appendText("was").appendValue(v)
48
+ true
49
+ else
50
+ if v <= @a
51
+ msg.appendValue(v)
52
+ .appendText("was lower or equal to the min value")
53
+ .appendValue(@a)
54
+ else
55
+ msg.appendValue(v)
56
+ .appendText("was greater or equal to the max value")
57
+ .appendValue(@b)
58
+ false
59
+
60
+ _describeTo: (msg) ->
61
+ msg.appendText("a Number between")
62
+ .appendValue(@a)
63
+ .appendText("and")
64
+ .appendValue(@b)
65
+
66
+ if @included
67
+ msg.appendText "inclusive"
68
+ else
69
+ msg.appendText "exclusive"
70
+
71
+ h4q.closeTo = (a, b) ->
72
+ if isNaN a then throw new Error message: "a must be a valid number"
73
+ if isNaN b then throw new Error message: "b must be a valid number"
74
+ new Matcher
75
+ a: a
76
+ b: b
77
+ _matches: (v, msg) ->
78
+ # required to solve the 2 - 1.9 = 0.1000000000000009 issue
79
+ decimal11 = (n) -> Math.floor(n * 10000000000) / 10000000000
80
+ delta = decimal11 Math.abs @a - v
81
+
82
+ if delta <= @b
83
+ msg.appendText("was").appendValue(v)
84
+ true
85
+ else
86
+ msg.appendValue(v)
87
+ .appendText("differed by")
88
+ .appendValue(delta - @b)
89
+ false
90
+
91
+ _describeTo: (msg) ->
92
+ msg.appendText("a Number within")
93
+ .appendValue(@b)
94
+ .appendText("of")
95
+ .appendValue(@a)
96
+
97
+ h4q.atLeast = (n) -> h4q.greaterThanOrEqualTo n
98
+ h4q.greaterThanOrEqualTo = (n) -> h4q.greaterThan n, true
99
+ h4q.greaterThan = (n, b) ->
100
+ if isNaN n
101
+ throw new Error message: "The comparison value must be a valid number"
102
+
103
+ new Matcher
104
+ n: n
105
+ included: b
106
+ inclusive: ->
107
+ @included = true
108
+ this
109
+
110
+ _matches: (v, msg) ->
111
+ if @included
112
+
113
+ if v >= @n
114
+ msg.appendText("was").appendValue(v)
115
+ true
116
+ else
117
+ msg.appendValue(v)
118
+ .appendText("was not greater than")
119
+ .appendText("or equal to")
120
+ .appendValue(@n)
121
+ false
122
+ else
123
+
124
+ if v > @n
125
+ msg.appendText("was").appendValue(v)
126
+ true
127
+ else
128
+ msg.appendValue(v)
129
+ .appendText("was not greater than")
130
+ .appendValue(@n)
131
+ false
132
+
133
+ _describeTo: (msg) ->
134
+ msg.appendText "a Number greater than"
135
+ msg.appendText "or equal to" if @included
136
+ msg.appendValue @n
137
+
138
+ h4q.atMost = (n) -> h4q.lowerThanOrEqualTo n
139
+ h4q.lowerThanOrEqualTo = (n) -> h4q.lowerThan n, true
140
+ h4q.lowerThan = (n,b) ->
141
+ if isNaN n
142
+ throw new Error message: "The comparison value must be a valid number"
143
+
144
+ new Matcher
145
+ n: n
146
+ included: b
147
+ inclusive: ->
148
+ @included = true
149
+ this
150
+
151
+ _matches: (v, msg)->
152
+ if @included
153
+ if v <= @n
154
+ msg.appendText("was").appendValue(v)
155
+ true
156
+ else
157
+ msg.appendValue(v)
158
+ .appendText("was not lower than")
159
+ .appendText("or equal to")
160
+ .appendValue(@n)
161
+ false
162
+ else
163
+ if v < @n
164
+ msg.appendText("was").appendValue(v)
165
+ true
166
+
167
+ else
168
+ msg.appendValue(v)
169
+ .appendText("was not lower than")
170
+ .appendValue(@n)
171
+ false
172
+
173
+ _describeTo: (msg) ->
174
+ msg.appendText "a Number lower than"
175
+ msg.appendText "or equal to" if @included
176
+ msg.appendValue @n
177
+
@@ -0,0 +1,249 @@
1
+ h4q.hasProperty = (p, v) ->
2
+ unless p?
3
+ throw new Error message: "hasProperty must have at least a property name"
4
+
5
+ v = equalTo v if v? and not (v instanceof Matcher)
6
+
7
+ return new Matcher
8
+ property: p
9
+ value: v
10
+ _matches: (v,msg) ->
11
+ unless v? and typeof v is "object"
12
+ msg.appendText("was").appendValue(v)
13
+ false
14
+ else unless v.hasOwnProperty @property
15
+ msg.appendValue(v)
16
+ .appendText( "don't have a property named" )
17
+ .appendRawValue(@property)
18
+ false
19
+ else if @value?
20
+ msgtmp = new Description()
21
+ res = @value.matches v[@property], msgtmp
22
+
23
+ if res then msg.appendText("was").appendValue(v)
24
+ else
25
+ msg.appendText msgtmp
26
+ msg.diff = msgtmp.diff
27
+ res
28
+ else
29
+ msg.appendText("was").appendValue(v)
30
+ true
31
+
32
+ _describeTo: (msg) ->
33
+ msg.appendText("an Object with a property").appendRawValue(@property)
34
+ if @value?
35
+ msg.appendText("of which value is").appendDescriptionOf(@value)
36
+
37
+ h4q.hasProperties = ( kwargs ) ->
38
+ unless kwargs?
39
+ throw new Error message: "the hasProperties kwargs arguments is mandatory"
40
+
41
+ for k, kwarg of kwargs
42
+ kwargs[k] = equalTo kwarg unless kwarg instanceof Matcher
43
+
44
+ new Matcher
45
+ kwargs: kwargs
46
+ _matches: (v,msg) ->
47
+ unless v? and typeof v is "object"
48
+ msg.appendText("was").appendValue(v)
49
+ return false
50
+ else
51
+ hasError = false
52
+ for p,m of @kwargs
53
+ unless v.hasOwnProperty p
54
+ if hasError
55
+ msg.appendText ","
56
+
57
+ msg.appendRawValue(p).appendText("was not defined")
58
+ hasError = true
59
+ else
60
+ val = v[p]
61
+ msgtmp = new Description()
62
+ unless m.matches val, msgtmp
63
+ msg.appendText(",") if hasError
64
+ msg.appendRawValue(p).appendText(": ").appendText msgtmp
65
+ hasError = true
66
+
67
+ msg.appendText("was").appendValue(v) unless hasError
68
+ return not hasError
69
+
70
+ _describeTo: (msg) ->
71
+ props = h4q.sortProperties @kwargs
72
+ l = props.length
73
+ msg.appendText("an Object which has properties")
74
+ for p,i in props
75
+ m = @kwargs[p]
76
+ if i is l - 1 then msg.appendText("and")
77
+ else if i > 0 then msg.appendText(",")
78
+
79
+ msg.appendRawValue(p).appendText(":").appendDescriptionOf(m)
80
+
81
+ h4q.hasMethod = (f, r, a, t) ->
82
+ unless f? and typeof f is "string"
83
+ throw new Error message: "hasMethod expect a function name"
84
+
85
+ r = equalTo r if r? and not (r instanceof Matcher)
86
+
87
+ new Matcher
88
+ method: f
89
+ returnsMatcher: r
90
+ throwsMatcher: t
91
+ checkThrows: false
92
+ noArgs: false
93
+ arguments: a
94
+ returns: (m) ->
95
+ m = equalTo m unless m instanceof Matcher
96
+ @returnsMatcher = m
97
+ this
98
+ withoutArgs: ->
99
+ @noArgs = true
100
+ this
101
+ withArgs: ->
102
+ @arguments = h4q.argumentsToArray arguments
103
+ this
104
+ throwsError: (m) ->
105
+ m = h4q.notNullValue() unless m?
106
+ m = h4q.equalTo m unless m instanceof Matcher
107
+ @throwsMatcher = m
108
+ @checkThrows = true
109
+ this
110
+ _matches: (v,msg) ->
111
+ unless v? and typeof v is "object"
112
+ msg.appendValue(v).appendText("is not an Object")
113
+ false
114
+ else
115
+ if typeof v[@method] is "function"
116
+ if @checkThrows
117
+ res = null
118
+ try
119
+ v[ @method ].apply v, if @noArgs then [] else @arguments
120
+ catch e
121
+ if @throwsMatcher
122
+ errorMsg = new Description()
123
+ res = @throwsMatcher.matches e, errorMsg
124
+ unless res
125
+ msg.appendText("an exception was thrown but")
126
+ .appendText(errorMsg)
127
+ msg.diff = errorMsg.diff
128
+ else
129
+ msg.appendText("an exception was thrown and")
130
+ .appendText( errorMsg)
131
+ else
132
+ msg.appendValue(e).appendText( "was thrown" )
133
+ res = true
134
+
135
+ unless res?
136
+ msg.appendText("no exception was thrown")
137
+ res = false
138
+
139
+ res
140
+
141
+ else if @returnsMatcher
142
+ res = v[@method].apply v, if @noArgs then [] else @arguments
143
+ rmsg = new Description()
144
+ mres = @returnsMatcher.matches res, rmsg
145
+
146
+ msg.diff = rmsg.diff
147
+ msg.appendText("was")
148
+ .appendValue( v )
149
+ .appendText("of which method")
150
+ .appendRawValue(@method)
151
+ .appendText("returns")
152
+ .appendText( rmsg )
153
+ mres
154
+ else
155
+ msg.appendText("was").appendValue(v)
156
+ true
157
+ else
158
+ unless v[@method]?
159
+ msg.appendValue(v)
160
+ .appendText("do not have a")
161
+ .appendRawValue(@method)
162
+ .appendText("method")
163
+ false
164
+ else
165
+ msg.appendValue(v)
166
+ .appendText("have a")
167
+ .appendRawValue(@method)
168
+ .appendText("property, but it is not a ")
169
+ false
170
+
171
+ _describeTo: (msg) ->
172
+ msg.appendText("an Object with a method").appendRawValue(@method)
173
+
174
+ if @checkThrows
175
+ if @throwsMatcher
176
+ msg.appendText("which throw")
177
+ .appendDescriptionOf(@throwsMatcher)
178
+ .appendText("when called")
179
+ else
180
+ msg.appendText("which throw an exception when called")
181
+
182
+ if not @noArgs and @arguments? and @arguments.length > 0
183
+ msg.appendText("with")
184
+ .appendValue(@arguments)
185
+ .appendText("as arguments")
186
+ else
187
+ msg.appendText("without arguments")
188
+
189
+ else if @returnsMatcher?
190
+ msg.appendText("which returns")
191
+ .appendDescriptionOf(@returnsMatcher)
192
+ .appendText("when called")
193
+ if not @noArgs and @arguments? and @arguments.length > 0
194
+ msg.appendText("with")
195
+ .appendValue(@arguments)
196
+ .appendText("as arguments")
197
+ else
198
+ msg.appendText("without arguments")
199
+
200
+ h4q.propertiesCount = (l) ->
201
+ if isNaN l
202
+ throw new Error message: "length argument must be a valid number"
203
+ if l < 0
204
+ throw new Error message: "length argument must greater than or equal to 0"
205
+
206
+ new Matcher
207
+ length: l
208
+ _matches: (v,msg) ->
209
+ unless v? and typeof v is "object" and not (v instanceof Array)
210
+ msg.appendText("was").appendValue(v)
211
+ false
212
+ else
213
+ l = h4q.sortProperties(v).length
214
+ if @length is l
215
+ msg.appendText("was").appendValue(v)
216
+ true
217
+ else
218
+ msg.appendText("was an Object with")
219
+ .appendValue(l)
220
+ .appendText(if l > 1 then "properties" else "property")
221
+ false
222
+
223
+ _describeTo: (msg) ->
224
+ msg.appendText("an Object with")
225
+ .appendValue(@length)
226
+ .appendText(if @length > 1 then "properties" else "property")
227
+
228
+ h4q.instanceOf = (t) ->
229
+ unless t?
230
+ throw new Error message: "type argument is mandatory"
231
+
232
+ new Matcher
233
+ type: t
234
+ _matches: (v,msg) ->
235
+ vtype = h4q.getType v
236
+ t = h4q.getTypeName @type
237
+ if t is vtype
238
+ msg.appendText("was #{aPrefix vtype}").appendRawValue(vtype)
239
+ true
240
+ else if v instanceof @type
241
+ msg.appendText("was #{aPrefix t}").appendRawValue(t)
242
+ true
243
+ else
244
+ msg.appendText("was #{aPrefix vtype}").appendRawValue(vtype)
245
+ false
246
+
247
+ _describeTo: (msg) ->
248
+ msg.appendText(aPrefix getTypeName @type)
249
+ .appendRawValue(getTypeName @type)