jasmine-core 1.1.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/jasmine-core.rb +17 -0
- data/lib/jasmine-core/example/SpecRunner.html +54 -0
- data/lib/jasmine-core/example/spec/PlayerSpec.js +58 -0
- data/lib/jasmine-core/example/spec/SpecHelper.js +9 -0
- data/lib/jasmine-core/example/src/Player.js +22 -0
- data/lib/jasmine-core/example/src/Song.js +7 -0
- data/lib/jasmine-core/jasmine-html.js +190 -0
- data/lib/jasmine-core/jasmine.css +166 -0
- data/lib/jasmine-core/jasmine.js +2476 -0
- data/lib/jasmine-core/json2.js +478 -0
- data/lib/jasmine-core/version.rb +8 -0
- data/spec/console/ConsoleReporterSpec.js +451 -0
- data/spec/core/BaseSpec.js +27 -0
- data/spec/core/CustomMatchersSpec.js +97 -0
- data/spec/core/EnvSpec.js +159 -0
- data/spec/core/ExceptionsSpec.js +149 -0
- data/spec/core/JsApiReporterSpec.js +103 -0
- data/spec/core/MatchersSpec.js +838 -0
- data/spec/core/MockClockSpec.js +38 -0
- data/spec/core/MultiReporterSpec.js +45 -0
- data/spec/core/NestedResultsSpec.js +54 -0
- data/spec/core/PrettyPrintSpec.js +87 -0
- data/spec/core/QueueSpec.js +23 -0
- data/spec/core/ReporterSpec.js +56 -0
- data/spec/core/RunnerSpec.js +267 -0
- data/spec/core/SpecRunningSpec.js +1258 -0
- data/spec/core/SpecSpec.js +124 -0
- data/spec/core/SpySpec.js +201 -0
- data/spec/core/SuiteSpec.js +120 -0
- data/spec/core/UtilSpec.js +39 -0
- data/spec/core/WaitsForBlockSpec.js +118 -0
- data/spec/html/MatchersHtmlSpec.js +38 -0
- data/spec/html/PrettyPrintHtmlSpec.js +8 -0
- data/spec/html/TrivialReporterSpec.js +239 -0
- data/spec/node_suite.js +127 -0
- data/spec/runner.html +79 -0
- data/spec/templates/runner.html.erb +49 -0
- data/spec/templates/script_tag.html.erb +1 -0
- metadata +164 -0
@@ -0,0 +1,838 @@
|
|
1
|
+
describe("jasmine.Matchers", function() {
|
2
|
+
var env, spec;
|
3
|
+
|
4
|
+
beforeEach(function() {
|
5
|
+
env = new jasmine.Env();
|
6
|
+
env.updateInterval = 0;
|
7
|
+
|
8
|
+
var suite = env.describe("suite", function() {
|
9
|
+
spec = env.it("spec", function() {
|
10
|
+
});
|
11
|
+
});
|
12
|
+
spyOn(spec, 'addMatcherResult');
|
13
|
+
|
14
|
+
this.addMatchers({
|
15
|
+
toPass: function() {
|
16
|
+
return lastResult().passed();
|
17
|
+
},
|
18
|
+
toFail: function() {
|
19
|
+
return !lastResult().passed();
|
20
|
+
}
|
21
|
+
});
|
22
|
+
});
|
23
|
+
|
24
|
+
function match(value) {
|
25
|
+
return spec.expect(value);
|
26
|
+
}
|
27
|
+
|
28
|
+
function lastResult() {
|
29
|
+
return spec.addMatcherResult.mostRecentCall.args[0];
|
30
|
+
}
|
31
|
+
|
32
|
+
function catchException(fn) {
|
33
|
+
try {
|
34
|
+
fn.call();
|
35
|
+
} catch (e) {
|
36
|
+
return e;
|
37
|
+
}
|
38
|
+
throw new Error("expected function to throw an exception");
|
39
|
+
}
|
40
|
+
|
41
|
+
it("toEqual with primitives, objects, dates, etc.", function() {
|
42
|
+
expect(match(true).toEqual(true)).toPass();
|
43
|
+
|
44
|
+
expect(match({foo:'bar'}).toEqual(null)).toFail();
|
45
|
+
|
46
|
+
var functionA = function() {
|
47
|
+
return 'hi';
|
48
|
+
};
|
49
|
+
var functionB = function() {
|
50
|
+
return 'hi';
|
51
|
+
};
|
52
|
+
expect(match({foo:functionA}).toEqual({foo:functionB})).toFail();
|
53
|
+
expect(match({foo:functionA}).toEqual({foo:functionA})).toPass();
|
54
|
+
|
55
|
+
expect((match(false).toEqual(true))).toFail();
|
56
|
+
|
57
|
+
var circularGraph = {};
|
58
|
+
circularGraph.referenceToSelf = circularGraph;
|
59
|
+
expect((match(circularGraph).toEqual(circularGraph))).toPass();
|
60
|
+
|
61
|
+
expect((match(new Date(2008, 1, 3, 15, 17, 19, 1234)).toEqual(new Date(2009, 1, 3, 15, 17, 19, 1234)))).toFail();
|
62
|
+
expect((match(new Date(2008, 1, 3, 15, 17, 19, 1234)).toEqual(new Date(2008, 1, 3, 15, 17, 19, 1234)))).toPass();
|
63
|
+
|
64
|
+
|
65
|
+
expect(match(true).toNotEqual(false)).toPass();
|
66
|
+
expect((match(true).toNotEqual(true))).toFail();
|
67
|
+
|
68
|
+
expect((match(['a', 'b']).toEqual(['a', jasmine.undefined]))).toFail();
|
69
|
+
expect((match(['a', 'b']).toEqual(['a', 'b', jasmine.undefined]))).toFail();
|
70
|
+
|
71
|
+
expect((match("cat").toEqual("cat"))).toPass();
|
72
|
+
expect((match("cat").toNotEqual("cat"))).toFail();
|
73
|
+
|
74
|
+
expect((match(5).toEqual(5))).toPass();
|
75
|
+
expect((match(parseInt('5', 10)).toEqual(5))).toPass();
|
76
|
+
expect((match(5).toNotEqual(5))).toFail();
|
77
|
+
expect((match(parseInt('5', 10)).toNotEqual(5))).toFail();
|
78
|
+
});
|
79
|
+
|
80
|
+
it("toEqual to build an Expectation Result", function() {
|
81
|
+
var actual = 'a';
|
82
|
+
var matcher = match(actual);
|
83
|
+
var expected = 'b';
|
84
|
+
matcher.toEqual(expected);
|
85
|
+
|
86
|
+
var result = lastResult();
|
87
|
+
|
88
|
+
expect(result.matcherName).toEqual("toEqual");
|
89
|
+
expect(result.passed()).toFail();
|
90
|
+
expect(result.message).toMatch(jasmine.pp(actual));
|
91
|
+
expect(result.message).toMatch(jasmine.pp(expected));
|
92
|
+
expect(result.expected).toEqual(expected);
|
93
|
+
expect(result.actual).toEqual(actual);
|
94
|
+
});
|
95
|
+
|
96
|
+
it("toNotEqual to build an Expectation Result", function() {
|
97
|
+
var str = 'a';
|
98
|
+
var matcher = match(str);
|
99
|
+
matcher.toNotEqual(str);
|
100
|
+
|
101
|
+
var result = lastResult();
|
102
|
+
|
103
|
+
expect(result.matcherName).toEqual("toNotEqual");
|
104
|
+
expect(result.passed()).toFail();
|
105
|
+
expect(result.message).toMatch(jasmine.pp(str));
|
106
|
+
expect(result.message).toMatch('not');
|
107
|
+
expect(result.expected).toEqual(str);
|
108
|
+
expect(result.actual).toEqual(str);
|
109
|
+
});
|
110
|
+
|
111
|
+
it('toBe should return true only if the expected and actual items === each other', function() {
|
112
|
+
var a = {};
|
113
|
+
var b = {};
|
114
|
+
//noinspection UnnecessaryLocalVariableJS
|
115
|
+
var c = a;
|
116
|
+
expect((match(a).toBe(b))).toFail();
|
117
|
+
expect((match(a).toBe(a))).toPass();
|
118
|
+
expect((match(a).toBe(c))).toPass();
|
119
|
+
expect((match(a).toNotBe(b))).toPass();
|
120
|
+
expect((match(a).toNotBe(a))).toFail();
|
121
|
+
expect((match(a).toNotBe(c))).toFail();
|
122
|
+
});
|
123
|
+
|
124
|
+
it("toBe to build an ExpectationResult", function() {
|
125
|
+
var expected = 'b';
|
126
|
+
var actual = 'a';
|
127
|
+
var matcher = match(actual);
|
128
|
+
matcher.toBe(expected);
|
129
|
+
|
130
|
+
var result = lastResult();
|
131
|
+
|
132
|
+
expect(result.matcherName).toEqual("toBe");
|
133
|
+
expect(result.passed()).toFail();
|
134
|
+
expect(result.message).toMatch(jasmine.pp(actual));
|
135
|
+
expect(result.message).toMatch(jasmine.pp(expected));
|
136
|
+
expect(result.expected).toEqual(expected);
|
137
|
+
expect(result.actual).toEqual(actual);
|
138
|
+
});
|
139
|
+
|
140
|
+
it("toNotBe to build an ExpectationResult", function() {
|
141
|
+
var str = 'a';
|
142
|
+
var matcher = match(str);
|
143
|
+
matcher.toNotBe(str);
|
144
|
+
|
145
|
+
var result = lastResult();
|
146
|
+
|
147
|
+
expect(result.matcherName).toEqual("toNotBe");
|
148
|
+
expect(result.passed()).toFail();
|
149
|
+
expect(result.message).toMatch(str);
|
150
|
+
expect(result.expected).toEqual(str);
|
151
|
+
expect(result.actual).toEqual(str);
|
152
|
+
});
|
153
|
+
|
154
|
+
it("toMatch and #toNotMatch should perform regular expression matching on strings", function() {
|
155
|
+
expect((match('foobarbel').toMatch(/bar/))).toPass();
|
156
|
+
expect((match('foobazbel').toMatch(/bar/))).toFail();
|
157
|
+
|
158
|
+
expect((match('foobarbel').toMatch("bar"))).toPass();
|
159
|
+
expect((match('foobazbel').toMatch("bar"))).toFail();
|
160
|
+
|
161
|
+
expect((match('foobarbel').toNotMatch(/bar/))).toFail();
|
162
|
+
expect((match('foobazbel').toNotMatch(/bar/))).toPass();
|
163
|
+
|
164
|
+
expect((match('foobarbel').toNotMatch("bar"))).toFail();
|
165
|
+
expect((match('foobazbel').toNotMatch("bar"))).toPass();
|
166
|
+
});
|
167
|
+
|
168
|
+
it("toMatch w/ RegExp to build an ExpectationResult", function() {
|
169
|
+
var actual = 'a';
|
170
|
+
var matcher = match(actual);
|
171
|
+
var expected = /b/;
|
172
|
+
matcher.toMatch(expected);
|
173
|
+
|
174
|
+
var result = lastResult();
|
175
|
+
|
176
|
+
expect(result.matcherName).toEqual("toMatch");
|
177
|
+
expect(result.passed()).toFail();
|
178
|
+
expect(result.message).toMatch(jasmine.pp(actual));
|
179
|
+
expect(result.message).toMatch(expected.toString());
|
180
|
+
expect(result.expected).toEqual(expected);
|
181
|
+
expect(result.actual).toEqual(actual);
|
182
|
+
});
|
183
|
+
|
184
|
+
it("toMatch w/ String to build an ExpectationResult", function() {
|
185
|
+
var actual = 'a';
|
186
|
+
var matcher = match(actual);
|
187
|
+
var expected = 'b';
|
188
|
+
matcher.toMatch(expected);
|
189
|
+
|
190
|
+
var result = lastResult();
|
191
|
+
|
192
|
+
expect(result.matcherName).toEqual("toMatch");
|
193
|
+
expect(result.passed()).toFail();
|
194
|
+
expect(result.message).toEqual("Expected 'a' to match 'b'.");
|
195
|
+
expect(result.expected).toEqual(expected);
|
196
|
+
expect(result.actual).toEqual(actual);
|
197
|
+
});
|
198
|
+
|
199
|
+
it("toNotMatch w/ RegExp to build an ExpectationResult", function() {
|
200
|
+
var actual = 'a';
|
201
|
+
var matcher = match(actual);
|
202
|
+
var expected = /a/;
|
203
|
+
matcher.toNotMatch(expected);
|
204
|
+
|
205
|
+
var result = lastResult();
|
206
|
+
|
207
|
+
expect(result.matcherName).toEqual("toNotMatch");
|
208
|
+
expect(result.passed()).toFail();
|
209
|
+
expect(result.message).toEqual("Expected 'a' to not match /a/.");
|
210
|
+
expect(result.expected).toEqual(expected);
|
211
|
+
expect(result.actual).toEqual(actual);
|
212
|
+
});
|
213
|
+
|
214
|
+
it("toNotMatch w/ String to build an ExpectationResult", function() {
|
215
|
+
var str = 'a';
|
216
|
+
var matcher = match(str);
|
217
|
+
matcher.toNotMatch(str);
|
218
|
+
|
219
|
+
var result = lastResult();
|
220
|
+
|
221
|
+
expect(result.matcherName).toEqual("toNotMatch");
|
222
|
+
expect(result.passed()).toFail();
|
223
|
+
expect(result.message).toEqual("Expected 'a' to not match 'a'.");
|
224
|
+
expect(result.expected).toEqual(str);
|
225
|
+
expect(result.actual).toEqual(str);
|
226
|
+
});
|
227
|
+
|
228
|
+
it("toBeDefined", function() {
|
229
|
+
expect(match('foo').toBeDefined()).toPass();
|
230
|
+
expect(match(jasmine.undefined).toBeDefined()).toFail();
|
231
|
+
});
|
232
|
+
|
233
|
+
it("toBeDefined to build an ExpectationResult", function() {
|
234
|
+
var matcher = match(jasmine.undefined);
|
235
|
+
matcher.toBeDefined();
|
236
|
+
|
237
|
+
var result = lastResult();
|
238
|
+
|
239
|
+
expect(result.matcherName).toEqual("toBeDefined");
|
240
|
+
expect(result.passed()).toFail();
|
241
|
+
expect(result.message).toEqual('Expected undefined to be defined.');
|
242
|
+
expect(result.actual).toEqual(jasmine.undefined);
|
243
|
+
});
|
244
|
+
|
245
|
+
it("toBeUndefined", function() {
|
246
|
+
expect(match('foo').toBeUndefined()).toFail();
|
247
|
+
expect(match(jasmine.undefined).toBeUndefined()).toPass();
|
248
|
+
});
|
249
|
+
|
250
|
+
it("toBeNull", function() {
|
251
|
+
expect(match(null).toBeNull()).toPass();
|
252
|
+
expect(match(jasmine.undefined).toBeNull()).toFail();
|
253
|
+
expect(match("foo").toBeNull()).toFail();
|
254
|
+
});
|
255
|
+
|
256
|
+
it("toBeNull w/ String to build an ExpectationResult", function() {
|
257
|
+
var actual = 'a';
|
258
|
+
var matcher = match(actual);
|
259
|
+
matcher.toBeNull();
|
260
|
+
|
261
|
+
var result = lastResult();
|
262
|
+
|
263
|
+
expect(result.matcherName).toEqual("toBeNull");
|
264
|
+
expect(result.passed()).toFail();
|
265
|
+
expect(result.message).toMatch(jasmine.pp(actual));
|
266
|
+
expect(result.message).toMatch('null');
|
267
|
+
expect(result.actual).toEqual(actual);
|
268
|
+
});
|
269
|
+
|
270
|
+
it("toBeNull w/ Object to build an ExpectationResult", function() {
|
271
|
+
var actual = {a: 'b'};
|
272
|
+
var matcher = match(actual);
|
273
|
+
matcher.toBeNull();
|
274
|
+
|
275
|
+
var result = lastResult();
|
276
|
+
|
277
|
+
expect(result.matcherName).toEqual("toBeNull");
|
278
|
+
expect(result.passed()).toFail();
|
279
|
+
expect(result.message).toMatch(jasmine.pp(actual));
|
280
|
+
expect(result.message).toMatch('null');
|
281
|
+
expect(result.actual).toEqual(actual);
|
282
|
+
});
|
283
|
+
|
284
|
+
it("toBeFalsy", function() {
|
285
|
+
expect(match(false).toBeFalsy()).toPass();
|
286
|
+
expect(match(true).toBeFalsy()).toFail();
|
287
|
+
expect(match(jasmine.undefined).toBeFalsy()).toPass();
|
288
|
+
expect(match(0).toBeFalsy()).toPass();
|
289
|
+
expect(match("").toBeFalsy()).toPass();
|
290
|
+
});
|
291
|
+
|
292
|
+
it("toBeFalsy to build an ExpectationResult", function() {
|
293
|
+
var actual = 'a';
|
294
|
+
var matcher = match(actual);
|
295
|
+
matcher.toBeFalsy();
|
296
|
+
|
297
|
+
var result = lastResult();
|
298
|
+
|
299
|
+
expect(result.matcherName).toEqual("toBeFalsy");
|
300
|
+
expect(result.passed()).toFail();
|
301
|
+
expect(result.message).toMatch(jasmine.pp(actual));
|
302
|
+
expect(result.message).toMatch('falsy');
|
303
|
+
expect(result.actual).toEqual(actual);
|
304
|
+
});
|
305
|
+
|
306
|
+
it("toBeTruthy", function() {
|
307
|
+
expect(match(false).toBeTruthy()).toFail();
|
308
|
+
expect(match(true).toBeTruthy()).toPass();
|
309
|
+
expect(match(jasmine.undefined).toBeTruthy()).toFail();
|
310
|
+
expect(match(0).toBeTruthy()).toFail();
|
311
|
+
expect(match("").toBeTruthy()).toFail();
|
312
|
+
expect(match("hi").toBeTruthy()).toPass();
|
313
|
+
expect(match(5).toBeTruthy()).toPass();
|
314
|
+
expect(match({foo: 1}).toBeTruthy()).toPass();
|
315
|
+
});
|
316
|
+
|
317
|
+
it("toBeTruthy to build an ExpectationResult", function() {
|
318
|
+
var matcher = match(false);
|
319
|
+
matcher.toBeTruthy();
|
320
|
+
|
321
|
+
var result = lastResult();
|
322
|
+
|
323
|
+
expect(result.matcherName).toEqual("toBeTruthy");
|
324
|
+
expect(result.passed()).toFail();
|
325
|
+
expect(result.message).toEqual("Expected false to be truthy.");
|
326
|
+
expect(result.actual).toFail();
|
327
|
+
});
|
328
|
+
|
329
|
+
it("toEqual", function() {
|
330
|
+
expect(match(jasmine.undefined).toEqual(jasmine.undefined)).toPass();
|
331
|
+
expect(match({foo:'bar'}).toEqual({foo:'bar'})).toPass();
|
332
|
+
expect(match("foo").toEqual({bar: jasmine.undefined})).toFail();
|
333
|
+
expect(match({foo: jasmine.undefined}).toEqual("goo")).toFail();
|
334
|
+
expect(match({foo: {bar :jasmine.undefined}}).toEqual("goo")).toFail();
|
335
|
+
});
|
336
|
+
|
337
|
+
it("toEqual with jasmine.any()", function() {
|
338
|
+
expect(match("foo").toEqual(jasmine.any(String))).toPass();
|
339
|
+
expect(match(3).toEqual(jasmine.any(Number))).toPass();
|
340
|
+
expect(match("foo").toEqual(jasmine.any(Function))).toFail();
|
341
|
+
expect(match("foo").toEqual(jasmine.any(Object))).toFail();
|
342
|
+
expect(match({someObj:'foo'}).toEqual(jasmine.any(Object))).toPass();
|
343
|
+
expect(match({someObj:'foo'}).toEqual(jasmine.any(Function))).toFail();
|
344
|
+
expect(match(
|
345
|
+
function() {
|
346
|
+
}).toEqual(jasmine.any(Object))).toFail();
|
347
|
+
expect(match(["foo", "goo"]).toEqual(["foo", jasmine.any(String)])).toPass();
|
348
|
+
expect(match(
|
349
|
+
function() {
|
350
|
+
}).toEqual(jasmine.any(Function))).toPass();
|
351
|
+
expect(match(["a", function() {
|
352
|
+
}]).toEqual(["a", jasmine.any(Function)])).toPass();
|
353
|
+
});
|
354
|
+
|
355
|
+
it("toEqual handles circular objects ok", function() {
|
356
|
+
expect(match({foo: "bar", baz: jasmine.undefined}).toEqual({foo: "bar", baz: jasmine.undefined})).toPass();
|
357
|
+
expect(match({foo:['bar','baz','quux']}).toEqual({foo:['bar','baz','quux']})).toPass();
|
358
|
+
expect(match({foo: {bar:'baz'}, quux:'corge'}).toEqual({foo:{bar:'baz'}, quux:'corge'})).toPass();
|
359
|
+
|
360
|
+
var circularObject = {};
|
361
|
+
var secondCircularObject = {};
|
362
|
+
circularObject.field = circularObject;
|
363
|
+
secondCircularObject.field = secondCircularObject;
|
364
|
+
expect(match(circularObject).toEqual(secondCircularObject)).toPass();
|
365
|
+
});
|
366
|
+
|
367
|
+
it("toNotEqual as slightly surprising behavior, but is it intentional?", function() {
|
368
|
+
expect(match({x:"x", y:"y", z:"w"}).toNotEqual({x:"x", y:"y", z:"z"})).toPass();
|
369
|
+
expect(match({x:"x", y:"y", w:"z"}).toNotEqual({x:"x", y:"y", z:"z"})).toPass();
|
370
|
+
expect(match({x:"x", y:"y", z:"z"}).toNotEqual({w: "w", x:"x", y:"y", z:"z"})).toPass();
|
371
|
+
expect(match({w: "w", x:"x", y:"y", z:"z"}).toNotEqual({x:"x", y:"y", z:"z"})).toPass();
|
372
|
+
});
|
373
|
+
|
374
|
+
it("toEqual handles arrays", function() {
|
375
|
+
expect(match([1, "A"]).toEqual([1, "A"])).toPass();
|
376
|
+
});
|
377
|
+
|
378
|
+
it("toContain and toNotContain", function() {
|
379
|
+
expect(match('ABC').toContain('A')).toPass();
|
380
|
+
expect(match('ABC').toContain('X')).toFail();
|
381
|
+
|
382
|
+
expect(match(['A', 'B', 'C']).toContain('A')).toPass();
|
383
|
+
expect(match(['A', 'B', 'C']).toContain('F')).toFail();
|
384
|
+
expect(match(['A', 'B', 'C']).toNotContain('F')).toPass();
|
385
|
+
expect(match(['A', 'B', 'C']).toNotContain('A')).toFail();
|
386
|
+
|
387
|
+
expect(match(['A', {some:'object'}, 'C']).toContain({some:'object'})).toPass();
|
388
|
+
expect(match(['A', {some:'object'}, 'C']).toContain({some:'other object'})).toFail();
|
389
|
+
});
|
390
|
+
|
391
|
+
it("toContain to build an ExpectationResult", function() {
|
392
|
+
var actual = ['a','b','c'];
|
393
|
+
var matcher = match(actual);
|
394
|
+
var expected = 'x';
|
395
|
+
matcher.toContain(expected);
|
396
|
+
|
397
|
+
var result = lastResult();
|
398
|
+
|
399
|
+
expect(result.matcherName).toEqual("toContain");
|
400
|
+
expect(result.passed()).toFail();
|
401
|
+
expect(result.message).toMatch(jasmine.pp(actual));
|
402
|
+
expect(result.message).toMatch('contain');
|
403
|
+
expect(result.message).toMatch(jasmine.pp(expected));
|
404
|
+
expect(result.actual).toEqual(actual);
|
405
|
+
expect(result.expected).toEqual(expected);
|
406
|
+
});
|
407
|
+
|
408
|
+
it("toNotContain to build an ExpectationResult", function() {
|
409
|
+
var actual = ['a','b','c'];
|
410
|
+
var matcher = match(actual);
|
411
|
+
var expected = 'b';
|
412
|
+
matcher.toNotContain(expected);
|
413
|
+
|
414
|
+
var result = lastResult();
|
415
|
+
|
416
|
+
expect(result.matcherName).toEqual("toNotContain");
|
417
|
+
expect(result.passed()).toFail();
|
418
|
+
expect(result.message).toMatch(jasmine.pp(actual));
|
419
|
+
expect(result.message).toMatch('not contain');
|
420
|
+
expect(result.message).toMatch(jasmine.pp(expected));
|
421
|
+
expect(result.actual).toEqual(actual);
|
422
|
+
expect(result.expected).toEqual(expected);
|
423
|
+
});
|
424
|
+
|
425
|
+
it("toBeLessThan should pass if actual is less than expected", function() {
|
426
|
+
expect(match(37).toBeLessThan(42)).toPass();
|
427
|
+
expect(match(37).toBeLessThan(-42)).toFail();
|
428
|
+
expect(match(37).toBeLessThan(37)).toFail();
|
429
|
+
});
|
430
|
+
|
431
|
+
it("toBeLessThan to build an ExpectationResult", function() {
|
432
|
+
var actual = 3;
|
433
|
+
var matcher = match(actual);
|
434
|
+
var expected = 1;
|
435
|
+
matcher.toBeLessThan(expected);
|
436
|
+
|
437
|
+
var result = lastResult();
|
438
|
+
|
439
|
+
expect(result.matcherName).toEqual("toBeLessThan");
|
440
|
+
expect(result.passed()).toFail();
|
441
|
+
expect(result.message).toMatch(jasmine.pp(actual) + ' to be less than');
|
442
|
+
expect(result.message).toMatch(jasmine.pp(expected));
|
443
|
+
expect(result.actual).toEqual(actual);
|
444
|
+
expect(result.expected).toEqual(expected);
|
445
|
+
});
|
446
|
+
|
447
|
+
it("toBeGreaterThan should pass if actual is greater than expected", function() {
|
448
|
+
expect(match(37).toBeGreaterThan(42)).toFail();
|
449
|
+
expect(match(37).toBeGreaterThan(-42)).toPass();
|
450
|
+
expect(match(37).toBeGreaterThan(37)).toFail();
|
451
|
+
});
|
452
|
+
|
453
|
+
it("toBeGreaterThan to build an ExpectationResult", function() {
|
454
|
+
var actual = 1;
|
455
|
+
var matcher = match(actual);
|
456
|
+
var expected = 3;
|
457
|
+
matcher.toBeGreaterThan(expected);
|
458
|
+
|
459
|
+
var result = lastResult();
|
460
|
+
|
461
|
+
expect(result.matcherName).toEqual("toBeGreaterThan");
|
462
|
+
expect(result.passed()).toFail();
|
463
|
+
expect(result.message).toMatch(jasmine.pp(actual) + ' to be greater than');
|
464
|
+
expect(result.message).toMatch(jasmine.pp(expected));
|
465
|
+
expect(result.actual).toEqual(actual);
|
466
|
+
expect(result.expected).toEqual(expected);
|
467
|
+
});
|
468
|
+
|
469
|
+
describe("toBeCloseTo", function() {
|
470
|
+
it("returns 'true' iff actual and expected are equal within 2 decimal points of precision", function() {
|
471
|
+
expect(0).toBeCloseTo(0);
|
472
|
+
expect(1).toBeCloseTo(1);
|
473
|
+
expect(1).not.toBeCloseTo(1.1);
|
474
|
+
expect(1).not.toBeCloseTo(1.01);
|
475
|
+
expect(1).toBeCloseTo(1.001);
|
476
|
+
|
477
|
+
expect(1.23).toBeCloseTo(1.234);
|
478
|
+
expect(1.23).toBeCloseTo(1.233);
|
479
|
+
expect(1.23).toBeCloseTo(1.232);
|
480
|
+
expect(1.23).not.toBeCloseTo(1.24);
|
481
|
+
|
482
|
+
expect(-1.23).toBeCloseTo(-1.234);
|
483
|
+
expect(-1.23).not.toBeCloseTo(-1.24);
|
484
|
+
});
|
485
|
+
|
486
|
+
it("accepts an optional precision argument", function() {
|
487
|
+
expect(1).toBeCloseTo(1.1, 0);
|
488
|
+
expect(1.2).toBeCloseTo(1.23, 1);
|
489
|
+
|
490
|
+
expect(1.234).toBeCloseTo(1.2343, 3);
|
491
|
+
expect(1.234).not.toBeCloseTo(1.233, 3);
|
492
|
+
});
|
493
|
+
|
494
|
+
it("rounds", function() {
|
495
|
+
expect(1.23).toBeCloseTo(1.229);
|
496
|
+
expect(1.23).toBeCloseTo(1.226);
|
497
|
+
expect(1.23).toBeCloseTo(1.225);
|
498
|
+
expect(1.23).not.toBeCloseTo(1.2249999);
|
499
|
+
|
500
|
+
expect(1.23).toBeCloseTo(1.234);
|
501
|
+
expect(1.23).toBeCloseTo(1.2349999);
|
502
|
+
expect(1.23).not.toBeCloseTo(1.235);
|
503
|
+
|
504
|
+
expect(-1.23).toBeCloseTo(-1.234);
|
505
|
+
expect(-1.23).not.toBeCloseTo(-1.235);
|
506
|
+
expect(-1.23).not.toBeCloseTo(-1.236);
|
507
|
+
});
|
508
|
+
});
|
509
|
+
|
510
|
+
describe("toThrow", function() {
|
511
|
+
describe("when code block throws an exception", function() {
|
512
|
+
var throwingFn;
|
513
|
+
|
514
|
+
beforeEach(function() {
|
515
|
+
throwingFn = function() {
|
516
|
+
throw new Error("Fake Error");
|
517
|
+
};
|
518
|
+
});
|
519
|
+
|
520
|
+
it("should match any exception", function() {
|
521
|
+
expect(match(throwingFn).toThrow()).toPass();
|
522
|
+
});
|
523
|
+
|
524
|
+
it("should match exceptions specified by message", function() {
|
525
|
+
expect(match(throwingFn).toThrow("Fake Error")).toPass();
|
526
|
+
expect(match(throwingFn).toThrow("Other Error")).toFail();
|
527
|
+
expect(lastResult().message).toMatch("Other Error");
|
528
|
+
});
|
529
|
+
|
530
|
+
it("should match exceptions specified by Error", function() {
|
531
|
+
expect(match(throwingFn).toThrow(new Error("Fake Error"))).toPass();
|
532
|
+
expect(match(throwingFn).toThrow(new Error("Other Error"))).toFail();
|
533
|
+
expect(lastResult().message).toMatch("Other Error");
|
534
|
+
});
|
535
|
+
|
536
|
+
describe("and matcher is inverted with .not", function() {
|
537
|
+
it("should match any exception", function() {
|
538
|
+
expect(match(throwingFn).not.toThrow()).toFail();
|
539
|
+
expect(lastResult().message).toMatch(/Expected function not to throw an exception/);
|
540
|
+
});
|
541
|
+
|
542
|
+
it("should match exceptions specified by message", function() {
|
543
|
+
expect(match(throwingFn).not.toThrow("Fake Error")).toFail();
|
544
|
+
// expect(lastResult().message).toMatch(/Expected function not to throw Fake Error./);
|
545
|
+
expect(match(throwingFn).not.toThrow("Other Error")).toPass();
|
546
|
+
});
|
547
|
+
|
548
|
+
it("should match exceptions specified by Error", function() {
|
549
|
+
expect(match(throwingFn).not.toThrow(new Error("Fake Error"))).toFail();
|
550
|
+
// expect(lastResult().message).toMatch("Other Error");
|
551
|
+
expect(match(throwingFn).not.toThrow(new Error("Other Error"))).toPass();
|
552
|
+
});
|
553
|
+
});
|
554
|
+
});
|
555
|
+
|
556
|
+
describe("when actual is not a function", function() {
|
557
|
+
it("should fail with an exception", function() {
|
558
|
+
var exception = catchException(function() {
|
559
|
+
match('not-a-function').toThrow();
|
560
|
+
});
|
561
|
+
expect(exception).toBeDefined();
|
562
|
+
expect(exception.message).toEqual('Actual is not a function');
|
563
|
+
});
|
564
|
+
|
565
|
+
describe("and matcher is inverted with .not", function() {
|
566
|
+
it("should fail with an exception", function() {
|
567
|
+
var exception = catchException(function() {
|
568
|
+
match('not-a-function').not.toThrow();
|
569
|
+
});
|
570
|
+
expect(exception).toBeDefined();
|
571
|
+
expect(exception.message).toEqual('Actual is not a function');
|
572
|
+
});
|
573
|
+
});
|
574
|
+
});
|
575
|
+
|
576
|
+
|
577
|
+
describe("when code block does not throw an exception", function() {
|
578
|
+
it("should fail (or pass when inverted with .not)", function() {
|
579
|
+
expect(match(
|
580
|
+
function() {
|
581
|
+
}).toThrow()).toFail();
|
582
|
+
expect(lastResult().message).toEqual('Expected function to throw an exception.');
|
583
|
+
});
|
584
|
+
});
|
585
|
+
});
|
586
|
+
|
587
|
+
describe(".not.matcher", function() {
|
588
|
+
it("should invert the sense of any matcher", function() {
|
589
|
+
expect(match(37).not.toBeGreaterThan(42)).toPass();
|
590
|
+
expect(match(42).not.toBeGreaterThan(37)).toFail();
|
591
|
+
expect(match("abc").not.toEqual("def")).toPass();
|
592
|
+
expect(match("abc").not.toEqual("abc")).toFail();
|
593
|
+
});
|
594
|
+
|
595
|
+
it("should provide an inverted default message", function() {
|
596
|
+
match(37).not.toBeGreaterThan(42);
|
597
|
+
expect(lastResult().message).toEqual("Passed.");
|
598
|
+
|
599
|
+
match(42).not.toBeGreaterThan(37);
|
600
|
+
expect(lastResult().message).toEqual("Expected 42 not to be greater than 37.");
|
601
|
+
});
|
602
|
+
|
603
|
+
it("should use the second message when the matcher sets an array of custom messages", function() {
|
604
|
+
spec.addMatchers({
|
605
|
+
custom: function() {
|
606
|
+
this.message = function() {
|
607
|
+
return ['Expected it was called.', 'Expected it wasn\'t called.'];
|
608
|
+
};
|
609
|
+
return this.actual;
|
610
|
+
}
|
611
|
+
});
|
612
|
+
|
613
|
+
match(true).custom();
|
614
|
+
expect(lastResult().message).toEqual("Passed.");
|
615
|
+
match(false).custom();
|
616
|
+
expect(lastResult().message).toEqual("Expected it was called.");
|
617
|
+
match(true).not.custom();
|
618
|
+
expect(lastResult().message).toEqual("Expected it wasn't called.");
|
619
|
+
match(false).not.custom();
|
620
|
+
expect(lastResult().message).toEqual("Passed.");
|
621
|
+
});
|
622
|
+
});
|
623
|
+
|
624
|
+
describe("spy matchers >>", function() {
|
625
|
+
var TestClass;
|
626
|
+
beforeEach(function() {
|
627
|
+
TestClass = {
|
628
|
+
normalFunction: function() {
|
629
|
+
},
|
630
|
+
spyFunction: jasmine.createSpy("My spy")
|
631
|
+
};
|
632
|
+
});
|
633
|
+
|
634
|
+
function shouldThrowAnExceptionWhenInvokedOnANonSpy(methodName) {
|
635
|
+
return function() {
|
636
|
+
expect(
|
637
|
+
function() {
|
638
|
+
match(TestClass.normalFunction)[methodName]();
|
639
|
+
}).toThrow('Expected a spy, but got Function.');
|
640
|
+
|
641
|
+
expect(
|
642
|
+
function() {
|
643
|
+
match(jasmine.undefined)[methodName]();
|
644
|
+
}).toThrow('Expected a spy, but got undefined.');
|
645
|
+
|
646
|
+
expect(
|
647
|
+
function() {
|
648
|
+
match({some:'object'})[methodName]();
|
649
|
+
}).toThrow('Expected a spy, but got { some : \'object\' }.');
|
650
|
+
|
651
|
+
expect(
|
652
|
+
function() {
|
653
|
+
match("<b>")[methodName]();
|
654
|
+
}).toThrow('Expected a spy, but got \'<b>\'.');
|
655
|
+
};
|
656
|
+
}
|
657
|
+
|
658
|
+
describe("toHaveBeenCalled", function() {
|
659
|
+
it("should pass if the spy was called", function() {
|
660
|
+
expect(match(TestClass.spyFunction).toHaveBeenCalled()).toFail();
|
661
|
+
|
662
|
+
TestClass.spyFunction();
|
663
|
+
expect(match(TestClass.spyFunction).toHaveBeenCalled()).toPass();
|
664
|
+
});
|
665
|
+
|
666
|
+
it("should throw an exception when invoked with any arguments", function() {
|
667
|
+
expect(
|
668
|
+
function() {
|
669
|
+
match(TestClass.normalFunction).toHaveBeenCalled("unwanted argument");
|
670
|
+
}).toThrow('toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith');
|
671
|
+
});
|
672
|
+
|
673
|
+
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('toHaveBeenCalled'));
|
674
|
+
});
|
675
|
+
|
676
|
+
describe("wasCalled", function() {
|
677
|
+
it("should alias toHaveBeenCalled", function() {
|
678
|
+
spyOn(TestClass, 'normalFunction');
|
679
|
+
|
680
|
+
TestClass.normalFunction();
|
681
|
+
|
682
|
+
expect(TestClass.normalFunction).wasCalled();
|
683
|
+
});
|
684
|
+
});
|
685
|
+
|
686
|
+
|
687
|
+
describe("wasNotCalled", function() {
|
688
|
+
it("should pass iff the spy was not called", function() {
|
689
|
+
expect(match(TestClass.spyFunction).wasNotCalled()).toPass();
|
690
|
+
|
691
|
+
TestClass.spyFunction();
|
692
|
+
expect(match(TestClass.spyFunction).wasNotCalled()).toFail();
|
693
|
+
});
|
694
|
+
|
695
|
+
it("should throw an exception when invoked with any arguments", function() {
|
696
|
+
expect(
|
697
|
+
function() {
|
698
|
+
match(TestClass.normalFunction).wasNotCalled("unwanted argument");
|
699
|
+
}).toThrow('wasNotCalled does not take arguments');
|
700
|
+
});
|
701
|
+
|
702
|
+
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('wasNotCalled'));
|
703
|
+
});
|
704
|
+
|
705
|
+
describe("toHaveBeenCalledWith", function() {
|
706
|
+
it('toHaveBeenCalledWith should return true if it was called with the expected args', function() {
|
707
|
+
TestClass.spyFunction('a', 'b', 'c');
|
708
|
+
expect(match(TestClass.spyFunction).toHaveBeenCalledWith('a', 'b', 'c')).toPass();
|
709
|
+
});
|
710
|
+
|
711
|
+
it('should return false if it was not called with the expected args', function() {
|
712
|
+
TestClass.spyFunction('a', 'b', 'c');
|
713
|
+
var expected = match(TestClass.spyFunction);
|
714
|
+
expect(expected.toHaveBeenCalledWith('c', 'b', 'a')).toFail();
|
715
|
+
var result = lastResult();
|
716
|
+
expect(result.passed()).toFail();
|
717
|
+
expect(result.expected).toEqual(['c', 'b', 'a']);
|
718
|
+
expect(result.actual.mostRecentCall.args).toEqual(['a', 'b', 'c']);
|
719
|
+
expect(result.message).toContain(jasmine.pp(result.expected));
|
720
|
+
expect(result.message).toContain(jasmine.pp(result.actual.mostRecentCall.args));
|
721
|
+
});
|
722
|
+
|
723
|
+
it('should return false if it was not called', function() {
|
724
|
+
var expected = match(TestClass.spyFunction);
|
725
|
+
expect(expected.toHaveBeenCalledWith('c', 'b', 'a')).toFail();
|
726
|
+
var result = lastResult();
|
727
|
+
expect(result.passed()).toFail();
|
728
|
+
expect(result.expected).toEqual(['c', 'b', 'a']);
|
729
|
+
expect(result.actual.argsForCall).toEqual([]);
|
730
|
+
expect(result.message).toContain(jasmine.pp(result.expected));
|
731
|
+
});
|
732
|
+
|
733
|
+
it('should allow matches across multiple calls', function() {
|
734
|
+
TestClass.spyFunction('a', 'b', 'c');
|
735
|
+
TestClass.spyFunction('d', 'e', 'f');
|
736
|
+
var expected = match(TestClass.spyFunction);
|
737
|
+
expect(expected.toHaveBeenCalledWith('a', 'b', 'c')).toPass();
|
738
|
+
expect(expected.toHaveBeenCalledWith('d', 'e', 'f')).toPass();
|
739
|
+
expect(expected.toHaveBeenCalledWith('x', 'y', 'z')).toFail();
|
740
|
+
});
|
741
|
+
|
742
|
+
it("should return a decent message", function() {
|
743
|
+
TestClass.spyFunction('a', 'b', 'c');
|
744
|
+
TestClass.spyFunction('d', 'e', 'f');
|
745
|
+
var expected = match(TestClass.spyFunction);
|
746
|
+
expect(expected.toHaveBeenCalledWith('a', 'b')).toFail();
|
747
|
+
expect(lastResult().message).toEqual("Expected spy My spy to have been called with [ 'a', 'b' ] but was called with [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ] ]");
|
748
|
+
});
|
749
|
+
|
750
|
+
it("should return a decent message when inverted", function() {
|
751
|
+
TestClass.spyFunction('a', 'b', 'c');
|
752
|
+
TestClass.spyFunction('d', 'e', 'f');
|
753
|
+
var expected = match(TestClass.spyFunction);
|
754
|
+
expect(expected.not.toHaveBeenCalledWith('a', 'b', 'c')).toFail();
|
755
|
+
expect(lastResult().message).toEqual("Expected spy My spy not to have been called with [ 'a', 'b', 'c' ] but was called with [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ] ]");
|
756
|
+
});
|
757
|
+
|
758
|
+
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('toHaveBeenCalledWith'));
|
759
|
+
|
760
|
+
describe("to build an ExpectationResult", function () {
|
761
|
+
beforeEach(function() {
|
762
|
+
var currentSuite;
|
763
|
+
var spec;
|
764
|
+
currentSuite = env.describe('default current suite', function() {
|
765
|
+
spec = env.it();
|
766
|
+
}, spec);
|
767
|
+
TestClass = { someFunction: function(a, b) {
|
768
|
+
} };
|
769
|
+
spec.spyOn(TestClass, 'someFunction');
|
770
|
+
});
|
771
|
+
|
772
|
+
it("should should handle the case of a spy", function() {
|
773
|
+
TestClass.someFunction('a', 'c');
|
774
|
+
var matcher = match(TestClass.someFunction);
|
775
|
+
matcher.toHaveBeenCalledWith('a', 'b');
|
776
|
+
|
777
|
+
var result = lastResult();
|
778
|
+
expect(result.matcherName).toEqual("toHaveBeenCalledWith");
|
779
|
+
expect(result.passed()).toFail();
|
780
|
+
expect(result.message).toContain(jasmine.pp(['a', 'b']));
|
781
|
+
expect(result.message).toContain(jasmine.pp(['a', 'c']));
|
782
|
+
expect(result.actual).toEqual(TestClass.someFunction);
|
783
|
+
expect(result.expected).toEqual(['a','b']);
|
784
|
+
});
|
785
|
+
});
|
786
|
+
});
|
787
|
+
|
788
|
+
describe("wasCalledWith", function() {
|
789
|
+
it("should alias toHaveBeenCalledWith", function() {
|
790
|
+
spyOn(TestClass, 'normalFunction');
|
791
|
+
|
792
|
+
TestClass.normalFunction(123);
|
793
|
+
|
794
|
+
expect(TestClass.normalFunction).wasCalledWith(123);
|
795
|
+
});
|
796
|
+
});
|
797
|
+
|
798
|
+
describe("wasNotCalledWith", function() {
|
799
|
+
it('should return true if the spy was NOT called with the expected args', function() {
|
800
|
+
TestClass.spyFunction('a', 'b', 'c');
|
801
|
+
expect(match(TestClass.spyFunction).wasNotCalledWith('c', 'b', 'a')).toPass();
|
802
|
+
});
|
803
|
+
|
804
|
+
it('should return false if it WAS called with the expected args', function() {
|
805
|
+
TestClass.spyFunction('a', 'b', 'c');
|
806
|
+
var expected = match(TestClass.spyFunction);
|
807
|
+
expect(expected.wasNotCalledWith('a', 'b', 'c')).toFail();
|
808
|
+
var result = lastResult();
|
809
|
+
expect(result.passed()).toFail();
|
810
|
+
expect(result.expected).toEqual(['a', 'b', 'c']);
|
811
|
+
expect(result.actual.mostRecentCall.args).toEqual(['a', 'b', 'c']);
|
812
|
+
expect(result.message).toContain(jasmine.pp(result.expected));
|
813
|
+
});
|
814
|
+
|
815
|
+
it('should return true if it was not called', function() {
|
816
|
+
var expected = match(TestClass.spyFunction);
|
817
|
+
expect(expected.wasNotCalledWith('c', 'b', 'a')).toPass();
|
818
|
+
});
|
819
|
+
|
820
|
+
it('should allow matches across multiple calls', function() {
|
821
|
+
var expected = match(TestClass.spyFunction);
|
822
|
+
TestClass.spyFunction('a', 'b', 'c');
|
823
|
+
TestClass.spyFunction('d', 'e', 'f');
|
824
|
+
expect(expected.wasNotCalledWith('a', 'b', 'c')).toFail();
|
825
|
+
expect(expected.wasNotCalledWith('d', 'e', 'f')).toFail();
|
826
|
+
expect(expected.wasNotCalledWith('x', 'y', 'z')).toPass();
|
827
|
+
});
|
828
|
+
|
829
|
+
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('wasNotCalledWith'));
|
830
|
+
});
|
831
|
+
});
|
832
|
+
|
833
|
+
describe("all matchers", function() {
|
834
|
+
it("should return null, for future-proofing, since we might eventually allow matcher chaining", function() {
|
835
|
+
expect(match(true).toBe(true)).toBeUndefined();
|
836
|
+
});
|
837
|
+
});
|
838
|
+
});
|