blob: ce84eabc34e1d47ba5ef2af53203f9767a23e7be [file] [log] [blame]
Roman Reissf29762f2015-05-19 11:00:061'use strict';
Oleg Efimov0665f022010-12-05 19:15:302var common = require('../common');
Ryan Dahla0159b42010-12-04 23:20:343var assert = require('assert');
Karl Guertin4f679fd2009-11-30 02:20:374var a = require('assert');
5
Oleg Efimov0665f022010-12-05 19:15:306function makeBlock(f) {
7 var args = Array.prototype.slice.call(arguments, 1);
8 return function() {
9 return f.apply(this, args);
Ryan Dahldd356372009-12-29 18:37:4010 };
Karl Guertin4f679fd2009-11-30 02:20:3711}
12
Herbert Vojčíkcf2b2062010-08-17 15:21:4313assert.ok(common.indirectInstanceOf(a.AssertionError.prototype, Error),
Oleg Efimov0665f022010-12-05 19:15:3014 'a.AssertionError instanceof Error');
Karl Guertin4f679fd2009-11-30 02:20:3715
Maciej Małecki365fdbf2011-10-01 11:52:2416assert.throws(makeBlock(a, false), a.AssertionError, 'ok(false)');
17
18assert.doesNotThrow(makeBlock(a, true), a.AssertionError, 'ok(true)');
19
20assert.doesNotThrow(makeBlock(a, 'test', 'ok(\'test\')'));
21
Ryan Dahldd356372009-12-29 18:37:4022assert.throws(makeBlock(a.ok, false),
Oleg Efimov0665f022010-12-05 19:15:3023 a.AssertionError, 'ok(false)');
Ryan Dahldd356372009-12-29 18:37:4024
25assert.doesNotThrow(makeBlock(a.ok, true),
Oleg Efimov0665f022010-12-05 19:15:3026 a.AssertionError, 'ok(true)');
Ryan Dahldd356372009-12-29 18:37:4027
Oleg Efimov0665f022010-12-05 19:15:3028assert.doesNotThrow(makeBlock(a.ok, 'test'), 'ok(\'test\')');
Karl Guertin4f679fd2009-11-30 02:20:3729
30assert.throws(makeBlock(a.equal, true, false), a.AssertionError, 'equal');
Ryan Dahldd356372009-12-29 18:37:4031
Karl Guertin4f679fd2009-11-30 02:20:3732assert.doesNotThrow(makeBlock(a.equal, null, null), 'equal');
Ryan Dahldd356372009-12-29 18:37:4033
Karl Guertin4f679fd2009-11-30 02:20:3734assert.doesNotThrow(makeBlock(a.equal, undefined, undefined), 'equal');
Ryan Dahldd356372009-12-29 18:37:4035
Karl Guertin4f679fd2009-11-30 02:20:3736assert.doesNotThrow(makeBlock(a.equal, null, undefined), 'equal');
Ryan Dahldd356372009-12-29 18:37:4037
Karl Guertin4f679fd2009-11-30 02:20:3738assert.doesNotThrow(makeBlock(a.equal, true, true), 'equal');
Ryan Dahldd356372009-12-29 18:37:4039
Oleg Efimov0665f022010-12-05 19:15:3040assert.doesNotThrow(makeBlock(a.equal, 2, '2'), 'equal');
Karl Guertin4f679fd2009-11-30 02:20:3741
42assert.doesNotThrow(makeBlock(a.notEqual, true, false), 'notEqual');
Ryan Dahldd356372009-12-29 18:37:4043
Oleg Efimov0665f022010-12-05 19:15:3044assert.throws(makeBlock(a.notEqual, true, true),
45 a.AssertionError, 'notEqual');
Karl Guertin4f679fd2009-11-30 02:20:3746
Oleg Efimov0665f022010-12-05 19:15:3047assert.throws(makeBlock(a.strictEqual, 2, '2'),
48 a.AssertionError, 'strictEqual');
Ryan Dahldd356372009-12-29 18:37:4049
Oleg Efimov0665f022010-12-05 19:15:3050assert.throws(makeBlock(a.strictEqual, null, undefined),
51 a.AssertionError, 'strictEqual');
Karl Guertin4f679fd2009-11-30 02:20:3752
Oleg Efimov0665f022010-12-05 19:15:3053assert.doesNotThrow(makeBlock(a.notStrictEqual, 2, '2'), 'notStrictEqual');
Karl Guertin4f679fd2009-11-30 02:20:3754
Ryan Dahldd356372009-12-29 18:37:4055// deepEquals joy!
56// 7.2
Oleg Efimov0665f022010-12-05 19:15:3057assert.doesNotThrow(makeBlock(a.deepEqual, new Date(2000, 3, 14),
58 new Date(2000, 3, 14)), 'deepEqual date');
Ryan Dahldd356372009-12-29 18:37:4059
Oleg Efimov0665f022010-12-05 19:15:3060assert.throws(makeBlock(a.deepEqual, new Date(), new Date(2000, 3, 14)),
Ryan Dahldd356372009-12-29 18:37:4061 a.AssertionError,
62 'deepEqual date');
63
64// 7.3
Pedro Teixeiraa8050122011-02-02 11:09:0265assert.doesNotThrow(makeBlock(a.deepEqual, /a/, /a/));
Maciej Małecki5c7532e2011-12-19 22:28:4266assert.doesNotThrow(makeBlock(a.deepEqual, /a/g, /a/g));
67assert.doesNotThrow(makeBlock(a.deepEqual, /a/i, /a/i));
68assert.doesNotThrow(makeBlock(a.deepEqual, /a/m, /a/m));
69assert.doesNotThrow(makeBlock(a.deepEqual, /a/igm, /a/igm));
Pedro Teixeiraa8050122011-02-02 11:09:0270assert.throws(makeBlock(a.deepEqual, /ab/, /a/));
Maciej Małecki5c7532e2011-12-19 22:28:4271assert.throws(makeBlock(a.deepEqual, /a/g, /a/));
72assert.throws(makeBlock(a.deepEqual, /a/i, /a/));
73assert.throws(makeBlock(a.deepEqual, /a/m, /a/));
74assert.throws(makeBlock(a.deepEqual, /a/igm, /a/im));
75
76var re1 = /a/;
77re1.lastIndex = 3;
78assert.throws(makeBlock(a.deepEqual, re1, /a/));
Pedro Teixeiraa8050122011-02-02 11:09:0279
80
81// 7.4
Oleg Efimov0665f022010-12-05 19:15:3082assert.doesNotThrow(makeBlock(a.deepEqual, 4, '4'), 'deepEqual == check');
Karl Guertin4f679fd2009-11-30 02:20:3783assert.doesNotThrow(makeBlock(a.deepEqual, true, 1), 'deepEqual == check');
Oleg Efimov0665f022010-12-05 19:15:3084assert.throws(makeBlock(a.deepEqual, 4, '5'),
Ryan Dahldd356372009-12-29 18:37:4085 a.AssertionError,
86 'deepEqual == check');
87
Pedro Teixeiraa8050122011-02-02 11:09:0288// 7.5
Karl Guertin4f679fd2009-11-30 02:20:3789// having the same number of owned properties && the same set of keys
Oleg Efimov0665f022010-12-05 19:15:3090assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4}, {a: 4}));
91assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'}));
92assert.doesNotThrow(makeBlock(a.deepEqual, [4], ['4']));
93assert.throws(makeBlock(a.deepEqual, {a: 4}, {a: 4, b: true}),
94 a.AssertionError);
95assert.doesNotThrow(makeBlock(a.deepEqual, ['a'], {0: 'a'}));
Karl Guertin4f679fd2009-11-30 02:20:3796//(although not necessarily the same order),
Oleg Efimov0665f022010-12-05 19:15:3097assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '1'}, {b: '1', a: 4}));
98var a1 = [1, 2, 3];
99var a2 = [1, 2, 3];
100a1.a = 'test';
Karl Guertin4f679fd2009-11-30 02:20:37101a1.b = true;
102a2.b = true;
Oleg Efimov0665f022010-12-05 19:15:30103a2.a = 'test';
104assert.throws(makeBlock(a.deepEqual, Object.keys(a1), Object.keys(a2)),
Ryan Dahldd356372009-12-29 18:37:40105 a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37106assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2));
107
108// having an identical prototype property
109var nbRoot = {
Oleg Efimov0665f022010-12-05 19:15:30110 toString: function() { return this.first + ' ' + this.last; }
Ryan Dahldd356372009-12-29 18:37:40111};
112
Oleg Efimov0665f022010-12-05 19:15:30113function nameBuilder(first, last) {
Ryan Dahldd356372009-12-29 18:37:40114 this.first = first;
115 this.last = last;
116 return this;
Karl Guertin4f679fd2009-11-30 02:20:37117}
118nameBuilder.prototype = nbRoot;
Ryan Dahldd356372009-12-29 18:37:40119
Oleg Efimov0665f022010-12-05 19:15:30120function nameBuilder2(first, last) {
Ryan Dahldd356372009-12-29 18:37:40121 this.first = first;
122 this.last = last;
123 return this;
Karl Guertin4f679fd2009-11-30 02:20:37124}
125nameBuilder2.prototype = nbRoot;
Ryan Dahldd356372009-12-29 18:37:40126
Karl Guertin4f679fd2009-11-30 02:20:37127var nb1 = new nameBuilder('Ryan', 'Dahl');
Oleg Efimov0665f022010-12-05 19:15:30128var nb2 = new nameBuilder2('Ryan', 'Dahl');
Karl Guertin4f679fd2009-11-30 02:20:37129
130assert.doesNotThrow(makeBlock(a.deepEqual, nb1, nb2));
131
132nameBuilder2.prototype = Object;
Oleg Efimov0665f022010-12-05 19:15:30133nb2 = new nameBuilder2('Ryan', 'Dahl');
Vladimir Kurchatkine7573f92015-01-28 15:27:20134assert.doesNotThrow(makeBlock(a.deepEqual, nb1, nb2));
Karl Guertin4f679fd2009-11-30 02:20:37135
teppeis00a74562014-12-21 15:56:33136// primitives and object
137assert.throws(makeBlock(a.deepEqual, null, {}), a.AssertionError);
138assert.throws(makeBlock(a.deepEqual, undefined, {}), a.AssertionError);
139assert.throws(makeBlock(a.deepEqual, 'a', ['a']), a.AssertionError);
140assert.throws(makeBlock(a.deepEqual, 'a', {0: 'a'}), a.AssertionError);
141assert.throws(makeBlock(a.deepEqual, 1, {}), a.AssertionError);
142assert.throws(makeBlock(a.deepEqual, true, {}), a.AssertionError);
143if (typeof Symbol === 'symbol') {
144 assert.throws(makeBlock(assert.deepEqual, Symbol(), {}), a.AssertionError);
145}
146
147// primitive wrappers and object
Roman Reissf29762f2015-05-19 11:00:06148assert.doesNotThrow(makeBlock(a.deepEqual, new String('a'), ['a']),
149 a.AssertionError);
150assert.doesNotThrow(makeBlock(a.deepEqual, new String('a'), {0: 'a'}),
151 a.AssertionError);
152assert.doesNotThrow(makeBlock(a.deepEqual, new Number(1), {}),
153 a.AssertionError);
154assert.doesNotThrow(makeBlock(a.deepEqual, new Boolean(true), {}),
155 a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37156
Vladimir Kurchatkin3f473ef2015-01-28 16:48:56157//deepStrictEqual
158assert.doesNotThrow(makeBlock(a.deepStrictEqual, new Date(2000, 3, 14),
159 new Date(2000, 3, 14)), 'deepStrictEqual date');
160
161assert.throws(makeBlock(a.deepStrictEqual, new Date(), new Date(2000, 3, 14)),
162 a.AssertionError,
163 'deepStrictEqual date');
164
165// 7.3 - strict
166assert.doesNotThrow(makeBlock(a.deepStrictEqual, /a/, /a/));
167assert.doesNotThrow(makeBlock(a.deepStrictEqual, /a/g, /a/g));
168assert.doesNotThrow(makeBlock(a.deepStrictEqual, /a/i, /a/i));
169assert.doesNotThrow(makeBlock(a.deepStrictEqual, /a/m, /a/m));
170assert.doesNotThrow(makeBlock(a.deepStrictEqual, /a/igm, /a/igm));
171assert.throws(makeBlock(a.deepStrictEqual, /ab/, /a/));
172assert.throws(makeBlock(a.deepStrictEqual, /a/g, /a/));
173assert.throws(makeBlock(a.deepStrictEqual, /a/i, /a/));
174assert.throws(makeBlock(a.deepStrictEqual, /a/m, /a/));
175assert.throws(makeBlock(a.deepStrictEqual, /a/igm, /a/im));
176
177var re1 = /a/;
178re1.lastIndex = 3;
179assert.throws(makeBlock(a.deepStrictEqual, re1, /a/));
180
181
182// 7.4 - strict
183assert.throws(makeBlock(a.deepStrictEqual, 4, '4'),
184 a.AssertionError,
185 'deepStrictEqual === check');
186
187assert.throws(makeBlock(a.deepStrictEqual, true, 1),
188 a.AssertionError,
189 'deepStrictEqual === check');
190
191assert.throws(makeBlock(a.deepStrictEqual, 4, '5'),
192 a.AssertionError,
193 'deepStrictEqual === check');
194
195// 7.5 - strict
196// having the same number of owned properties && the same set of keys
197assert.doesNotThrow(makeBlock(a.deepStrictEqual, {a: 4}, {a: 4}));
198assert.doesNotThrow(makeBlock(a.deepStrictEqual,
199 {a: 4, b: '2'},
200 {a: 4, b: '2'}));
201assert.throws(makeBlock(a.deepStrictEqual, [4], ['4']));
202assert.throws(makeBlock(a.deepStrictEqual, {a: 4}, {a: 4, b: true}),
203 a.AssertionError);
204assert.throws(makeBlock(a.deepStrictEqual, ['a'], {0: 'a'}));
205//(although not necessarily the same order),
206assert.doesNotThrow(makeBlock(a.deepStrictEqual,
207 {a: 4, b: '1'},
208 {b: '1', a: 4}));
209
210assert.throws(makeBlock(a.deepStrictEqual,
211 [0, 1, 2, 'a', 'b'],
212 [0, 1, 2, 'b', 'a']),
213 a.AssertionError);
214
215assert.doesNotThrow(makeBlock(a.deepStrictEqual, a1, a2));
216
217// Prototype check
218function Constructor1(first, last) {
219 this.first = first;
220 this.last = last;
221}
222
223function Constructor2(first, last) {
224 this.first = first;
225 this.last = last;
226}
227
228var obj1 = new Constructor1('Ryan', 'Dahl');
229var obj2 = new Constructor2('Ryan', 'Dahl');
230
231assert.throws(makeBlock(a.deepStrictEqual, obj1, obj2), a.AssertionError);
232
233Constructor2.prototype = Constructor1.prototype;
234obj2 = new Constructor2('Ryan', 'Dahl');
235
236assert.doesNotThrow(makeBlock(a.deepStrictEqual, obj1, obj2));
237
238// primitives
239assert.throws(makeBlock(assert.deepStrictEqual, 4, '4'),
240 a.AssertionError);
241assert.throws(makeBlock(assert.deepStrictEqual, true, 1),
242 a.AssertionError);
243assert.throws(makeBlock(assert.deepStrictEqual, Symbol(), Symbol()),
244 a.AssertionError);
245
246var s = Symbol();
247assert.doesNotThrow(makeBlock(assert.deepStrictEqual, s, s));
248
249
250// primitives and object
251assert.throws(makeBlock(a.deepStrictEqual, null, {}), a.AssertionError);
252assert.throws(makeBlock(a.deepStrictEqual, undefined, {}), a.AssertionError);
253assert.throws(makeBlock(a.deepStrictEqual, 'a', ['a']), a.AssertionError);
254assert.throws(makeBlock(a.deepStrictEqual, 'a', {0: 'a'}), a.AssertionError);
255assert.throws(makeBlock(a.deepStrictEqual, 1, {}), a.AssertionError);
256assert.throws(makeBlock(a.deepStrictEqual, true, {}), a.AssertionError);
257assert.throws(makeBlock(assert.deepStrictEqual, Symbol(), {}),
258 a.AssertionError);
259
260
261// primitive wrappers and object
262assert.throws(makeBlock(a.deepStrictEqual, new String('a'), ['a']),
263 a.AssertionError);
264assert.throws(makeBlock(a.deepStrictEqual, new String('a'), {0: 'a'}),
265 a.AssertionError);
266assert.throws(makeBlock(a.deepStrictEqual, new Number(1), {}),
267 a.AssertionError);
268assert.throws(makeBlock(a.deepStrictEqual, new Boolean(true), {}),
269 a.AssertionError);
270
271
Ryan Dahldd356372009-12-29 18:37:40272// Testing the throwing
Oleg Efimov0665f022010-12-05 19:15:30273function thrower(errorConstructor) {
Ryan Dahldd356372009-12-29 18:37:40274 throw new errorConstructor('test');
Karl Guertin4f679fd2009-11-30 02:20:37275}
Ryan Dahla0159b42010-12-04 23:20:34276var aethrow = makeBlock(thrower, a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37277aethrow = makeBlock(thrower, a.AssertionError);
Ryan Dahldd356372009-12-29 18:37:40278
279// the basic calls work
Oleg Efimov0665f022010-12-05 19:15:30280assert.throws(makeBlock(thrower, a.AssertionError),
281 a.AssertionError, 'message');
Ryan Dahldd356372009-12-29 18:37:40282assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError);
283assert.throws(makeBlock(thrower, a.AssertionError));
284
285// if not passing an error, catch all.
286assert.throws(makeBlock(thrower, TypeError));
287
288// when passing a type, only catch errors of the appropriate type
Karl Guertin4f679fd2009-11-30 02:20:37289var threw = false;
Ryan Dahldd356372009-12-29 18:37:40290try {
291 a.throws(makeBlock(thrower, TypeError), a.AssertionError);
292} catch (e) {
293 threw = true;
294 assert.ok(e instanceof TypeError, 'type');
Karl Guertin4f679fd2009-11-30 02:20:37295}
Oleg Efimov0665f022010-12-05 19:15:30296assert.equal(true, threw,
297 'a.throws with an explicit error is eating extra errors',
298 a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37299threw = false;
Ryan Dahldd356372009-12-29 18:37:40300
301// doesNotThrow should pass through all errors
302try {
303 a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError);
Oleg Efimov0665f022010-12-05 19:15:30304} catch (e) {
305 threw = true;
Ryan Dahldd356372009-12-29 18:37:40306 assert.ok(e instanceof TypeError);
Karl Guertin4f679fd2009-11-30 02:20:37307}
Oleg Efimov0665f022010-12-05 19:15:30308assert.equal(true, threw,
309 'a.doesNotThrow with an explicit error is eating extra errors');
Ryan Dahldd356372009-12-29 18:37:40310
311// key difference is that throwing our correct error makes an assertion error
312try {
313 a.doesNotThrow(makeBlock(thrower, TypeError), TypeError);
Oleg Efimov0665f022010-12-05 19:15:30314} catch (e) {
315 threw = true;
Ryan Dahldd356372009-12-29 18:37:40316 assert.ok(e instanceof a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37317}
Oleg Efimov0665f022010-12-05 19:15:30318assert.equal(true, threw,
319 'a.doesNotThrow is not catching type matching errors');
Ryan Dahldd356372009-12-29 18:37:40320
Roman Reissf29762f2015-05-19 11:00:06321assert.throws(function() {assert.ifError(new Error('test error'));});
322assert.doesNotThrow(function() {assert.ifError(null);});
323assert.doesNotThrow(function() {assert.ifError();});
Oleg Slobodskoi02083412010-11-26 23:03:31324
Oleg Slobodskoi23cf9382010-12-21 17:42:52325// make sure that validating using constructor really works
326threw = false;
327try {
328 assert.throws(
Colton Baker87286cc2011-10-04 22:08:18329 function() {
isaacs0cdf85e2012-02-18 23:01:35330 throw ({});
Colton Baker87286cc2011-10-04 22:08:18331 },
332 Array
Oleg Slobodskoi23cf9382010-12-21 17:42:52333 );
Colton Baker87286cc2011-10-04 22:08:18334} catch (e) {
Oleg Slobodskoi23cf9382010-12-21 17:42:52335 threw = true;
336}
Colton Baker87286cc2011-10-04 22:08:18337assert.ok(threw, 'wrong constructor validation');
Oleg Slobodskoi23cf9382010-12-21 17:42:52338
Oleg Slobodskoi02083412010-11-26 23:03:31339// use a RegExp to validate error message
Oleg Efimov0665f022010-12-05 19:15:30340a.throws(makeBlock(thrower, TypeError), /test/);
Oleg Slobodskoi02083412010-11-26 23:03:31341
342// use a fn to validate error object
343a.throws(makeBlock(thrower, TypeError), function(err) {
Colton Baker87286cc2011-10-04 22:08:18344 if ((err instanceof TypeError) && /test/.test(err)) {
Oleg Slobodskoi23cf9382010-12-21 17:42:52345 return true;
Oleg Efimov0665f022010-12-05 19:15:30346 }
Oleg Slobodskoi02083412010-11-26 23:03:31347});
Ryan Dahl6394ba22011-03-30 17:18:12348
349
350// GH-207. Make sure deepEqual doesn't loop forever on circular refs
351
352var b = {};
353b.b = b;
354
355var c = {};
356c.b = c;
357
358var gotError = false;
359try {
360 assert.deepEqual(b, c);
Colton Baker87286cc2011-10-04 22:08:18361} catch (e) {
Ryan Dahl6394ba22011-03-30 17:18:12362 gotError = true;
363}
364
Mike Pennisiaae51ec2014-02-24 19:16:40365// GH-7178. Ensure reflexivity of deepEqual with `arguments` objects.
366var args = (function() { return arguments; })();
367a.throws(makeBlock(a.deepEqual, [], args));
368a.throws(makeBlock(a.deepEqual, args, []));
Ryan Dahl6394ba22011-03-30 17:18:12369assert.ok(gotError);
370
koichik5f97c9a2011-07-10 09:47:41371
cjihrig40e29dc2015-01-30 15:25:32372var circular = {y: 1};
373circular.x = circular;
374
koichik5f97c9a2011-07-10 09:47:41375function testAssertionMessage(actual, expected) {
376 try {
377 assert.equal(actual, '');
378 } catch (e) {
379 assert.equal(e.toString(),
cjihrig40e29dc2015-01-30 15:25:32380 ['AssertionError:', expected, '==', '\'\''].join(' '));
Roman Reissf29762f2015-05-19 11:00:06381 assert.ok(e.generatedMessage, 'Message not marked as generated');
koichik5f97c9a2011-07-10 09:47:41382 }
383}
cjihrig40e29dc2015-01-30 15:25:32384
385testAssertionMessage(undefined, 'undefined');
koichik5f97c9a2011-07-10 09:47:41386testAssertionMessage(null, 'null');
387testAssertionMessage(true, 'true');
388testAssertionMessage(false, 'false');
389testAssertionMessage(0, '0');
390testAssertionMessage(100, '100');
cjihrig40e29dc2015-01-30 15:25:32391testAssertionMessage(NaN, 'NaN');
392testAssertionMessage(Infinity, 'Infinity');
393testAssertionMessage(-Infinity, '-Infinity');
koichik5f97c9a2011-07-10 09:47:41394testAssertionMessage('', '""');
cjihrig40e29dc2015-01-30 15:25:32395testAssertionMessage('foo', '\'foo\'');
koichik5f97c9a2011-07-10 09:47:41396testAssertionMessage([], '[]');
cjihrig40e29dc2015-01-30 15:25:32397testAssertionMessage([1, 2, 3], '[ 1, 2, 3 ]');
398testAssertionMessage(/a/, '/a/');
399testAssertionMessage(/abc/gim, '/abc/gim');
400testAssertionMessage(function f() {}, '[Function: f]');
Roman Reissf29762f2015-05-19 11:00:06401testAssertionMessage(function() {}, '[Function]');
koichik5f97c9a2011-07-10 09:47:41402testAssertionMessage({}, '{}');
cjihrig40e29dc2015-01-30 15:25:32403testAssertionMessage(circular, '{ y: 1, x: [Circular] }');
404testAssertionMessage({a: undefined, b: null}, '{ a: undefined, b: null }');
Colton Baker87286cc2011-10-04 22:08:18405testAssertionMessage({a: NaN, b: Infinity, c: -Infinity},
cjihrig40e29dc2015-01-30 15:25:32406 '{ a: NaN, b: Infinity, c: -Infinity }');
koichik5f97c9a2011-07-10 09:47:41407
koichik72bc4dc2012-03-24 07:00:14408// #2893
409try {
Roman Reissf29762f2015-05-19 11:00:06410 assert.throws(function() {
koichik72bc4dc2012-03-24 07:00:14411 assert.ifError(null);
412 });
413} catch (e) {
414 threw = true;
415 assert.equal(e.message, 'Missing expected exception..');
416}
417assert.ok(threw);
Ryan Doenges6101eb12013-04-14 01:48:00418
419// #5292
420try {
421 assert.equal(1, 2);
422} catch (e) {
Roman Reissf29762f2015-05-19 11:00:06423 assert.equal(e.toString().split('\n')[0], 'AssertionError: 1 == 2');
Glen Mailer66b8c3c2013-09-11 16:18:25424 assert.ok(e.generatedMessage, 'Message not marked as generated');
Ryan Doenges6101eb12013-04-14 01:48:00425}
426
427try {
428 assert.equal(1, 2, 'oh no');
429} catch (e) {
Roman Reissf29762f2015-05-19 11:00:06430 assert.equal(e.toString().split('\n')[0], 'AssertionError: oh no');
Glen Mailer66b8c3c2013-09-11 16:18:25431 assert.equal(e.generatedMessage, false,
432 'Message incorrectly marked as generated');
Ryan Doenges6101eb12013-04-14 01:48:00433}
cjihrig14dc9172015-01-12 16:13:18434
435// Verify that throws() and doesNotThrow() throw on non-function block
436function testBlockTypeError(method, block) {
437 var threw = true;
438
439 try {
440 method(block);
441 threw = false;
442 } catch (e) {
443 assert.equal(e.toString(), 'TypeError: block must be a function');
444 }
445
446 assert.ok(threw);
447}
448
449testBlockTypeError(assert.throws, 'string');
450testBlockTypeError(assert.doesNotThrow, 'string');
451testBlockTypeError(assert.throws, 1);
452testBlockTypeError(assert.doesNotThrow, 1);
453testBlockTypeError(assert.throws, true);
454testBlockTypeError(assert.doesNotThrow, true);
455testBlockTypeError(assert.throws, false);
456testBlockTypeError(assert.doesNotThrow, false);
457testBlockTypeError(assert.throws, []);
458testBlockTypeError(assert.doesNotThrow, []);
459testBlockTypeError(assert.throws, {});
460testBlockTypeError(assert.doesNotThrow, {});
461testBlockTypeError(assert.throws, /foo/);
462testBlockTypeError(assert.doesNotThrow, /foo/);
463testBlockTypeError(assert.throws, null);
464testBlockTypeError(assert.doesNotThrow, null);
465testBlockTypeError(assert.throws, undefined);
466testBlockTypeError(assert.doesNotThrow, undefined);
467
468console.log('All OK');