assert
Advanced tools
Comparing version 1.3.0 to 1.4.0
281
assert.js
@@ -25,10 +25,52 @@ // http://wiki.commonjs.org/wiki/Unit_Testing/1.0 | ||
// when used in node, this will actually load the util module we depend on | ||
// versus loading the builtin util module as happens otherwise | ||
// this is a bug in node module loading as far as I am concerned | ||
'use strict'; | ||
// UTILITY | ||
function compare(bufa, bufb) { | ||
var cmpLen = Math.min(bufa, bufb); | ||
if (cmpLen <= 0) { | ||
return 0; | ||
} | ||
var i = -1; | ||
var a,b; | ||
while (++i < cmpLen) { | ||
a = bufa[i]; | ||
b = bufb[i]; | ||
if (a < b) { | ||
return -1; | ||
} else if (a > b) { | ||
return 1; | ||
} | ||
} | ||
return 0; | ||
} | ||
var util = require('util/'); | ||
var Buffer = require('buffer').Buffer; | ||
var BufferShim = require('buffer-shims'); | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
var pSlice = Array.prototype.slice; | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
var functionsHaveNames = (function () { | ||
return function foo() {}.name === 'foo'; | ||
}()); | ||
function pToString (obj) { | ||
return Object.prototype.toString.call(obj); | ||
} | ||
function isView(arrbuf) { | ||
if (typeof global.ArrayBuffer !== 'function') { | ||
return false; | ||
} | ||
if (typeof ArrayBuffer.isView === 'function') { | ||
return ArrayBuffer.isView(arrbuf); | ||
} | ||
if (!arrbuf) { | ||
return false; | ||
} | ||
if (arrbuf instanceof DataView) { | ||
return true; | ||
} | ||
if (arrbuf.buffer && arrbuf.buffer instanceof ArrayBuffer) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
// 1. The assert module provides functions that throw | ||
@@ -45,2 +87,15 @@ // AssertionError's when particular conditions are not met. The | ||
var regex = /\s*function\s+([^\(\s]*)\s*/; | ||
// based on https://github.com/ljharb/function.prototype.name/blob/adeeeec8bfcc6068b187d7d9fb3d5bb1d3a30899/implementation.js | ||
function getName(func) { | ||
if (!util.isFunction(func)) { | ||
return; | ||
} | ||
if (functionsHaveNames) { | ||
return func.name; | ||
} | ||
var str = func.toString(); | ||
var match = str.match(regex); | ||
return match && match[1]; | ||
} | ||
assert.AssertionError = function AssertionError(options) { | ||
@@ -59,24 +114,22 @@ this.name = 'AssertionError'; | ||
var stackStartFunction = options.stackStartFunction || fail; | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, stackStartFunction); | ||
} | ||
else { | ||
// non v8 browsers so we can have a stacktrace | ||
var err = new Error(); | ||
if (err.stack) { | ||
var out = err.stack; | ||
Error.captureStackTrace(this, stackStartFunction); | ||
} else { | ||
// non v8 browsers so we can have a stacktrace | ||
var err = new Error(); | ||
if (err.stack) { | ||
var out = err.stack; | ||
// try to strip useless frames | ||
var fn_name = stackStartFunction.name; | ||
var idx = out.indexOf('\n' + fn_name); | ||
if (idx >= 0) { | ||
// once we have located the function frame | ||
// we need to strip out everything before it (and its line) | ||
var next_line = out.indexOf('\n', idx + 1); | ||
out = out.substring(next_line + 1); | ||
} | ||
// try to strip useless frames | ||
var fn_name = getName(stackStartFunction); | ||
var idx = out.indexOf('\n' + fn_name); | ||
if (idx >= 0) { | ||
// once we have located the function frame | ||
// we need to strip out everything before it (and its line) | ||
var next_line = out.indexOf('\n', idx + 1); | ||
out = out.substring(next_line + 1); | ||
} | ||
this.stack = out; | ||
} | ||
this.stack = out; | ||
} | ||
} | ||
@@ -88,17 +141,4 @@ }; | ||
function replacer(key, value) { | ||
if (util.isUndefined(value)) { | ||
return '' + value; | ||
} | ||
if (util.isNumber(value) && !isFinite(value)) { | ||
return value.toString(); | ||
} | ||
if (util.isFunction(value) || util.isRegExp(value)) { | ||
return value.toString(); | ||
} | ||
return value; | ||
} | ||
function truncate(s, n) { | ||
if (util.isString(s)) { | ||
if (typeof s === 'string') { | ||
return s.length < n ? s : s.slice(0, n); | ||
@@ -109,7 +149,14 @@ } else { | ||
} | ||
function inspect(something) { | ||
if (functionsHaveNames || !util.isFunction(something)) { | ||
return util.inspect(something); | ||
} | ||
var rawname = getName(something); | ||
var name = rawname ? ': ' + rawname : ''; | ||
return '[Function' + name + ']'; | ||
} | ||
function getMessage(self) { | ||
return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' + | ||
return truncate(inspect(self.actual), 128) + ' ' + | ||
self.operator + ' ' + | ||
truncate(JSON.stringify(self.expected, replacer), 128); | ||
truncate(inspect(self.expected), 128); | ||
} | ||
@@ -174,3 +221,3 @@ | ||
assert.deepEqual = function deepEqual(actual, expected, message) { | ||
if (!_deepEqual(actual, expected)) { | ||
if (!_deepEqual(actual, expected, false)) { | ||
fail(actual, expected, message, 'deepEqual', assert.deepEqual); | ||
@@ -180,16 +227,15 @@ } | ||
function _deepEqual(actual, expected) { | ||
assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) { | ||
if (!_deepEqual(actual, expected, true)) { | ||
fail(actual, expected, message, 'deepStrictEqual', assert.deepStrictEqual); | ||
} | ||
}; | ||
function _deepEqual(actual, expected, strict, memos) { | ||
// 7.1. All identical values are equivalent, as determined by ===. | ||
if (actual === expected) { | ||
return true; | ||
} else if (Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) { | ||
return compare(actual, expected) === 0; | ||
} else if (util.isBuffer(actual) && util.isBuffer(expected)) { | ||
if (actual.length != expected.length) return false; | ||
for (var i = 0; i < actual.length; i++) { | ||
if (actual[i] !== expected[i]) return false; | ||
} | ||
return true; | ||
// 7.2. If the expected value is a Date object, the actual value is | ||
@@ -212,5 +258,19 @@ // equivalent if it is also a Date object that refers to the same time. | ||
// equivalence is determined by ==. | ||
} else if (!util.isObject(actual) && !util.isObject(expected)) { | ||
return actual == expected; | ||
} else if ((actual === null || typeof actual !== 'object') && | ||
(expected === null || typeof expected !== 'object')) { | ||
return strict ? actual === expected : actual == expected; | ||
// If both values are instances of typed arrays, wrap their underlying | ||
// ArrayBuffers in a Buffer each to increase performance | ||
// This optimization requires the arrays to have the same type as checked by | ||
// Object.prototype.toString (aka pToString). Never perform binary | ||
// comparisons for Float*Arrays, though, since e.g. +0 === -0 but their | ||
// bit patterns are not identical. | ||
} else if (isView(actual) && isView(expected) && | ||
pToString(actual) === pToString(expected) && | ||
!(actual instanceof Float32Array || | ||
actual instanceof Float64Array)) { | ||
return compare(BufferShim.from(actual.buffer), | ||
BufferShim.from(expected.buffer)) === 0; | ||
// 7.5 For all other Object pairs, including Array objects, equivalence is | ||
@@ -223,3 +283,15 @@ // determined by having the same number of owned properties (as verified | ||
} else { | ||
return objEquiv(actual, expected); | ||
memos = memos || {actual: [], expected: []}; | ||
var actualIndex = memos.actual.indexOf(actual); | ||
if (actualIndex !== -1) { | ||
if (actualIndex === memos.expected.indexOf(expected)) { | ||
return true; | ||
} | ||
} | ||
memos.actual.push(actual); | ||
memos.expected.push(expected); | ||
return objEquiv(actual, expected, strict, memos); | ||
} | ||
@@ -232,13 +304,12 @@ } | ||
function objEquiv(a, b) { | ||
if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b)) | ||
function objEquiv(a, b, strict, actualVisitedObjects) { | ||
if (a === null || a === undefined || b === null || b === undefined) | ||
return false; | ||
// an identical 'prototype' property. | ||
if (a.prototype !== b.prototype) return false; | ||
// if one is a primitive, the other must be same | ||
if (util.isPrimitive(a) || util.isPrimitive(b)) { | ||
if (util.isPrimitive(a) || util.isPrimitive(b)) | ||
return a === b; | ||
} | ||
var aIsArgs = isArguments(a), | ||
bIsArgs = isArguments(b); | ||
if (strict && Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) | ||
return false; | ||
var aIsArgs = isArguments(a); | ||
var bIsArgs = isArguments(b); | ||
if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs)) | ||
@@ -249,10 +320,10 @@ return false; | ||
b = pSlice.call(b); | ||
return _deepEqual(a, b); | ||
return _deepEqual(a, b, strict); | ||
} | ||
var ka = objectKeys(a), | ||
kb = objectKeys(b), | ||
key, i; | ||
var ka = objectKeys(a); | ||
var kb = objectKeys(b); | ||
var key, i; | ||
// having the same number of owned properties (keys incorporates | ||
// hasOwnProperty) | ||
if (ka.length != kb.length) | ||
if (ka.length !== kb.length) | ||
return false; | ||
@@ -264,3 +335,3 @@ //the same set of keys (although not necessarily the same order), | ||
for (i = ka.length - 1; i >= 0; i--) { | ||
if (ka[i] != kb[i]) | ||
if (ka[i] !== kb[i]) | ||
return false; | ||
@@ -272,3 +343,4 @@ } | ||
key = ka[i]; | ||
if (!_deepEqual(a[key], b[key])) return false; | ||
if (!_deepEqual(a[key], b[key], strict, actualVisitedObjects)) | ||
return false; | ||
} | ||
@@ -282,3 +354,3 @@ return true; | ||
assert.notDeepEqual = function notDeepEqual(actual, expected, message) { | ||
if (_deepEqual(actual, expected)) { | ||
if (_deepEqual(actual, expected, false)) { | ||
fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual); | ||
@@ -288,2 +360,10 @@ } | ||
assert.notDeepStrictEqual = notDeepStrictEqual; | ||
function notDeepStrictEqual(actual, expected, message) { | ||
if (_deepEqual(actual, expected, true)) { | ||
fail(actual, expected, message, 'notDeepStrictEqual', notDeepStrictEqual); | ||
} | ||
} | ||
// 9. The strict equality assertion tests strict equality, as determined by ===. | ||
@@ -314,15 +394,37 @@ // assert.strictEqual(actual, expected, message_opt); | ||
return expected.test(actual); | ||
} else if (actual instanceof expected) { | ||
return true; | ||
} else if (expected.call({}, actual) === true) { | ||
return true; | ||
} | ||
return false; | ||
try { | ||
if (actual instanceof expected) { | ||
return true; | ||
} | ||
} catch (e) { | ||
// Ignore. The instanceof check doesn't work for arrow functions. | ||
} | ||
if (Error.isPrototypeOf(expected)) { | ||
return false; | ||
} | ||
return expected.call({}, actual) === true; | ||
} | ||
function _tryBlock(block) { | ||
var error; | ||
try { | ||
block(); | ||
} catch (e) { | ||
error = e; | ||
} | ||
return error; | ||
} | ||
function _throws(shouldThrow, block, expected, message) { | ||
var actual; | ||
if (util.isString(expected)) { | ||
if (typeof block !== 'function') { | ||
throw new TypeError('"block" argument must be a function'); | ||
} | ||
if (typeof expected === 'string') { | ||
message = expected; | ||
@@ -332,7 +434,3 @@ expected = null; | ||
try { | ||
block(); | ||
} catch (e) { | ||
actual = e; | ||
} | ||
actual = _tryBlock(block); | ||
@@ -346,3 +444,10 @@ message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + | ||
if (!shouldThrow && expectedException(actual, expected)) { | ||
var userProvidedMessage = typeof message === 'string'; | ||
var isUnwantedException = !shouldThrow && util.isError(actual); | ||
var isUnexpectedException = !shouldThrow && actual && !expected; | ||
if ((isUnwantedException && | ||
userProvidedMessage && | ||
expectedException(actual, expected)) || | ||
isUnexpectedException) { | ||
fail(actual, expected, 'Got unwanted exception' + message); | ||
@@ -361,11 +466,11 @@ } | ||
assert.throws = function(block, /*optional*/error, /*optional*/message) { | ||
_throws.apply(this, [true].concat(pSlice.call(arguments))); | ||
_throws(true, block, error, message); | ||
}; | ||
// EXTENSION! This is annoying to write outside this module. | ||
assert.doesNotThrow = function(block, /*optional*/message) { | ||
_throws.apply(this, [false].concat(pSlice.call(arguments))); | ||
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) { | ||
_throws(false, block, error, message); | ||
}; | ||
assert.ifError = function(err) { if (err) {throw err;}}; | ||
assert.ifError = function(err) { if (err) throw err; }; | ||
@@ -372,0 +477,0 @@ var objectKeys = Object.keys || function (obj) { |
@@ -7,3 +7,3 @@ { | ||
], | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"homepage": "https://github.com/defunctzombie/commonjs-assert", | ||
@@ -16,6 +16,7 @@ "repository": { | ||
"dependencies": { | ||
"buffer-shims": "1.0.0", | ||
"util": "0.10.3" | ||
}, | ||
"devDependencies": { | ||
"zuul": "~1.10.2", | ||
"zuul": "~3.9.0", | ||
"mocha": "~1.21.4" | ||
@@ -25,4 +26,7 @@ }, | ||
"scripts": { | ||
"test": "mocha --ui qunit test.js && zuul -- test.js" | ||
"test-node": "mocha --ui qunit test.js", | ||
"test-browser": "zuul -- test.js", | ||
"test": "npm run test-node && npm run test-browser", | ||
"test-native": "TEST_NATIVE=true mocha --ui qunit test.js" | ||
} | ||
} |
517
test.js
@@ -22,5 +22,10 @@ // Copyright Joyent, Inc. and other Node contributors. | ||
var assert = require('./'); | ||
var nodeAssert = require('assert'); | ||
var ourAssert = require('./'); | ||
var keys = Object.keys; | ||
if (process.env.TEST_NATIVE === true) { | ||
tests(nodeAssert, 'node assert'); | ||
} else { | ||
tests(ourAssert, 'our assert'); | ||
} | ||
@@ -34,312 +39,294 @@ function makeBlock(f) { | ||
test('assert.ok', function () { | ||
assert.throws(makeBlock(assert, false), assert.AssertionError, 'ok(false)'); | ||
function tests (assert, what) { | ||
test('assert.ok', function () { | ||
assert.throws(makeBlock(assert, false), assert.AssertionError, 'ok(false)'); | ||
assert.doesNotThrow(makeBlock(assert, true), assert.AssertionError, 'ok(true)'); | ||
assert.doesNotThrow(makeBlock(assert, true), assert.AssertionError, 'ok(true)'); | ||
assert.doesNotThrow(makeBlock(assert, 'test', 'ok(\'test\')')); | ||
assert.doesNotThrow(makeBlock(assert, 'test', 'ok(\'test\')')); | ||
assert.throws(makeBlock(assert.ok, false), | ||
assert.AssertionError, 'ok(false)'); | ||
assert.throws(makeBlock(assert.ok, false), | ||
assert.AssertionError, 'ok(false)'); | ||
assert.doesNotThrow(makeBlock(assert.ok, true), | ||
assert.AssertionError, 'ok(true)'); | ||
assert.doesNotThrow(makeBlock(assert.ok, true), | ||
assert.AssertionError, 'ok(true)'); | ||
assert.doesNotThrow(makeBlock(assert.ok, 'test'), 'ok(\'test\')'); | ||
}); | ||
assert.doesNotThrow(makeBlock(assert.ok, 'test'), 'ok(\'test\')'); | ||
}); | ||
test('assert.equal', function () { | ||
assert.throws(makeBlock(assert.equal, true, false), assert.AssertionError, 'equal'); | ||
test('assert.equal', function () { | ||
assert.throws(makeBlock(assert.equal, true, false), assert.AssertionError, 'equal'); | ||
assert.doesNotThrow(makeBlock(assert.equal, null, null), 'equal'); | ||
assert.doesNotThrow(makeBlock(assert.equal, null, null), 'equal'); | ||
assert.doesNotThrow(makeBlock(assert.equal, undefined, undefined), 'equal'); | ||
assert.doesNotThrow(makeBlock(assert.equal, undefined, undefined), 'equal'); | ||
assert.doesNotThrow(makeBlock(assert.equal, null, undefined), 'equal'); | ||
assert.doesNotThrow(makeBlock(assert.equal, null, undefined), 'equal'); | ||
assert.doesNotThrow(makeBlock(assert.equal, true, true), 'equal'); | ||
assert.doesNotThrow(makeBlock(assert.equal, true, true), 'equal'); | ||
assert.doesNotThrow(makeBlock(assert.equal, 2, '2'), 'equal'); | ||
assert.doesNotThrow(makeBlock(assert.equal, 2, '2'), 'equal'); | ||
assert.doesNotThrow(makeBlock(assert.notEqual, true, false), 'notEqual'); | ||
assert.doesNotThrow(makeBlock(assert.notEqual, true, false), 'notEqual'); | ||
assert.throws(makeBlock(assert.notEqual, true, true), | ||
assert.AssertionError, 'notEqual'); | ||
}); | ||
assert.throws(makeBlock(assert.notEqual, true, true), | ||
assert.AssertionError, 'notEqual'); | ||
}); | ||
test('assert.strictEqual', function () { | ||
assert.throws(makeBlock(assert.strictEqual, 2, '2'), | ||
assert.AssertionError, 'strictEqual'); | ||
test('assert.strictEqual', function () { | ||
assert.throws(makeBlock(assert.strictEqual, 2, '2'), | ||
assert.AssertionError, 'strictEqual'); | ||
assert.throws(makeBlock(assert.strictEqual, null, undefined), | ||
assert.AssertionError, 'strictEqual'); | ||
assert.throws(makeBlock(assert.strictEqual, null, undefined), | ||
assert.AssertionError, 'strictEqual'); | ||
assert.doesNotThrow(makeBlock(assert.notStrictEqual, 2, '2'), 'notStrictEqual'); | ||
}); | ||
assert.doesNotThrow(makeBlock(assert.notStrictEqual, 2, '2'), 'notStrictEqual'); | ||
}); | ||
test('assert.deepEqual - 7.2', function () { | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, new Date(2000, 3, 14), | ||
new Date(2000, 3, 14)), 'deepEqual date'); | ||
test('assert.deepStrictEqual', function () { | ||
assert.throws(makeBlock(assert.deepStrictEqual, [2], ['2']), | ||
assert.AssertionError, 'deepStrictEqual'); | ||
assert.throws(makeBlock(assert.deepEqual, new Date(), new Date(2000, 3, 14)), | ||
assert.AssertionError, | ||
'deepEqual date'); | ||
}); | ||
assert.throws(makeBlock(assert.deepStrictEqual, [null], [undefined]), | ||
assert.AssertionError, 'deepStrictEqual'); | ||
test('assert.deepEqual - 7.3', function () { | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/, /a/)); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/g, /a/g)); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/i, /a/i)); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/m, /a/m)); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/igm, /a/igm)); | ||
assert.throws(makeBlock(assert.deepEqual, /ab/, /a/)); | ||
assert.throws(makeBlock(assert.deepEqual, /a/g, /a/)); | ||
assert.throws(makeBlock(assert.deepEqual, /a/i, /a/)); | ||
assert.throws(makeBlock(assert.deepEqual, /a/m, /a/)); | ||
assert.throws(makeBlock(assert.deepEqual, /a/igm, /a/im)); | ||
assert.doesNotThrow(makeBlock(assert.notDeepStrictEqual, [2], ['2']), 'notDeepStrictEqual'); | ||
}); | ||
var re1 = /a/; | ||
re1.lastIndex = 3; | ||
assert.throws(makeBlock(assert.deepEqual, re1, /a/)); | ||
}); | ||
test('assert.deepEqual - 7.2', function () { | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, new Date(2000, 3, 14), | ||
new Date(2000, 3, 14)), 'deepEqual date'); | ||
test('assert.deepEqual - 7.4', function () { | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, 4, '4'), 'deepEqual == check'); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, true, 1), 'deepEqual == check'); | ||
assert.throws(makeBlock(assert.deepEqual, 4, '5'), | ||
assert.AssertionError, | ||
'deepEqual == check'); | ||
}); | ||
assert.throws(makeBlock(assert.deepEqual, new Date(), new Date(2000, 3, 14)), | ||
assert.AssertionError, | ||
'deepEqual date'); | ||
}); | ||
test('assert.deepEqual - 7.5', function () { | ||
// having the same number of owned properties && the same set of keys | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4}, {a: 4})); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'})); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, [4], ['4'])); | ||
assert.throws(makeBlock(assert.deepEqual, {a: 4}, {a: 4, b: true}), | ||
assert.AssertionError); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, ['a'], {0: 'a'})); | ||
//(although not necessarily the same order), | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4, b: '1'}, {b: '1', a: 4})); | ||
var a1 = [1, 2, 3]; | ||
var a2 = [1, 2, 3]; | ||
a1.a = 'test'; | ||
a1.b = true; | ||
a2.b = true; | ||
a2.a = 'test'; | ||
assert.throws(makeBlock(assert.deepEqual, keys(a1), keys(a2)), | ||
assert.AssertionError); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, a1, a2)); | ||
}); | ||
test('assert.deepEqual - 7.3', function () { | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/, /a/)); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/g, /a/g)); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/i, /a/i)); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/m, /a/m)); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, /a/igm, /a/igm)); | ||
assert.throws(makeBlock(assert.deepEqual, /ab/, /a/)); | ||
assert.throws(makeBlock(assert.deepEqual, /a/g, /a/)); | ||
assert.throws(makeBlock(assert.deepEqual, /a/i, /a/)); | ||
assert.throws(makeBlock(assert.deepEqual, /a/m, /a/)); | ||
assert.throws(makeBlock(assert.deepEqual, /a/igm, /a/im)); | ||
test('assert.deepEqual - instances', function () { | ||
// having an identical prototype property | ||
var nbRoot = { | ||
toString: function() { return this.first + ' ' + this.last; } | ||
}; | ||
var re1 = /a/; | ||
re1.lastIndex = 3; | ||
assert.throws(makeBlock(assert.deepEqual, re1, /a/)); | ||
}); | ||
function nameBuilder(first, last) { | ||
this.first = first; | ||
this.last = last; | ||
return this; | ||
} | ||
nameBuilder.prototype = nbRoot; | ||
test('assert.deepEqual - 7.4', function () { | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, 4, '4'), 'deepEqual == check'); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, true, 1), 'deepEqual == check'); | ||
assert.throws(makeBlock(assert.deepEqual, 4, '5'), | ||
assert.AssertionError, | ||
'deepEqual == check'); | ||
}); | ||
function nameBuilder2(first, last) { | ||
this.first = first; | ||
this.last = last; | ||
return this; | ||
} | ||
nameBuilder2.prototype = nbRoot; | ||
test('assert.deepEqual - 7.5', function () { | ||
// having the same number of owned properties && the same set of keys | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4}, {a: 4})); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'})); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, [4], ['4'])); | ||
assert.throws(makeBlock(assert.deepEqual, {a: 4}, {a: 4, b: true}), | ||
assert.AssertionError); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, ['a'], {0: 'a'})); | ||
//(although not necessarily the same order), | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4, b: '1'}, {b: '1', a: 4})); | ||
var a1 = [1, 2, 3]; | ||
var a2 = [1, 2, 3]; | ||
a1.a = 'test'; | ||
a1.b = true; | ||
a2.b = true; | ||
a2.a = 'test'; | ||
assert.throws(makeBlock(assert.deepEqual, keys(a1), keys(a2)), | ||
assert.AssertionError); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, a1, a2)); | ||
}); | ||
var nb1 = new nameBuilder('Ryan', 'Dahl'); | ||
var nb2 = new nameBuilder2('Ryan', 'Dahl'); | ||
test('assert.deepEqual - ES6 primitives', function () { | ||
assert.throws(makeBlock(assert.deepEqual, null, {}), assert.AssertionError); | ||
assert.throws(makeBlock(assert.deepEqual, undefined, {}), assert.AssertionError); | ||
assert.throws(makeBlock(assert.deepEqual, 'a', ['a']), assert.AssertionError); | ||
assert.throws(makeBlock(assert.deepEqual, 'a', {0: 'a'}), assert.AssertionError); | ||
assert.throws(makeBlock(assert.deepEqual, 1, {}), assert.AssertionError); | ||
assert.throws(makeBlock(assert.deepEqual, true, {}), assert.AssertionError); | ||
if (typeof Symbol === 'symbol') { | ||
assert.throws(makeBlock(assert.deepEqual, Symbol(), {}), assert.AssertionError); | ||
} | ||
}); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, nb1, nb2)); | ||
test('assert.deepEqual - object wrappers', function () { | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, new String('a'), ['a'])); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, new String('a'), {0: 'a'})); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, new Number(1), {})); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, new Boolean(true), {})); | ||
}); | ||
nameBuilder2.prototype = Object; | ||
nb2 = new nameBuilder2('Ryan', 'Dahl'); | ||
assert.throws(makeBlock(assert.deepEqual, nb1, nb2), assert.AssertionError); | ||
}); | ||
function thrower(errorConstructor) { | ||
throw new errorConstructor('test'); | ||
} | ||
test('assert.deepEqual - ES6 primitives', function () { | ||
assert.throws(makeBlock(assert.deepEqual, null, {}), assert.AssertionError); | ||
assert.throws(makeBlock(assert.deepEqual, undefined, {}), assert.AssertionError); | ||
assert.throws(makeBlock(assert.deepEqual, 'a', ['a']), assert.AssertionError); | ||
assert.throws(makeBlock(assert.deepEqual, 'a', {0: 'a'}), assert.AssertionError); | ||
assert.throws(makeBlock(assert.deepEqual, 1, {}), assert.AssertionError); | ||
assert.throws(makeBlock(assert.deepEqual, true, {}), assert.AssertionError); | ||
if (typeof Symbol === 'symbol') { | ||
assert.throws(makeBlock(assert.deepEqual, Symbol(), {}), assert.AssertionError); | ||
} | ||
}); | ||
test('assert - Testing the throwing', function () { | ||
var aethrow = makeBlock(thrower, assert.AssertionError); | ||
aethrow = makeBlock(thrower, assert.AssertionError); | ||
test('assert.deepEqual - object wrappers', function () { | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, new String('a'), ['a'])); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, new String('a'), {0: 'a'})); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, new Number(1), {})); | ||
assert.doesNotThrow(makeBlock(assert.deepEqual, new Boolean(true), {})); | ||
}); | ||
// the basic calls work | ||
assert.throws(makeBlock(thrower, assert.AssertionError), | ||
assert.AssertionError, 'message'); | ||
assert.throws(makeBlock(thrower, assert.AssertionError), assert.AssertionError); | ||
assert.throws(makeBlock(thrower, assert.AssertionError)); | ||
function thrower(errorConstructor) { | ||
throw new errorConstructor('test'); | ||
} | ||
// if not passing an error, catch all. | ||
assert.throws(makeBlock(thrower, TypeError)); | ||
test('assert - Testing the throwing', function () { | ||
var aethrow = makeBlock(thrower, assert.AssertionError); | ||
aethrow = makeBlock(thrower, assert.AssertionError); | ||
// when passing a type, only catch errors of the appropriate type | ||
var threw = false; | ||
try { | ||
assert.throws(makeBlock(thrower, TypeError), assert.AssertionError); | ||
} catch (e) { | ||
threw = true; | ||
assert.ok(e instanceof TypeError, 'type'); | ||
} | ||
assert.equal(true, threw, | ||
'a.throws with an explicit error is eating extra errors', | ||
assert.AssertionError); | ||
threw = false; | ||
// the basic calls work | ||
assert.throws(makeBlock(thrower, assert.AssertionError), | ||
assert.AssertionError, 'message'); | ||
assert.throws(makeBlock(thrower, assert.AssertionError), assert.AssertionError); | ||
assert.throws(makeBlock(thrower, assert.AssertionError)); | ||
// doesNotThrow should pass through all errors | ||
try { | ||
assert.doesNotThrow(makeBlock(thrower, TypeError), assert.AssertionError); | ||
} catch (e) { | ||
threw = true; | ||
assert.ok(e instanceof TypeError); | ||
} | ||
assert.equal(true, threw, | ||
'a.doesNotThrow with an explicit error is eating extra errors'); | ||
// if not passing an error, catch all. | ||
assert.throws(makeBlock(thrower, TypeError)); | ||
// key difference is that throwing our correct error makes an assertion error | ||
try { | ||
assert.doesNotThrow(makeBlock(thrower, TypeError), TypeError); | ||
} catch (e) { | ||
threw = true; | ||
assert.ok(e instanceof assert.AssertionError); | ||
} | ||
assert.equal(true, threw, | ||
'a.doesNotThrow is not catching type matching errors'); | ||
}); | ||
// when passing a type, only catch errors of the appropriate type | ||
var threw = false; | ||
try { | ||
assert.throws(makeBlock(thrower, TypeError), assert.AssertionError); | ||
} catch (e) { | ||
threw = true; | ||
assert.ok(e instanceof TypeError, 'type'); | ||
} | ||
assert.equal(true, threw, | ||
'a.throws with an explicit error is eating extra errors', | ||
assert.AssertionError); | ||
threw = false; | ||
test('assert.ifError', function () { | ||
assert.throws(function() {assert.ifError(new Error('test error'))}); | ||
assert.doesNotThrow(function() {assert.ifError(null)}); | ||
assert.doesNotThrow(function() {assert.ifError()}); | ||
}); | ||
// doesNotThrow should pass through all errors | ||
try { | ||
assert.doesNotThrow(makeBlock(thrower, TypeError), assert.AssertionError); | ||
} catch (e) { | ||
threw = true; | ||
assert.ok(e instanceof TypeError); | ||
} | ||
assert.equal(true, threw, | ||
'a.doesNotThrow with an explicit error is eating extra errors'); | ||
test('assert - make sure that validating using constructor really works', function () { | ||
var threw = false; | ||
try { | ||
assert.throws( | ||
function() { | ||
throw ({}); | ||
}, | ||
Array | ||
); | ||
} catch (e) { | ||
threw = true; | ||
} | ||
assert.ok(threw, 'wrong constructor validation'); | ||
}); | ||
// key difference is that throwing our correct error makes an assertion error | ||
try { | ||
assert.doesNotThrow(makeBlock(thrower, TypeError), TypeError); | ||
} catch (e) { | ||
threw = true; | ||
assert.ok(e instanceof assert.AssertionError); | ||
} | ||
assert.equal(true, threw, | ||
'a.doesNotThrow is not catching type matching errors'); | ||
}); | ||
test('assert - use a RegExp to validate error message', function () { | ||
assert.throws(makeBlock(thrower, TypeError), /test/); | ||
}); | ||
test('assert.ifError', function () { | ||
assert.throws(function() {assert.ifError(new Error('test error'))}); | ||
assert.doesNotThrow(function() {assert.ifError(null)}); | ||
assert.doesNotThrow(function() {assert.ifError()}); | ||
}); | ||
test('assert - se a fn to validate error object', function () { | ||
assert.throws(makeBlock(thrower, TypeError), function(err) { | ||
if ((err instanceof TypeError) && /test/.test(err)) { | ||
return true; | ||
} | ||
}); | ||
}); | ||
test('assert - make sure that validating using constructor really works', function () { | ||
var threw = false; | ||
try { | ||
assert.throws( | ||
function() { | ||
throw ({}); | ||
}, | ||
Array | ||
); | ||
} catch (e) { | ||
threw = true; | ||
} | ||
assert.ok(threw, 'wrong constructor validation'); | ||
}); | ||
test('assert - Make sure deepEqual doesn\'t loop forever on circular refs', function () { | ||
var b = {}; | ||
b.b = b; | ||
test('assert - use a RegExp to validate error message', function () { | ||
assert.throws(makeBlock(thrower, TypeError), /test/); | ||
}); | ||
var c = {}; | ||
c.b = c; | ||
test('assert - se a fn to validate error object', function () { | ||
assert.throws(makeBlock(thrower, TypeError), function(err) { | ||
if ((err instanceof TypeError) && /test/.test(err)) { | ||
return true; | ||
} | ||
}); | ||
}); | ||
var gotError = false; | ||
var equal = true; | ||
try { | ||
equal = assert.deepEqual(b, c); | ||
} catch (e) { | ||
gotError = true; | ||
} | ||
assert.ok(gotError || !equal, gotError ? 'got error': 'are equal'); | ||
}); | ||
test('assert - Make sure deepEqual doesn\'t loop forever on circular refs', function () { | ||
var b = {}; | ||
b.b = b; | ||
test('assert - Ensure reflexivity of deepEqual with `arguments` objects', function() { | ||
var args = (function() { return arguments; })(); | ||
assert.throws(makeBlock(assert.deepEqual, [], args), assert.AssertionError); | ||
assert.throws(makeBlock(assert.deepEqual, args, []), assert.AssertionError); | ||
}); | ||
var c = {}; | ||
c.b = c; | ||
test('assert - test assertion message', function () { | ||
function testAssertionMessage(actual, expected) { | ||
try { | ||
assert.equal(actual, ''); | ||
} catch (e) { | ||
assert.equal(e.toString(), | ||
['AssertionError:', expected, '==', '\'\''].join(' ')); | ||
} | ||
} | ||
testAssertionMessage(undefined, 'undefined'); | ||
testAssertionMessage(null, 'null'); | ||
testAssertionMessage(true, 'true'); | ||
testAssertionMessage(false, 'false'); | ||
testAssertionMessage(0, '0'); | ||
testAssertionMessage(100, '100'); | ||
testAssertionMessage(NaN, 'NaN'); | ||
testAssertionMessage(Infinity, 'Infinity'); | ||
testAssertionMessage(-Infinity, '-Infinity'); | ||
testAssertionMessage('', '""'); | ||
testAssertionMessage('foo', '\'foo\''); | ||
testAssertionMessage([], '[]'); | ||
testAssertionMessage([1, 2, 3], '[ 1, 2, 3 ]'); | ||
testAssertionMessage(/a/, '/a/'); | ||
testAssertionMessage(function f() {}, '[Function: f]'); | ||
testAssertionMessage({}, '{}'); | ||
testAssertionMessage({a: undefined, b: null}, '{ a: undefined, b: null }'); | ||
testAssertionMessage({a: NaN, b: Infinity, c: -Infinity}, | ||
'{ a: NaN, b: Infinity, c: -Infinity }'); | ||
}); | ||
var gotError = false; | ||
try { | ||
assert.deepEqual(b, c); | ||
} catch (e) { | ||
gotError = true; | ||
} | ||
test('assert - regressions from node.js testcase', function () { | ||
var threw = false; | ||
assert.ok(gotError); | ||
}); | ||
try { | ||
assert.throws(function () { | ||
assert.ifError(null); | ||
}); | ||
} catch (e) { | ||
threw = true; | ||
assert.equal(e.message, 'Missing expected exception..'); | ||
} | ||
assert.ok(threw); | ||
test('assert - Ensure reflexivity of deepEqual with `arguments` objects', function() { | ||
var args = (function() { return arguments; })(); | ||
assert.throws(makeBlock(assert.deepEqual, [], args), assert.AssertionError); | ||
assert.throws(makeBlock(assert.deepEqual, args, []), assert.AssertionError); | ||
}); | ||
try { | ||
assert.equal(1, 2); | ||
} catch (e) { | ||
assert.equal(e.toString().split('\n')[0], 'AssertionError: 1 == 2'); | ||
} | ||
test('assert - test assertion message', function () { | ||
function testAssertionMessage(actual, expected) { | ||
try { | ||
assert.equal(actual, ''); | ||
} catch (e) { | ||
assert.equal(e.toString(), | ||
['AssertionError:', expected, '==', '""'].join(' ')); | ||
} | ||
} | ||
testAssertionMessage(undefined, '"undefined"'); | ||
testAssertionMessage(null, 'null'); | ||
testAssertionMessage(true, 'true'); | ||
testAssertionMessage(false, 'false'); | ||
testAssertionMessage(0, '0'); | ||
testAssertionMessage(100, '100'); | ||
testAssertionMessage(NaN, '"NaN"'); | ||
testAssertionMessage(Infinity, '"Infinity"'); | ||
testAssertionMessage(-Infinity, '"-Infinity"'); | ||
testAssertionMessage('', '""'); | ||
testAssertionMessage('foo', '"foo"'); | ||
testAssertionMessage([], '[]'); | ||
testAssertionMessage([1, 2, 3], '[1,2,3]'); | ||
testAssertionMessage(/a/, '"/a/"'); | ||
testAssertionMessage(function f() {}, '"function f() {}"'); | ||
testAssertionMessage({}, '{}'); | ||
testAssertionMessage({a: undefined, b: null}, '{"a":"undefined","b":null}'); | ||
testAssertionMessage({a: NaN, b: Infinity, c: -Infinity}, | ||
'{"a":"NaN","b":"Infinity","c":"-Infinity"}'); | ||
}); | ||
test('assert - regressions from node.js testcase', function () { | ||
var threw = false; | ||
try { | ||
assert.throws(function () { | ||
assert.ifError(null); | ||
try { | ||
assert.equal(1, 2, 'oh no'); | ||
} catch (e) { | ||
assert.equal(e.toString().split('\n')[0], 'AssertionError: oh no'); | ||
} | ||
}); | ||
} catch (e) { | ||
threw = true; | ||
assert.equal(e.message, 'Missing expected exception..'); | ||
} | ||
assert.ok(threw); | ||
try { | ||
assert.equal(1, 2); | ||
} catch (e) { | ||
assert.equal(e.toString().split('\n')[0], 'AssertionError: 1 == 2'); | ||
} | ||
try { | ||
assert.equal(1, 2, 'oh no'); | ||
} catch (e) { | ||
assert.equal(e.toString().split('\n')[0], 'AssertionError: oh no'); | ||
} | ||
}); | ||
} |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
32865
683
2
1
1
+ Addedbuffer-shims@1.0.0
+ Addedbuffer-shims@1.0.0(transitive)