hamcrest4qunit 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +38 -0
- data/Rakefile +2 -0
- data/app/assets/javascripts/hamcrest4qunit/description.js.coffee +23 -0
- data/app/assets/javascripts/hamcrest4qunit/h4q.js.coffee +158 -0
- data/app/assets/javascripts/hamcrest4qunit/index.js.coffee +7 -0
- data/app/assets/javascripts/hamcrest4qunit/matcher.js.coffee +31 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/arrays.js.coffee +221 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/compounds.js.coffee +134 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/core.js.coffee +126 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/dates.js.coffee +166 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/dom.js.coffee +73 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/functions.js.coffee +142 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/index.js.coffee +1 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/numbers.js.coffee +177 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/objects.js.coffee +249 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/strings.js.coffee +123 -0
- data/app/assets/javascripts/hamcrest4qunit/test/index.js +920 -0
- data/hamcrest4qunit.gemspec +20 -0
- data/lib/hamcrest4qunit.rb +11 -0
- data/lib/hamcrest4qunit/engine.rb +9 -0
- data/lib/hamcrest4qunit/version.rb +3 -0
- metadata +122 -0
@@ -0,0 +1,123 @@
|
|
1
|
+
h4q.startsWith = (s) ->
|
2
|
+
unless s? then throw new Error message: "startsWith must receive an argument"
|
3
|
+
|
4
|
+
new Matcher
|
5
|
+
start: s
|
6
|
+
_matches: (v,msg) ->
|
7
|
+
msg.appendText("was").appendValue(v)
|
8
|
+
|
9
|
+
unless v? and typeof v is "string"
|
10
|
+
false
|
11
|
+
else
|
12
|
+
v.indexOf(@start) is 0
|
13
|
+
|
14
|
+
_describeTo: (msg) ->
|
15
|
+
msg.appendText("a String starting with").appendValue(@start)
|
16
|
+
|
17
|
+
h4q.endsWith = (s) ->
|
18
|
+
unless s?
|
19
|
+
throw new Error message: "endsWith must receive an argument"
|
20
|
+
|
21
|
+
new Matcher
|
22
|
+
end: s
|
23
|
+
_matches: (v,msg) ->
|
24
|
+
msg.appendText("was").appendValue(v)
|
25
|
+
unless v? and typeof v is "string"
|
26
|
+
false
|
27
|
+
else
|
28
|
+
v.indexOf(@end) is v.length - @end.length
|
29
|
+
|
30
|
+
_describeTo: (msg) ->
|
31
|
+
msg.appendText("a String ending with").appendValue(@end)
|
32
|
+
|
33
|
+
h4q.contains = (s) ->
|
34
|
+
unless s?
|
35
|
+
throw new Error message: "endsWith must receive an argument"
|
36
|
+
|
37
|
+
new Matcher
|
38
|
+
search: s
|
39
|
+
_matches: (v,msg) ->
|
40
|
+
msg.appendText("was").appendValue(v)
|
41
|
+
unless v? and typeof v is "string"
|
42
|
+
false
|
43
|
+
else
|
44
|
+
v.indexOf(@search) isnt -1
|
45
|
+
|
46
|
+
_describeTo: (msg) ->
|
47
|
+
msg.appendText("a String containing").appendValue(@search)
|
48
|
+
|
49
|
+
h4q.stringWithLength = (l) ->
|
50
|
+
|
51
|
+
if isNaN l
|
52
|
+
throw new Error message: "length argument must be a valid number"
|
53
|
+
if l < 0
|
54
|
+
throw new Error message: "length argument must be greater than
|
55
|
+
or equal to 0"
|
56
|
+
|
57
|
+
return h4q.emptyString() if l is 0
|
58
|
+
|
59
|
+
new Matcher
|
60
|
+
length: l
|
61
|
+
_matches: (v,msg) ->
|
62
|
+
unless v?
|
63
|
+
msg.appendText("was").appendValue(v)
|
64
|
+
false
|
65
|
+
else if typeof v isnt "string"
|
66
|
+
msg.appendText("was").appendValue(v)
|
67
|
+
false
|
68
|
+
else
|
69
|
+
if v.length is @length
|
70
|
+
msg.appendText("was").appendValue(v)
|
71
|
+
true
|
72
|
+
else
|
73
|
+
msg.appendValue(v)
|
74
|
+
.appendText("is a String of length")
|
75
|
+
.appendValue(v.length)
|
76
|
+
false
|
77
|
+
|
78
|
+
_describeTo: (msg) ->
|
79
|
+
msg.appendText("a String with a length of").appendValue(@length)
|
80
|
+
|
81
|
+
h4q.emptyString = ->
|
82
|
+
new Matcher
|
83
|
+
_matches: (v,msg) ->
|
84
|
+
|
85
|
+
unless v?
|
86
|
+
msg.appendText("was").appendValue(v)
|
87
|
+
false
|
88
|
+
else if typeof v isnt "string"
|
89
|
+
msg.appendText("was").appendValue(v)
|
90
|
+
false
|
91
|
+
else
|
92
|
+
if v.length is 0
|
93
|
+
msg.appendText("was").appendValue(v)
|
94
|
+
true
|
95
|
+
else
|
96
|
+
msg.appendValue(v)
|
97
|
+
.appendText("is a String of length")
|
98
|
+
.appendValue(v.length)
|
99
|
+
false
|
100
|
+
|
101
|
+
_describeTo: (msg) ->
|
102
|
+
msg.appendText("an empty String")
|
103
|
+
|
104
|
+
h4q.matchRe = (re) ->
|
105
|
+
unless re?
|
106
|
+
throw new Error message: "The 're' argument can't be null"
|
107
|
+
unless re instanceof RegExp or typeof re is "string"
|
108
|
+
throw new Error message: "the re argument must be a regexp or a string"
|
109
|
+
if typeof re is "string"
|
110
|
+
re = new RegExp re, "gi"
|
111
|
+
|
112
|
+
new Matcher
|
113
|
+
re: re
|
114
|
+
_matches: (v,msg) ->
|
115
|
+
msg.appendText("was").appendValue(v)
|
116
|
+
unless v? and typeof v is "string"
|
117
|
+
false
|
118
|
+
else
|
119
|
+
@re.exec v
|
120
|
+
|
121
|
+
_describeTo: (msg) ->
|
122
|
+
msg.appendText("a String which match").appendRawValue(String @re)
|
123
|
+
|
@@ -0,0 +1,920 @@
|
|
1
|
+
QUnit.jsDump.parsers["node"] = function(v){
|
2
|
+
return nodeToString( v );
|
3
|
+
}
|
4
|
+
|
5
|
+
module("hamcrest core");
|
6
|
+
|
7
|
+
test( "assertThat polymorphism", function(){
|
8
|
+
raises ( function(){ assertThat(); }, "assertThat should throw an error for empty assertions" );
|
9
|
+
assertThat( true );
|
10
|
+
assertThat( "foo", "foo" );
|
11
|
+
assertThat( true, true, "assertThat with a custom message" );
|
12
|
+
});
|
13
|
+
|
14
|
+
test( "Matcher object", function(){
|
15
|
+
raises( function(){
|
16
|
+
new Matcher();
|
17
|
+
}, "new Matcher without options in constructor" );
|
18
|
+
raises( function(){
|
19
|
+
new Matcher({'_matches':function(){} });
|
20
|
+
}, "new Matcher with a missing handler");
|
21
|
+
raises( function(){
|
22
|
+
new Matcher({'_matches':"foo", '_describeTo':function(){} });
|
23
|
+
}, "new Matcher with an invalid handler");
|
24
|
+
raises( function(){
|
25
|
+
new Matcher({'_matches':function(){}, '_describeTo':15 });
|
26
|
+
}, "new Matcher with an invalid handler");
|
27
|
+
|
28
|
+
notEqual( new Matcher({
|
29
|
+
'_matches':function(){},
|
30
|
+
'_describeTo':function(){}
|
31
|
+
}),
|
32
|
+
null );
|
33
|
+
});
|
34
|
+
|
35
|
+
test( "Description object", function(){
|
36
|
+
var d = new Description()
|
37
|
+
|
38
|
+
notEqual( d, null );
|
39
|
+
notEqual( d.message, null );
|
40
|
+
equals( d.diff, null );
|
41
|
+
equals( d.hasDiff(), false );
|
42
|
+
|
43
|
+
d.diff = "foo";
|
44
|
+
equals( d.diff, "foo" );
|
45
|
+
equals( d.hasDiff(), true );
|
46
|
+
|
47
|
+
d.appendText("foo");
|
48
|
+
equals( d.message, "foo" );
|
49
|
+
d.appendText("foo");
|
50
|
+
equals( d.message, "foo foo" );
|
51
|
+
|
52
|
+
d.clearMessage();
|
53
|
+
equals( d.message, "" );
|
54
|
+
|
55
|
+
d.appendValue( "foo" );
|
56
|
+
equals( d.message, "<span class='value'>\"foo\"</span>" );
|
57
|
+
|
58
|
+
d.clearMessage();
|
59
|
+
d.appendRawValue( "foo" );
|
60
|
+
equals( d.message, "<span class='value'>foo</span>" );
|
61
|
+
|
62
|
+
var m = new Matcher({'_matches':function(){}, '_describeTo':function(msg){
|
63
|
+
msg.appendText( "foo" );
|
64
|
+
}})
|
65
|
+
d.clearMessage();
|
66
|
+
d.appendDescriptionOf( m );
|
67
|
+
|
68
|
+
equals( d.message, "foo" );
|
69
|
+
});
|
70
|
+
|
71
|
+
module( "hamcrest utilities");
|
72
|
+
|
73
|
+
test( "nodeToString function", function(){
|
74
|
+
|
75
|
+
raises ( function(){
|
76
|
+
nodeToString();
|
77
|
+
}, "nodeToString fail when no argument");
|
78
|
+
|
79
|
+
raises ( function(){
|
80
|
+
nodeToString( "foo" );
|
81
|
+
}, "nodeToString fail argument is not a node");
|
82
|
+
|
83
|
+
var n;
|
84
|
+
|
85
|
+
n = document.createElement( "div" );
|
86
|
+
assertThat( nodeToString, returns( escapeHtml("<div></div>") ).withArgs( n ) );
|
87
|
+
|
88
|
+
n = document.createElement( "div" );
|
89
|
+
n.setAttribute( "class", "foo" )
|
90
|
+
assertThat( nodeToString, returns( escapeHtml("<div class=\"foo\"></div>") ).withArgs( n ) );
|
91
|
+
|
92
|
+
n = document.createElement( "div" );
|
93
|
+
n.textContent = "abc"
|
94
|
+
assertThat( nodeToString, returns( escapeHtml("<div>abc</div>") ).withArgs( n ) );
|
95
|
+
|
96
|
+
n = document.createElement( "div" );
|
97
|
+
n.innerHTML = "<h1>abc</h1>def";
|
98
|
+
assertThat( nodeToString, returns( escapeHtml("<div>\n <h1>abc</h1>\n def\n</div>") ).withArgs( n ) );
|
99
|
+
|
100
|
+
n = document.createElement( "div" );
|
101
|
+
n.innerHTML = "<h1>abc</h1><h2>def</h2><h3><span>ghi</span>jkl<span>mno</span></h3>";
|
102
|
+
assertThat( nodeToString, returns( escapeHtml("<div>\n <h1>abc</h1>\n <h2>def</h2>\n <h3>\n <span>ghi</span>\n jkl\n <span>mno</span>\n </h3>\n</div>") ).withArgs( n ) );
|
103
|
+
|
104
|
+
n = document.createElement( "div" );
|
105
|
+
n.innerHTML = "<h1 class=\"foo\">abc</h1><h2 id=\"foo\" class=\"foo\">def</h2><h3><span>ghi</span>jkl<span class=\"foo\">mno</span></h3>";
|
106
|
+
assertThat( nodeToString, returns( escapeHtml("<div>\n <h1 class=\"foo\">abc</h1>\n <h2 class=\"foo\" id=\"foo\">def</h2>\n <h3>\n <span>ghi</span>\n jkl\n <span class=\"foo\">mno</span>\n </h3>\n</div>") ).withArgs( n ) );
|
107
|
+
|
108
|
+
});
|
109
|
+
|
110
|
+
module("hamcrest core matchers");
|
111
|
+
test("not matcher", function(){
|
112
|
+
|
113
|
+
assertThat( true, not(false) );
|
114
|
+
assertThat( 5, not(10) );
|
115
|
+
assertThat( 5, not( equalTo( 10 ) ) );
|
116
|
+
|
117
|
+
// forced fail
|
118
|
+
// assertThat( 10, not( equalTo( 10 ) ) );
|
119
|
+
// assertThat( "A sample text", equalTo( "A SamPLE TeXT" ) );
|
120
|
+
});
|
121
|
+
|
122
|
+
test("equalTo matcher", function(){
|
123
|
+
assertThat( 10, equalTo( 10 ) );
|
124
|
+
assertThat( "10", equalTo( 10 ) );
|
125
|
+
assertThat( 5, not( equalTo( 10 ) ) );
|
126
|
+
assertThat( [1,"foo",false], equalTo( [1,"foo",false] ) );
|
127
|
+
|
128
|
+
// forced fail
|
129
|
+
// assertThat( 5, equalTo( 10 ) );
|
130
|
+
// assertThat( [1,"foo",true], equalTo( [14,"foo",false] ) );
|
131
|
+
});
|
132
|
+
|
133
|
+
test("nullValue matcher", function(){
|
134
|
+
|
135
|
+
assertThat( null, nullValue() );
|
136
|
+
assertThat( {}, not(nullValue()) );
|
137
|
+
|
138
|
+
// forced fail
|
139
|
+
// assertThat( {}, nullValue() );
|
140
|
+
});
|
141
|
+
|
142
|
+
test("notNullValue matcher", function(){
|
143
|
+
|
144
|
+
assertThat( null, not(notNullValue()) );
|
145
|
+
assertThat( {}, notNullValue() );
|
146
|
+
|
147
|
+
// forced fail
|
148
|
+
// assertThat( null, notNullValue() );
|
149
|
+
});
|
150
|
+
|
151
|
+
test("anything matcher", function(){
|
152
|
+
|
153
|
+
assertThat( 10, anything() );
|
154
|
+
assertThat( true, anything() );
|
155
|
+
assertThat( null, anything() );
|
156
|
+
assertThat( {}, anything() );
|
157
|
+
});
|
158
|
+
test("isA matcher", function(){
|
159
|
+
|
160
|
+
assertThat( 10, isA( "number" ) );
|
161
|
+
assertThat( "foo", isA( "string" ) );
|
162
|
+
assertThat( false, isA( "boolean" ) );
|
163
|
+
assertThat( null, not( isA( "number" ) ) );
|
164
|
+
});
|
165
|
+
|
166
|
+
test( "describedAs matcher", function(){
|
167
|
+
|
168
|
+
raises( function(){
|
169
|
+
describedAs();
|
170
|
+
}, "describedAs expect two arguments" );
|
171
|
+
|
172
|
+
raises( function(){
|
173
|
+
describedAs( "foo", "foo" );
|
174
|
+
}, "describedAs first argument must be a valid matcher" );
|
175
|
+
|
176
|
+
raises( function(){
|
177
|
+
describedAs( new Matcher({'_matches':function(){},'_describeTo':function(){} }) );
|
178
|
+
}, "describedAs second argument must be a valid string" );
|
179
|
+
|
180
|
+
var desc = new Description();
|
181
|
+
var m = describedAs(anything(), "a message");
|
182
|
+
assertThat( m, hasMethod( "matches" ).returns(true).withArgs("foo",new Description()) );
|
183
|
+
m.describeTo( desc );
|
184
|
+
assertThat( desc, hasProperty("message", equalTo("a message") ) );
|
185
|
+
|
186
|
+
desc = new Description();
|
187
|
+
m = describedAs(equalTo(5), "a message");
|
188
|
+
assertThat( m, hasMethod( "matches" ).returns(false).withArgs(10,new Description()) );
|
189
|
+
m.describeTo( desc );
|
190
|
+
assertThat( desc, hasProperty("message", equalTo("a message") ) );
|
191
|
+
|
192
|
+
desc = new Description();
|
193
|
+
m = describedAs(anything(), "a message $0", "foo");
|
194
|
+
m.describeTo( desc );
|
195
|
+
assertThat( desc, hasProperty("message", equalTo("a message foo") ) );
|
196
|
+
|
197
|
+
desc = new Description();
|
198
|
+
m = describedAs(anything(), "a message ${foo}, $0", {'foo':"bar"}, "foo" );
|
199
|
+
m.describeTo( desc );
|
200
|
+
assertThat( desc, hasProperty("message", equalTo("a message bar, foo") ) );
|
201
|
+
|
202
|
+
});
|
203
|
+
|
204
|
+
module( "hamcrest compound matchers");
|
205
|
+
|
206
|
+
test( "allOf matcher", function(){
|
207
|
+
assertThat( 10, allOf( notNullValue(), equalTo(10) ) );
|
208
|
+
assertThat( 10, allOf( notNullValue(), 10 ) );
|
209
|
+
assertThat( 10, not( allOf( nullValue(), equalTo(10) ) ) );
|
210
|
+
assertThat( 5, allOf( notNullValue(), not( equalTo(10) ) ) );
|
211
|
+
|
212
|
+
// forced fail
|
213
|
+
// assertThat( 5, allOf( notNullValue(), not( equalTo(5) ) ) );
|
214
|
+
} );
|
215
|
+
|
216
|
+
test( "anyOf matcher", function(){
|
217
|
+
assertThat( 10, anyOf( equalTo(10), not( equalTo(5) ) ) );
|
218
|
+
assertThat( 10, anyOf( 1, 2, 3, 4, allOf(notNullValue(), 10 ) ) );
|
219
|
+
assertThat( 10, not( anyOf( 1,2,3,4 ) ) );
|
220
|
+
|
221
|
+
// forced fail
|
222
|
+
// assertThat( 5, anyOf( 1, 2, 3, 4 ) );
|
223
|
+
});
|
224
|
+
|
225
|
+
test("both...and matcher",function(){
|
226
|
+
|
227
|
+
raises( function(){ assertThat( 16, both(16) ); }, "calling both without the and fail" );
|
228
|
+
|
229
|
+
assertThat( 16, both( notNullValue() ).and( equalTo(16) ) );
|
230
|
+
assertThat( 16, both( notNullValue() ).and( 16 ) );
|
231
|
+
|
232
|
+
assertThat( 16, not( both( notNullValue() ).and( closeTo( 12,1 ) ) ) );
|
233
|
+
assertThat( null, not( both( notNullValue() ).and( 12 ) ) );
|
234
|
+
|
235
|
+
});
|
236
|
+
|
237
|
+
test("either...or matcher",function(){
|
238
|
+
|
239
|
+
raises( function(){ assertThat( 16, either(16) ); }, "calling either without the or fail" );
|
240
|
+
|
241
|
+
assertThat( 16, either( notNullValue() ).or( equalTo(16) ) );
|
242
|
+
assertThat( 16, either( true ).or( 16 ) );
|
243
|
+
});
|
244
|
+
|
245
|
+
module( "hamcrest number matchers" );
|
246
|
+
|
247
|
+
test( "nanValue matcher", function(){
|
248
|
+
assertThat( NaN, nanValue() );
|
249
|
+
assertThat( 12, not( nanValue() ) );
|
250
|
+
assertThat( "foo", nanValue() );
|
251
|
+
});
|
252
|
+
test( "notNanValue matcher", function(){
|
253
|
+
assertThat( 12, notNanValue() );
|
254
|
+
assertThat( NaN, not( notNanValue() ) );
|
255
|
+
});
|
256
|
+
|
257
|
+
test( "between matcher", function(){
|
258
|
+
raises( function(){
|
259
|
+
assertThat( 12, between( 16, 4 ) );
|
260
|
+
}, "first argument is lower than the second" );
|
261
|
+
raises( function(){
|
262
|
+
assertThat( 12, between( NaN, 4 ) );
|
263
|
+
}, "first argument is NaN" );
|
264
|
+
raises( function(){
|
265
|
+
assertThat( 12, between( 4, NaN ) );
|
266
|
+
}, "second argument is NaN" );
|
267
|
+
|
268
|
+
|
269
|
+
assertThat( 16, between( 12, 18 ) );
|
270
|
+
assertThat( 12, not( between( 12, 18 ) ) );
|
271
|
+
assertThat( 12, between( 12, 18 ).inclusive() );
|
272
|
+
assertThat( 18, between( 12, 18 ).inclusive() );
|
273
|
+
} )
|
274
|
+
|
275
|
+
test( "closeTo matcher", function(){
|
276
|
+
raises( function(){
|
277
|
+
assertThat( 12, closeTo( NaN, 4 ) );
|
278
|
+
}, "first argument is NaN" );
|
279
|
+
raises( function(){
|
280
|
+
assertThat( 12, closeTo( 4, NaN ) );
|
281
|
+
}, "second argument is NaN" );
|
282
|
+
|
283
|
+
assertThat( 4, closeTo( 5, 1 ) );
|
284
|
+
assertThat( 3, not( closeTo( 5, 1 ) ) );
|
285
|
+
assertThat( 1.9, closeTo( 2, 0.1 ) );
|
286
|
+
})
|
287
|
+
|
288
|
+
test( "greaterThan matcher", function(){
|
289
|
+
raises( function(){
|
290
|
+
assertThat( 12, greaterThan( NaN ) );
|
291
|
+
}, "comparison value is NaN" );
|
292
|
+
|
293
|
+
assertThat( 12, greaterThan( 8 ) );
|
294
|
+
assertThat( 12, greaterThan( 12 ).inclusive() );
|
295
|
+
assertThat( 12, not(greaterThan( 18 )) );
|
296
|
+
})
|
297
|
+
test( "greaterThanOrEqualTo matcher", function(){
|
298
|
+
raises( function(){
|
299
|
+
assertThat( 12, greaterThanOrEqualTo( NaN ) );
|
300
|
+
}, "comparison value is NaN" );
|
301
|
+
|
302
|
+
assertThat( 12, greaterThanOrEqualTo( 12 ) );
|
303
|
+
assertThat( 12, not( greaterThanOrEqualTo( 19 ) ) );
|
304
|
+
})
|
305
|
+
test( "atLeast matcher", function(){
|
306
|
+
raises( function(){
|
307
|
+
assertThat( 12, atLeast( NaN ) );
|
308
|
+
}, "comparison value is NaN" );
|
309
|
+
|
310
|
+
assertThat( 12, atLeast( 12 ) );
|
311
|
+
assertThat( 12, not( atLeast( 19 ) ) );
|
312
|
+
})
|
313
|
+
|
314
|
+
test( "lowerThan matcher", function(){
|
315
|
+
raises( function(){
|
316
|
+
assertThat( 12, lowerThan( NaN ) );
|
317
|
+
}, "comparison value is NaN" );
|
318
|
+
|
319
|
+
assertThat( 12, lowerThan( 18 ) );
|
320
|
+
assertThat( 12, lowerThan( 12 ).inclusive() );
|
321
|
+
assertThat( 12, not(lowerThan( 11 )) );
|
322
|
+
})
|
323
|
+
test( "lowerThanOrEqualTo matcher", function(){
|
324
|
+
raises( function(){
|
325
|
+
assertThat( 12, lowerThanOrEqualTo( NaN ) );
|
326
|
+
}, "comparison value is NaN" );
|
327
|
+
|
328
|
+
assertThat( 12, lowerThanOrEqualTo( 12 ) );
|
329
|
+
assertThat( 12, not( lowerThanOrEqualTo( 11 ) ) );
|
330
|
+
})
|
331
|
+
test( "atMost matcher", function(){
|
332
|
+
raises( function(){
|
333
|
+
assertThat( 12, atMost( NaN ) );
|
334
|
+
}, "comparison value is NaN" );
|
335
|
+
|
336
|
+
assertThat( 12, atMost( 12 ) );
|
337
|
+
assertThat( 12, not( atMost( 11 ) ) );
|
338
|
+
})
|
339
|
+
|
340
|
+
module("hamcrest array matchers");
|
341
|
+
|
342
|
+
test( "emptyArray matcher", function(){
|
343
|
+
assertThat( [], emptyArray() );
|
344
|
+
assertThat( [5], not( emptyArray() ) );
|
345
|
+
assertThat( null, not( emptyArray() ) );
|
346
|
+
assertThat( "", not( emptyArray() ) );
|
347
|
+
assertThat( "foo", not( emptyArray() ) );
|
348
|
+
})
|
349
|
+
test( "arrayWithLength matcher", function(){
|
350
|
+
raises( function(){
|
351
|
+
assertThat( [], arrayWithLength() );
|
352
|
+
}, "length argument must be a valid number");
|
353
|
+
raises( function(){
|
354
|
+
assertThat( [], arrayWithLength(-10) );
|
355
|
+
}, "length argument must be greater than or equal to 0");
|
356
|
+
|
357
|
+
assertThat( [], arrayWithLength(0) );
|
358
|
+
assertThat( [5], not( arrayWithLength(0) ) );
|
359
|
+
assertThat( [], not( arrayWithLength(4) ) );
|
360
|
+
assertThat( [1,2,3,4], arrayWithLength(4) );
|
361
|
+
})
|
362
|
+
|
363
|
+
test( "array matcher", function(){
|
364
|
+
assertThat( [], array() );
|
365
|
+
assertThat( [1, 1.9, 4], array( equalTo(1), closeTo(2, 0.1), equalTo(4) ) );
|
366
|
+
assertThat( [1,2,3], array(1,2,3) );
|
367
|
+
assertThat( [], not( array(1,2,3) ) );
|
368
|
+
assertThat( [1,2], not( array(4,6) ) );
|
369
|
+
assertThat( null, not( array(4,6) ) );
|
370
|
+
assertThat( "46", not( array(4,6) ) );
|
371
|
+
})
|
372
|
+
|
373
|
+
test( "everyItem matcher", function(){
|
374
|
+
raises( function(){
|
375
|
+
assertThat([1,2,3], everyItem() );
|
376
|
+
}, "everyItem must have an argument" );
|
377
|
+
|
378
|
+
assertThat( [1,2,3], everyItem(isA("number")) );
|
379
|
+
assertThat( [1,2,"foo"], not( everyItem(isA("number"))) );
|
380
|
+
assertThat( [1,1,1,1], everyItem(1) );
|
381
|
+
assertThat( null, not(everyItem(isA("number"))) );
|
382
|
+
assertThat( "foo", not(everyItem(isA("number"))) );
|
383
|
+
})
|
384
|
+
|
385
|
+
test( "hasItem matcher", function(){
|
386
|
+
raises( function(){
|
387
|
+
assertThat([1,2,3], hasItem() );
|
388
|
+
}, "hasItem must have an argument" );
|
389
|
+
|
390
|
+
assertThat( [1,"foo",false], hasItem(isA("number")) );
|
391
|
+
assertThat( ["foo","bar"], not( hasItem(isA("number"))) );
|
392
|
+
assertThat( [1,2,3,4], hasItem(1) );
|
393
|
+
assertThat( null, not(hasItem(isA("number"))) );
|
394
|
+
assertThat( "foo", not(hasItem(isA("number"))) );
|
395
|
+
})
|
396
|
+
|
397
|
+
test( "hasItems matcher", function(){
|
398
|
+
raises( function(){
|
399
|
+
assertThat([1,2,3], hasItems() );
|
400
|
+
}, "hasItems must have at least one argument" );
|
401
|
+
|
402
|
+
assertThat( [ 1, "foo", false], hasItems( isA("number"), "foo" ) );
|
403
|
+
assertThat( [ 1, "foo", false], not( hasItems( isA("number"), isA("object") ) ) );
|
404
|
+
assertThat( [ "bla", "foo", false], not( hasItems( isA("number"), isA("object") ) ) );
|
405
|
+
assertThat( null, not( hasItems( isA("number"), isA("object") ) ) );
|
406
|
+
assertThat( false, not( hasItems( isA("number"), isA("object") ) ) );
|
407
|
+
})
|
408
|
+
|
409
|
+
module("hamcrest string matchers")
|
410
|
+
|
411
|
+
test( "startsWith matcher", function(){
|
412
|
+
raises( function(){
|
413
|
+
assertThat( "foo", startsWith() );
|
414
|
+
}, "startsWith must have an argument");
|
415
|
+
|
416
|
+
assertThat( "hello world", startsWith("hello") );
|
417
|
+
assertThat( "Hello World", not( startsWith("hello") ) );
|
418
|
+
assertThat( null, not( startsWith("hello") ) );
|
419
|
+
assertThat( [1,2,3], not( startsWith("hello") ) );
|
420
|
+
})
|
421
|
+
|
422
|
+
test( "endsWith matcher", function(){
|
423
|
+
raises( function(){
|
424
|
+
assertThat( "foo", endsWith() );
|
425
|
+
}, "endsWith must have an argument");
|
426
|
+
|
427
|
+
assertThat( "hello world", endsWith("world") );
|
428
|
+
assertThat( "Hello World", not( endsWith("world") ) );
|
429
|
+
assertThat( null, not( endsWith("world") ) );
|
430
|
+
assertThat( [1,2,3], not( endsWith("world") ) );
|
431
|
+
})
|
432
|
+
|
433
|
+
test( "contains matcher", function(){
|
434
|
+
raises( function(){
|
435
|
+
assertThat( "foo", contains() );
|
436
|
+
}, "contains must have an argument");
|
437
|
+
|
438
|
+
assertThat( "hello world", contains( "o w" ) );
|
439
|
+
assertThat( "Hello World", not( contains( "o w" ) ) );
|
440
|
+
assertThat( null, not( contains( "ull" ) ) );
|
441
|
+
assertThat( [1,2,3], not( contains( "1,2" ) ) );
|
442
|
+
})
|
443
|
+
|
444
|
+
test( "emptyString matcher", function(){
|
445
|
+
|
446
|
+
assertThat( "", emptyString() );
|
447
|
+
assertThat( "foo", not( emptyString() ) );
|
448
|
+
assertThat( null, not( emptyString() ) );
|
449
|
+
assertThat( [], not( emptyString() ) );
|
450
|
+
})
|
451
|
+
|
452
|
+
test( "stringWithLength matcher", function(){
|
453
|
+
raises( function(){
|
454
|
+
assertThat( 'foo', stringWithLength() );
|
455
|
+
}, "length argument must be a valid number");
|
456
|
+
raises( function(){
|
457
|
+
assertThat( 'foo', stringWithLength(-10) );
|
458
|
+
}, "length argument must be a valid number");
|
459
|
+
|
460
|
+
assertThat( "", stringWithLength(0) );
|
461
|
+
assertThat( "foo", stringWithLength( 3 ) );
|
462
|
+
assertThat( "foo", not( stringWithLength( 2 ) ) );
|
463
|
+
})
|
464
|
+
|
465
|
+
test( "matchRe matcher", function(){
|
466
|
+
raises( function(){
|
467
|
+
assertThat( "foo", matchRe() );
|
468
|
+
}, "matchRe must receive an argument");
|
469
|
+
raises( function(){
|
470
|
+
assertThat( "foo", matchRe(15) );
|
471
|
+
}, "matchRe must receive either a string or a regexp");
|
472
|
+
|
473
|
+
assertThat( "foo", matchRe( "^foo$" ) )
|
474
|
+
assertThat( "foo", matchRe( new RegExp( "^foo$" ) ) );
|
475
|
+
assertThat( null, not( matchRe( new RegExp( "ull$" ) ) ) );
|
476
|
+
assertThat( [1,2,3], not( matchRe( new RegExp( "1,2" ) ) ) );
|
477
|
+
})
|
478
|
+
|
479
|
+
module( "hamcrest object matchers" )
|
480
|
+
test( "strictlyEqualTo matcher", function(){
|
481
|
+
assertThat( 10, strictlyEqualTo( 10 ) );
|
482
|
+
assertThat( "foo", strictlyEqualTo( "foo" ) );
|
483
|
+
|
484
|
+
var o1 = {'foo':"bar",id:0};
|
485
|
+
var o2 = {'foo':"bar",id:0};
|
486
|
+
var a1 = ["foo",15,false];
|
487
|
+
var a2 = ["foo",15,false];
|
488
|
+
|
489
|
+
assertThat( o1, strictlyEqualTo(o1) );
|
490
|
+
assertThat( o1, not(strictlyEqualTo(o2)) );
|
491
|
+
assertThat( a1, strictlyEqualTo(a1) );
|
492
|
+
assertThat( a1, not(strictlyEqualTo(a2)) );
|
493
|
+
})
|
494
|
+
|
495
|
+
test( "hasProperty matcher", function(){
|
496
|
+
var o = {
|
497
|
+
'foo':"bar",
|
498
|
+
'id':1
|
499
|
+
};
|
500
|
+
|
501
|
+
raises( function(){
|
502
|
+
assertThat( o, hasProperty() )
|
503
|
+
}, "hasProperty must receive at least the property name" );
|
504
|
+
|
505
|
+
|
506
|
+
assertThat( o, hasProperty("foo") );
|
507
|
+
assertThat( o, hasProperty("foo","bar") );
|
508
|
+
assertThat( o, hasProperty("id",isA("number") ) );
|
509
|
+
assertThat( o, not( hasProperty( "foo", "foo" ) ) );
|
510
|
+
assertThat( o, not( hasProperty( "bar" ) ) );
|
511
|
+
assertThat( o, not( hasProperty( "id", closeTo(10,2) ) ) );
|
512
|
+
assertThat( null, not( hasProperty( "bar" ) ) );
|
513
|
+
assertThat( "foo", not( hasProperty( "bar" ) ) );
|
514
|
+
})
|
515
|
+
|
516
|
+
test( "hasProperties matcher", function(){
|
517
|
+
var o = {
|
518
|
+
'foo':"bar",
|
519
|
+
'id':1,
|
520
|
+
'name':"John Doe",
|
521
|
+
'age':35
|
522
|
+
};
|
523
|
+
|
524
|
+
raises( function(){
|
525
|
+
assertThat( o, hasProperties() );
|
526
|
+
}, "hasProperties expect a list of matchers");
|
527
|
+
|
528
|
+
assertThat( o, hasProperties( { "foo":"bar", "id":1 } ) );
|
529
|
+
assertThat( o, hasProperties( { "name":startsWith("Joh"), "age":greaterThan(30) } ) );
|
530
|
+
assertThat( o, not( hasProperties( { "name":contains("Bill"), "age":closeTo(30,3) } ) ) );
|
531
|
+
assertThat( o, not( hasProperties( { "bar":65, "age":isA("string") } ) ) );
|
532
|
+
assertThat( null, not( hasProperties( { "name":contains("Bill"), "age":closeTo(30,3) } ) ) );
|
533
|
+
assertThat( 'foo', not( hasProperties( { "name":contains("Bill"), "age":closeTo(30,3) } ) ) );
|
534
|
+
|
535
|
+
})
|
536
|
+
|
537
|
+
test( "hasMethod matcher", function(){
|
538
|
+
|
539
|
+
var objectWithFunction = {
|
540
|
+
'foo':function(a,b,c){
|
541
|
+
return a == 1 && b == 2 && c == 3;
|
542
|
+
}
|
543
|
+
};
|
544
|
+
var objectWithFunctionThatThrowAnError = {
|
545
|
+
'foo':function(a,b,c){
|
546
|
+
if( a == 1 && b == 2 && c == 3 )
|
547
|
+
return true;
|
548
|
+
else
|
549
|
+
throw "foo";
|
550
|
+
}
|
551
|
+
};
|
552
|
+
var objectWithDynamicallySetFunction = {};
|
553
|
+
objectWithDynamicallySetFunction["foo"] = function(){ return "foo"; };
|
554
|
+
|
555
|
+
var objectWithProperty = {
|
556
|
+
'foo':"bar"
|
557
|
+
};
|
558
|
+
var objectWithoutFunction = {};
|
559
|
+
|
560
|
+
function TestObject (){
|
561
|
+
|
562
|
+
}
|
563
|
+
TestObject.prototype = {
|
564
|
+
'foo':function(){ return "foo"; }
|
565
|
+
}
|
566
|
+
|
567
|
+
raises( function(){
|
568
|
+
assertThat( o, hasMethod() );
|
569
|
+
}, "hasMethod expect a function name");
|
570
|
+
|
571
|
+
assertThat( objectWithFunction, hasMethod( "foo" ) );
|
572
|
+
assertThat( objectWithFunction, hasMethod( "foo" ).returns( false ) );
|
573
|
+
assertThat( objectWithFunction, hasMethod( "foo" ).returns( false ).withoutArgs() );
|
574
|
+
|
575
|
+
assertThat( objectWithFunction, hasMethod( "foo" ).returns( false ).withArgs(1,2,3).withoutArgs() );
|
576
|
+
assertThat( objectWithFunction, hasMethod( "foo" ).returns( false ).withoutArgs().withArgs(1,2,3) );
|
577
|
+
assertThat( objectWithFunction, hasMethod( "foo" ).returns( equalTo(false) ) );
|
578
|
+
assertThat( objectWithFunction, hasMethod( "foo" ).returns( true ).withArgs( 1,2,3 ) );
|
579
|
+
|
580
|
+
assertThat( objectWithDynamicallySetFunction, hasMethod( "foo" ) );
|
581
|
+
assertThat( objectWithDynamicallySetFunction, hasMethod( "foo" ).returns("foo") );
|
582
|
+
|
583
|
+
assertThat( new TestObject(), hasMethod( "foo" ) );
|
584
|
+
assertThat( new TestObject(), hasMethod( "foo" ).returns("foo") );
|
585
|
+
|
586
|
+
|
587
|
+
assertThat( objectWithFunctionThatThrowAnError, hasMethod( "foo" ).throwsError() );
|
588
|
+
assertThat( objectWithFunctionThatThrowAnError, hasMethod( "foo" ).throwsError().withoutArgs() );
|
589
|
+
assertThat( objectWithFunctionThatThrowAnError, hasMethod( "foo" ).throwsError( "foo" ).withArgs(4,5,6) );
|
590
|
+
assertThat( objectWithFunctionThatThrowAnError, not( hasMethod( "foo" ).throwsError( "foo" ).withArgs(1,2,3) ) );
|
591
|
+
assertThat( objectWithFunctionThatThrowAnError, not( hasMethod( "foo" ).throwsError( isA( "object" ) ) ) );
|
592
|
+
|
593
|
+
assertThat( objectWithProperty, not( hasMethod("foo") ) )
|
594
|
+
assertThat( objectWithoutFunction, not( hasMethod( "foo" ) ) );
|
595
|
+
assertThat( null, not( hasMethod( "foo" ) ) );
|
596
|
+
assertThat( "foo", not( hasMethod( "foo" ) ) );
|
597
|
+
|
598
|
+
})
|
599
|
+
|
600
|
+
test( "propertiesCount matcher", function(){
|
601
|
+
var o = {
|
602
|
+
'foo':"bar",
|
603
|
+
'id':1,
|
604
|
+
'name':"John Doe",
|
605
|
+
'age':35
|
606
|
+
};
|
607
|
+
|
608
|
+
raises( function(){
|
609
|
+
assertThat( o, propertiesCount() );
|
610
|
+
}, "length argument is mandatory");
|
611
|
+
raises( function(){
|
612
|
+
assertThat( o, propertiesCount(-10) );
|
613
|
+
}, "length argument must be greater than or equal to 0");
|
614
|
+
|
615
|
+
assertThat( o, propertiesCount(4) );
|
616
|
+
assertThat( o, not( propertiesCount( 2 ) ) );
|
617
|
+
assertThat( {}, propertiesCount(0) );
|
618
|
+
assertThat( null, not( propertiesCount(2) ) );
|
619
|
+
assertThat( [1,2], not( propertiesCount(2) ) );
|
620
|
+
})
|
621
|
+
|
622
|
+
test( "instanceOf matcher", function(){
|
623
|
+
raises( function(){
|
624
|
+
assertThat( "foo", instanceOf() );
|
625
|
+
}, "type argument is mandatory" );
|
626
|
+
|
627
|
+
assertThat( new String("foo"), instanceOf( String ) );
|
628
|
+
assertThat( new Number(10), instanceOf( Number ) );
|
629
|
+
assertThat( new RegExp("foo"), instanceOf( RegExp ) );
|
630
|
+
assertThat( new Description(), instanceOf( Description ) );
|
631
|
+
|
632
|
+
assertThat( "foo", instanceOf( String ) );
|
633
|
+
assertThat( 10, instanceOf( Number ) );
|
634
|
+
assertThat( true, instanceOf( Boolean ) );
|
635
|
+
|
636
|
+
assertThat( true, not( instanceOf( String ) ) );
|
637
|
+
})
|
638
|
+
|
639
|
+
module("hamcrest date matchers")
|
640
|
+
|
641
|
+
test( "dateAfter matcher", function(){
|
642
|
+
raises( function(){
|
643
|
+
assertThat( new Date(), dateAfter() );
|
644
|
+
}, "dateAfter must have a valid comparison date" );
|
645
|
+
raises( function(){
|
646
|
+
assertThat( new Date(), dateAfter( 15 ) );
|
647
|
+
}, "dateAfter must have a valid comparison date" );
|
648
|
+
|
649
|
+
assertThat( new Date( 2000, 0, 1 ), dateAfter( new Date( 1999, 11, 31 ) ) );
|
650
|
+
assertThat( new Date( 2000, 0, 1 ), dateAfter( new Date( 2000, 0, 1 ) ).inclusive() );
|
651
|
+
assertThat( new Date( 2000, 0, 1 ), not( dateAfter( new Date( 2001, 11, 31 ) ) ) );
|
652
|
+
assertThat( new Date( 2000, 0, 1 ), not( dateAfter( new Date( 2001, 11, 31 ) ).inclusive() ) );
|
653
|
+
assertThat( null, not( dateAfter( new Date( 2001, 11, 31 ) ).inclusive() ) );
|
654
|
+
assertThat( "foo", not( dateAfter( new Date( 2001, 11, 31 ) ).inclusive() ) );
|
655
|
+
})
|
656
|
+
test( "dateAfterOrEqualTo matcher", function(){
|
657
|
+
raises( function(){
|
658
|
+
assertThat( new Date(), dateAfterOrEqualTo() );
|
659
|
+
}, "dateAfterOrEqualTo must have a valid comparison date" );
|
660
|
+
raises( function(){
|
661
|
+
assertThat( new Date(), dateAfterOrEqualTo( 15 ) );
|
662
|
+
}, "dateAfterOrEqualTo must have a valid comparison date" );
|
663
|
+
|
664
|
+
assertThat( new Date( 2000, 0, 1 ), dateAfterOrEqualTo( new Date( 2000, 0, 1 ) ) );
|
665
|
+
assertThat( new Date( 2000, 0, 1 ), not( dateAfterOrEqualTo( new Date( 2001, 11, 31 ) ) ) );
|
666
|
+
assertThat( null, not( dateAfterOrEqualTo( new Date( 2001, 11, 31 ) ) ) );
|
667
|
+
assertThat( "foo", not( dateAfterOrEqualTo( new Date( 2001, 11, 31 ) ) ) );
|
668
|
+
})
|
669
|
+
|
670
|
+
test( "dateBefore matcher", function(){
|
671
|
+
raises( function(){
|
672
|
+
assertThat( new Date(), dateBefore() );
|
673
|
+
}, "dateBefore must have a valid comparison date" );
|
674
|
+
raises( function(){
|
675
|
+
assertThat( new Date(), dateBefore(15) );
|
676
|
+
}, "dateBefore must have a valid comparison date" );
|
677
|
+
|
678
|
+
assertThat( new Date( 2000, 0, 1 ), dateBefore( new Date( 2001, 11, 31 ) ) );
|
679
|
+
assertThat( new Date( 2000, 0, 1 ), dateBefore( new Date( 2000, 0, 1 ) ).inclusive() );
|
680
|
+
assertThat( new Date( 2000, 0, 1 ), not( dateBefore( new Date( 1999, 11, 31 ) ) ) );
|
681
|
+
assertThat( new Date( 2000, 0, 1 ), not( dateBefore( new Date( 1999, 11, 31 ) ).inclusive() ) );
|
682
|
+
assertThat( null, not( dateBefore( new Date( 1999, 11, 31 ) ).inclusive() ) );
|
683
|
+
assertThat( "foo", not( dateBefore( new Date( 1999, 11, 31 ) ).inclusive() ) );
|
684
|
+
})
|
685
|
+
test( "dateBeforeOrEqualTo matcher", function(){
|
686
|
+
raises( function(){
|
687
|
+
assertThat( new Date(), dateBeforeOrEqualTo() );
|
688
|
+
}, "dateBeforeOrEqualTo must have a valid comparison date" );
|
689
|
+
raises( function(){
|
690
|
+
assertThat( new Date(), dateBeforeOrEqualTo(15) );
|
691
|
+
}, "dateBeforeOrEqualTo must have a valid comparison date" );
|
692
|
+
|
693
|
+
assertThat( new Date( 2000, 0, 1 ), dateBeforeOrEqualTo( new Date( 2000, 0, 1 ) ) );
|
694
|
+
assertThat( new Date( 2000, 0, 1 ), not( dateBeforeOrEqualTo( new Date( 1999, 11, 31 ) ) ) );
|
695
|
+
assertThat( null, not( dateBeforeOrEqualTo( new Date( 1999, 11, 31 ) ) ) );
|
696
|
+
assertThat( "foo", not( dateBeforeOrEqualTo( new Date( 1999, 11, 31 ) ) ) );
|
697
|
+
})
|
698
|
+
|
699
|
+
test( "dateBetween matcher", function(){
|
700
|
+
raises( function(){
|
701
|
+
assertThat( new Date(), dateBetween() );
|
702
|
+
}, "dateBetween must have a valid first date" );
|
703
|
+
raises( function(){
|
704
|
+
assertThat( new Date(), dateBetween(new Date()) );
|
705
|
+
}, "dateBetween must have a valid second date" );
|
706
|
+
raises( function(){
|
707
|
+
assertThat( new Date(), dateBetween(new Date(), new Date(2000,0,1)) );
|
708
|
+
}, "dateBetween first date must be before the second date");
|
709
|
+
raises( function(){
|
710
|
+
assertThat( new Date(), dateBetween( 15, new Date() ) );
|
711
|
+
}, "dateBetween must have a valid first date" );
|
712
|
+
raises( function(){
|
713
|
+
assertThat( new Date(), dateBetween( new Date(), 15 ) );
|
714
|
+
}, "dateBetween must have a valid second date" );
|
715
|
+
|
716
|
+
assertThat( new Date(2000, 0, 1), dateBetween( new Date(1999,0,1), new Date(2001,0,1) ) );
|
717
|
+
assertThat( new Date(2000, 0, 1), dateBetween( new Date(1999,0,1), new Date(2000,0,1) ).inclusive() );
|
718
|
+
|
719
|
+
assertThat( new Date(2000, 0, 1), not( dateBetween( new Date(1999,0,1), new Date(2000,0,1) ) ) );
|
720
|
+
assertThat( new Date(2001, 0, 1), not( dateBetween( new Date(1999,0,1), new Date(2000,0,1) ) ) );
|
721
|
+
|
722
|
+
assertThat( new Date(1998, 0, 1), not( dateBetween( new Date(1999,0,1), new Date(2000,0,1) ).inclusive() ) );
|
723
|
+
assertThat( new Date(2000, 0, 2), not( dateBetween( new Date(1999,0,1), new Date(2000,0,1) ).inclusive() ) );
|
724
|
+
|
725
|
+
assertThat( null, not( dateBetween( new Date(1999,0,1), new Date(2000,0,1) ).inclusive() ) );
|
726
|
+
assertThat( "foo", not( dateBetween( new Date(1999,0,1), new Date(2000,0,1) ).inclusive() ) );
|
727
|
+
})
|
728
|
+
|
729
|
+
test( "dateEquals matcher", function(){
|
730
|
+
|
731
|
+
raises( function(){
|
732
|
+
assertThat( new Date(), dateEquals() );
|
733
|
+
}, "dateEquals must have a valid comparison date" );
|
734
|
+
raises( function(){
|
735
|
+
assertThat( new Date(), dateEquals( 15 ) );
|
736
|
+
}, "dateEquals must have a valid comparison date" );
|
737
|
+
|
738
|
+
assertThat( new Date(2000,0,1), dateEquals( new Date(2000,0,1) ) );
|
739
|
+
|
740
|
+
assertThat( new Date(2000,0,1), not( dateEquals( new Date(2001,0,1) ) ) );
|
741
|
+
assertThat( null, not( dateEquals( new Date(2001,0,1) ) ) );
|
742
|
+
assertThat( "foo", not( dateEquals( new Date(2001,0,1) ) ) );
|
743
|
+
|
744
|
+
})
|
745
|
+
|
746
|
+
module ( "hamcrest function matchers" )
|
747
|
+
|
748
|
+
test( "throwsError matcher", function(){
|
749
|
+
var errorMsg = "This is an error";
|
750
|
+
var scopeObject = {"foo":"bla"};
|
751
|
+
var errorScopeObject = {};
|
752
|
+
|
753
|
+
var arguments0 = "foo";
|
754
|
+
var arguments1 = 25;
|
755
|
+
var arguments2 = true;
|
756
|
+
|
757
|
+
function functionThatNotThrowAnError(){}
|
758
|
+
function functionThatThrowAnErrorMessage(){
|
759
|
+
throw errorMsg;
|
760
|
+
}
|
761
|
+
function functionThatThrowAnErrorObject(){
|
762
|
+
throw new Error(errorMsg);
|
763
|
+
}
|
764
|
+
function functionThatThrowAnErrorIfInvalidScope(){
|
765
|
+
if( this.hasOwnProperty( "foo") )
|
766
|
+
return;
|
767
|
+
else
|
768
|
+
throw errorMsg;
|
769
|
+
}
|
770
|
+
function functionThatThrowAnErrorIfInvalidArguments(){
|
771
|
+
if( arguments[0] != arguments0 )
|
772
|
+
throw errorMsg;
|
773
|
+
else if( arguments[1] != arguments1 )
|
774
|
+
throw errorMsg;
|
775
|
+
else if( arguments[2] != arguments2 )
|
776
|
+
throw errorMsg;
|
777
|
+
}
|
778
|
+
|
779
|
+
assertThat( functionThatThrowAnErrorMessage, throwsError() );
|
780
|
+
assertThat( functionThatThrowAnErrorMessage, throwsError().withoutArgs() );
|
781
|
+
assertThat( functionThatThrowAnErrorMessage, throwsError( errorMsg ) );
|
782
|
+
|
783
|
+
assertThat( functionThatThrowAnErrorObject, throwsError() );
|
784
|
+
assertThat( functionThatThrowAnErrorObject, throwsError().withoutArgs() );
|
785
|
+
assertThat( functionThatThrowAnErrorObject, throwsError( isA( "object" ) ) );
|
786
|
+
|
787
|
+
assertThat( functionThatThrowAnErrorIfInvalidScope, throwsError().withScope( errorScopeObject ) );
|
788
|
+
assertThat( functionThatThrowAnErrorIfInvalidScope, throwsError().withScope( errorScopeObject ).withoutArgs() );
|
789
|
+
assertThat( functionThatThrowAnErrorIfInvalidScope, not( throwsError().withScope( scopeObject ) ) );
|
790
|
+
|
791
|
+
assertThat( functionThatThrowAnErrorIfInvalidArguments, throwsError() );
|
792
|
+
assertThat( functionThatThrowAnErrorIfInvalidArguments, throwsError().withArgs() );
|
793
|
+
assertThat( functionThatThrowAnErrorIfInvalidArguments, throwsError().withoutArgs() );
|
794
|
+
assertThat( functionThatThrowAnErrorIfInvalidArguments, not( throwsError().withArgs( arguments0, arguments1, arguments2 ) ) );
|
795
|
+
assertThat( functionThatThrowAnErrorIfInvalidArguments, throwsError().withoutArgs().withArgs( arguments0, arguments1, arguments2 ) );
|
796
|
+
assertThat( functionThatThrowAnErrorIfInvalidArguments, throwsError().withArgs( arguments0, arguments1, arguments2 ).withoutArgs() );
|
797
|
+
|
798
|
+
assertThat( functionThatNotThrowAnError, not( throwsError() ) );
|
799
|
+
assertThat( functionThatNotThrowAnError, not( throwsError().withoutArgs() ) );
|
800
|
+
|
801
|
+
assertThat( null, not( throwsError() ) );
|
802
|
+
assertThat( "foo", not( throwsError() ) );
|
803
|
+
})
|
804
|
+
|
805
|
+
test( "returns matcher", function(){
|
806
|
+
|
807
|
+
var scopeObject = {'foo':"bar"};
|
808
|
+
var errorScope = {};
|
809
|
+
|
810
|
+
function functionWithScope()
|
811
|
+
{
|
812
|
+
return this.foo;
|
813
|
+
}
|
814
|
+
function functionWithScopeAndArgs( a, b )
|
815
|
+
{
|
816
|
+
return this.foo + a + b;
|
817
|
+
}
|
818
|
+
function functionWithReturn(a,b,c)
|
819
|
+
{
|
820
|
+
return a == 1 && b == 2 && c == 3;
|
821
|
+
}
|
822
|
+
function functionWithoutReturn(){}
|
823
|
+
|
824
|
+
assertThat( functionWithReturn, returns() );
|
825
|
+
assertThat( functionWithoutReturn, not( returns() ) );
|
826
|
+
|
827
|
+
assertThat( functionWithReturn, returns( false ) );
|
828
|
+
assertThat( functionWithReturn, returns( false ).withoutArgs() );
|
829
|
+
|
830
|
+
assertThat( functionWithReturn, returns( false ).withoutArgs().withArgs( 1,2,3 ) );
|
831
|
+
assertThat( functionWithReturn, returns( false ).withArgs( 1,2,3 ).withoutArgs() );
|
832
|
+
|
833
|
+
assertThat( functionWithReturn, returns( true ).withArgs( 1,2,3 ) );
|
834
|
+
assertThat( functionWithReturn, returns( false ).withArgs( 4,5,6 ) );
|
835
|
+
|
836
|
+
assertThat( functionWithScope, returns( "bar" ).withScope( scopeObject ) );
|
837
|
+
assertThat( functionWithScope, returns( "bar" ).withScope( scopeObject ).withoutArgs() );
|
838
|
+
assertThat( functionWithScope, not( returns( "bar" ).withScope( errorScope ) ) );
|
839
|
+
|
840
|
+
assertThat( functionWithScopeAndArgs, returns( both( isA("string") ).and("barabar") ).withScope( scopeObject ).withArgs("ab","ar") );
|
841
|
+
|
842
|
+
assertThat( functionWithScopeAndArgs, not( returns( "barabar" ).withScope( scopeObject ).withArgs("ob","ar") ) );
|
843
|
+
|
844
|
+
assertThat( null, not( returns() ) );
|
845
|
+
assertThat( "foo", not( returns() ) );
|
846
|
+
|
847
|
+
})
|
848
|
+
|
849
|
+
module( "hamcrest DOM matchers" );
|
850
|
+
test( "equalToNode matcher", function(){
|
851
|
+
|
852
|
+
raises( function(){
|
853
|
+
equalToNode();
|
854
|
+
}, "equalToNode expect an argument" );
|
855
|
+
|
856
|
+
raises( function(){
|
857
|
+
equalToNode( "foo" );
|
858
|
+
}, "equalToNode expect a Node object as argument" );
|
859
|
+
|
860
|
+
var nodeA = document.createElement("div");
|
861
|
+
nodeA.innerHTML = "<h1>A sample node</h1>";
|
862
|
+
|
863
|
+
var nodeB = document.createElement("div");
|
864
|
+
nodeB.innerHTML = "<h1>A sample node</h1>";
|
865
|
+
|
866
|
+
assertThat( nodeA, equalToNode( nodeB ) );
|
867
|
+
|
868
|
+
nodeA = document.createElement("div");
|
869
|
+
nodeA.setAttribute( "class", "foo" );
|
870
|
+
nodeA.innerHTML = "<h1>A sample node</h1>";
|
871
|
+
|
872
|
+
nodeB = document.createElement("div");
|
873
|
+
nodeB.setAttribute( "class", "foo" );
|
874
|
+
nodeB.innerHTML = "<h1>A sample node</h1>";
|
875
|
+
|
876
|
+
assertThat( nodeA, equalToNode( nodeB ) );
|
877
|
+
|
878
|
+
nodeA = document.createElement("div");
|
879
|
+
nodeA.setAttribute( "class", "foo" );
|
880
|
+
nodeA.innerHTML = "<h1>A sample node</h1>";
|
881
|
+
|
882
|
+
nodeB = document.createElement("div");
|
883
|
+
nodeB.innerHTML = "<h1>A sample node</h1>";
|
884
|
+
assertThat( nodeA, not( equalToNode( nodeB ) ) );
|
885
|
+
|
886
|
+
nodeA = document.createElement("a");
|
887
|
+
nodeB = document.createElement("b");
|
888
|
+
assertThat( nodeA, not( equalToNode( nodeB ) ) );
|
889
|
+
|
890
|
+
assertThat( "foo", not( equalToNode( nodeB ) ) );
|
891
|
+
assertThat( null, not( equalToNode( nodeB ) ) );
|
892
|
+
});
|
893
|
+
|
894
|
+
test("hasAttribute matcher", function(){
|
895
|
+
raises( function(){
|
896
|
+
hasAttribute();
|
897
|
+
}, "hasAttribute expect at least an attribute name");
|
898
|
+
|
899
|
+
var node = document.createElement( "div" );
|
900
|
+
node.setAttribute("class", "foo");
|
901
|
+
node.setAttribute("id", "foo");
|
902
|
+
node.setAttribute("name", "bla");
|
903
|
+
node.setAttribute("style", "border:1px solid yellow;");
|
904
|
+
|
905
|
+
assertThat( node, hasAttribute( "class" ) )
|
906
|
+
assertThat( node, hasAttribute( "id", "foo" ) )
|
907
|
+
assertThat( node, hasAttribute( "name", "bla" ) )
|
908
|
+
assertThat( node, hasAttribute( "style", contains( "border" ) ) );
|
909
|
+
|
910
|
+
assertThat( node, not( hasAttribute( "href" ) ))
|
911
|
+
assertThat( node, not( hasAttribute( "style", contains( "color" ) ) ))
|
912
|
+
|
913
|
+
assertThat( "foo", not( hasAttribute( "foo" ) ))
|
914
|
+
assertThat( null, not( hasAttribute( "foo" ) ))
|
915
|
+
})
|
916
|
+
|
917
|
+
|
918
|
+
|
919
|
+
|
920
|
+
|