1 |
3a515b92
|
cagy
|
// 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 |
|
|
|
22 |
|
|
var nodeAssert = require('assert');
|
23 |
|
|
var ourAssert = require('./');
|
24 |
|
|
var keys = Object.keys;
|
25 |
|
|
if (process.env.TEST_NATIVE === true) {
|
26 |
|
|
tests(nodeAssert, 'node assert');
|
27 |
|
|
} else {
|
28 |
|
|
tests(ourAssert, 'our assert');
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
function makeBlock(f) {
|
32 |
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
33 |
|
|
return function() {
|
34 |
|
|
return f.apply(this, args);
|
35 |
|
|
};
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
function tests (assert, what) {
|
39 |
|
|
test('assert.ok', function () {
|
40 |
|
|
assert.throws(makeBlock(assert, false), assert.AssertionError, 'ok(false)');
|
41 |
|
|
|
42 |
|
|
assert.doesNotThrow(makeBlock(assert, true), assert.AssertionError, 'ok(true)');
|
43 |
|
|
|
44 |
|
|
assert.doesNotThrow(makeBlock(assert, 'test', 'ok(\'test\')'));
|
45 |
|
|
|
46 |
|
|
assert.throws(makeBlock(assert.ok, false),
|
47 |
|
|
assert.AssertionError, 'ok(false)');
|
48 |
|
|
|
49 |
|
|
assert.doesNotThrow(makeBlock(assert.ok, true),
|
50 |
|
|
assert.AssertionError, 'ok(true)');
|
51 |
|
|
|
52 |
|
|
assert.doesNotThrow(makeBlock(assert.ok, 'test'), 'ok(\'test\')');
|
53 |
|
|
});
|
54 |
|
|
|
55 |
|
|
test('assert.equal', function () {
|
56 |
|
|
assert.throws(makeBlock(assert.equal, true, false), assert.AssertionError, 'equal');
|
57 |
|
|
|
58 |
|
|
assert.doesNotThrow(makeBlock(assert.equal, null, null), 'equal');
|
59 |
|
|
|
60 |
|
|
assert.doesNotThrow(makeBlock(assert.equal, undefined, undefined), 'equal');
|
61 |
|
|
|
62 |
|
|
assert.doesNotThrow(makeBlock(assert.equal, null, undefined), 'equal');
|
63 |
|
|
|
64 |
|
|
assert.doesNotThrow(makeBlock(assert.equal, true, true), 'equal');
|
65 |
|
|
|
66 |
|
|
assert.doesNotThrow(makeBlock(assert.equal, 2, '2'), 'equal');
|
67 |
|
|
|
68 |
|
|
assert.doesNotThrow(makeBlock(assert.notEqual, true, false), 'notEqual');
|
69 |
|
|
|
70 |
|
|
assert.throws(makeBlock(assert.notEqual, true, true),
|
71 |
|
|
assert.AssertionError, 'notEqual');
|
72 |
|
|
});
|
73 |
|
|
|
74 |
|
|
test('assert.strictEqual', function () {
|
75 |
|
|
assert.throws(makeBlock(assert.strictEqual, 2, '2'),
|
76 |
|
|
assert.AssertionError, 'strictEqual');
|
77 |
|
|
|
78 |
|
|
assert.throws(makeBlock(assert.strictEqual, null, undefined),
|
79 |
|
|
assert.AssertionError, 'strictEqual');
|
80 |
|
|
|
81 |
|
|
assert.doesNotThrow(makeBlock(assert.notStrictEqual, 2, '2'), 'notStrictEqual');
|
82 |
|
|
});
|
83 |
|
|
|
84 |
|
|
test('assert.deepStrictEqual', function () {
|
85 |
|
|
assert.throws(makeBlock(assert.deepStrictEqual, [2], ['2']),
|
86 |
|
|
assert.AssertionError, 'deepStrictEqual');
|
87 |
|
|
|
88 |
|
|
assert.throws(makeBlock(assert.deepStrictEqual, [null], [undefined]),
|
89 |
|
|
assert.AssertionError, 'deepStrictEqual');
|
90 |
|
|
|
91 |
|
|
assert.doesNotThrow(makeBlock(assert.notDeepStrictEqual, [2], ['2']), 'notDeepStrictEqual');
|
92 |
|
|
});
|
93 |
|
|
|
94 |
|
|
test('assert.deepEqual - 7.2', function () {
|
95 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, new Date(2000, 3, 14),
|
96 |
|
|
new Date(2000, 3, 14)), 'deepEqual date');
|
97 |
|
|
|
98 |
|
|
assert.throws(makeBlock(assert.deepEqual, new Date(), new Date(2000, 3, 14)),
|
99 |
|
|
assert.AssertionError,
|
100 |
|
|
'deepEqual date');
|
101 |
|
|
});
|
102 |
|
|
|
103 |
|
|
test('assert.deepEqual - 7.3', function () {
|
104 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/, /a/));
|
105 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/g, /a/g));
|
106 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/i, /a/i));
|
107 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/m, /a/m));
|
108 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/igm, /a/igm));
|
109 |
|
|
assert.throws(makeBlock(assert.deepEqual, /ab/, /a/));
|
110 |
|
|
assert.throws(makeBlock(assert.deepEqual, /a/g, /a/));
|
111 |
|
|
assert.throws(makeBlock(assert.deepEqual, /a/i, /a/));
|
112 |
|
|
assert.throws(makeBlock(assert.deepEqual, /a/m, /a/));
|
113 |
|
|
assert.throws(makeBlock(assert.deepEqual, /a/igm, /a/im));
|
114 |
|
|
|
115 |
|
|
var re1 = /a/;
|
116 |
|
|
re1.lastIndex = 3;
|
117 |
|
|
assert.throws(makeBlock(assert.deepEqual, re1, /a/));
|
118 |
|
|
});
|
119 |
|
|
|
120 |
|
|
test('assert.deepEqual - 7.4', function () {
|
121 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, 4, '4'), 'deepEqual == check');
|
122 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, true, 1), 'deepEqual == check');
|
123 |
|
|
assert.throws(makeBlock(assert.deepEqual, 4, '5'),
|
124 |
|
|
assert.AssertionError,
|
125 |
|
|
'deepEqual == check');
|
126 |
|
|
});
|
127 |
|
|
|
128 |
|
|
test('assert.deepEqual - 7.5', function () {
|
129 |
|
|
// having the same number of owned properties && the same set of keys
|
130 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4}, {a: 4}));
|
131 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'}));
|
132 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, [4], ['4']));
|
133 |
|
|
assert.throws(makeBlock(assert.deepEqual, {a: 4}, {a: 4, b: true}),
|
134 |
|
|
assert.AssertionError);
|
135 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, ['a'], {0: 'a'}));
|
136 |
|
|
//(although not necessarily the same order),
|
137 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4, b: '1'}, {b: '1', a: 4}));
|
138 |
|
|
var a1 = [1, 2, 3];
|
139 |
|
|
var a2 = [1, 2, 3];
|
140 |
|
|
a1.a = 'test';
|
141 |
|
|
a1.b = true;
|
142 |
|
|
a2.b = true;
|
143 |
|
|
a2.a = 'test';
|
144 |
|
|
assert.throws(makeBlock(assert.deepEqual, keys(a1), keys(a2)),
|
145 |
|
|
assert.AssertionError);
|
146 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, a1, a2));
|
147 |
|
|
});
|
148 |
|
|
|
149 |
|
|
test('assert.deepEqual - ES6 primitives', function () {
|
150 |
|
|
assert.throws(makeBlock(assert.deepEqual, null, {}), assert.AssertionError);
|
151 |
|
|
assert.throws(makeBlock(assert.deepEqual, undefined, {}), assert.AssertionError);
|
152 |
|
|
assert.throws(makeBlock(assert.deepEqual, 'a', ['a']), assert.AssertionError);
|
153 |
|
|
assert.throws(makeBlock(assert.deepEqual, 'a', {0: 'a'}), assert.AssertionError);
|
154 |
|
|
assert.throws(makeBlock(assert.deepEqual, 1, {}), assert.AssertionError);
|
155 |
|
|
assert.throws(makeBlock(assert.deepEqual, true, {}), assert.AssertionError);
|
156 |
|
|
if (typeof Symbol === 'symbol') {
|
157 |
|
|
assert.throws(makeBlock(assert.deepEqual, Symbol(), {}), assert.AssertionError);
|
158 |
|
|
}
|
159 |
|
|
});
|
160 |
|
|
|
161 |
|
|
test('assert.deepEqual - object wrappers', function () {
|
162 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, new String('a'), ['a']));
|
163 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, new String('a'), {0: 'a'}));
|
164 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, new Number(1), {}));
|
165 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, new Boolean(true), {}));
|
166 |
|
|
});
|
167 |
|
|
|
168 |
|
|
test('assert.deepEqual - Buffers', function () {
|
169 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, new Buffer([1, 2, 3]), new Buffer([1, 2, 3])));
|
170 |
|
|
if (typeof global.Uint8Array === 'function') {
|
171 |
|
|
assert.throws(makeBlock(assert.deepEqual, new Buffer([1, 2, 3]), new Uint8Array([1, 2, 3])));
|
172 |
|
|
}
|
173 |
|
|
if (typeof global.Uint16Array === 'function') {
|
174 |
|
|
assert.doesNotThrow(makeBlock(assert.deepEqual, new Uint16Array([1, 2, 3]), new Uint16Array([1, 2, 3])));
|
175 |
|
|
}
|
176 |
|
|
});
|
177 |
|
|
|
178 |
|
|
function thrower(errorConstructor) {
|
179 |
|
|
throw new errorConstructor('test');
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
test('assert - testing the throwing', function () {
|
183 |
|
|
var aethrow = makeBlock(thrower, assert.AssertionError);
|
184 |
|
|
aethrow = makeBlock(thrower, assert.AssertionError);
|
185 |
|
|
|
186 |
|
|
// the basic calls work
|
187 |
|
|
assert.throws(makeBlock(thrower, assert.AssertionError),
|
188 |
|
|
assert.AssertionError, 'message');
|
189 |
|
|
assert.throws(makeBlock(thrower, assert.AssertionError), assert.AssertionError);
|
190 |
|
|
assert.throws(makeBlock(thrower, assert.AssertionError));
|
191 |
|
|
|
192 |
|
|
// if not passing an error, catch all.
|
193 |
|
|
assert.throws(makeBlock(thrower, TypeError));
|
194 |
|
|
|
195 |
|
|
// when passing a type, only catch errors of the appropriate type
|
196 |
|
|
var threw = false;
|
197 |
|
|
try {
|
198 |
|
|
assert.throws(makeBlock(thrower, TypeError), assert.AssertionError);
|
199 |
|
|
} catch (e) {
|
200 |
|
|
threw = true;
|
201 |
|
|
assert.ok(e instanceof TypeError, 'type');
|
202 |
|
|
}
|
203 |
|
|
assert.equal(true, threw,
|
204 |
|
|
'a.throws with an explicit error is eating extra errors',
|
205 |
|
|
assert.AssertionError);
|
206 |
|
|
threw = false;
|
207 |
|
|
|
208 |
|
|
// doesNotThrow should pass through all errors
|
209 |
|
|
try {
|
210 |
|
|
assert.doesNotThrow(makeBlock(thrower, TypeError), assert.AssertionError);
|
211 |
|
|
} catch (e) {
|
212 |
|
|
threw = true;
|
213 |
|
|
assert.ok(e instanceof TypeError);
|
214 |
|
|
}
|
215 |
|
|
assert.equal(true, threw,
|
216 |
|
|
'a.doesNotThrow with an explicit error is eating extra errors');
|
217 |
|
|
|
218 |
|
|
// key difference is that throwing our correct error makes an assertion error
|
219 |
|
|
try {
|
220 |
|
|
assert.doesNotThrow(makeBlock(thrower, TypeError), TypeError);
|
221 |
|
|
} catch (e) {
|
222 |
|
|
threw = true;
|
223 |
|
|
assert.ok(e instanceof assert.AssertionError);
|
224 |
|
|
}
|
225 |
|
|
assert.equal(true, threw,
|
226 |
|
|
'a.doesNotThrow is not catching type matching errors');
|
227 |
|
|
});
|
228 |
|
|
|
229 |
|
|
test('assert.ifError', function () {
|
230 |
|
|
assert.throws(function() {assert.ifError(new Error('test error'))});
|
231 |
|
|
assert.doesNotThrow(function() {assert.ifError(null)});
|
232 |
|
|
assert.doesNotThrow(function() {assert.ifError()});
|
233 |
|
|
});
|
234 |
|
|
|
235 |
|
|
test('assert - make sure that validating using constructor really works', function () {
|
236 |
|
|
var threw = false;
|
237 |
|
|
try {
|
238 |
|
|
assert.throws(
|
239 |
|
|
function() {
|
240 |
|
|
throw ({});
|
241 |
|
|
},
|
242 |
|
|
Array
|
243 |
|
|
);
|
244 |
|
|
} catch (e) {
|
245 |
|
|
threw = true;
|
246 |
|
|
}
|
247 |
|
|
assert.ok(threw, 'wrong constructor validation');
|
248 |
|
|
});
|
249 |
|
|
|
250 |
|
|
test('assert - use a RegExp to validate error message', function () {
|
251 |
|
|
assert.throws(makeBlock(thrower, TypeError), /test/);
|
252 |
|
|
});
|
253 |
|
|
|
254 |
|
|
test('assert - use a fn to validate error object', function () {
|
255 |
|
|
assert.throws(makeBlock(thrower, TypeError), function(err) {
|
256 |
|
|
if ((err instanceof TypeError) && /test/.test(err)) {
|
257 |
|
|
return true;
|
258 |
|
|
}
|
259 |
|
|
});
|
260 |
|
|
});
|
261 |
|
|
|
262 |
|
|
test('assert - make sure deepEqual doesn\'t loop forever on circular refs', function () {
|
263 |
|
|
var b = {};
|
264 |
|
|
b.b = b;
|
265 |
|
|
|
266 |
|
|
var c = {};
|
267 |
|
|
c.b = c;
|
268 |
|
|
|
269 |
|
|
var gotError = false;
|
270 |
|
|
var equal = true;
|
271 |
|
|
try {
|
272 |
|
|
equal = assert.deepEqual(b, c);
|
273 |
|
|
} catch (e) {
|
274 |
|
|
gotError = true;
|
275 |
|
|
}
|
276 |
|
|
assert.ok(gotError || !equal, gotError ? 'got error': 'are equal');
|
277 |
|
|
});
|
278 |
|
|
|
279 |
|
|
test('assert - ensure reflexivity of deepEqual with `arguments` objects', function() {
|
280 |
|
|
var args = (function() { return arguments; })();
|
281 |
|
|
assert.throws(makeBlock(assert.deepEqual, [], args), assert.AssertionError);
|
282 |
|
|
assert.throws(makeBlock(assert.deepEqual, args, []), assert.AssertionError);
|
283 |
|
|
});
|
284 |
|
|
|
285 |
|
|
test('assert - test assertion message', function () {
|
286 |
|
|
function testAssertionMessage(actual, expected) {
|
287 |
|
|
try {
|
288 |
|
|
assert.equal(actual, '');
|
289 |
|
|
} catch (e) {
|
290 |
|
|
assert.equal(e.toString(),
|
291 |
|
|
['AssertionError:', expected, '==', '\'\''].join(' '));
|
292 |
|
|
}
|
293 |
|
|
}
|
294 |
|
|
testAssertionMessage(undefined, 'undefined');
|
295 |
|
|
testAssertionMessage(null, 'null');
|
296 |
|
|
testAssertionMessage(true, 'true');
|
297 |
|
|
testAssertionMessage(false, 'false');
|
298 |
|
|
testAssertionMessage(0, '0');
|
299 |
|
|
testAssertionMessage(100, '100');
|
300 |
|
|
testAssertionMessage(NaN, 'NaN');
|
301 |
|
|
testAssertionMessage(Infinity, 'Infinity');
|
302 |
|
|
testAssertionMessage(-Infinity, '-Infinity');
|
303 |
|
|
testAssertionMessage('', '""');
|
304 |
|
|
testAssertionMessage('foo', '\'foo\'');
|
305 |
|
|
testAssertionMessage([], '[]');
|
306 |
|
|
testAssertionMessage([1, 2, 3], '[ 1, 2, 3 ]');
|
307 |
|
|
testAssertionMessage(new Buffer([1, 2, 3]), '<Buffer 01 02 03>');
|
308 |
|
|
if (typeof global.Uint8Array === 'function' && Object.getOwnPropertyNames( new Uint8Array([])).length === 0) {
|
309 |
|
|
// todo fix util.inspect
|
310 |
|
|
testAssertionMessage(new Uint8Array([1, 2, 3]), '{ \'0\': 1, \'1\': 2, \'2\': 3 }');
|
311 |
|
|
}
|
312 |
|
|
testAssertionMessage(/a/, '/a/');
|
313 |
|
|
testAssertionMessage(function f() {}, '[Function: f]');
|
314 |
|
|
testAssertionMessage({}, '{}');
|
315 |
|
|
testAssertionMessage({a: undefined, b: null}, '{ a: undefined, b: null }');
|
316 |
|
|
testAssertionMessage({a: NaN, b: Infinity, c: -Infinity},
|
317 |
|
|
'{ a: NaN, b: Infinity, c: -Infinity }');
|
318 |
|
|
});
|
319 |
|
|
|
320 |
|
|
test('assert - regressions from node.js testcase', function () {
|
321 |
|
|
var threw = false;
|
322 |
|
|
|
323 |
|
|
try {
|
324 |
|
|
assert.throws(function () {
|
325 |
|
|
assert.ifError(null);
|
326 |
|
|
});
|
327 |
|
|
} catch (e) {
|
328 |
|
|
threw = true;
|
329 |
|
|
assert.equal(e.message, 'Missing expected exception..');
|
330 |
|
|
}
|
331 |
|
|
assert.ok(threw);
|
332 |
|
|
|
333 |
|
|
try {
|
334 |
|
|
assert.equal(1, 2);
|
335 |
|
|
} catch (e) {
|
336 |
|
|
assert.equal(e.toString().split('\n')[0], 'AssertionError: 1 == 2');
|
337 |
|
|
}
|
338 |
|
|
|
339 |
|
|
try {
|
340 |
|
|
assert.equal(1, 2, 'oh no');
|
341 |
|
|
} catch (e) {
|
342 |
|
|
assert.equal(e.toString().split('\n')[0], 'AssertionError: oh no');
|
343 |
|
|
}
|
344 |
|
|
});
|
345 |
|
|
|
346 |
|
|
test('assert - strict mode', function () {
|
347 |
|
|
var assertStrict = assert.strict;
|
348 |
|
|
|
349 |
|
|
assertStrict.throws(makeBlock(assertStrict.equal, 1, true), assertStrict.AssertionError);
|
350 |
|
|
assertStrict.notEqual(0, false);
|
351 |
|
|
assertStrict.throws(makeBlock(assertStrict.deepEqual, 1, true), assertStrict.AssertionError);
|
352 |
|
|
assertStrict.notDeepEqual(0, false);
|
353 |
|
|
assertStrict.equal(assertStrict.strict, assertStrict.strict.strict);
|
354 |
|
|
assertStrict.equal(assertStrict.equal, assertStrict.strictEqual);
|
355 |
|
|
assertStrict.equal(assertStrict.deepEqual, assertStrict.deepStrictEqual);
|
356 |
|
|
assertStrict.equal(assertStrict.notEqual, assertStrict.notStrictEqual);
|
357 |
|
|
assertStrict.equal(assertStrict.notDeepEqual, assertStrict.notDeepStrictEqual);
|
358 |
|
|
assertStrict.equal(Object.keys(assertStrict).length, Object.keys(assert).length);
|
359 |
|
|
});
|
360 |
|
|
}
|