forked from nkallen/screw-unit
/
matchers_spec.js
271 lines (231 loc) · 9.87 KB
/
matchers_spec.js
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
Screw.Unit(function() {
describe("Matchers", function() {
describe('#equal', function() {
it("invokes the provided matcher on a call to expect", function() {
expect(true).to(equal, true);
expect(true).to_not(equal, false);
});
describe('when given an object', function() {
it("matches Objects with the same keys and values", function() {
expect({a: 'b', c: 'd'}).to(equal, {a: 'b', c: 'd'});
expect({a: 'b', c: 'd', e: 'f'}).to_not(equal, {a: 'b', c: 'd', e: 'G'});
});
});
describe('when given an array', function() {
it("matches Arrays with the same elements", function() {
expect([1, 2, 4]).to(equal, [1, 2, 4]);
expect([1, 2, 3]).to_not(equal, [3, 2, 1]);
});
it("recursively applies equality to complex elements", function() {
expect([{a: 'b'}, {c: 'd'}]).to(equal, [{a: 'b'}, {c: 'd'}]);
expect([{a: 'b'}, {c: 'd'}]).to_not(equal, [{a: 'b'}, {c: 'E'}]);
});
});
describe(".failure_message", function() {
it('prints "expected [expected] to (not) be equal [actual]"', function() {
var message = null;
try { expect(1).to(equal, 2) } catch(e) { message = e }
expect(message).to(equal, 'expected 1 to equal 2');
try { expect(1).to_not(equal, 1) } catch(e) { message = e }
expect(message).to(equal, 'expected 1 to not equal 1');
});
});
});
describe('#match', function() {
describe('when given a regular expression', function() {
it("matches Strings produced by the grammar", function() {
expect("The wheels of the bus").to(match, /bus/);
expect("The wheels of the bus").to_not(match, /boat/);
});
});
describe('when given a string', function() {
it("matches [expected]s containing [actual]s", function() {
expect("The wheels of the bus").to(match, "wheels");
expect("The wheels of the bus").to_not(match, "oars");
});
});
describe('when given an integer', function() {
it("matches [expected]s containing [actual]s", function() {
expect("1 time").to(match, 1);
expect("2 times").to_not(match, 3);
});
});
describe(".failure_message", function() {
it('prints "expected [actual] to (not) match [expected]', function() {
var message = null;
try { expect("hello").to(match, "schmello") } catch(e) { message = e }
expect(message).to(equal, 'expected "hello" to match "schmello"');
try { expect("hello").to_not(match, "ello") } catch(e) { message = e }
expect(message).to(equal, 'expected "hello" to not match "ello"');
});
});
});
describe('#be_empty', function() {
it("matches Arrays with no elements", function() {
expect([]).to(be_empty);
expect([1]).to_not(be_empty);
});
describe(".failure_message", function() {
it("prints 'expected [actual] to (not) be empty", function() {
var message = null;
try { expect([1]).to(be_empty) } catch(e) { message = e }
expect(message).to(equal, 'expected [ 1 ] to be empty');
try { expect([]).to_not(be_empty) } catch(e) { message = e }
expect(message).to(equal, 'expected [] to not be empty');
});
});
});
describe('#have_length', function() {
it("matches Arrays of the expected length", function() {
expect([]).to(have_length, 0);
expect([1]).to(have_length, 1);
expect([1, 2, 3]).to_not(have_length, 4);
});
describe(".failure_message", function() {
it("prints 'expected [actual] to (not) have length [expected]", function() {
var message = null;
try { expect([1, 2]).to(have_length, 4) } catch(e) { message = e }
expect(message).to(equal, 'expected [ 1, 2 ] to have length 4');
try { expect([1]).to_not(have_length, 1) } catch(e) { message = e }
expect(message).to(equal, 'expected [ 1 ] to not have length 1');
});
});
});
describe('#be_null', function() {
it("matches null", function() {
expect(null).to(be_null);
expect(1).to_not(be_null);
});
describe(".failure_message", function() {
it("prints 'expected [actual] to (not) be null", function() {
var message = null;
try { expect(1).to(be_null) } catch(e) { message = e }
expect(message).to(equal, 'expected 1 to be null');
try { expect(null).to_not(be_null) } catch(e) { message = e }
expect(message).to(equal, 'expected null to not be null');
});
});
});
describe('#be_undefined', function() {
it("matches undefined", function() {
expect(undefined).to(be_undefined);
expect(1).to_not(be_undefined);
});
describe(".failure_message", function() {
it("prints 'expected [actual] to (not) be undefined", function() {
var message = undefined;
try { expect(1).to(be_undefined) } catch(e) { message = e }
expect(message).to(equal, 'expected 1 to be undefined');
try { expect(undefined).to_not(be_undefined) } catch(e) { message = e }
expect(message).to(equal, 'expected undefined to not be undefined');
});
});
});
describe('#be_true', function() {
it("matches values that are considered true conditions", function() {
expect(true).to(be_true);
expect(1).to(be_true);
expect(false).to_not(be_true);
expect(undefined).to_not(be_true);
expect(null).to_not(be_true);
});
describe(".failure_message", function() {
it("prints 'expected [actual] to (not) be true", function() {
var message = true;
try { expect(false).to(be_true) } catch(e) { message = e }
expect(message).to(equal, 'expected false to be true');
try { expect(true).to_not(be_true) } catch(e) { message = e }
expect(message).to(equal, 'expected true to not be true');
});
});
});
describe('#be_false', function() {
it("matches values that are considered false conditions", function() {
expect(false).to(be_false);
expect(undefined).to(be_false);
expect(null).to(be_false);
expect(true).to_not(be_false);
expect(1).to_not(be_false);
});
describe(".failure_message", function() {
it("prints 'expected [actual] to (not) be false", function() {
var message = false;
try { expect(true).to(be_false) } catch(e) { message = e }
expect(message).to(equal, 'expected true to be false');
try { expect(false).to_not(be_false) } catch(e) { message = e }
expect(message).to(equal, 'expected false to not be false');
});
});
});
describe('#match_selector', function() {
var elt;
before(function() {
elt = $("<div class='foo'></div>");
});
it("matches a jQuery element against the expected selector", function() {
expect(elt).to(match_selector, 'div.foo');
expect(elt).to_not(match_selector, 'div.bar');
});
describe(".failure_message", function() {
it("prints 'expected [actual] to (not) match selector [expected]", function() {
var message = false;
try { expect(elt).to(match_selector, 'div.bar') } catch(e) { message = e }
expect(message).to(equal, 'expected $([ <div class="foo"> ]) to match selector div.bar');
try { expect(elt).to_not(match_selector, 'div.foo') } catch(e) { message = e }
expect(message).to(equal, 'expected $([ <div class="foo"> ]) to not match selector div.foo');
});
});
});
describe('#contain_selector', function() {
var elt;
before(function() {
elt = $("<div><div class='foo'></div></div>");
});
it("matches a jQuery element against the expected selector", function() {
expect(elt).to(contain_selector, 'div.foo');
expect(elt).to_not(contain_selector, 'div.bar');
});
describe(".failure_message", function() {
it("prints 'expected [actual] to (not) match selector [expected]", function() {
var message = false;
try { expect(elt).to(contain_selector, 'div.bar') } catch(e) { message = e }
expect(message).to(equal, 'expected $([ <div> ]) to contain selector div.bar');
try { expect(elt).to_not(contain_selector, 'div.foo') } catch(e) { message = e }
expect(message).to(equal, 'expected $([ <div> ]) to not contain selector div.foo');
});
});
});
describe("#be_an_instance_of", function() {
var j;
var s;
before(function() {
j = new jQuery;
s = new RegExp("Foo+bar");
});
it("returns true for instances of the class", function() {
expect(j).to(be_an_instance_of,jQuery);
});
it("returns false for instances not of the class", function() {
expect(s).to_not(be_an_instance_of,jQuery);
});
});
describe("#have_method", function() {
var a;
before(function() {
a = new Array(42);
});
it("returns true for objects that have the method", function() {
expect(a).to(have_method,'pop');
});
it("returns false for objects that do not have the method", function() {
expect(a).to_not(have_method,'bessel');
});
});
describe("#throw_error", function() {
it("returns true for functions that throw the error msg", function() {
expect(function() { var m = new Math; }).to(throw_error,
'TypeError: Math is not a constructor');
});
});
});
});