blob: 3bd171597094353b1891247f66733a09cf340fa6 [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
Oleg Efimov0665f022010-12-05 19:15:3085assert.doesNotThrow(makeBlock(a.deepEqual, 4, '4'), 'deepEqual == check');
Karl Guertin4f679fd2009-11-30 02:20:3786assert.doesNotThrow(makeBlock(a.deepEqual, true, 1), 'deepEqual == check');
Oleg Efimov0665f022010-12-05 19:15:3087assert.throws(makeBlock(a.deepEqual, 4, '5'),
Ryan Dahldd356372009-12-29 18:37:4088 a.AssertionError,
89 'deepEqual == check');
90
91// 7.4
Karl Guertin4f679fd2009-11-30 02:20:3792// having the same number of owned properties && the same set of keys
Oleg Efimov0665f022010-12-05 19:15:3093assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4}, {a: 4}));
94assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'}));
95assert.doesNotThrow(makeBlock(a.deepEqual, [4], ['4']));
96assert.throws(makeBlock(a.deepEqual, {a: 4}, {a: 4, b: true}),
97 a.AssertionError);
98assert.doesNotThrow(makeBlock(a.deepEqual, ['a'], {0: 'a'}));
Karl Guertin4f679fd2009-11-30 02:20:3799//(although not necessarily the same order),
Oleg Efimov0665f022010-12-05 19:15:30100assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '1'}, {b: '1', a: 4}));
101var a1 = [1, 2, 3];
102var a2 = [1, 2, 3];
103a1.a = 'test';
Karl Guertin4f679fd2009-11-30 02:20:37104a1.b = true;
105a2.b = true;
Oleg Efimov0665f022010-12-05 19:15:30106a2.a = 'test';
107assert.throws(makeBlock(a.deepEqual, Object.keys(a1), Object.keys(a2)),
Ryan Dahldd356372009-12-29 18:37:40108 a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37109assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2));
110
111// having an identical prototype property
112var nbRoot = {
Oleg Efimov0665f022010-12-05 19:15:30113 toString: function() { return this.first + ' ' + this.last; }
Ryan Dahldd356372009-12-29 18:37:40114};
115
Oleg Efimov0665f022010-12-05 19:15:30116function nameBuilder(first, last) {
Ryan Dahldd356372009-12-29 18:37:40117 this.first = first;
118 this.last = last;
119 return this;
Karl Guertin4f679fd2009-11-30 02:20:37120}
121nameBuilder.prototype = nbRoot;
Ryan Dahldd356372009-12-29 18:37:40122
Oleg Efimov0665f022010-12-05 19:15:30123function nameBuilder2(first, last) {
Ryan Dahldd356372009-12-29 18:37:40124 this.first = first;
125 this.last = last;
126 return this;
Karl Guertin4f679fd2009-11-30 02:20:37127}
128nameBuilder2.prototype = nbRoot;
Ryan Dahldd356372009-12-29 18:37:40129
Karl Guertin4f679fd2009-11-30 02:20:37130var nb1 = new nameBuilder('Ryan', 'Dahl');
Oleg Efimov0665f022010-12-05 19:15:30131var nb2 = new nameBuilder2('Ryan', 'Dahl');
Karl Guertin4f679fd2009-11-30 02:20:37132
133assert.doesNotThrow(makeBlock(a.deepEqual, nb1, nb2));
134
135nameBuilder2.prototype = Object;
Oleg Efimov0665f022010-12-05 19:15:30136nb2 = new nameBuilder2('Ryan', 'Dahl');
Ryan Dahldd356372009-12-29 18:37:40137assert.throws(makeBlock(a.deepEqual, nb1, nb2), a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37138
Ryan Dahldd356372009-12-29 18:37:40139// String literal + object blew up my implementation...
140assert.throws(makeBlock(a.deepEqual, 'a', {}), a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37141
Ryan Dahldd356372009-12-29 18:37:40142// Testing the throwing
Oleg Efimov0665f022010-12-05 19:15:30143function thrower(errorConstructor) {
Ryan Dahldd356372009-12-29 18:37:40144 throw new errorConstructor('test');
Karl Guertin4f679fd2009-11-30 02:20:37145}
Ryan Dahla0159b42010-12-04 23:20:34146var aethrow = makeBlock(thrower, a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37147aethrow = makeBlock(thrower, a.AssertionError);
Ryan Dahldd356372009-12-29 18:37:40148
149// the basic calls work
Oleg Efimov0665f022010-12-05 19:15:30150assert.throws(makeBlock(thrower, a.AssertionError),
151 a.AssertionError, 'message');
Ryan Dahldd356372009-12-29 18:37:40152assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError);
153assert.throws(makeBlock(thrower, a.AssertionError));
154
155// if not passing an error, catch all.
156assert.throws(makeBlock(thrower, TypeError));
157
158// when passing a type, only catch errors of the appropriate type
Karl Guertin4f679fd2009-11-30 02:20:37159var threw = false;
Ryan Dahldd356372009-12-29 18:37:40160try {
161 a.throws(makeBlock(thrower, TypeError), a.AssertionError);
162} catch (e) {
163 threw = true;
164 assert.ok(e instanceof TypeError, 'type');
Karl Guertin4f679fd2009-11-30 02:20:37165}
Oleg Efimov0665f022010-12-05 19:15:30166assert.equal(true, threw,
167 'a.throws with an explicit error is eating extra errors',
168 a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37169threw = false;
Ryan Dahldd356372009-12-29 18:37:40170
171// doesNotThrow should pass through all errors
172try {
173 a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError);
Oleg Efimov0665f022010-12-05 19:15:30174} catch (e) {
175 threw = true;
Ryan Dahldd356372009-12-29 18:37:40176 assert.ok(e instanceof TypeError);
Karl Guertin4f679fd2009-11-30 02:20:37177}
Oleg Efimov0665f022010-12-05 19:15:30178assert.equal(true, threw,
179 'a.doesNotThrow with an explicit error is eating extra errors');
Ryan Dahldd356372009-12-29 18:37:40180
181// key difference is that throwing our correct error makes an assertion error
182try {
183 a.doesNotThrow(makeBlock(thrower, TypeError), TypeError);
Oleg Efimov0665f022010-12-05 19:15:30184} catch (e) {
185 threw = true;
Ryan Dahldd356372009-12-29 18:37:40186 assert.ok(e instanceof a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37187}
Oleg Efimov0665f022010-12-05 19:15:30188assert.equal(true, threw,
189 'a.doesNotThrow is not catching type matching errors');
Ryan Dahldd356372009-12-29 18:37:40190
Oleg Efimov0665f022010-12-05 19:15:30191assert.throws(function() {assert.ifError(new Error('test error'))});
192assert.doesNotThrow(function() {assert.ifError(null)});
193assert.doesNotThrow(function() {assert.ifError()});
Oleg Slobodskoi02083412010-11-26 23:03:31194
Oleg Slobodskoi23cf9382010-12-21 17:42:52195// make sure that validating using constructor really works
196threw = false;
197try {
198 assert.throws(
199 function() {
200 throw {};
201 },
202 Array
203 );
204} catch(e) {
205 threw = true;
206}
207assert.ok(threw, "wrong constructor validation");
208
Oleg Slobodskoi02083412010-11-26 23:03:31209// use a RegExp to validate error message
Oleg Efimov0665f022010-12-05 19:15:30210a.throws(makeBlock(thrower, TypeError), /test/);
Oleg Slobodskoi02083412010-11-26 23:03:31211
212// use a fn to validate error object
213a.throws(makeBlock(thrower, TypeError), function(err) {
Oleg Slobodskoi23cf9382010-12-21 17:42:52214 if ( (err instanceof TypeError) && /test/.test(err)) {
215 return true;
Oleg Efimov0665f022010-12-05 19:15:30216 }
Oleg Slobodskoi02083412010-11-26 23:03:31217});
Ryan Dahl6394ba22011-03-30 17:18:12218
219
220// GH-207. Make sure deepEqual doesn't loop forever on circular refs
221
222var b = {};
223b.b = b;
224
225var c = {};
226c.b = c;
227
228var gotError = false;
229try {
230 assert.deepEqual(b, c);
231} catch(e) {
232 gotError = true;
233}
234
235console.log('All OK');
236assert.ok(gotError);
237
koichik5f97c9a2011-07-10 09:47:41238
239// #217
240function testAssertionMessage(actual, expected) {
241 try {
242 assert.equal(actual, '');
243 } catch (e) {
244 assert.equal(e.toString(),
Ryan Dahl0696e782011-08-09 21:18:16245 ['AssertionError:', expected, '==', '""'].join(' '));
koichik5f97c9a2011-07-10 09:47:41246 }
247}
248testAssertionMessage(undefined, '"undefined"');
249testAssertionMessage(null, 'null');
250testAssertionMessage(true, 'true');
251testAssertionMessage(false, 'false');
252testAssertionMessage(0, '0');
253testAssertionMessage(100, '100');
254testAssertionMessage(NaN, '"NaN"');
255testAssertionMessage(Infinity, '"Infinity"');
256testAssertionMessage(-Infinity, '"-Infinity"');
257testAssertionMessage('', '""');
258testAssertionMessage('foo', '"foo"');
259testAssertionMessage([], '[]');
260testAssertionMessage([1,2,3], '[1,2,3]');
261testAssertionMessage(/a/, '"/a/"');
262testAssertionMessage(/abc/gim, '"/abc/gim"');
263testAssertionMessage(function f() {}, '"function f() {}"');
264testAssertionMessage({}, '{}');
265testAssertionMessage({a:undefined, b:null}, '{"a":"undefined","b":null}');
266testAssertionMessage({a:NaN, b:Infinity, c:-Infinity},
267 '{"a":"NaN","b":"Infinity","c":"-Infinity"}');
268