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