Skip to content

Commit

Permalink
Dependency free assert module with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grayrest authored and ry committed Dec 5, 2009
1 parent 530328f commit 4f679fd
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 1,084 deletions.
210 changes: 160 additions & 50 deletions lib/assert.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,72 @@
var sys = require('sys');
var util = require('util');
// http://wiki.commonjs.org/wiki/Unit_Testing/1.0
//
// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
//
// Originally from narwhal.js (http://narwhaljs.org)
// Copyright (c) 2009 Thomas Robinson <280north.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the “Software”), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// UTILITY

var pSlice = Array.prototype.slice;

// 1. The assert module provides functions that throw
// AssertionError's when particular conditions are not met. The
// assert module must conform to the following interface.

var assert = exports;

assert.AssertionError = function (options) {
// 2. The AssertionError is defined in assert.
// new assert.AssertionError({message: message, actual: actual, expected: expected})

assert.AssertionError = function AssertionError(options) {
this.name = "AssertionError";
this.message = options.message;
this.actual = options.actual;
this.expected = options.expected;
this.operator = options.operator;

Error.captureStackTrace(this, fail);
//v8 specific
if(Error.captureStackTrace){
Error.captureStackTrace(this,fail);
//node specific, removes the node machinery stack frames
if(typeof(__filename) !== undefined){
var stack = this.stack.split('\n');
for (var i = stack.length - 1; i >= 0; i--) {
if(stack[i].indexOf(__filename) != -1){
this.stack = stack.slice(0,i+2).join('\n');
break;
}
}
}
}
};
sys.inherits(assert.AssertionError, Error);

assert.AssertionError.prototype.toString = function(){
if(this.message)
return [this.name+":", this.message].join(' ');
else
return [this.name+":", JSON.stringify(this.expected ), this.operator , JSON.stringify(this.actual)].join(" ");
}

// assert.AssertionError instanceof Error

// assert.AssertionError.prototype = Object.create(Error.prototype);
assert.AssertionError.__proto__ = Error.prototype;

// At present only the three keys mentioned above are used and
// understood by the spec. Implementations or sub modules can pass
Expand All @@ -37,14 +88,17 @@ function fail(actual, expected, message, operator) {
});
}

// EXTENSION! allows for well behaved errors defined elsewhere.
assert.fail = fail;

// 4. Pure assertion tests whether a value is truthy, as determined
// by !!guard.
// assert.ok(guard, message_opt);
// This statement is equivalent to assert.equal(true, guard,
// message_opt);. To test strictly for the value true, use
// assert.strictEqual(true, guard, message_opt);.

