| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658 |
1
1
1
1
1
1
1
1
17
5
1
1
55
22
1
8
4
1
123
41
1
40
14
1
53
27
1
7
4
1
2
2
1
1
1
160
1
1
6
3
1
134
5
1
76
1
29
1
3
1
1
2
1
1
4
3
1
4
1
1
5
1
5
1
3
1
4
1
49
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
2
1
2
1
1
2
1
1
10
4
1
6
3
1
9
2
7
6
1
3
2
1
3
1
1
3
2
1
3
1
1
1
8
8
1
1
1
1
1
1
1
1
1
1
176
176
176
176
176
176
176
176
1
1
1
1
20
1
159
1
1
1
6844
1
10231
1
16
16
4
8
2
14
4
2
12
10
5
7
1
1
6817
6817
6817
6840
6839
6816
1
3419
3
3416
5
3411
3
3
3
3411
3411
3411
2
3409
3409
3409
3409
3409
9
9
3400
3399
1
3408
1
3407
3407
3407
3407
3407
3407
3418
2
3405
3412
3412
3389
16
1
3475
27
3448
4
3444
16
3428
9
3419
1
294
294
294
294
168
168
294
141
294
294
294
5
289
9
280
1
274
6
268
8
260
250
10
1
164
164
164
1
328
1
18
18
30
18
1
6827
1
134
1
134
1
1
1
1
1
1
| /* eslint strict: ['error', 'function'] */
(function(undef) {
'use strict';
var root = {};
// Set `root` to window (in a browser)
Iif (typeof window === 'object' && window) {
root = window;
// Self (in a web worker),
} else Iif (typeof self === 'object' && self) {
root = self;
// global (in Node/IOjs)
} else Eif (typeof global === 'object' && global) {
root = global;
}
var proclaim = ok;
// Assertions as outlined in
// http://wiki.commonjs.org/wiki/Unit_Testing/1.0#Assert
// -----------------------------------------------------
// Assert that a value is truthy
function ok(val, msg) {
if (!val) {
fail(val, true, msg, '==');
}
}
proclaim.ok = ok;
// Assert that two values are equal
proclaim.equal = function(actual, expected, msg) {
/* eslint-disable eqeqeq */
if (actual != expected) {
fail(actual, expected, msg, '==');
}
/* eslint-enable eqeqeq */
};
// Assert that two values are not equal
proclaim.notEqual = function(actual, expected, msg) {
/* eslint-disable eqeqeq */
if (actual == expected) {
fail(actual, expected, msg, '!=');
}
/* eslint-enable eqeqeq */
};
// Assert that two values are equal with strict comparison
proclaim.strictEqual = function(actual, expected, msg) {
if (actual !== expected) {
fail(actual, expected, msg, '===');
}
};
// Assert that two values are not equal with strict comparison
proclaim.notStrictEqual = function(actual, expected, msg) {
if (actual === expected) {
fail(actual, expected, msg, '!==');
}
};
// Assert that two values are deeply equal
proclaim.deepEqual = function(actual, expected, msg) {
if (!isDeepEqual(actual, expected)) {
fail(actual, expected, msg, 'deepEqual');
}
};
// Assert that two values are not deeply equal
proclaim.notDeepEqual = function(actual, expected, msg) {
if (isDeepEqual(actual, expected)) {
fail(actual, expected, msg, '!deepEqual');
}
};
// Assert that two values are strictly deeply equal
proclaim.deepStrictEqual = function(actual, expected, msg) {
Eif (!isDeepEqual(actual, expected, true)) {
fail(actual, expected, msg, 'deepStrictEqual', proclaim.deepStrictEqual);
}
};
// Assert that two values are not strictly deeply equal
proclaim.notDeepStrictEqual = function(actual, expected, msg) {
Iif (isDeepEqual(actual, expected, true)) {
fail(actual, expected, msg, 'notDeepStrictEqual', proclaim.notDeepStrictEqual);
}
};
// Assert that a function throws an error
proclaim.throws = function(fn, expected, msg) {
if (!functionThrows(fn, expected, true)) {
fail(fn, expected, msg, 'throws');
}
};
// Additional assertions
// ---------------------
// Assert that a value is falsy
proclaim.notOk = function(val, msg) {
if (val) {
fail(val, true, msg, '!=');
}
};
// Assert that a function does not throw an error
proclaim.doesNotThrow = function(fn, expected, msg) {
if (functionThrows(fn, expected, false)) {
fail(fn, expected, msg, '!throws');
}
};
// Assert that a value is a specific type
proclaim.isTypeOf = function(val, type, msg) {
proclaim.strictEqual(typeof val, type, msg);
};
// Assert that a value is not a specific type
proclaim.isNotTypeOf = function(val, type, msg) {
proclaim.notStrictEqual(typeof val, type, msg);
};
// Assert that a value is an instance of a constructor
proclaim.isInstanceOf = function(val, constructor, msg) {
if (!(val instanceof constructor)) {
fail(val, constructor, msg, 'instanceof');
}
};
// Assert that a value not an instance of a constructor
proclaim.isNotInstanceOf = function(val, constructor, msg) {
if (val instanceof constructor) {
fail(val, constructor, msg, '!instanceof');
}
};
// Assert that a value is an array
proclaim.isArray = function(val, msg) {
if (!isArray(val)) {
fail(typeof val, 'array', msg, '===');
}
};
// Assert that a value is not an array
proclaim.isNotArray = function(val, msg) {
if (isArray(val)) {
fail(typeof val, 'array', msg, '!==');
}
};
// Assert that a value is a boolean
proclaim.isBoolean = function(val, msg) {
proclaim.isTypeOf(val, 'boolean', msg);
};
// Assert that a value is not a boolean
proclaim.isNotBoolean = function(val, msg) {
proclaim.isNotTypeOf(val, 'boolean', msg);
};
// Assert that a value is true
proclaim.isTrue = function(val, msg) {
proclaim.strictEqual(val, true, msg);
};
// Assert that a value is false
proclaim.isFalse = function(val, msg) {
proclaim.strictEqual(val, false, msg);
};
// Assert that a value is a function
proclaim.isFunction = function(val, msg) {
proclaim.isTypeOf(val, 'function', msg);
};
// Assert that a value is not a function
proclaim.isNotFunction = function(val, msg) {
proclaim.isNotTypeOf(val, 'function', msg);
};
// Assert that a value is null
proclaim.isNull = function(val, msg) {
proclaim.strictEqual(val, null, msg);
};
// Assert that a value is not null
proclaim.isNotNull = function(val, msg) {
proclaim.notStrictEqual(val, null, msg);
};
// Assert that a value is a number
proclaim.isNumber = function(val, msg) {
proclaim.isTypeOf(val, 'number', msg);
};
// Assert that a value is not a number
proclaim.isNotNumber = function(val, msg) {
proclaim.isNotTypeOf(val, 'number', msg);
};
// Assert that a value is an object
proclaim.isObject = function(val, msg) {
proclaim.isTypeOf(val, 'object', msg);
};
// Assert that a value is not an object
proclaim.isNotObject = function(val, msg) {
proclaim.isNotTypeOf(val, 'object', msg);
};
// Assert that a value is a string
proclaim.isString = function(val, msg) {
proclaim.isTypeOf(val, 'string', msg);
};
// Assert that a value is not a string
proclaim.isNotString = function(val, msg) {
proclaim.isNotTypeOf(val, 'string', msg);
};
// Assert that a value is undefined
proclaim.isUndefined = function(val, msg) {
proclaim.isTypeOf(val, 'undefined', msg);
};
// Assert that a value is defined
proclaim.isDefined = function(val, msg) {
proclaim.isNotTypeOf(val, 'undefined', msg);
};
// Assert that a value matches a regular expression
proclaim.match = function(actual, expected, msg) {
if (!expected.test(actual)) {
fail(actual, expected, msg, 'match');
}
};
// Assert that a value does not match a regular expression
proclaim.notMatch = function(actual, expected, msg) {
if (expected.test(actual)) {
fail(actual, expected, msg, '!match');
}
};
// Assert that an object includes something
proclaim.include = function(haystack, needle, msg) {
if (!includes(haystack, needle)) {
fail(haystack, needle, msg, 'include');
}
};
// Assert that an object does not include something
proclaim.doesNotInclude = function(haystack, needle, msg) {
if (includes(haystack, needle)) {
fail(haystack, needle, msg, '!include');
}
};
// Assert that an object (Array, String, etc.) has the expected length
proclaim.lengthEquals = function(obj, expected, msg) {
if (isUndefinedOrNull(obj)) {
return fail(undef, expected, msg, 'length');
}
if (obj.length !== expected) {
fail(obj.length, expected, msg, 'length');
}
};
// Assert that a value is less than another value
proclaim.lessThan = function(actual, expected, msg) {
if (actual >= expected) {
fail(actual, expected, msg, '<');
}
};
// Assert that a value is less than or equal to another value
proclaim.lessThanOrEqual = function(actual, expected, msg) {
if (actual > expected) {
fail(actual, expected, msg, '<=');
}
};
// Assert that a value is greater than another value
proclaim.greaterThan = function(actual, expected, msg) {
if (actual <= expected) {
fail(actual, expected, msg, '>');
}
};
// Assert that a value is greater than another value
proclaim.greaterThanOrEqual = function(actual, expected, msg) {
if (actual < expected) {
fail(actual, expected, msg, '>=');
}
};
var functionsHaveNames = (function foo() { }).name === 'foo';
proclaim.hasName = function(fn, expected, msg) {
Eif (functionsHaveNames) {
proclaim.strictEqual(fn.name, expected, msg);
} else {
proclaim.equal(Function.prototype.toString.call(fn).match(/function\s*([^\s]*)\s*\(/)[1], expected, msg);
}
};
// Aliases
// -------
proclaim.notThrows = proclaim.doesNotThrow;
proclaim.typeOf = proclaim.isTypeOf;
proclaim.notTypeOf = proclaim.isNotTypeOf;
proclaim.instanceOf = proclaim.isInstanceOf;
proclaim.notInstanceOf = proclaim.isNotInstanceOf;
proclaim.notInclude = proclaim.doesNotInclude;
proclaim.lengthOf = proclaim.lengthEquals;
proclaim.isBelow = proclaim.lessThan;
proclaim.isAbove = proclaim.greaterThan;
// Error handling
// --------------
// Assertion error class
function AssertionError(opts) {
opts = opts || {};
this.name = 'AssertionError';
this.actual = opts.actual;
this.expected = opts.expected;
this.operator = opts.operator || '';
this.message = opts.message || getAssertionErrorMessage(this);
Eif (Error.captureStackTrace) {
Error.captureStackTrace(this, opts.stackStartFunction || fail);
}
}
AssertionError.prototype = (Object.create ? Object.create(Error.prototype) : new Error());
AssertionError.prototype.name = 'AssertionError';
AssertionError.prototype.constructor = AssertionError;
// Assertion error to string
AssertionError.prototype.toString = function() {
return this.name + ': ' + this.message;
};
// Fail a test
function fail(actual, expected, message, operator, stackStartFunction) {
throw new AssertionError({
message: message,
actual: actual,
expected: expected,
operator: operator,
stackStartFunction: stackStartFunction
});
}
// Expose error handling tools
proclaim.AssertionError = AssertionError;
proclaim.fail = fail;
// Utilities
// ---------
// Utility for checking whether a value is undefined or null
function isUndefinedOrNull(val) {
return (val === null || typeof val === 'undefined');
}
// Utility for checking whether a value is an arguments object
function isArgumentsObject(val) {
return (Object.prototype.toString.call(val) === '[object Arguments]');
}
// Utility for checking whether an object contains another object
function includes(haystack, needle) {
var i;
// Array#indexOf, but ie...
if (isArray(haystack)) {
for (i = haystack.length - 1; i >= 0; i -= 1) {
if (haystack[i] === needle) {
return true;
}
}
}
// String#indexOf
if (typeof haystack === 'string') {
if (haystack.indexOf(needle) !== -1) {
return true;
}
}
// Object#hasOwnProperty
if (typeof haystack === 'object' && haystack !== null) {
/* eslint-disable no-prototype-builtins */
if (Object.prototype.hasOwnProperty.call(haystack, needle)) {
return true;
}
/* eslint-enable no-prototype-builtins */
}
return false;
}
// Utility for checking whether a value is an array
var isArray = Array.isArray || function(val) {
return (Object.prototype.toString.call(val) === '[object Array]');
};
// Utility for getting object keys
function getObjectKeys(obj) {
var key;
var keys = [];
for (key in obj) {
/* eslint-disable no-prototype-builtins */
Eif (obj.hasOwnProperty(key)) {
keys.push(key);
}
/* eslint-enable no-prototype-builtins */
}
return keys;
}
// Utility for deep equality testing of objects
function objectsEqual(obj1, obj2, strict) {
/* eslint-disable eqeqeq */
// Check for undefined or null
if (isUndefinedOrNull(obj1) || isUndefinedOrNull(obj2)) {
return false;
}
if (isPrimitive(obj1) || isPrimitive(obj2)) {
return obj1 === obj2;
}
if (strict) {
// Object prototypes must be the same
/* eslint-disable no-proto */
var obj1prototype = obj1.prototype || (obj1).__proto__;
var obj2prototype = obj2.prototype || (obj2).__proto__;
/* eslint-enable no-proto */
Iif (obj1prototype !== obj2prototype) {
return false;
}
}
// Handle argument objects
var obj1IsArgumentsObject = isArgumentsObject(obj1);
var obj2IsArgumentsObject = isArgumentsObject(obj2);
if (
(obj1IsArgumentsObject && !obj2IsArgumentsObject) ||
(!obj1IsArgumentsObject && obj2IsArgumentsObject)
) {
return false;
}
Iif (isArgumentsObject(obj1)) {
if (!isArgumentsObject(obj2)) {
return false;
}
obj1 = Array.prototype.slice.call(obj1);
obj2 = Array.prototype.slice.call(obj2);
}
var obj1Keys;
var obj2Keys;
try {
if (isArray(obj1) && isArray(obj2)) {
obj1Keys = getObjectKeys(cloneArray(obj1));
obj2Keys = getObjectKeys(cloneArray(obj2));
} else {
// Check number of own properties
obj1Keys = getObjectKeys(obj1);
obj2Keys = getObjectKeys(obj2);
}
} catch (error) {
return false;
}
if (obj1Keys.length !== obj2Keys.length) {
return false;
}
obj1Keys.sort();
obj2Keys.sort();
// Cheap initial key test (see https://github.com/joyent/node/blob/master/lib/assert.js)
var key;
var i;
var len = obj1Keys.length;
for (i = 0; i < len; i += 1) {
if (obj1Keys[i] != obj2Keys[i]) {
return false;
}
}
// Expensive deep test
for (i = 0; i < len; i += 1) {
key = obj1Keys[i];
if (!isDeepEqual(obj1[key], obj2[key], strict)) {
return false;
}
}
// If it got this far...
return true;
/* eslint-enable eqeqeq */
}
// Utility for deep equality testing
function isDeepEqual(actual, expected, strict) {
/* eslint-disable eqeqeq */
if (actual === expected) {
return true;
}
if (expected instanceof Date && actual instanceof Date) {
return actual.getTime() === expected.getTime();
}
if (actual instanceof RegExp && expected instanceof RegExp) {
return (
actual.source === expected.source &&
actual.global === expected.global &&
actual.multiline === expected.multiline &&
actual.lastIndex === expected.lastIndex &&
actual.ignoreCase === expected.ignoreCase
);
}
if (typeof actual !== 'object' && typeof expected !== 'object') {
return strict ? actual === expected : actual == expected;
}
return objectsEqual(actual, expected, strict);
}
// Utility for testing whether a function throws an error
function functionThrows(fn, expected, shouldThrow) {
// Try/catch
var thrown = false;
var thrownError;
try {
fn();
} catch (error) {
thrown = true;
thrownError = error;
}
// Check error
if (thrown && expected) {
thrown = errorMatches(thrownError, expected);
}
var isUnexpectedException = !shouldThrow && thrownError && !expected;
var isUnwantedException = !shouldThrow && isError(thrownError);
if (isUnexpectedException || (isUnwantedException && thrown)) {
return thrown;
}
if ((shouldThrow && thrownError && expected &&
!errorMatches(thrownError, expected)) || (!shouldThrow && thrownError)) {
throw thrownError;
}
return thrown;
}
// Utility for checking whether an error matches a given constructor, regexp or string
function errorMatches(actual, expected) {
if (typeof expected === 'string') {
return actual.message === expected;
}
if (expected instanceof RegExp) {
return expected.test(actual.message);
}
if (actual instanceof expected) {
return true;
}
return expected.call({}, actual) === true;
}
// Get a formatted assertion error message
function getAssertionErrorMessage(error) {
Eif (typeof require === 'function' && typeof module !== 'undefined' && module.exports) {
var inspect = require('util-inspect');
return [
truncateString(inspect(error.actual, {depth: null}), 128),
error.operator,
truncateString(inspect(error.expected, {depth: null}), 128)
].join(' ');
}
return error.actual + ' ' + error.operator + ' ' + error.expected;
}
// Truncate a string to a length
function truncateString(string, length) {
return (string.length < length ? string : string.slice(0, length) + '…');
}
// Utility for cloning an array, useful for ensuring undefined items are iterated over.
function cloneArray(arr) {
var clone = [];
for (var i = 0; i < arr.length; i += 1) {
clone.push(arr[i]);
}
return clone;
}
// Utility for detecting if a value is a primitive. A primitive value is a member of one of
// the following built-in types: Undefined, Null, Boolean, Number, String, and Symbol;
function isPrimitive(value) {
return value === null || (typeof value !== 'function' && typeof value !== 'object');
}
// Utility for checking if a value is an Error.
function isError(error) {
return (
isObject(error) &&
(Object.prototype.toString.call(error) === '[object Error]' || error instanceof Error)
);
}
// Utility for checking if a value is an Object.
function isObject(arg) {
return typeof arg === 'object' && arg !== null;
}
// Exports
// -------
// AMD
Iif (typeof define !== 'undefined' && define.amd) {
define([], function() {
return proclaim;
});
}
// CommonJS
Eif (typeof module !== 'undefined' && module.exports) {
module.exports = proclaim;
}
// Script tag
root.proclaim = proclaim;
Eif (typeof global !== 'undefined') {
global.proclaim = proclaim;
}
}());
|