From a6447e809edb3e867350eff1c9a42bafa71712b8 Mon Sep 17 00:00:00 2001 From: Jake Luer Date: Thu, 1 Mar 2012 19:18:31 -0500 Subject: [PATCH] full support for actual/expected where relevant --- lib/assertion.js | 128 +++++++++++++++++++++++++++++------------------ 1 file changed, 80 insertions(+), 48 deletions(-) diff --git a/lib/assertion.js b/lib/assertion.js index e891a49d2..c48f6ee8e 100644 --- a/lib/assertion.js +++ b/lib/assertion.js @@ -98,15 +98,18 @@ Assertion.includeStack = false; * @api private */ -Assertion.prototype.assert = function (expr, msg, negateMsg, expected) { +Assertion.prototype.assert = function (expr, msg, negateMsg, expected, actual) { + actual = actual || this.obj; var msg = (this.negate ? negateMsg : msg) - , ok = this.negate ? !expr : expr; + , ok = this.negate ? !expr : expr + , act = this.negate ? expected : actual + , exp = this.negate ? actual : expected; if (!ok) { throw new AssertionError({ message: this.msg ? this.msg + ': ' + msg : msg - , actual: this.obj - , expected: expected + , actual: act + , expected: exp , stackStartFunction: (Assertion.includeStack) ? this.assert : this.ssfi }); } @@ -124,8 +127,8 @@ Assertion.prototype.assert = function (expr, msg, negateMsg, expected) { Object.defineProperty(Assertion.prototype, 'inspect', { get: function () { return inspect(this.obj); - }, - configurable: true + } + , configurable: true }); /** @@ -140,8 +143,8 @@ Object.defineProperty(Assertion.prototype, 'inspect', Object.defineProperty(Assertion.prototype, 'to', { get: function () { return this; - }, - configurable: true + } + , configurable: true }); /** @@ -156,8 +159,8 @@ Object.defineProperty(Assertion.prototype, 'to', Object.defineProperty(Assertion.prototype, 'be', { get: function () { return this; - }, - configurable: true + } + , configurable: true }); /** @@ -174,8 +177,8 @@ Object.defineProperty(Assertion.prototype, 'been', { get: function () { this.tense = 'past'; return this; - }, - configurable: true + } + , configurable: true }); /** @@ -190,8 +193,8 @@ Object.defineProperty(Assertion.prototype, 'been', Object.defineProperty(Assertion.prototype, 'an', { get: function () { return this; - }, - configurable: true + } + , configurable: true }); /** * # is @@ -205,8 +208,8 @@ Object.defineProperty(Assertion.prototype, 'an', Object.defineProperty(Assertion.prototype, 'is', { get: function () { return this; - }, - configurable: true + } + , configurable: true }); /** @@ -221,8 +224,8 @@ Object.defineProperty(Assertion.prototype, 'is', Object.defineProperty(Assertion.prototype, 'and', { get: function () { return this; - }, - configurable: true + } + , configurable: true }); /** @@ -237,8 +240,8 @@ Object.defineProperty(Assertion.prototype, 'and', Object.defineProperty(Assertion.prototype, 'have', { get: function () { return this; - }, - configurable: true + } + , configurable: true }); /** @@ -253,8 +256,8 @@ Object.defineProperty(Assertion.prototype, 'have', Object.defineProperty(Assertion.prototype, 'with', { get: function () { return this; - }, - configurable: true + } + , configurable: true }); /** @@ -270,8 +273,8 @@ Object.defineProperty(Assertion.prototype, 'not', { get: function () { this.negate = true; return this; - }, - configurable: true + } + , configurable: true }); /** @@ -296,8 +299,8 @@ Object.defineProperty(Assertion.prototype, 'ok', , 'expected ' + this.inspect + ' to be falsy'); return this; - }, - configurable: true + } + , configurable: true }); /** @@ -314,11 +317,13 @@ Object.defineProperty(Assertion.prototype, 'true', this.assert( true === this.obj , 'expected ' + this.inspect + ' to be true' - , 'expected ' + this.inspect + ' to be false'); + , 'expected ' + this.inspect + ' to be false' + , this.negate ? false : true + ); return this; - }, - configurable: true + } + , configurable: true }); /** @@ -335,11 +340,13 @@ Object.defineProperty(Assertion.prototype, 'false', this.assert( false === this.obj , 'expected ' + this.inspect + ' to be false' - , 'expected ' + this.inspect + ' to be true'); + , 'expected ' + this.inspect + ' to be true' + , this.negate ? true : false + ); return this; - }, - configurable: true + } + , configurable: true }); /** @@ -361,11 +368,12 @@ Object.defineProperty(Assertion.prototype, 'exist', this.assert( null != this.obj , 'expected ' + this.inspect + ' to exist' - , 'expected ' + this.inspect + ' to not exist'); + , 'expected ' + this.inspect + ' to not exist' + ); return this; - }, - configurable: true + } + , configurable: true }); /** @@ -389,8 +397,8 @@ Object.defineProperty(Assertion.prototype, 'empty', , 'expected ' + this.inspect + ' not to be empty'); return this; - }, - configurable: true + } + , configurable: true }); /** @@ -411,11 +419,14 @@ Object.defineProperty(Assertion.prototype, 'arguments', this.assert( '[object Arguments]' == Object.prototype.toString.call(this.obj) , 'expected ' + this.inspect + ' to be arguments' - , 'expected ' + this.inspect + ' to not be arguments'); + , 'expected ' + this.inspect + ' to not be arguments' + , '[object Arguments]' + , Object.prototype.toString.call(this.obj) + ); return this; - }, - configurable: true + } + , configurable: true }); /** @@ -434,7 +445,8 @@ Assertion.prototype.equal = function (val) { this.assert( val === this.obj , 'expected ' + this.inspect + ' to equal ' + inspect(val) - , 'expected ' + this.inspect + ' to not equal ' + inspect(val)); + , 'expected ' + this.inspect + ' to not equal ' + inspect(val) + , val ); return this; }; @@ -455,7 +467,9 @@ Assertion.prototype.eql = function (obj) { this.assert( eql(obj, this.obj) , 'expected ' + this.inspect + ' to equal ' + inspect(obj) - , 'expected ' + this.inspect + ' to not equal ' + inspect(obj)); + , 'expected ' + this.inspect + ' to not equal ' + inspect(obj) + , obj ); + return this; }; @@ -543,7 +557,10 @@ Assertion.prototype.a = function (type) { this.assert( '[object ' + klass + ']' === toString.call(this.obj) , 'expected ' + this.inspect + ' to be a ' + type - , 'expected ' + this.inspect + ' not to be a ' + type); + , 'expected ' + this.inspect + ' not to be a ' + type + , '[object ' + klass + ']' + , toString.call(this.obj) + ); return this; }; @@ -608,7 +625,10 @@ Assertion.prototype.property = function (name, val) { val === this.obj[name] , 'expected ' + this.inspect + ' to have a property ' + inspect(name) + ' of ' + inspect(val) + ', but got ' + inspect(this.obj[name]) - , 'expected ' + this.inspect + ' to not have a property ' + inspect(name) + ' of ' + inspect(val)); + , 'expected ' + this.inspect + ' to not have a property ' + inspect(name) + ' of ' + inspect(val) + , val + , this.obj[val] + ); } this.obj = this.obj[name]; @@ -657,7 +677,10 @@ Assertion.prototype.length = function (n) { this.assert( len == n , 'expected ' + this.inspect + ' to have a length of ' + n + ' but got ' + len - , 'expected ' + this.inspect + ' to not have a length of ' + len); + , 'expected ' + this.inspect + ' to not have a length of ' + len + , n + , len + ); return this; }; @@ -804,7 +827,10 @@ Assertion.prototype.keys = function(keys) { this.assert( ok , 'expected ' + this.inspect + ' to ' + str - , 'expected ' + this.inspect + ' to not ' + str); + , 'expected ' + this.inspect + ' to not ' + str + , keys + , Object.keys(this.obj) + ); return this; } @@ -881,7 +907,10 @@ Assertion.prototype.respondTo = function (method) { this.assert( 'function' === typeof context , 'expected ' + this.inspect + ' to respond to ' + inspect(method) - , 'expected ' + this.inspect + ' to not respond to ' + inspect(method)); + , 'expected ' + this.inspect + ' to not respond to ' + inspect(method) + , 'function' + , typeof context + ); return this; }; @@ -902,7 +931,10 @@ Assertion.prototype.satisfy = function (matcher) { this.assert( matcher(this.obj) , 'expected ' + this.inspect + ' to satisfy ' + inspect(matcher) - , 'expected ' + this.inspect + ' to not satisfy' + inspect(matcher)); + , 'expected ' + this.inspect + ' to not satisfy' + inspect(matcher) + , this.negate ? false : true + , matcher(this.obj) + ); return this; };