Skip to content

Commit

Permalink
full support for actual/expected where relevant
Browse files Browse the repository at this point in the history
  • Loading branch information
logicalparadox committed Mar 2, 2012
1 parent 56db8a6 commit a6447e8
Showing 1 changed file with 80 additions and 48 deletions.
128 changes: 80 additions & 48 deletions lib/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
Expand All @@ -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
});

/**
Expand All @@ -140,8 +143,8 @@ Object.defineProperty(Assertion.prototype, 'inspect',
Object.defineProperty(Assertion.prototype, 'to',
{ get: function () {
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -156,8 +159,8 @@ Object.defineProperty(Assertion.prototype, 'to',
Object.defineProperty(Assertion.prototype, 'be',
{ get: function () {
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -174,8 +177,8 @@ Object.defineProperty(Assertion.prototype, 'been',
{ get: function () {
this.tense = 'past';
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -190,8 +193,8 @@ Object.defineProperty(Assertion.prototype, 'been',
Object.defineProperty(Assertion.prototype, 'an',
{ get: function () {
return this;
},
configurable: true
}
, configurable: true
});
/**
* # is
Expand All @@ -205,8 +208,8 @@ Object.defineProperty(Assertion.prototype, 'an',
Object.defineProperty(Assertion.prototype, 'is',
{ get: function () {
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -221,8 +224,8 @@ Object.defineProperty(Assertion.prototype, 'is',
Object.defineProperty(Assertion.prototype, 'and',
{ get: function () {
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -237,8 +240,8 @@ Object.defineProperty(Assertion.prototype, 'and',
Object.defineProperty(Assertion.prototype, 'have',
{ get: function () {
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -253,8 +256,8 @@ Object.defineProperty(Assertion.prototype, 'have',
Object.defineProperty(Assertion.prototype, 'with',
{ get: function () {
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -270,8 +273,8 @@ Object.defineProperty(Assertion.prototype, 'not',
{ get: function () {
this.negate = true;
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -296,8 +299,8 @@ Object.defineProperty(Assertion.prototype, 'ok',
, 'expected ' + this.inspect + ' to be falsy');

return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -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
});

/**
Expand All @@ -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
});

/**
Expand All @@ -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
});

/**
Expand All @@ -389,8 +397,8 @@ Object.defineProperty(Assertion.prototype, 'empty',
, 'expected ' + this.inspect + ' not to be empty');

return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -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
});

/**
Expand All @@ -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;
};
Expand All @@ -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;
};

Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand Down

0 comments on commit a6447e8

Please sign in to comment.