Skip to content

Commit

Permalink
Merge pull request #32 from visionmedia/add/lazy-messages
Browse files Browse the repository at this point in the history
Add/lazy messages
  • Loading branch information
rauchg committed Aug 1, 2012
2 parents e0de582 + e8a6c47 commit de2fdcb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -3,7 +3,7 @@ REPORTER = dot

test:
@./node_modules/.bin/mocha \
--require $(shell pwd)/test/common \
--require ./test/common \
--reporter $(REPORTER) \
--growl \
test/expect.js
Expand Down
78 changes: 39 additions & 39 deletions expect.js
Expand Up @@ -96,7 +96,7 @@
, ok = this.flags.not ? !truth : truth;

if (!ok) {
throw new Error(msg);
throw new Error(msg.call(this));
}

this.and = new Assertion(this.obj);
Expand All @@ -111,8 +111,8 @@
Assertion.prototype.ok = function () {
this.assert(
!!this.obj
, 'expected ' + i(this.obj) + ' to be truthy'
, 'expected ' + i(this.obj) + ' to be falsy');
, function(){ return 'expected ' + i(this.obj) + ' to be truthy' }
, function(){ return 'expected ' + i(this.obj) + ' to be falsy' });
};

/**
Expand Down Expand Up @@ -154,8 +154,8 @@
var name = this.obj.name || 'fn';
this.assert(
thrown
, 'expected ' + name + ' to throw an exception'
, 'expected ' + name + ' not to throw an exception');
, function(){ return 'expected ' + name + ' to throw an exception' }
, function(){ return 'expected ' + name + ' not to throw an exception' });
};

/**
Expand Down Expand Up @@ -184,8 +184,8 @@

this.assert(
expectation
, 'expected ' + i(this.obj) + ' to be empty'
, 'expected ' + i(this.obj) + ' to not be empty');
, function(){ return 'expected ' + i(this.obj) + ' to be empty' }
, function(){ return 'expected ' + i(this.obj) + ' to not be empty' });
return this;
};

Expand All @@ -199,8 +199,8 @@
Assertion.prototype.equal = function (obj) {
this.assert(
obj === this.obj
, 'expected ' + i(this.obj) + ' to equal ' + i(obj)
, 'expected ' + i(this.obj) + ' to not equal ' + i(obj));
, function(){ return 'expected ' + i(this.obj) + ' to equal ' + i(obj) }
, function(){ return 'expected ' + i(this.obj) + ' to not equal ' + i(obj) });
return this;
};

Expand All @@ -213,8 +213,8 @@
Assertion.prototype.eql = function (obj) {
this.assert(
expect.eql(obj, this.obj)
, 'expected ' + i(this.obj) + ' to sort of equal ' + i(obj)
, 'expected ' + i(this.obj) + ' to sort of not equal ' + i(obj));
, function(){ return 'expected ' + i(this.obj) + ' to sort of equal ' + i(obj) }
, function(){ return 'expected ' + i(this.obj) + ' to sort of not equal ' + i(obj) });
return this;
};

Expand All @@ -230,8 +230,8 @@
var range = start + '..' + finish;
this.assert(
this.obj >= start && this.obj <= finish
, 'expected ' + i(this.obj) + ' to be within ' + range
, 'expected ' + i(this.obj) + ' to not be within ' + range);
, function(){ return 'expected ' + i(this.obj) + ' to be within ' + range }
, function(){ return 'expected ' + i(this.obj) + ' to not be within ' + range });
return this;
};

Expand All @@ -253,15 +253,15 @@
'object' == type
? 'object' == typeof this.obj && null !== this.obj
: type == typeof this.obj
, 'expected ' + i(this.obj) + ' to be a' + n + ' ' + type
, 'expected ' + i(this.obj) + ' not to be a' + n + ' ' + type);
, function(){ return 'expected ' + i(this.obj) + ' to be a' + n + ' ' + type }
, function(){ return 'expected ' + i(this.obj) + ' not to be a' + n + ' ' + type });
} else {
// instanceof
var name = type.name || 'supplied constructor';
this.assert(
this.obj instanceof type
, 'expected ' + i(this.obj) + ' to be an instance of ' + name
, 'expected ' + i(this.obj) + ' not to be an instance of ' + name);
, function(){ return 'expected ' + i(this.obj) + ' to be an instance of ' + name }
, function(){ return 'expected ' + i(this.obj) + ' not to be an instance of ' + name });
}

return this;
Expand All @@ -278,8 +278,8 @@
Assertion.prototype.above = function (n) {
this.assert(
this.obj > n
, 'expected ' + i(this.obj) + ' to be above ' + n
, 'expected ' + i(this.obj) + ' to be below ' + n);
, function(){ return 'expected ' + i(this.obj) + ' to be above ' + n }
, function(){ return 'expected ' + i(this.obj) + ' to be below ' + n });
return this;
};

Expand All @@ -294,8 +294,8 @@
Assertion.prototype.below = function (n) {
this.assert(
this.obj < n
, 'expected ' + i(this.obj) + ' to be below ' + n
, 'expected ' + i(this.obj) + ' to be above ' + n);
, function(){ return 'expected ' + i(this.obj) + ' to be below ' + n }
, function(){ return 'expected ' + i(this.obj) + ' to be above ' + n });
return this;
};

Expand All @@ -309,8 +309,8 @@
Assertion.prototype.match = function (regexp) {
this.assert(
regexp.exec(this.obj)
, 'expected ' + i(this.obj) + ' to match ' + regexp
, 'expected ' + i(this.obj) + ' not to match ' + regexp);
, function(){ return 'expected ' + i(this.obj) + ' to match ' + regexp }
, function(){ return 'expected ' + i(this.obj) + ' not to match ' + regexp });
return this;
};

Expand All @@ -326,8 +326,8 @@
var len = this.obj.length;
this.assert(
n == len
, 'expected ' + i(this.obj) + ' to have a length of ' + n + ' but got ' + len
, 'expected ' + i(this.obj) + ' to not have a length of ' + len);
, function(){ return 'expected ' + i(this.obj) + ' to have a length of ' + n + ' but got ' + len }
, function(){ return 'expected ' + i(this.obj) + ' to not have a length of ' + len });
return this;
};

Expand All @@ -343,8 +343,8 @@
if (this.flags.own) {
this.assert(
Object.prototype.hasOwnProperty.call(this.obj, name)
, 'expected ' + i(this.obj) + ' to have own property ' + i(name)
, 'expected ' + i(this.obj) + ' to not have own property ' + i(name));
, function(){ return 'expected ' + i(this.obj) + ' to have own property ' + i(name) }
, function(){ return 'expected ' + i(this.obj) + ' to not have own property ' + i(name) });
return this;
}

Expand All @@ -362,17 +362,17 @@

this.assert(
hasProp
, 'expected ' + i(this.obj) + ' to have a property ' + i(name)
, 'expected ' + i(this.obj) + ' to not have a property ' + i(name));
, function(){ return 'expected ' + i(this.obj) + ' to have a property ' + i(name) }
, function(){ return 'expected ' + i(this.obj) + ' to not have a property ' + i(name) });
}

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

this.obj = this.obj[name];
Expand All @@ -391,13 +391,13 @@
if ('string' == typeof this.obj) {
this.assert(
~this.obj.indexOf(obj)
, 'expected ' + i(this.obj) + ' to contain ' + i(obj)
, 'expected ' + i(this.obj) + ' to not contain ' + i(obj));
, function(){ return 'expected ' + i(this.obj) + ' to contain ' + i(obj) }
, function(){ return 'expected ' + i(this.obj) + ' to not contain ' + i(obj) });
} else {
this.assert(
~indexOf(this.obj, obj)
, 'expected ' + i(this.obj) + ' to contain ' + i(obj)
, 'expected ' + i(this.obj) + ' to not contain ' + i(obj));
, function(){ return 'expected ' + i(this.obj) + ' to contain ' + i(obj) }
, function(){ return 'expected ' + i(this.obj) + ' to not contain ' + i(obj) });
}
return this;
};
Expand Down Expand Up @@ -454,8 +454,8 @@
// Assertion
this.assert(
ok
, 'expected ' + i(this.obj) + ' to ' + str
, 'expected ' + i(this.obj) + ' to not ' + str);
, function(){ return 'expected ' + i(this.obj) + ' to ' + str }
, function(){ return 'expected ' + i(this.obj) + ' to not ' + str });

return this;
};
Expand Down

0 comments on commit de2fdcb

Please sign in to comment.