blob: 13dffe2fe011e92ec6eeff1aa9641d56065cf5d0 [file] [log] [blame]
Ryan Dahl55048cd2011-03-10 08:54:521// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
Oleg Efimov0665f022010-12-05 19:15:3022var common = require('../common');
Ryan Dahla0159b42010-12-04 23:20:3423var assert = require('assert');
Karl Guertin4f679fd2009-11-30 02:20:3724var a = require('assert');
25
Oleg Efimov0665f022010-12-05 19:15:3026function makeBlock(f) {
27 var args = Array.prototype.slice.call(arguments, 1);
28 return function() {
29 return f.apply(this, args);
Ryan Dahldd356372009-12-29 18:37:4030 };
Karl Guertin4f679fd2009-11-30 02:20:3731}
32
Herbert Vojčíkcf2b2062010-08-17 15:21:4333assert.ok(common.indirectInstanceOf(a.AssertionError.prototype, Error),
Oleg Efimov0665f022010-12-05 19:15:3034 'a.AssertionError instanceof Error');
Karl Guertin4f679fd2009-11-30 02:20:3735
Maciej Małecki365fdbf2011-10-01 11:52:2436assert.throws(makeBlock(a, false), a.AssertionError, 'ok(false)');
37
38assert.doesNotThrow(makeBlock(a, true), a.AssertionError, 'ok(true)');
39
40assert.doesNotThrow(makeBlock(a, 'test', 'ok(\'test\')'));
41
Ryan Dahldd356372009-12-29 18:37:4042assert.throws(makeBlock(a.ok, false),
Oleg Efimov0665f022010-12-05 19:15:3043 a.AssertionError, 'ok(false)');
Ryan Dahldd356372009-12-29 18:37:4044
45assert.doesNotThrow(makeBlock(a.ok, true),
Oleg Efimov0665f022010-12-05 19:15:3046 a.AssertionError, 'ok(true)');
Ryan Dahldd356372009-12-29 18:37:4047
Oleg Efimov0665f022010-12-05 19:15:3048assert.doesNotThrow(makeBlock(a.ok, 'test'), 'ok(\'test\')');
Karl Guertin4f679fd2009-11-30 02:20:3749
50assert.throws(makeBlock(a.equal, true, false), a.AssertionError, 'equal');
Ryan Dahldd356372009-12-29 18:37:4051
Karl Guertin4f679fd2009-11-30 02:20:3752assert.doesNotThrow(makeBlock(a.equal, null, null), 'equal');
Ryan Dahldd356372009-12-29 18:37:4053
Karl Guertin4f679fd2009-11-30 02:20:3754assert.doesNotThrow(makeBlock(a.equal, undefined, undefined), 'equal');
Ryan Dahldd356372009-12-29 18:37:4055
Karl Guertin4f679fd2009-11-30 02:20:3756assert.doesNotThrow(makeBlock(a.equal, null, undefined), 'equal');
Ryan Dahldd356372009-12-29 18:37:4057
Karl Guertin4f679fd2009-11-30 02:20:3758assert.doesNotThrow(makeBlock(a.equal, true, true), 'equal');
Ryan Dahldd356372009-12-29 18:37:4059
Oleg Efimov0665f022010-12-05 19:15:3060assert.doesNotThrow(makeBlock(a.equal, 2, '2'), 'equal');
Karl Guertin4f679fd2009-11-30 02:20:3761
62assert.doesNotThrow(makeBlock(a.notEqual, true, false), 'notEqual');
Ryan Dahldd356372009-12-29 18:37:4063
Oleg Efimov0665f022010-12-05 19:15:3064assert.throws(makeBlock(a.notEqual, true, true),
65 a.AssertionError, 'notEqual');
Karl Guertin4f679fd2009-11-30 02:20:3766
Oleg Efimov0665f022010-12-05 19:15:3067assert.throws(makeBlock(a.strictEqual, 2, '2'),
68 a.AssertionError, 'strictEqual');
Ryan Dahldd356372009-12-29 18:37:4069
Oleg Efimov0665f022010-12-05 19:15:3070assert.throws(makeBlock(a.strictEqual, null, undefined),
71 a.AssertionError, 'strictEqual');
Karl Guertin4f679fd2009-11-30 02:20:3772
Oleg Efimov0665f022010-12-05 19:15:3073assert.doesNotThrow(makeBlock(a.notStrictEqual, 2, '2'), 'notStrictEqual');
Karl Guertin4f679fd2009-11-30 02:20:3774
Ryan Dahldd356372009-12-29 18:37:4075// deepEquals joy!
76// 7.2
Oleg Efimov0665f022010-12-05 19:15:3077assert.doesNotThrow(makeBlock(a.deepEqual, new Date(2000, 3, 14),
78 new Date(2000, 3, 14)), 'deepEqual date');
Ryan Dahldd356372009-12-29 18:37:4079
Oleg Efimov0665f022010-12-05 19:15:3080assert.throws(makeBlock(a.deepEqual, new Date(), new Date(2000, 3, 14)),
Ryan Dahldd356372009-12-29 18:37:4081 a.AssertionError,
82 'deepEqual date');
83
84// 7.3
Pedro Teixeiraa8050122011-02-02 11:09:0285assert.doesNotThrow(makeBlock(a.deepEqual, /a/, /a/));
Maciej Małecki5c7532e2011-12-19 22:28:4286assert.doesNotThrow(makeBlock(a.deepEqual, /a/g, /a/g));
87assert.doesNotThrow(makeBlock(a.deepEqual, /a/i, /a/i));
88assert.doesNotThrow(makeBlock(a.deepEqual, /a/m, /a/m));
89assert.doesNotThrow(makeBlock(a.deepEqual, /a/igm, /a/igm));
Pedro Teixeiraa8050122011-02-02 11:09:0290assert.throws(makeBlock(a.deepEqual, /ab/, /a/));
Maciej Małecki5c7532e2011-12-19 22:28:4291assert.throws(makeBlock(a.deepEqual, /a/g, /a/));
92assert.throws(makeBlock(a.deepEqual, /a/i, /a/));
93assert.throws(makeBlock(a.deepEqual, /a/m, /a/));
94assert.throws(makeBlock(a.deepEqual, /a/igm, /a/im));
95
96var re1 = /a/;
97re1.lastIndex = 3;
98assert.throws(makeBlock(a.deepEqual, re1, /a/));
Pedro Teixeiraa8050122011-02-02 11:09:0299
100
101// 7.4
Oleg Efimov0665f022010-12-05 19:15:30102assert.doesNotThrow(makeBlock(a.deepEqual, 4, '4'), 'deepEqual == check');
Karl Guertin4f679fd2009-11-30 02:20:37103assert.doesNotThrow(makeBlock(a.deepEqual, true, 1), 'deepEqual == check');
Oleg Efimov0665f022010-12-05 19:15:30104assert.throws(makeBlock(a.deepEqual, 4, '5'),
Ryan Dahldd356372009-12-29 18:37:40105 a.AssertionError,
106 'deepEqual == check');
107
Pedro Teixeiraa8050122011-02-02 11:09:02108// 7.5
Karl Guertin4f679fd2009-11-30 02:20:37109// having the same number of owned properties && the same set of keys
Oleg Efimov0665f022010-12-05 19:15:30110assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4}, {a: 4}));
111assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'}));
112assert.doesNotThrow(makeBlock(a.deepEqual, [4], ['4']));
113assert.throws(makeBlock(a.deepEqual, {a: 4}, {a: 4, b: true}),
114 a.AssertionError);
115assert.doesNotThrow(makeBlock(a.deepEqual, ['a'], {0: 'a'}));
Karl Guertin4f679fd2009-11-30 02:20:37116//(although not necessarily the same order),
Oleg Efimov0665f022010-12-05 19:15:30117assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '1'}, {b: '1', a: 4}));
118var a1 = [1, 2, 3];
119var a2 = [1, 2, 3];
120a1.a = 'test';
Karl Guertin4f679fd2009-11-30 02:20:37121a1.b = true;
122a2.b = true;
Oleg Efimov0665f022010-12-05 19:15:30123a2.a = 'test';
124assert.throws(makeBlock(a.deepEqual, Object.keys(a1), Object.keys(a2)),
Ryan Dahldd356372009-12-29 18:37:40125 a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37126assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2));
127
128// having an identical prototype property
129var nbRoot = {
Oleg Efimov0665f022010-12-05 19:15:30130 toString: function() { return this.first + ' ' + this.last; }
Ryan Dahldd356372009-12-29 18:37:40131};
132
Oleg Efimov0665f022010-12-05 19:15:30133function nameBuilder(first, last) {
Ryan Dahldd356372009-12-29 18:37:40134 this.first = first;
135 this.last = last;
136 return this;
Karl Guertin4f679fd2009-11-30 02:20:37137}
138nameBuilder.prototype = nbRoot;
Ryan Dahldd356372009-12-29 18:37:40139
Oleg Efimov0665f022010-12-05 19:15:30140function nameBuilder2(first, last) {
Ryan Dahldd356372009-12-29 18:37:40141 this.first = first;
142 this.last = last;
143 return this;
Karl Guertin4f679fd2009-11-30 02:20:37144}
145nameBuilder2.prototype = nbRoot;
Ryan Dahldd356372009-12-29 18:37:40146
Karl Guertin4f679fd2009-11-30 02:20:37147var nb1 = new nameBuilder('Ryan', 'Dahl');
Oleg Efimov0665f022010-12-05 19:15:30148var nb2 = new nameBuilder2('Ryan', 'Dahl');
Karl Guertin4f679fd2009-11-30 02:20:37149
150assert.doesNotThrow(makeBlock(a.deepEqual, nb1, nb2));
151
152nameBuilder2.prototype = Object;
Oleg Efimov0665f022010-12-05 19:15:30153nb2 = new nameBuilder2('Ryan', 'Dahl');
Ryan Dahldd356372009-12-29 18:37:40154assert.throws(makeBlock(a.deepEqual, nb1, nb2), a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37155
teppeis00a74562014-12-21 15:56:33156// primitives and object
157assert.throws(makeBlock(a.deepEqual, null, {}), a.AssertionError);
158assert.throws(makeBlock(a.deepEqual, undefined, {}), a.AssertionError);
159assert.throws(makeBlock(a.deepEqual, 'a', ['a']), a.AssertionError);
160assert.throws(makeBlock(a.deepEqual, 'a', {0: 'a'}), a.AssertionError);
161assert.throws(makeBlock(a.deepEqual, 1, {}), a.AssertionError);
162assert.throws(makeBlock(a.deepEqual, true, {}), a.AssertionError);
163if (typeof Symbol === 'symbol') {
164 assert.throws(makeBlock(assert.deepEqual, Symbol(), {}), a.AssertionError);
165}
166
167// primitive wrappers and object
168assert.doesNotThrow(makeBlock(a.deepEqual, new String('a'), ['a']), a.AssertionError);
169assert.doesNotThrow(makeBlock(a.deepEqual, new String('a'), {0: 'a'}), a.AssertionError);
170assert.doesNotThrow(makeBlock(a.deepEqual, new Number(1), {}), a.AssertionError);
171assert.doesNotThrow(makeBlock(a.deepEqual, new Boolean(true), {}), a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37172
Ryan Dahldd356372009-12-29 18:37:40173// Testing the throwing
Oleg Efimov0665f022010-12-05 19:15:30174function thrower(errorConstructor) {
Ryan Dahldd356372009-12-29 18:37:40175 throw new errorConstructor('test');
Karl Guertin4f679fd2009-11-30 02:20:37176}
Ryan Dahla0159b42010-12-04 23:20:34177var aethrow = makeBlock(thrower, a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37178aethrow = makeBlock(thrower, a.AssertionError);
Ryan Dahldd356372009-12-29 18:37:40179
180// the basic calls work
Oleg Efimov0665f022010-12-05 19:15:30181assert.throws(makeBlock(thrower, a.AssertionError),
182 a.AssertionError, 'message');
Ryan Dahldd356372009-12-29 18:37:40183assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError);
184assert.throws(makeBlock(thrower, a.AssertionError));
185
186// if not passing an error, catch all.
187assert.throws(makeBlock(thrower, TypeError));
188
189// when passing a type, only catch errors of the appropriate type
Karl Guertin4f679fd2009-11-30 02:20:37190var threw = false;
Ryan Dahldd356372009-12-29 18:37:40191try {
192 a.throws(makeBlock(thrower, TypeError), a.AssertionError);
193} catch (e) {
194 threw = true;
195 assert.ok(e instanceof TypeError, 'type');
Karl Guertin4f679fd2009-11-30 02:20:37196}
Oleg Efimov0665f022010-12-05 19:15:30197assert.equal(true, threw,
198 'a.throws with an explicit error is eating extra errors',
199 a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37200threw = false;
Ryan Dahldd356372009-12-29 18:37:40201
202// doesNotThrow should pass through all errors
203try {
204 a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError);
Oleg Efimov0665f022010-12-05 19:15:30205} catch (e) {
206 threw = true;
Ryan Dahldd356372009-12-29 18:37:40207 assert.ok(e instanceof TypeError);
Karl Guertin4f679fd2009-11-30 02:20:37208}
Oleg Efimov0665f022010-12-05 19:15:30209assert.equal(true, threw,
210 'a.doesNotThrow with an explicit error is eating extra errors');
Ryan Dahldd356372009-12-29 18:37:40211
212// key difference is that throwing our correct error makes an assertion error
213try {
214 a.doesNotThrow(makeBlock(thrower, TypeError), TypeError);
Oleg Efimov0665f022010-12-05 19:15:30215} catch (e) {
216 threw = true;
Ryan Dahldd356372009-12-29 18:37:40217 assert.ok(e instanceof a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37218}
Oleg Efimov0665f022010-12-05 19:15:30219assert.equal(true, threw,
220 'a.doesNotThrow is not catching type matching errors');
Ryan Dahldd356372009-12-29 18:37:40221
Oleg Efimov0665f022010-12-05 19:15:30222assert.throws(function() {assert.ifError(new Error('test error'))});
223assert.doesNotThrow(function() {assert.ifError(null)});
224assert.doesNotThrow(function() {assert.ifError()});
Oleg Slobodskoi02083412010-11-26 23:03:31225
Oleg Slobodskoi23cf9382010-12-21 17:42:52226// make sure that validating using constructor really works
227threw = false;
228try {
229 assert.throws(
Colton Baker87286cc2011-10-04 22:08:18230 function() {
isaacs0cdf85e2012-02-18 23:01:35231 throw ({});
Colton Baker87286cc2011-10-04 22:08:18232 },
233 Array
Oleg Slobodskoi23cf9382010-12-21 17:42:52234 );
Colton Baker87286cc2011-10-04 22:08:18235} catch (e) {
Oleg Slobodskoi23cf9382010-12-21 17:42:52236 threw = true;
237}
Colton Baker87286cc2011-10-04 22:08:18238assert.ok(threw, 'wrong constructor validation');
Oleg Slobodskoi23cf9382010-12-21 17:42:52239
Oleg Slobodskoi02083412010-11-26 23:03:31240// use a RegExp to validate error message
Oleg Efimov0665f022010-12-05 19:15:30241a.throws(makeBlock(thrower, TypeError), /test/);
Oleg Slobodskoi02083412010-11-26 23:03:31242
243// use a fn to validate error object
244a.throws(makeBlock(thrower, TypeError), function(err) {
Colton Baker87286cc2011-10-04 22:08:18245 if ((err instanceof TypeError) && /test/.test(err)) {
Oleg Slobodskoi23cf9382010-12-21 17:42:52246 return true;
Oleg Efimov0665f022010-12-05 19:15:30247 }
Oleg Slobodskoi02083412010-11-26 23:03:31248});
Ryan Dahl6394ba22011-03-30 17:18:12249
250
251// GH-207. Make sure deepEqual doesn't loop forever on circular refs
252
253var b = {};
254b.b = b;
255
256var c = {};
257c.b = c;
258
259var gotError = false;
260try {
261 assert.deepEqual(b, c);
Colton Baker87286cc2011-10-04 22:08:18262} catch (e) {
Ryan Dahl6394ba22011-03-30 17:18:12263 gotError = true;
264}
265
Mike Pennisiaae51ec2014-02-24 19:16:40266// GH-7178. Ensure reflexivity of deepEqual with `arguments` objects.
267var args = (function() { return arguments; })();
268a.throws(makeBlock(a.deepEqual, [], args));
269a.throws(makeBlock(a.deepEqual, args, []));
270
Ryan Dahl6394ba22011-03-30 17:18:12271console.log('All OK');
272assert.ok(gotError);
273
koichik5f97c9a2011-07-10 09:47:41274
275// #217
276function testAssertionMessage(actual, expected) {
277 try {
278 assert.equal(actual, '');
279 } catch (e) {
280 assert.equal(e.toString(),
Ryan Dahl0696e782011-08-09 21:18:16281 ['AssertionError:', expected, '==', '""'].join(' '));
Glen Mailer66b8c3c2013-09-11 16:18:25282 assert.ok(e.generatedMessage, "Message not marked as generated");
koichik5f97c9a2011-07-10 09:47:41283 }
284}
285testAssertionMessage(undefined, '"undefined"');
286testAssertionMessage(null, 'null');
287testAssertionMessage(true, 'true');
288testAssertionMessage(false, 'false');
289testAssertionMessage(0, '0');
290testAssertionMessage(100, '100');
291testAssertionMessage(NaN, '"NaN"');
292testAssertionMessage(Infinity, '"Infinity"');
293testAssertionMessage(-Infinity, '"-Infinity"');
294testAssertionMessage('', '""');
295testAssertionMessage('foo', '"foo"');
296testAssertionMessage([], '[]');
Colton Baker87286cc2011-10-04 22:08:18297testAssertionMessage([1, 2, 3], '[1,2,3]');
koichik5f97c9a2011-07-10 09:47:41298testAssertionMessage(/a/, '"/a/"');
299testAssertionMessage(/abc/gim, '"/abc/gim"');
300testAssertionMessage(function f() {}, '"function f() {}"');
301testAssertionMessage({}, '{}');
Colton Baker87286cc2011-10-04 22:08:18302testAssertionMessage({a: undefined, b: null}, '{"a":"undefined","b":null}');
303testAssertionMessage({a: NaN, b: Infinity, c: -Infinity},
koichik5f97c9a2011-07-10 09:47:41304 '{"a":"NaN","b":"Infinity","c":"-Infinity"}');
305
koichik72bc4dc2012-03-24 07:00:14306// #2893
307try {
308 assert.throws(function () {
309 assert.ifError(null);
310 });
311} catch (e) {
312 threw = true;
313 assert.equal(e.message, 'Missing expected exception..');
314}
315assert.ok(threw);
Ryan Doenges6101eb12013-04-14 01:48:00316
317// #5292
318try {
319 assert.equal(1, 2);
320} catch (e) {
321 assert.equal(e.toString().split('\n')[0], 'AssertionError: 1 == 2')
Glen Mailer66b8c3c2013-09-11 16:18:25322 assert.ok(e.generatedMessage, 'Message not marked as generated');
Ryan Doenges6101eb12013-04-14 01:48:00323}
324
325try {
326 assert.equal(1, 2, 'oh no');
327} catch (e) {
328 assert.equal(e.toString().split('\n')[0], 'AssertionError: oh no')
Glen Mailer66b8c3c2013-09-11 16:18:25329 assert.equal(e.generatedMessage, false,
330 'Message incorrectly marked as generated');
Ryan Doenges6101eb12013-04-14 01:48:00331}