Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80713 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
// String literal + object blew up my implementation...
163
assert.throws(makeBlock(assert.deepEqual, 'a', {}), assert.AssertionError);
164
});
165
166
function thrower(errorConstructor) {
167
throw new errorConstructor('test');
168
}
169
170
test('assert - Testing the throwing', function () {
171
var aethrow = makeBlock(thrower, assert.AssertionError);
172
aethrow = makeBlock(thrower, assert.AssertionError);
173
174
// the basic calls work
175
assert.throws(makeBlock(thrower, assert.AssertionError),
176
assert.AssertionError, 'message');
177
assert.throws(makeBlock(thrower, assert.AssertionError), assert.AssertionError);
178
assert.throws(makeBlock(thrower, assert.AssertionError));
179
180
// if not passing an error, catch all.
181
assert.throws(makeBlock(thrower, TypeError));
182
183
// when passing a type, only catch errors of the appropriate type
184
var threw = false;
185
try {
186
assert.throws(makeBlock(thrower, TypeError), assert.AssertionError);
187
} catch (e) {
188
threw = true;
189
assert.ok(e instanceof TypeError, 'type');
190
}
191
assert.equal(true, threw,
192
'a.throws with an explicit error is eating extra errors',
193
assert.AssertionError);
194
threw = false;
195
196
// doesNotThrow should pass through all errors
197
try {
198
assert.doesNotThrow(makeBlock(thrower, TypeError), assert.AssertionError);
199
} catch (e) {
200
threw = true;
201
assert.ok(e instanceof TypeError);
202
}
203
assert.equal(true, threw,
204
'a.doesNotThrow with an explicit error is eating extra errors');
205
206
// key difference is that throwing our correct error makes an assertion error
207
try {
208
assert.doesNotThrow(makeBlock(thrower, TypeError), TypeError);
209
} catch (e) {
210
threw = true;
211
assert.ok(e instanceof assert.AssertionError);
212
}
213
assert.equal(true, threw,
214
'a.doesNotThrow is not catching type matching errors');
215
});
216
217
test('assert.ifError', function () {
218
assert.throws(function() {assert.ifError(new Error('test error'))});
219
assert.doesNotThrow(function() {assert.ifError(null)});
220
assert.doesNotThrow(function() {assert.ifError()});
221
});
222
223
test('assert - make sure that validating using constructor really works', function () {
224
var threw = false;
225
try {
226
assert.throws(
227
function() {
228
throw ({});
229
},
230
Array
231
);
232
} catch (e) {
233
threw = true;
234
}
235
assert.ok(threw, 'wrong constructor validation');
236
});
237
238
test('assert - use a RegExp to validate error message', function () {
239
assert.throws(makeBlock(thrower, TypeError), /test/);
240
});
241
242
test('assert - se a fn to validate error object', function () {
243
assert.throws(makeBlock(thrower, TypeError), function(err) {
244
if ((err instanceof TypeError) && /test/.test(err)) {
245
return true;
246
}
247
});
248
});
249
250
test('assert - Make sure deepEqual doesn\'t loop forever on circular refs', function () {
251
var b = {};
252
b.b = b;
253
254
var c = {};
255
c.b = c;
256
257
var gotError = false;
258
try {
259
assert.deepEqual(b, c);
260
} catch (e) {
261
gotError = true;
262
}
263
264
assert.ok(gotError);
265
});
266
267
268
test('assert - test assertion message', function () {
269
function testAssertionMessage(actual, expected) {
270
try {
271
assert.equal(actual, '');
272
} catch (e) {
273
assert.equal(e.toString(),
274
['AssertionError:', expected, '==', '""'].join(' '));
275
}
276
}
277
testAssertionMessage(undefined, '"undefined"');
278
testAssertionMessage(null, 'null');
279
testAssertionMessage(true, 'true');
280
testAssertionMessage(false, 'false');
281
testAssertionMessage(0, '0');
282
testAssertionMessage(100, '100');
283
testAssertionMessage(NaN, '"NaN"');
284
testAssertionMessage(Infinity, '"Infinity"');
285
testAssertionMessage(-Infinity, '"-Infinity"');
286
testAssertionMessage('', '""');
287
testAssertionMessage('foo', '"foo"');
288
testAssertionMessage([], '[]');
289
testAssertionMessage([1, 2, 3], '[1,2,3]');
290
testAssertionMessage(/a/, '"/a/"');
291
testAssertionMessage(function f() {}, '"function f() {}"');
292
testAssertionMessage({}, '{}');
293
testAssertionMessage({a: undefined, b: null}, '{"a":"undefined","b":null}');
294
testAssertionMessage({a: NaN, b: Infinity, c: -Infinity},
295
'{"a":"NaN","b":"Infinity","c":"-Infinity"}');
296
});
297
298
test('assert - regressions from node.js testcase', function () {
299
var threw = false;
300
301
try {
302
assert.throws(function () {
303
assert.ifError(null);
304
});
305
} catch (e) {
306
threw = true;
307
assert.equal(e.message, 'Missing expected exception..');
308
}
309
assert.ok(threw);
310
311
try {
312
assert.equal(1, 2);
313
} catch (e) {
314
assert.equal(e.toString().split('\n')[0], 'AssertionError: 1 == 2');
315
}
316
317
try {
318
assert.equal(1, 2, 'oh no');
319
} catch (e) {
320
assert.equal(e.toString().split('\n')[0], 'AssertionError: oh no');
321
}
322
});
323
324