assert.ok = function (value, message) {
assert.ok = function ok(value, message) {
if (!!!value)
fail(value, true, message, "==");
};
Expand All @@ -53,7 +107,7 @@ assert.ok = function (value, message) {
// ==.
// assert.equal(actual, expected, message_opt);

assert.equal = function (actual, expected, message) {
assert.equal = function equal(actual, expected, message) {
if (actual != expected)
fail(actual, expected, message, "==");
};
Expand All @@ -62,21 +116,20 @@ assert.equal = function (actual, expected, message) {
// 6. The non-equality assertion tests for whether two objects are not equal
// with != assert.notEqual(actual, expected, message_opt);

assert.notEqual = function (actual, expected, message) {
assert.notEqual = function notEqual(actual, expected, message) {
if (actual == expected)
fail(actual, expected, message, "!=");
};

// 7. The equivalence assertion tests a deep equality relation.
// assert.deepEqual(actual, expected, message_opt);

exports.deepEqual = function (actual, expected, message) {
if (!deepEqual(actual, expected))
exports.deepEqual = function deepEqual(actual, expected, message) {
if (!_deepEqual(actual, expected))
fail(actual, expected, message, "deepEqual");
};

function deepEqual(actual, expected) {

function _deepEqual(actual, expected) {
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
return true;
Expand All @@ -85,7 +138,7 @@ function deepEqual(actual, expected) {
// equivalent if it is also a Date object that refers to the same time.
} else if (actual instanceof Date
&& expected instanceof Date) {
return actual.toValue() === expected.toValue();
return actual.getTime() === expected.getTime();

// 7.3. Other pairs that do not both pass typeof value == "object",
// equivalence is determined by ==.
Expand All @@ -100,68 +153,113 @@ function deepEqual(actual, expected) {
// corresponding key, and an identical "prototype" property. Note: this
// accounts for both named and indexed properties on Arrays.
} else {
return actual.prototype === expected.prototype && objEquiv(actual, expected);
return objEquiv(actual, expected);
}
}

function objEquiv(a, b, stack) {
return (
!util.no(a) && !util.no(b) &&
arrayEquiv(
util.sort(util.object.keys(a)),
util.sort(util.object.keys(b))
) &&
util.object.keys(a).every(function (key) {
return deepEqual(a[key], b[key], stack);
})
);
function isUndefinedOrNull (value) {
return value === null || value === undefined;
}

function arrayEquiv(a, b, stack) {
return util.isArrayLike(b) &&
a.length == b.length &&
util.zip(a, b).every(util.apply(function (a, b) {
return deepEqual(a, b, stack);
}));
function isArguments (object){
return Object.prototype.toString.call(object) == '[object Arguments]';
}

function objEquiv (a, b) {
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
return false;
// an identical "prototype" property.
if (a.prototype !== b.prototype) return false;
//~~~I've managed to break Object.keys through screwy arguments passing.
// Converting to array solves the problem.
if(isArguments(a)){
if(!isArguments(b)){
return false;
}
a = pSlice.call(a);
b = pSlice.call(b);
return _deepEqual(a,b);
}
try{
var ka = Object.keys(a),
kb = Object.keys(b),
key, i;
} catch (e) {//happens when one is a string literal and the other isn't
return false;
}
// having the same number of owned properties (keys incorporates hasOwnProperty)
if(ka.length != kb.length)
return false;
//the same set of keys (although not necessarily the same order),
ka.sort();
kb.sort();
//~~~cheap key test
for (i = ka.length - 1; i >= 0; i--) {
if(ka[i] != kb[i])
return false;
}
//equivalent values for every corresponding key, and
//~~~possibly expensive deep test
for (i = ka.length - 1; i >= 0; i--) {
key = ka[i];
if(!_deepEqual(a[key], b[key] ))
return false;
}
return true;
}

// 8. The non-equivalence assertion tests for any deep inequality.
// assert.notDeepEqual(actual, expected, message_opt);

exports.notDeepEqual = function (actual, expected, message) {
if (deepEqual(actual, expected))
exports.notDeepEqual = function notDeepEqual(actual, expected, message) {
if (_deepEqual(actual, expected))
fail(actual, expected, message, "notDeepEqual");
};

// 9. The strict equality assertion tests strict equality, as determined by ===.
// assert.strictEqual(actual, expected, message_opt);

assert.strictEqual = function (actual, expected, message) {
assert.strictEqual = function strictEqual(actual, expected, message) {
if (actual !== expected)
fail(actual, expected, message, "===");
};

// 10. The strict non-equality assertion tests for strict inequality, as determined by !==.
// assert.notStrictEqual(actual, expected, message_opt);

assert.notStrictEqual = function (actual, expected, message) {
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
if (actual === expected)
fail(actual, expected, message, "!==");
};

// 11. Expected to throw an error:
// assert.throws(block, Error_opt, message_opt);

assert["throws"] = function (block, Error, message) {
var threw = false,
exception = null;

if (typeof Error == "string") {
message = Error;
Error = undefined;
} else {
Error = message;
message = "";
assert.throws = function(block, /*optional*/error, /*optional*/message){
var args = [true]
_throws.apply(this, args.concat(pSlice.call(arguments)));
}

// EXTENSION! This is annoying to write outside this module.
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message){
var args = [false]
_throws.apply(this, args.concat(pSlice.call(arguments)));
}

var _throws = function (shouldThrow, block, err, message) {
var exception = null
threw = false
typematters = true;
message = message || "";

//handle optional arguments
if(arguments.length == 3){
if(typeof(err) == "string"){
message = err;
typematters = false;
}
} else if(arguments.length == 2) {
typematters = false;
}

try {
Expand All @@ -170,7 +268,19 @@ assert["throws"] = function (block, Error, message) {
threw = true;
exception = e;
}

if (!threw)
fail("Expected exception" + (message ? ": " + message : ""));

if(shouldThrow && !threw){
fail("Missing expected exception"+
(err && err.name ? " ("+err.name+")." : '.') +
(message ? " " + message : ""));
}
if(!shouldThrow && threw && typematters && exception instanceof err){
fail("Got unwanted exception" +
(err && err.name ? " ("+err.name+")." : '.') +
(message ? " " + message : ""));
}
if((shouldThrow && threw && typematters && !(exception instanceof err)) ||
(!shouldThrow && threw)){
throw exception;
}
};
Loading

0 comments on commit 4f679fd

Please sign in to comment.