Browse Source

Fix assert.js code style

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
dd35637603
  1. 127
      lib/assert.js
  2. 119
      test/mjsunit/test-assert.js

127
lib/assert.js

@ -41,15 +41,16 @@ assert.AssertionError = function AssertionError(options) {
this.actual = options.actual; this.actual = options.actual;
this.expected = options.expected; this.expected = options.expected;
this.operator = options.operator; this.operator = options.operator;
//v8 specific //v8 specific
if(Error.captureStackTrace){ if (Error.captureStackTrace) {
Error.captureStackTrace(this,fail); Error.captureStackTrace(this, fail);
//node specific, removes the node machinery stack frames //node specific, removes the node machinery stack frames
if(typeof(__filename) !== undefined){ if (typeof(__filename) !== undefined) {
var stack = this.stack.split('\n'); var stack = this.stack.split('\n');
for (var i = stack.length - 1; i >= 0; i--) { for (var i = stack.length - 1; i >= 0; i--) {
if(stack[i].indexOf(__filename) != -1){ if (stack[i].indexOf(__filename) != -1) {
this.stack = stack.slice(0,i+2).join('\n'); this.stack = stack.slice(0, i+2).join('\n');
break; break;
} }
} }
@ -57,11 +58,16 @@ assert.AssertionError = function AssertionError(options) {
} }
}; };
assert.AssertionError.prototype.toString = function(){ assert.AssertionError.prototype.toString = function() {
if(this.message) if (this.message) {
return [this.name+":", this.message].join(' '); return [this.name+":", this.message].join(' ');
else } else {
return [this.name+":", JSON.stringify(this.expected ), this.operator , JSON.stringify(this.actual)].join(" "); return [ this.name+":"
, JSON.stringify(this.expected )
, this.operator
, JSON.stringify(this.actual)
].join(" ");
}
} }
// assert.AssertionError instanceof Error // assert.AssertionError instanceof Error
@ -99,8 +105,7 @@ assert.fail = fail;
// assert.strictEqual(true, guard, message_opt);. // assert.strictEqual(true, guard, message_opt);.
assert.ok = function ok(value, message) { assert.ok = function ok(value, message) {
if (!!!value) if (!!!value) fail(value, true, message, "==");
fail(value, true, message, "==");
}; };
// 5. The equality assertion tests shallow, coercive equality with // 5. The equality assertion tests shallow, coercive equality with
@ -108,25 +113,23 @@ assert.ok = function ok(value, message) {
// assert.equal(actual, expected, message_opt); // assert.equal(actual, expected, message_opt);
assert.equal = function equal(actual, expected, message) { assert.equal = function equal(actual, expected, message) {
if (actual != expected) if (actual != expected) fail(actual, expected, message, "==");
fail(actual, expected, message, "==");
}; };
// 6. The non-equality assertion tests for whether two objects are not equal // 6. The non-equality assertion tests for whether two objects are not equal
// with != assert.notEqual(actual, expected, message_opt); // with != assert.notEqual(actual, expected, message_opt);
assert.notEqual = function notEqual(actual, expected, message) { assert.notEqual = function notEqual(actual, expected, message) {
if (actual == expected) if (actual == expected) fail(actual, expected, message, "!=");
fail(actual, expected, message, "!=");
}; };
// 7. The equivalence assertion tests a deep equality relation. // 7. The equivalence assertion tests a deep equality relation.
// assert.deepEqual(actual, expected, message_opt); // assert.deepEqual(actual, expected, message_opt);
exports.deepEqual = function deepEqual(actual, expected, message) { exports.deepEqual = function deepEqual(actual, expected, message) {
if (!_deepEqual(actual, expected)) if (!_deepEqual(actual, expected)) {
fail(actual, expected, message, "deepEqual"); fail(actual, expected, message, "deepEqual");
}
}; };
function _deepEqual(actual, expected) { function _deepEqual(actual, expected) {
@ -136,14 +139,12 @@ function _deepEqual(actual, expected) {
// 7.2. If the expected value is a Date object, the actual value is // 7.2. If the expected value is a Date object, the actual value is
// equivalent if it is also a Date object that refers to the same time. // equivalent if it is also a Date object that refers to the same time.
} else if (actual instanceof Date } else if (actual instanceof Date && expected instanceof Date) {
&& expected instanceof Date) {
return actual.getTime() === expected.getTime(); return actual.getTime() === expected.getTime();
// 7.3. Other pairs that do not both pass typeof value == "object", // 7.3. Other pairs that do not both pass typeof value == "object",
// equivalence is determined by ==. // equivalence is determined by ==.
} else if (typeof actual != 'object' } else if (typeof actual != 'object' && typeof expected != 'object') {
&& typeof expected != 'object') {
return actual == expected; return actual == expected;
// 7.4. For all other Object pairs, including Array objects, equivalence is // 7.4. For all other Object pairs, including Array objects, equivalence is
@ -161,7 +162,7 @@ function isUndefinedOrNull (value) {
return value === null || value === undefined; return value === null || value === undefined;
} }
function isArguments (object){ function isArguments (object) {
return Object.prototype.toString.call(object) == '[object Arguments]'; return Object.prototype.toString.call(object) == '[object Arguments]';
} }
@ -172,13 +173,13 @@ function objEquiv (a, b) {
if (a.prototype !== b.prototype) return false; if (a.prototype !== b.prototype) return false;
//~~~I've managed to break Object.keys through screwy arguments passing. //~~~I've managed to break Object.keys through screwy arguments passing.
// Converting to array solves the problem. // Converting to array solves the problem.
if(isArguments(a)){ if (isArguments(a)) {
if(!isArguments(b)){ if (!isArguments(b)) {
return false; return false;
} }
a = pSlice.call(a); a = pSlice.call(a);
b = pSlice.call(b); b = pSlice.call(b);
return _deepEqual(a,b); return _deepEqual(a, b);
} }
try{ try{
var ka = Object.keys(a), var ka = Object.keys(a),
@ -188,21 +189,21 @@ function objEquiv (a, b) {
return false; return false;
} }
// having the same number of owned properties (keys incorporates hasOwnProperty) // having the same number of owned properties (keys incorporates hasOwnProperty)
if(ka.length != kb.length) if (ka.length != kb.length)
return false; return false;
//the same set of keys (although not necessarily the same order), //the same set of keys (although not necessarily the same order),
ka.sort(); ka.sort();
kb.sort(); kb.sort();
//~~~cheap key test //~~~cheap key test
for (i = ka.length - 1; i >= 0; i--) { for (i = ka.length - 1; i >= 0; i--) {
if(ka[i] != kb[i]) if (ka[i] != kb[i])
return false; return false;
} }
//equivalent values for every corresponding key, and //equivalent values for every corresponding key, and
//~~~possibly expensive deep test //~~~possibly expensive deep test
for (i = ka.length - 1; i >= 0; i--) { for (i = ka.length - 1; i >= 0; i--) {
key = ka[i]; key = ka[i];
if(!_deepEqual(a[key], b[key] )) if (!_deepEqual(a[key], b[key] ))
return false; return false;
} }
return true; return true;
@ -212,53 +213,39 @@ function objEquiv (a, b) {
// assert.notDeepEqual(actual, expected, message_opt); // assert.notDeepEqual(actual, expected, message_opt);
exports.notDeepEqual = function notDeepEqual(actual, expected, message) { exports.notDeepEqual = function notDeepEqual(actual, expected, message) {
if (_deepEqual(actual, expected)) if (_deepEqual(actual, expected)) {
fail(actual, expected, message, "notDeepEqual"); fail(actual, expected, message, "notDeepEqual");
}
}; };
// 9. The strict equality assertion tests strict equality, as determined by ===. // 9. The strict equality assertion tests strict equality, as determined by ===.
// assert.strictEqual(actual, expected, message_opt); // assert.strictEqual(actual, expected, message_opt);
assert.strictEqual = function strictEqual(actual, expected, message) { assert.strictEqual = function strictEqual(actual, expected, message) {
if (actual !== expected) if (actual !== expected) fail(actual, expected, message, "===");
fail(actual, expected, message, "===");
}; };
// 10. The strict non-equality assertion tests for strict inequality, as determined by !==. // 10. The strict non-equality assertion tests for strict inequality, as determined by !==.
// assert.notStrictEqual(actual, expected, message_opt); // assert.notStrictEqual(actual, expected, message_opt);
assert.notStrictEqual = function notStrictEqual(actual, expected, message) { assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
if (actual === expected) if (actual === expected) fail(actual, expected, message, "!==");
fail(actual, expected, message, "!==");
}; };
// 11. Expected to throw an error: function _throws (shouldThrow, block, err, message) {
// assert.throws(block, Error_opt, message_opt); var exception = null,
threw = false,
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; typematters = true;
message = message || ""; message = message || "";
//handle optional arguments //handle optional arguments
if(arguments.length == 3){ if (arguments.length == 3) {
if(typeof(err) == "string"){ if (typeof(err) == "string") {
message = err; message = err;
typematters = false; typematters = false;
} }
} else if(arguments.length == 2) { } else if (arguments.length == 2) {
typematters = false; typematters = false;
} }
@ -269,18 +256,32 @@ var _throws = function (shouldThrow, block, err, message) {
exception = e; exception = e;
} }
if(shouldThrow && !threw){ if (shouldThrow && !threw) {
fail("Missing expected exception"+ fail( "Missing expected exception"
(err && err.name ? " ("+err.name+")." : '.') + + (err && err.name ? " ("+err.name+")." : '.')
(message ? " " + message : "")); + (message ? " " + message : "")
);
} }
if(!shouldThrow && threw && typematters && exception instanceof err){ if (!shouldThrow && threw && typematters && exception instanceof err) {
fail("Got unwanted exception" + fail( "Got unwanted exception"
(err && err.name ? " ("+err.name+")." : '.') + + (err && err.name ? " ("+err.name+")." : '.')
(message ? " " + message : "")); + (message ? " " + message : "")
);
} }
if((shouldThrow && threw && typematters && !(exception instanceof err)) || if ((shouldThrow && threw && typematters && !(exception instanceof err)) ||
(!shouldThrow && threw)){ (!shouldThrow && threw)) {
throw exception; throw exception;
} }
}
// 11. Expected to throw an error:
// assert.throws(block, Error_opt, message_opt);
assert.throws = function(block, /*optional*/error, /*optional*/message) {
_throws.apply(this, [true].concat(pSlice.call(arguments)));
};
// EXTENSION! This is annoying to write outside this module.
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
_throws.apply(this, [false].concat(pSlice.call(arguments)));
}; };

119
test/mjsunit/test-assert.js

@ -1,52 +1,69 @@
process.mixin(require("./common")); process.mixin(require("./common"));
//strangely meta, no?
var a = require('assert'); var a = require('assert');
function makeBlock (f) {
function makeBlock(f){
var args = Array.prototype.slice.call(arguments,1); var args = Array.prototype.slice.call(arguments,1);
return function(){ return function () {
return f.apply(this,args); return f.apply(this,args);
} };
} }
assert.ok(a.AssertionError instanceof Error, "a.AssertionError instanceof Error"); assert.ok(a.AssertionError instanceof Error,
"a.AssertionError instanceof Error");
assert.throws(makeBlock(a.ok, false),
a.AssertionError, "ok(false)");
assert.throws(makeBlock(a.ok, false), a.AssertionError, "ok(false)"); assert.doesNotThrow(makeBlock(a.ok, true),
assert.doesNotThrow(makeBlock(a.ok, true), a.AssertionError, "ok(true)"); a.AssertionError, "ok(true)");
assert.doesNotThrow(makeBlock(a.ok, "test"),"ok('test')");
assert.doesNotThrow(makeBlock(a.ok, "test"), "ok('test')");
assert.throws(makeBlock(a.equal, true, false), a.AssertionError, 'equal'); assert.throws(makeBlock(a.equal, true, false), a.AssertionError, 'equal');
assert.doesNotThrow(makeBlock(a.equal, null, null), 'equal'); assert.doesNotThrow(makeBlock(a.equal, null, null), 'equal');
assert.doesNotThrow(makeBlock(a.equal, undefined, undefined), 'equal'); assert.doesNotThrow(makeBlock(a.equal, undefined, undefined), 'equal');
assert.doesNotThrow(makeBlock(a.equal, null, undefined), 'equal'); assert.doesNotThrow(makeBlock(a.equal, null, undefined), 'equal');
assert.doesNotThrow(makeBlock(a.equal, true, true), 'equal'); assert.doesNotThrow(makeBlock(a.equal, true, true), 'equal');
assert.doesNotThrow(makeBlock(a.equal, 2, "2"), 'equal'); assert.doesNotThrow(makeBlock(a.equal, 2, "2"), 'equal');
assert.doesNotThrow(makeBlock(a.notEqual, true, false), 'notEqual'); assert.doesNotThrow(makeBlock(a.notEqual, true, false), 'notEqual');
assert.throws(makeBlock(a.notEqual, true, true), a.AssertionError, 'notEqual'); assert.throws(makeBlock(a.notEqual, true, true), a.AssertionError, 'notEqual');
assert.throws(makeBlock(a.strictEqual, 2, "2"), a.AssertionError, 'strictEqual'); assert.throws(makeBlock(a.strictEqual, 2, "2"), a.AssertionError, 'strictEqual');
assert.throws(makeBlock(a.strictEqual, null, undefined), a.AssertionError, 'strictEqual'); assert.throws(makeBlock(a.strictEqual, null, undefined), a.AssertionError, 'strictEqual');
assert.doesNotThrow(makeBlock(a.notStrictEqual, 2, "2"), 'notStrictEqual'); assert.doesNotThrow(makeBlock(a.notStrictEqual, 2, "2"), 'notStrictEqual');
//deepEquals joy! // deepEquals joy!
//7.2 // 7.2
assert.doesNotThrow(makeBlock(a.deepEqual, new Date(2000,3,14), new Date(2000,3,14)), 'deepEqual date'); assert.doesNotThrow(makeBlock(a.deepEqual, new Date(2000,3,14), new Date(2000,3,14)),
assert.throws( makeBlock(a.deepEqual, new Date(), new Date(2000,3,14)), a.AssertionError, 'deepEqual date'); 'deepEqual date');
//7.3
assert.throws(makeBlock(a.deepEqual, new Date(), new Date(2000,3,14)),
a.AssertionError,
'deepEqual date');
// 7.3
assert.doesNotThrow(makeBlock(a.deepEqual, 4, "4"), 'deepEqual == check'); assert.doesNotThrow(makeBlock(a.deepEqual, 4, "4"), 'deepEqual == check');
assert.doesNotThrow(makeBlock(a.deepEqual, true, 1), 'deepEqual == check'); assert.doesNotThrow(makeBlock(a.deepEqual, true, 1), 'deepEqual == check');
assert.throws( makeBlock(a.deepEqual, 4, "5"), a.AssertionError, 'deepEqual == check'); assert.throws(makeBlock(a.deepEqual, 4, "5"),
//7.4 a.AssertionError,
'deepEqual == check');
// 7.4
// having the same number of owned properties && the same set of keys // having the same number of owned properties && the same set of keys
assert.doesNotThrow(makeBlock(a.deepEqual, {a:4}, {a:4})); assert.doesNotThrow(makeBlock(a.deepEqual, {a:4}, {a:4}));
assert.doesNotThrow(makeBlock(a.deepEqual, {a:4,b:"2"}, {a:4,b:"2"})); assert.doesNotThrow(makeBlock(a.deepEqual, {a:4,b:"2"}, {a:4,b:"2"}));
assert.doesNotThrow(makeBlock(a.deepEqual, [4], ["4"])); assert.doesNotThrow(makeBlock(a.deepEqual, [4], ["4"]));
assert.throws( makeBlock(a.deepEqual, {a:4}, {a:4,b:true}), a.AssertionError); assert.throws(makeBlock(a.deepEqual, {a:4}, {a:4,b:true}), a.AssertionError);
assert.doesNotThrow(makeBlock(a.deepEqual, ["a"], {0:"a"})); assert.doesNotThrow(makeBlock(a.deepEqual, ["a"], {0:"a"}));
//(although not necessarily the same order), //(although not necessarily the same order),
assert.doesNotThrow(makeBlock(a.deepEqual, {a:4,b:"1"}, {b:"1",a:4})); assert.doesNotThrow(makeBlock(a.deepEqual, {a:4,b:"1"}, {b:"1",a:4}));
@ -55,26 +72,32 @@ var a2 = [1,2,3];
a1.a = "test"; a1.a = "test";
a1.b = true; a1.b = true;
a2.b = true; a2.b = true;
a2.a = "test" a2.a = "test";
assert.throws( makeBlock(a.deepEqual, Object.keys(a1), Object.keys(a2)), a.AssertionError); assert.throws(makeBlock(a.deepEqual,
Object.keys(a1),
Object.keys(a2)),
a.AssertionError);
assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2)); assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2));
// having an identical prototype property // having an identical prototype property
var nbRoot = { var nbRoot = {
toString: function(){return this.first+' '+this.last;} toString: function () { return this.first+' '+this.last; }
} };
var nameBuilder = function(first,last){
function nameBuilder (first,last) {
this.first = first; this.first = first;
this.last = last; this.last = last;
return this; return this;
} }
nameBuilder.prototype = nbRoot; nameBuilder.prototype = nbRoot;
var nameBuilder2 = function(first,last){
function nameBuilder2 (first,last) {
this.first = first; this.first = first;
this.last = last; this.last = last;
return this; return this;
} }
nameBuilder2.prototype = nbRoot; nameBuilder2.prototype = nbRoot;
var nb1 = new nameBuilder('Ryan', 'Dahl'); var nb1 = new nameBuilder('Ryan', 'Dahl');
var nb2 = new nameBuilder2('Ryan','Dahl'); var nb2 = new nameBuilder2('Ryan','Dahl');
@ -82,46 +105,52 @@ assert.doesNotThrow(makeBlock(a.deepEqual, nb1, nb2));
nameBuilder2.prototype = Object; nameBuilder2.prototype = Object;
nb2 = new nameBuilder2('Ryan','Dahl'); nb2 = new nameBuilder2('Ryan','Dahl');
assert.throws( makeBlock(a.deepEqual, nb1, nb2), a.AssertionError); assert.throws(makeBlock(a.deepEqual, nb1, nb2), a.AssertionError);
//String literal + object blew up my implementation... // String literal + object blew up my implementation...
assert.throws( makeBlock(a.deepEqual, 'a', {}), a.AssertionError); assert.throws(makeBlock(a.deepEqual, 'a', {}), a.AssertionError);
//Testing the throwing // Testing the throwing
function thrower(errorConstructor){ function thrower (errorConstructor){
throw new errorConstructor('test'); throw new errorConstructor('test');
} }
aethrow = makeBlock(thrower, a.AssertionError); aethrow = makeBlock(thrower, a.AssertionError);
aethrow = makeBlock(thrower, a.AssertionError); aethrow = makeBlock(thrower, a.AssertionError);
//the basic calls work
assert.throws( makeBlock(thrower, a.AssertionError), a.AssertionError, 'message'); // the basic calls work
assert.throws( makeBlock(thrower, a.AssertionError), a.AssertionError); assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError, 'message');
assert.throws( makeBlock(thrower, a.AssertionError)); assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError);
//if not passing an error, catch all. assert.throws(makeBlock(thrower, a.AssertionError));
assert.throws( makeBlock(thrower, TypeError));
//when passing a type, only catch errors of the appropriate type // if not passing an error, catch all.
assert.throws(makeBlock(thrower, TypeError));
// when passing a type, only catch errors of the appropriate type
var threw = false; var threw = false;
try{ try {
a.throws( makeBlock(thrower, TypeError), a.AssertionError); a.throws(makeBlock(thrower, TypeError), a.AssertionError);
}catch (e){ } catch (e) {
threw = true; threw = true;
assert.ok(e instanceof TypeError, 'type'); assert.ok(e instanceof TypeError, 'type');
} }
assert.equal(true,threw,'a.throws with an explicit error is eating extra errors', a.AssertionError); assert.equal(true,threw,'a.throws with an explicit error is eating extra errors', a.AssertionError);
threw = false; threw = false;
//doesNotThrow should pass through all errors
try{ // doesNotThrow should pass through all errors
try {
a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError); a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError);
}catch(e){ } catch(e) {
threw = true threw = true
assert.ok(e instanceof TypeError); assert.ok(e instanceof TypeError);
} }
assert.equal(true,threw,'a.doesNotThrow with an explicit error is eating extra errors'); assert.equal(true,threw,'a.doesNotThrow with an explicit error is eating extra errors');
//the key difference is that throwing our correct error makes an assertion error
try{ // key difference is that throwing our correct error makes an assertion error
try {
a.doesNotThrow(makeBlock(thrower, TypeError), TypeError); a.doesNotThrow(makeBlock(thrower, TypeError), TypeError);
}catch(e){ } catch(e) {
threw = true threw = true
assert.ok(e instanceof a.AssertionError); assert.ok(e instanceof a.AssertionError);
} }
assert.equal(true,threw,'a.doesNotThrow is not catching type matching errors'); assert.equal(true,threw,'a.doesNotThrow is not catching type matching errors');

Loading…
Cancel
Save