blob: 6a64cbd5da50833ae6d8f188e06311397e541c6e [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/));
86assert.throws(makeBlock(a.deepEqual, /ab/, /a/));
87
88
89// 7.4
Oleg Efimov0665f022010-12-05 19:15:3090assert.doesNotThrow(makeBlock(a.deepEqual, 4, '4'), 'deepEqual == check');
Karl Guertin4f679fd2009-11-30 02:20:3791assert.doesNotThrow(makeBlock(a.deepEqual, true, 1), 'deepEqual == check');
Oleg Efimov0665f022010-12-05 19:15:3092assert.throws(makeBlock(a.deepEqual, 4, '5'),
Ryan Dahldd356372009-12-29 18:37:4093 a.AssertionError,
94 'deepEqual == check');
95
Pedro Teixeiraa8050122011-02-02 11:09:0296// 7.5
Karl Guertin4f679fd2009-11-30 02:20:3797// having the same number of owned properties && the same set of keys
Oleg Efimov0665f022010-12-05 19:15:3098assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4}, {a: 4}));
99assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'}));
100assert.doesNotThrow(makeBlock(a.deepEqual, [4], ['4']));
101assert.throws(makeBlock(a.deepEqual, {a: 4}, {a: 4, b: true}),
102 a.AssertionError);
103assert.doesNotThrow(makeBlock(a.deepEqual, ['a'], {0: 'a'}));
Karl Guertin4f679fd2009-11-30 02:20:37104//(although not necessarily the same order),
Oleg Efimov0665f022010-12-05 19:15:30105assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '1'}, {b: '1', a: 4}));
106var a1 = [1, 2, 3];
107var a2 = [1, 2, 3];
108a1.a = 'test';
Karl Guertin4f679fd2009-11-30 02:20:37109a1.b = true;
110a2.b = true;
Oleg Efimov0665f022010-12-05 19:15:30111a2.a = 'test';
112assert.throws(makeBlock(a.deepEqual, Object.keys(a1), Object.keys(a2)),
Ryan Dahldd356372009-12-29 18:37:40113 a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37114assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2));
115
116// having an identical prototype property
117var nbRoot = {
Oleg Efimov0665f022010-12-05 19:15:30118 toString: function() { return this.first + ' ' + this.last; }
Ryan Dahldd356372009-12-29 18:37:40119};
120
Oleg Efimov0665f022010-12-05 19:15:30121function nameBuilder(first, last) {
Ryan Dahldd356372009-12-29 18:37:40122 this.first = first;
123 this.last = last;
124 return this;
Karl Guertin4f679fd2009-11-30 02:20:37125}
126nameBuilder.prototype = nbRoot;
Ryan Dahldd356372009-12-29 18:37:40127
Oleg Efimov0665f022010-12-05 19:15:30128function nameBuilder2(first, last) {
Ryan Dahldd356372009-12-29 18:37:40129 this.first = first;
130 this.last = last;
131 return this;
Karl Guertin4f679fd2009-11-30 02:20:37132}
133nameBuilder2.prototype = nbRoot;
Ryan Dahldd356372009-12-29 18:37:40134
Karl Guertin4f679fd2009-11-30 02:20:37135var nb1 = new nameBuilder('Ryan', 'Dahl');
Oleg Efimov0665f022010-12-05 19:15:30136var nb2 = new nameBuilder2('Ryan', 'Dahl');
Karl Guertin4f679fd2009-11-30 02:20:37137
138assert.doesNotThrow(makeBlock(a.deepEqual, nb1, nb2));
139
140nameBuilder2.prototype = Object;
Oleg Efimov0665f022010-12-05 19:15:30141nb2 = new nameBuilder2('Ryan', 'Dahl');
Ryan Dahldd356372009-12-29 18:37:40142assert.throws(makeBlock(a.deepEqual, nb1, nb2), a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37143
Ryan Dahldd356372009-12-29 18:37:40144// String literal + object blew up my implementation...
145assert.throws(makeBlock(a.deepEqual, 'a', {}), a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37146
Ryan Dahldd356372009-12-29 18:37:40147// Testing the throwing
Oleg Efimov0665f022010-12-05 19:15:30148function thrower(errorConstructor) {
Ryan Dahldd356372009-12-29 18:37:40149 throw new errorConstructor('test');
Karl Guertin4f679fd2009-11-30 02:20:37150}
Ryan Dahla0159b42010-12-04 23:20:34151var aethrow = makeBlock(thrower, a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37152aethrow = makeBlock(thrower, a.AssertionError);
Ryan Dahldd356372009-12-29 18:37:40153
154// the basic calls work
Oleg Efimov0665f022010-12-05 19:15:30155assert.throws(makeBlock(thrower, a.AssertionError),
156 a.AssertionError, 'message');
Ryan Dahldd356372009-12-29 18:37:40157assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError);
158assert.throws(makeBlock(thrower, a.AssertionError));
159
160// if not passing an error, catch all.
161assert.throws(makeBlock(thrower, TypeError));
162
163// when passing a type, only catch errors of the appropriate type
Karl Guertin4f679fd2009-11-30 02:20:37164var threw = false;
Ryan Dahldd356372009-12-29 18:37:40165try {
166 a.throws(makeBlock(thrower, TypeError), a.AssertionError);
167} catch (e) {
168 threw = true;
169 assert.ok(e instanceof TypeError, 'type');
Karl Guertin4f679fd2009-11-30 02:20:37170}
Oleg Efimov0665f022010-12-05 19:15:30171assert.equal(true, threw,
172 'a.throws with an explicit error is eating extra errors',
173 a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37174threw = false;
Ryan Dahldd356372009-12-29 18:37:40175
176// doesNotThrow should pass through all errors
177try {
178 a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError);
Oleg Efimov0665f022010-12-05 19:15:30179} catch (e) {
180 threw = true;
Ryan Dahldd356372009-12-29 18:37:40181 assert.ok(e instanceof TypeError);
Karl Guertin4f679fd2009-11-30 02:20:37182}
Oleg Efimov0665f022010-12-05 19:15:30183assert.equal(true, threw,
184 'a.doesNotThrow with an explicit error is eating extra errors');
Ryan Dahldd356372009-12-29 18:37:40185
186// key difference is that throwing our correct error makes an assertion error
187try {
188 a.doesNotThrow(makeBlock(thrower, TypeError), TypeError);
Oleg Efimov0665f022010-12-05 19:15:30189} catch (e) {
190 threw = true;
Ryan Dahldd356372009-12-29 18:37:40191 assert.ok(e instanceof a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37192}
Oleg Efimov0665f022010-12-05 19:15:30193assert.equal(true, threw,
194 'a.doesNotThrow is not catching type matching errors');
Ryan Dahldd356372009-12-29 18:37:40195
Oleg Efimov0665f022010-12-05 19:15:30196assert.throws(function() {assert.ifError(new Error('test error'))});
197assert.doesNotThrow(function() {assert.ifError(null)});
198assert.doesNotThrow(function() {assert.ifError()});
Oleg Slobodskoi02083412010-11-26 23:03:31199
Oleg Slobodskoi23cf9382010-12-21 17:42:52200// make sure that validating using constructor really works
201threw = false;
202try {
203 assert.throws(
Colton Baker87286cc2011-10-04 22:08:18204 function() {
205 throw {}
206 },
207 Array
Oleg Slobodskoi23cf9382010-12-21 17:42:52208 );
Colton Baker87286cc2011-10-04 22:08:18209} catch (e) {
Oleg Slobodskoi23cf9382010-12-21 17:42:52210 threw = true;
211}
Colton Baker87286cc2011-10-04 22:08:18212assert.ok(threw, 'wrong constructor validation');
Oleg Slobodskoi23cf9382010-12-21 17:42:52213
Oleg Slobodskoi02083412010-11-26 23:03:31214// use a RegExp to validate error message
Oleg Efimov0665f022010-12-05 19:15:30215a.throws(makeBlock(thrower, TypeError), /test/);
Oleg Slobodskoi02083412010-11-26 23:03:31216
217// use a fn to validate error object
218a.throws(makeBlock(thrower, TypeError), function(err) {
Colton Baker87286cc2011-10-04 22:08:18219 if ((err instanceof TypeError) && /test/.test(err)) {
Oleg Slobodskoi23cf9382010-12-21 17:42:52220 return true;
Oleg Efimov0665f022010-12-05 19:15:30221 }
Oleg Slobodskoi02083412010-11-26 23:03:31222});
Ryan Dahl6394ba22011-03-30 17:18:12223
224
225// GH-207. Make sure deepEqual doesn't loop forever on circular refs
226
227var b = {};
228b.b = b;
229
230var c = {};
231c.b = c;
232
233var gotError = false;
234try {
235 assert.deepEqual(b, c);
Colton Baker87286cc2011-10-04 22:08:18236} catch (e) {
Ryan Dahl6394ba22011-03-30 17:18:12237 gotError = true;
238}
239
240console.log('All OK');
241assert.ok(gotError);
242
koichik5f97c9a2011-07-10 09:47:41243
244// #217
245function testAssertionMessage(actual, expected) {
246 try {
247 assert.equal(actual, '');
248 } catch (e) {
249 assert.equal(e.toString(),
Ryan Dahl0696e782011-08-09 21:18:16250 ['AssertionError:', expected, '==', '""'].join(' '));
koichik5f97c9a2011-07-10 09:47:41251 }
252}
253testAssertionMessage(undefined, '"undefined"');
254testAssertionMessage(null, 'null');
255testAssertionMessage(true, 'true');
256testAssertionMessage(false, 'false');
257testAssertionMessage(0, '0');
258testAssertionMessage(100, '100');
259testAssertionMessage(NaN, '"NaN"');
260testAssertionMessage(Infinity, '"Infinity"');
261testAssertionMessage(-Infinity, '"-Infinity"');
262testAssertionMessage('', '""');
263testAssertionMessage('foo', '"foo"');
264testAssertionMessage([], '[]');
Colton Baker87286cc2011-10-04 22:08:18265testAssertionMessage([1, 2, 3], '[1,2,3]');
koichik5f97c9a2011-07-10 09:47:41266testAssertionMessage(/a/, '"/a/"');
267testAssertionMessage(/abc/gim, '"/abc/gim"');
268testAssertionMessage(function f() {}, '"function f() {}"');
269testAssertionMessage({}, '{}');
Colton Baker87286cc2011-10-04 22:08:18270testAssertionMessage({a: undefined, b: null}, '{"a":"undefined","b":null}');
271testAssertionMessage({a: NaN, b: Infinity, c: -Infinity},
koichik5f97c9a2011-07-10 09:47:41272 '{"a":"NaN","b":"Infinity","c":"-Infinity"}');
273