Skip to content

Commit

Permalink
add enclosing of failure message functions. Closes tj#81
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jul 30, 2012
1 parent 05132d3 commit b6a64d1
Showing 1 changed file with 51 additions and 51 deletions.
102 changes: 51 additions & 51 deletions lib/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Assertion.prototype = {
if ('string' != typeof expected) expected = i(expected);

throw new AssertionError({
message: msg
message: msg.call(this)
, actual: obj
, expected: expected
, stackStartFunction: this.assert
Expand Down Expand Up @@ -215,8 +215,8 @@ Assertion.prototype = {
get arguments() {
this.assert(
'[object Arguments]' == Object.prototype.toString.call(this.obj)
, 'expected ' + this.inspect + ' to be arguments'
, 'expected ' + this.inspect + ' to not be arguments');
, function(){ return 'expected ' + this.inspect + ' to be arguments' }
, function(){ return 'expected ' + this.inspect + ' to not be arguments' });
return this;
},

Expand All @@ -230,8 +230,8 @@ Assertion.prototype = {
this.obj.should.have.property('length');
this.assert(
0 === this.obj.length
, 'expected ' + this.inspect + ' to be empty'
, 'expected ' + this.inspect + ' not to be empty');
, function(){ return 'expected ' + this.inspect + ' to be empty' }
, function(){ return 'expected ' + this.inspect + ' not to be empty' });
return this;
},

Expand All @@ -244,8 +244,8 @@ Assertion.prototype = {
get ok() {
this.assert(
this.obj
, 'expected ' + this.inspect + ' to be truthy'
, 'expected ' + this.inspect + ' to be falsey');
, function(){ return 'expected ' + this.inspect + ' to be truthy' }
, function(){ return 'expected ' + this.inspect + ' to be falsey' });
return this;
},

Expand All @@ -258,8 +258,8 @@ Assertion.prototype = {
get true() {
this.assert(
true === this.obj
, 'expected ' + this.inspect + ' to be true'
, 'expected ' + this.inspect + ' not to be true');
, function(){ return 'expected ' + this.inspect + ' to be true' }
, function(){ return 'expected ' + this.inspect + ' not to be true' });
return this;
},

Expand All @@ -272,8 +272,8 @@ Assertion.prototype = {
get false() {
this.assert(
false === this.obj
, 'expected ' + this.inspect + ' to be false'
, 'expected ' + this.inspect + ' not to be false');
, function(){ return 'expected ' + this.inspect + ' to be false' }
, function(){ return 'expected ' + this.inspect + ' not to be false' });
return this;
},

Expand All @@ -288,8 +288,8 @@ Assertion.prototype = {
eql: function(val, desc){
this.assert(
eql(val, this.obj)
, 'expected ' + this.inspect + ' to equal ' + i(val) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not equal ' + i(val) + (desc ? " | " + desc : "")
, function(){ return 'expected ' + this.inspect + ' to equal ' + i(val) + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' to not equal ' + i(val) + (desc ? " | " + desc : "") }
, val);
return this;
},
Expand All @@ -305,8 +305,8 @@ Assertion.prototype = {
equal: function(val, desc){
this.assert(
val.valueOf() === this.obj
, 'expected ' + this.inspect + ' to equal ' + i(val) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not equal ' + i(val) + (desc ? " | " + desc : "")
, function(){ return 'expected ' + this.inspect + ' to equal ' + i(val) + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' to not equal ' + i(val) + (desc ? " | " + desc : "") }
, val);
return this;
},
Expand All @@ -324,8 +324,8 @@ Assertion.prototype = {
var range = start + '..' + finish;
this.assert(
this.obj >= start && this.obj <= finish
, 'expected ' + this.inspect + ' to be within ' + range + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not be within ' + range + (desc ? " | " + desc : ""));
, function(){ return 'expected ' + this.inspect + ' to be within ' + range + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' to not be within ' + range + (desc ? " | " + desc : "") });
return this;
},

Expand All @@ -340,8 +340,8 @@ Assertion.prototype = {
a: function(type, desc){
this.assert(
type == typeof this.obj
, 'expected ' + this.inspect + ' to be a ' + type + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' not to be a ' + type + (desc ? " | " + desc : ""));
, function(){ return 'expected ' + this.inspect + ' to be a ' + type + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' not to be a ' + type + (desc ? " | " + desc : "") })
return this;
},

Expand All @@ -357,8 +357,8 @@ Assertion.prototype = {
var name = constructor.name;
this.assert(
this.obj instanceof constructor
, 'expected ' + this.inspect + ' to be an instance of ' + name + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' not to be an instance of ' + name + (desc ? " | " + desc : ""));
, function(){ return 'expected ' + this.inspect + ' to be an instance of ' + name + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' not to be an instance of ' + name + (desc ? " | " + desc : "") });
return this;
},

Expand All @@ -373,8 +373,8 @@ Assertion.prototype = {
above: function(n, desc){
this.assert(
this.obj > n
, 'expected ' + this.inspect + ' to be above ' + n + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to be below ' + n + (desc ? " | " + desc : ""));
, function(){ return 'expected ' + this.inspect + ' to be above ' + n + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' to be below ' + n + (desc ? " | " + desc : "") });
return this;
},

Expand All @@ -389,8 +389,8 @@ Assertion.prototype = {
below: function(n, desc){
this.assert(
this.obj < n
, 'expected ' + this.inspect + ' to be below ' + n + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to be above ' + n + (desc ? " | " + desc : ""));
, function(){ return 'expected ' + this.inspect + ' to be below ' + n + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' to be above ' + n + (desc ? " | " + desc : "") });
return this;
},

Expand All @@ -405,8 +405,8 @@ Assertion.prototype = {
match: function(regexp, desc){
this.assert(
regexp.exec(this.obj)
, 'expected ' + this.inspect + ' to match ' + regexp + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' not to match ' + regexp + (desc ? " | " + desc : ""));
, function(){ return 'expected ' + this.inspect + ' to match ' + regexp + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' not to match ' + regexp + (desc ? " | " + desc : "") });
return this;
},

Expand All @@ -423,8 +423,8 @@ Assertion.prototype = {
var len = this.obj.length;
this.assert(
n == len
, 'expected ' + this.inspect + ' to have a length of ' + n + ' but got ' + len + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not have a length of ' + len + (desc ? " | " + desc : ""));
, function(){ return 'expected ' + this.inspect + ' to have a length of ' + n + ' but got ' + len + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' to not have a length of ' + len + (desc ? " | " + desc : "") });
return this;
},

Expand All @@ -445,16 +445,16 @@ Assertion.prototype = {
} else {
this.assert(
undefined !== this.obj[name]
, 'expected ' + this.inspect + ' to have a property ' + i(name) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not have a property ' + i(name) + (desc ? " | " + desc : ""));
, function(){ return 'expected ' + this.inspect + ' to have a property ' + i(name) + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' to not have a property ' + i(name) + (desc ? " | " + desc : "") });
}

if (undefined !== val) {
this.assert(
val === this.obj[name]
, 'expected ' + this.inspect + ' to have a property ' + i(name)
+ ' of ' + i(val) + ', but got ' + i(this.obj[name]) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not have a property ' + i(name) + ' of ' + i(val) + (desc ? " | " + desc : ""));
, function(){ return 'expected ' + this.inspect + ' to have a property ' + i(name)
+ ' of ' + i(val) + ', but got ' + i(this.obj[name]) + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' to not have a property ' + i(name) + ' of ' + i(val) + (desc ? " | " + desc : "") });
}

this.obj = this.obj[name];
Expand All @@ -472,8 +472,8 @@ Assertion.prototype = {
ownProperty: function(name, desc){
this.assert(
this.obj.hasOwnProperty(name)
, 'expected ' + this.inspect + ' to have own property ' + i(name) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not have own property ' + i(name) + (desc ? " | " + desc : ""));
, function(){ return 'expected ' + this.inspect + ' to have own property ' + i(name) + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' to not have own property ' + i(name) + (desc ? " | " + desc : "") });
return this;
},

Expand All @@ -491,13 +491,13 @@ Assertion.prototype = {
for (var key in obj) cmp[key] = this.obj[key];
this.assert(
eql(cmp, obj)
, 'expected ' + this.inspect + ' to include an object equal to ' + i(obj) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not include an object equal to ' + i(obj) + (desc ? " | " + desc : ""));
, function(){ return 'expected ' + this.inspect + ' to include an object equal to ' + i(obj) + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' to not include an object equal to ' + i(obj) + (desc ? " | " + desc : "") });
} else {
this.assert(
~this.obj.indexOf(obj)
, 'expected ' + this.inspect + ' to include ' + i(obj) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not include ' + i(obj) + (desc ? " | " + desc : ""));
, function(){ return 'expected ' + this.inspect + ' to include ' + i(obj) + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' to not include ' + i(obj) + (desc ? " | " + desc : "") });
}
return this;
},
Expand All @@ -513,8 +513,8 @@ Assertion.prototype = {
includeEql: function(obj, desc){
this.assert(
this.obj.some(function(item) { return eql(obj, item); })
, 'expected ' + this.inspect + ' to include an object equal to ' + i(obj) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not include an object equal to ' + i(obj) + (desc ? " | " + desc : ""));
, function(){ return 'expected ' + this.inspect + ' to include an object equal to ' + i(obj) + (desc ? " | " + desc : "") }
, function(){ return 'expected ' + this.inspect + ' to not include an object equal to ' + i(obj) + (desc ? " | " + desc : "") });
return this;
},

Expand All @@ -530,8 +530,8 @@ Assertion.prototype = {
this.obj.should.be.an.instanceof(Array);
this.assert(
~this.obj.indexOf(obj)
, 'expected ' + this.inspect + ' to contain ' + i(obj)
, 'expected ' + this.inspect + ' to not contain ' + i(obj));
, function(){ return 'expected ' + this.inspect + ' to contain ' + i(obj) }
, function(){ return 'expected ' + this.inspect + ' to not contain ' + i(obj) });
return this;
},

Expand Down Expand Up @@ -580,8 +580,8 @@ Assertion.prototype = {

this.assert(
ok
, 'expected ' + this.inspect + ' to ' + str
, 'expected ' + this.inspect + ' to not ' + str);
, function(){ return 'expected ' + this.inspect + ' to ' + str }
, function(){ return 'expected ' + this.inspect + ' to not ' + str });

return this;
},
Expand Down Expand Up @@ -616,9 +616,9 @@ Assertion.prototype = {

this.assert(
code == status
, 'expected response code of ' + code + ' ' + i(statusCodes[code])
+ ', but got ' + status + ' ' + i(statusCodes[status])
, 'expected to not respond with ' + code + ' ' + i(statusCodes[code]));
, function(){ return 'expected response code of ' + code + ' ' + i(statusCodes[code])
+ ', but got ' + status + ' ' + i(statusCodes[status]) }
, function(){ return 'expected to not respond with ' + code + ' ' + i(statusCodes[code]) });

return this;
},
Expand Down Expand Up @@ -694,8 +694,8 @@ Assertion.prototype = {

this.assert(
ok
, 'expected an exception to be thrown' + errorInfo
, 'expected no exception to be thrown, got "' + err.message + '"');
, function(){ return 'expected an exception to be thrown' + errorInfo }
, function(){ return 'expected no exception to be thrown, got "' + err.message + '"' });

return this;
}
Expand Down

0 comments on commit b6a64d1

Please sign in to comment.