Merge branch 'master' of git://github.com/Liffft/chai into Liffft-master

* 'master' of git://github.com/Liffft/chai:
  Cleaning up the js style…
  expect tests now include message pass-through
  packaging up browser-side changes…
  Increasing Throws error message verbosity
  Should syntax: piping message through

Conflicts:
	test/expect.js
This commit is contained in:
Jake Luer 2012-10-01 15:06:24 -04:00
commit ca18882362
6 changed files with 354 additions and 264 deletions

129
chai.js
View file

@ -396,7 +396,8 @@
* @api public
*/
function an(type) {
function an(type, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, klassStart = type.charAt(0).toUpperCase()
, klass = klassStart + type.slice(1)
@ -434,7 +435,8 @@
flag(this, 'contains', true);
}
function include (val) {
function include (val, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
this.assert(
~obj.indexOf(val)
@ -655,7 +657,8 @@
* @api public
*/
function assertEqual (val) {
function assertEqual (val, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
if (flag(this, 'deep')) {
return this.eql(val);
@ -686,7 +689,8 @@
* @api public
*/
Assertion.addMethod('eql', function (obj) {
Assertion.addMethod('eql', function (obj, msg) {
if (msg) flag(this, 'message', msg);
this.assert(
_.eql(obj, flag(this, 'object'))
, 'expected #{this} to deeply equal #{exp}'
@ -717,10 +721,11 @@
* @api public
*/
function assertAbove (n) {
function assertAbove (n, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
if (flag(this, 'doLength')) {
new Assertion(obj).to.have.property('length');
new Assertion(obj, msg).to.have.property('length');
var len = obj.length;
this.assert(
len > n
@ -764,10 +769,11 @@
* @api public
*/
function assertBelow (n) {
function assertBelow (n, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
if (flag(this, 'doLength')) {
new Assertion(obj).to.have.property('length');
new Assertion(obj, msg).to.have.property('length');
var len = obj.length;
this.assert(
len < n
@ -810,11 +816,12 @@
* @api public
*/
Assertion.addMethod('within', function (start, finish) {
Assertion.addMethod('within', function (start, finish, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, range = start + '..' + finish;
if (flag(this, 'doLength')) {
new Assertion(obj).to.have.property('length');
new Assertion(obj, msg).to.have.property('length');
var len = obj.length;
this.assert(
len >= start && len <= finish
@ -847,7 +854,8 @@
* @api public
*/
function assertInstanceOf (constructor) {
function assertInstanceOf (constructor, msg) {
if (msg) flag(this, 'message', msg);
var name = _.getName(constructor);
this.assert(
flag(this, 'object') instanceof constructor
@ -917,7 +925,9 @@
* @api public
*/
Assertion.addMethod('property', function (name, val) {
Assertion.addMethod('property', function (name, val, msg) {
if (msg) flag(this, 'message', msg);
var descriptor = flag(this, 'deep') ? 'deep property ' : 'property '
, negate = flag(this, 'negate')
, obj = flag(this, 'object')
@ -927,7 +937,8 @@
if (negate && undefined !== val) {
if (undefined === value) {
throw new Error(_.inspect(obj) + ' has no ' + descriptor + _.inspect(name));
msg = (msg != null) ? msg + ': ' : '';
throw new Error(msg + _.inspect(obj) + ' has no ' + descriptor + _.inspect(name));
}
} else {
this.assert(
@ -963,7 +974,8 @@
* @api public
*/
function assertOwnProperty (name) {
function assertOwnProperty (name, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
this.assert(
obj.hasOwnProperty(name)
@ -1004,9 +1016,10 @@
flag(this, 'doLength', true);
}
function assertLength (n) {
function assertLength (n, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
new Assertion(obj).to.have.property('length');
new Assertion(obj, msg).to.have.property('length');
var len = obj.length;
this.assert(
@ -1033,7 +1046,8 @@
* @api public
*/
Assertion.addMethod('match', function (re) {
Assertion.addMethod('match', function (re, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
this.assert(
re.exec(obj)
@ -1054,9 +1068,10 @@
* @api public
*/
Assertion.addMethod('string', function (str) {
Assertion.addMethod('string', function (str, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
new Assertion(obj).is.a('string');
new Assertion(obj, msg).is.a('string');
this.assert(
~obj.indexOf(str)
@ -1167,24 +1182,26 @@
* @api public
*/
function assertThrows (constructor, msg) {
function assertThrows (constructor, errMsg, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
new Assertion(obj).is.a('function');
new Assertion(obj, msg).is.a('function');
var thrown = false
, desiredError = null
, name = null;
, name = null
, thrownError = null;
if (arguments.length === 0) {
msg = null;
errMsg = null;
constructor = null;
} else if (constructor && (constructor instanceof RegExp || 'string' === typeof constructor)) {
msg = constructor;
errMsg = constructor;
constructor = null;
} else if (constructor && constructor instanceof Error) {
desiredError = constructor;
constructor = null;
msg = null;
errMsg = null;
} else if (typeof constructor === 'function') {
name = (new constructor()).name;
} else {
@ -1207,38 +1224,43 @@
if (constructor) {
this.assert(
err instanceof constructor
, 'expected #{this} to throw ' + name + ' but a ' + err.name + ' was thrown'
, 'expected #{this} to not throw ' + name );
if (!msg) return this;
, 'expected #{this} to throw ' + name + ' but ' + _.inspect(err) + ' was thrown'
, 'expected #{this} to not throw ' + name + ' but ' + _.inspect(err) + ' was thrown');
if (!errMsg) return this;
}
// next, check message
if (err.message && msg && msg instanceof RegExp) {
if (err.message && errMsg && errMsg instanceof RegExp) {
this.assert(
msg.exec(err.message)
, 'expected #{this} to throw error matching ' + msg + ' but got ' + _.inspect(err.message)
, 'expected #{this} to throw error not matching ' + msg
errMsg.exec(err.message)
, 'expected #{this} to throw error matching ' + errMsg + ' but got ' + _.inspect(err.message)
, 'expected #{this} to throw error not matching ' + errMsg
);
return this;
} else if (err.message && msg && 'string' === typeof msg) {
} else if (err.message && errMsg && 'string' === typeof errMsg) {
this.assert(
~err.message.indexOf(msg)
~err.message.indexOf(errMsg)
, 'expected #{this} to throw error including #{exp} but got #{act}'
, 'expected #{this} to throw error not including #{act}'
, msg
, errMsg
, err.message
);
return this;
} else {
thrown = true;
thrownError = err;
}
}
var expectedThrown = name ? name : desiredError ? _.inspect(desiredError) : 'an error';
var actuallyGot = ''
if (thrown) {
actuallyGot = ' but ' + _.inspect(thrownError) + ' was thrown'
}
this.assert(
thrown === true
, 'expected #{this} to throw ' + expectedThrown
, 'expected #{this} to not throw ' + expectedThrown
, 'expected #{this} to throw ' + expectedThrown + actuallyGot
, 'expected #{this} to not throw ' + expectedThrown + actuallyGot
);
};
@ -1266,7 +1288,8 @@
* @api public
*/
Assertion.addMethod('respondTo', function (method) {
Assertion.addMethod('respondTo', function (method, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, itself = flag(this, 'itself')
, context = ('function' === typeof obj && !itself)
@ -1312,7 +1335,8 @@
* @api public
*/
Assertion.addMethod('satisfy', function (matcher) {
Assertion.addMethod('satisfy', function (matcher, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
this.assert(
matcher(obj)
@ -1336,7 +1360,8 @@
* @api public
*/
Assertion.addMethod('closeTo', function (expected, delta) {
Assertion.addMethod('closeTo', function (expected, delta, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
this.assert(
Math.abs(obj - expected) <= delta
@ -2392,31 +2417,31 @@
var should = {};
should.equal = function (val1, val2) {
new Assertion(val1).to.equal(val2);
should.equal = function (val1, val2, msg) {
new Assertion(val1, msg).to.equal(val2);
};
should.Throw = function (fn, errt, errs) {
new Assertion(fn).to.Throw(errt, errs);
should.Throw = function (fn, errt, errs, msg) {
new Assertion(fn, msg).to.Throw(errt, errs);
};
should.exist = function (val) {
new Assertion(val).to.exist;
should.exist = function (val, msg) {
new Assertion(val, msg).to.exist;
}
// negation
should.not = {}
should.not.equal = function (val1, val2) {
new Assertion(val1).to.not.equal(val2);
should.not.equal = function (val1, val2, msg) {
new Assertion(val1, msg).to.not.equal(val2);
};
should.not.Throw = function (fn, errt, errs) {
new Assertion(fn).to.not.Throw(errt, errs);
should.not.Throw = function (fn, errt, errs, msg) {
new Assertion(fn, msg).to.not.Throw(errt, errs);
};
should.not.exist = function (val) {
new Assertion(val).to.not.exist;
should.not.exist = function (val, msg) {
new Assertion(val, msg).to.not.exist;
}
should['throw'] = should['Throw'];

View file

@ -99,7 +99,8 @@ module.exports = function (chai, _) {
* @api public
*/
function an(type) {
function an(type, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, klassStart = type.charAt(0).toUpperCase()
, klass = klassStart + type.slice(1)
@ -137,7 +138,8 @@ module.exports = function (chai, _) {
flag(this, 'contains', true);
}
function include (val) {
function include (val, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
this.assert(
~obj.indexOf(val)
@ -358,7 +360,8 @@ module.exports = function (chai, _) {
* @api public
*/
function assertEqual (val) {
function assertEqual (val, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
if (flag(this, 'deep')) {
return this.eql(val);
@ -389,7 +392,8 @@ module.exports = function (chai, _) {
* @api public
*/
Assertion.addMethod('eql', function (obj) {
Assertion.addMethod('eql', function (obj, msg) {
if (msg) flag(this, 'message', msg);
this.assert(
_.eql(obj, flag(this, 'object'))
, 'expected #{this} to deeply equal #{exp}'
@ -420,10 +424,11 @@ module.exports = function (chai, _) {
* @api public
*/
function assertAbove (n) {
function assertAbove (n, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
if (flag(this, 'doLength')) {
new Assertion(obj).to.have.property('length');
new Assertion(obj, msg).to.have.property('length');
var len = obj.length;
this.assert(
len > n
@ -467,10 +472,11 @@ module.exports = function (chai, _) {
* @api public
*/
function assertBelow (n) {
function assertBelow (n, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
if (flag(this, 'doLength')) {
new Assertion(obj).to.have.property('length');
new Assertion(obj, msg).to.have.property('length');
var len = obj.length;
this.assert(
len < n
@ -513,11 +519,12 @@ module.exports = function (chai, _) {
* @api public
*/
Assertion.addMethod('within', function (start, finish) {
Assertion.addMethod('within', function (start, finish, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, range = start + '..' + finish;
if (flag(this, 'doLength')) {
new Assertion(obj).to.have.property('length');
new Assertion(obj, msg).to.have.property('length');
var len = obj.length;
this.assert(
len >= start && len <= finish
@ -550,7 +557,8 @@ module.exports = function (chai, _) {
* @api public
*/
function assertInstanceOf (constructor) {
function assertInstanceOf (constructor, msg) {
if (msg) flag(this, 'message', msg);
var name = _.getName(constructor);
this.assert(
flag(this, 'object') instanceof constructor
@ -620,7 +628,9 @@ module.exports = function (chai, _) {
* @api public
*/
Assertion.addMethod('property', function (name, val) {
Assertion.addMethod('property', function (name, val, msg) {
if (msg) flag(this, 'message', msg);
var descriptor = flag(this, 'deep') ? 'deep property ' : 'property '
, negate = flag(this, 'negate')
, obj = flag(this, 'object')
@ -630,7 +640,8 @@ module.exports = function (chai, _) {
if (negate && undefined !== val) {
if (undefined === value) {
throw new Error(_.inspect(obj) + ' has no ' + descriptor + _.inspect(name));
msg = (msg != null) ? msg + ': ' : '';
throw new Error(msg + _.inspect(obj) + ' has no ' + descriptor + _.inspect(name));
}
} else {
this.assert(
@ -666,7 +677,8 @@ module.exports = function (chai, _) {
* @api public
*/
function assertOwnProperty (name) {
function assertOwnProperty (name, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
this.assert(
obj.hasOwnProperty(name)
@ -707,9 +719,10 @@ module.exports = function (chai, _) {
flag(this, 'doLength', true);
}
function assertLength (n) {
function assertLength (n, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
new Assertion(obj).to.have.property('length');
new Assertion(obj, msg).to.have.property('length');
var len = obj.length;
this.assert(
@ -736,7 +749,8 @@ module.exports = function (chai, _) {
* @api public
*/
Assertion.addMethod('match', function (re) {
Assertion.addMethod('match', function (re, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
this.assert(
re.exec(obj)
@ -757,9 +771,10 @@ module.exports = function (chai, _) {
* @api public
*/
Assertion.addMethod('string', function (str) {
Assertion.addMethod('string', function (str, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
new Assertion(obj).is.a('string');
new Assertion(obj, msg).is.a('string');
this.assert(
~obj.indexOf(str)
@ -870,24 +885,26 @@ module.exports = function (chai, _) {
* @api public
*/
function assertThrows (constructor, msg) {
function assertThrows (constructor, errMsg, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
new Assertion(obj).is.a('function');
new Assertion(obj, msg).is.a('function');
var thrown = false
, desiredError = null
, name = null;
, name = null
, thrownError = null;
if (arguments.length === 0) {
msg = null;
errMsg = null;
constructor = null;
} else if (constructor && (constructor instanceof RegExp || 'string' === typeof constructor)) {
msg = constructor;
errMsg = constructor;
constructor = null;
} else if (constructor && constructor instanceof Error) {
desiredError = constructor;
constructor = null;
msg = null;
errMsg = null;
} else if (typeof constructor === 'function') {
name = (new constructor()).name;
} else {
@ -910,38 +927,43 @@ module.exports = function (chai, _) {
if (constructor) {
this.assert(
err instanceof constructor
, 'expected #{this} to throw ' + name + ' but a ' + err.name + ' was thrown'
, 'expected #{this} to not throw ' + name );
if (!msg) return this;
, 'expected #{this} to throw ' + name + ' but ' + _.inspect(err) + ' was thrown'
, 'expected #{this} to not throw ' + name + ' but ' + _.inspect(err) + ' was thrown');
if (!errMsg) return this;
}
// next, check message
if (err.message && msg && msg instanceof RegExp) {
if (err.message && errMsg && errMsg instanceof RegExp) {
this.assert(
msg.exec(err.message)
, 'expected #{this} to throw error matching ' + msg + ' but got ' + _.inspect(err.message)
, 'expected #{this} to throw error not matching ' + msg
errMsg.exec(err.message)
, 'expected #{this} to throw error matching ' + errMsg + ' but got ' + _.inspect(err.message)
, 'expected #{this} to throw error not matching ' + errMsg
);
return this;
} else if (err.message && msg && 'string' === typeof msg) {
} else if (err.message && errMsg && 'string' === typeof errMsg) {
this.assert(
~err.message.indexOf(msg)
~err.message.indexOf(errMsg)
, 'expected #{this} to throw error including #{exp} but got #{act}'
, 'expected #{this} to throw error not including #{act}'
, msg
, errMsg
, err.message
);
return this;
} else {
thrown = true;
thrownError = err;
}
}
var expectedThrown = name ? name : desiredError ? _.inspect(desiredError) : 'an error';
var actuallyGot = ''
if (thrown) {
actuallyGot = ' but ' + _.inspect(thrownError) + ' was thrown'
}
this.assert(
thrown === true
, 'expected #{this} to throw ' + expectedThrown
, 'expected #{this} to not throw ' + expectedThrown
, 'expected #{this} to throw ' + expectedThrown + actuallyGot
, 'expected #{this} to not throw ' + expectedThrown + actuallyGot
);
};
@ -969,7 +991,8 @@ module.exports = function (chai, _) {
* @api public
*/
Assertion.addMethod('respondTo', function (method) {
Assertion.addMethod('respondTo', function (method, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, itself = flag(this, 'itself')
, context = ('function' === typeof obj && !itself)
@ -1015,7 +1038,8 @@ module.exports = function (chai, _) {
* @api public
*/
Assertion.addMethod('satisfy', function (matcher) {
Assertion.addMethod('satisfy', function (matcher, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
this.assert(
matcher(obj)
@ -1039,7 +1063,8 @@ module.exports = function (chai, _) {
* @api public
*/
Assertion.addMethod('closeTo', function (expected, delta) {
Assertion.addMethod('closeTo', function (expected, delta, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
this.assert(
Math.abs(obj - expected) <= delta

View file

@ -38,31 +38,31 @@ module.exports = function (chai, util) {
var should = {};
should.equal = function (val1, val2) {
new Assertion(val1).to.equal(val2);
should.equal = function (val1, val2, msg) {
new Assertion(val1, msg).to.equal(val2);
};
should.Throw = function (fn, errt, errs) {
new Assertion(fn).to.Throw(errt, errs);
should.Throw = function (fn, errt, errs, msg) {
new Assertion(fn, msg).to.Throw(errt, errs);
};
should.exist = function (val) {
new Assertion(val).to.exist;
should.exist = function (val, msg) {
new Assertion(val, msg).to.exist;
}
// negation
should.not = {}
should.not.equal = function (val1, val2) {
new Assertion(val1).to.not.equal(val2);
should.not.equal = function (val1, val2, msg) {
new Assertion(val1, msg).to.not.equal(val2);
};
should.not.Throw = function (fn, errt, errs) {
new Assertion(fn).to.not.Throw(errt, errs);
should.not.Throw = function (fn, errt, errs, msg) {
new Assertion(fn, msg).to.not.Throw(errt, errs);
};
should.not.exist = function (val) {
new Assertion(val).to.not.exist;
should.not.exist = function (val, msg) {
new Assertion(val, msg).to.not.exist;
}
should['throw'] = should['Throw'];

View file

@ -418,7 +418,7 @@ suite('assert', function () {
err(function () {
assert.doesNotThrow(function() { throw new Error('foo'); });
}, 'expected [Function] to not throw an error');
}, 'expected [Function] to not throw an error but [Error: foo] was thrown');
});
test('ifError', function() {

View file

@ -121,8 +121,8 @@ suite('expect', function () {
expect(function() {}).to.be.a('function');
err(function(){
expect(5).to.not.be.a('number');
}, "expected 5 not to be a number");
expect(5).to.not.be.a('number', 'blah');
}, "blah: expected 5 not to be a number");
});
test('instanceof', function(){
@ -130,8 +130,8 @@ suite('expect', function () {
expect(new Foo()).to.be.an.instanceof(Foo);
err(function(){
expect(3).to.an.instanceof(Foo);
}, "expected 3 to be an instance of Foo");
expect(3).to.an.instanceof(Foo, 'blah');
}, "blah: expected 3 to be an instance of Foo");
});
test('within(start, finish)', function(){
@ -143,20 +143,20 @@ suite('expect', function () {
expect([ 1, 2, 3 ]).to.have.length.within(2,4);
err(function(){
expect(5).to.not.be.within(4,6);
}, "expected 5 to not be within 4..6");
expect(5).to.not.be.within(4,6, 'blah');
}, "blah: expected 5 to not be within 4..6", 'blah');
err(function(){
expect(10).to.be.within(50,100);
}, "expected 10 to be within 50..100");
expect(10).to.be.within(50,100, 'blah');
}, "blah: expected 10 to be within 50..100");
err(function () {
expect('foo').to.have.length.within(5,7);
}, "expected \'foo\' to have a length within 5..7");
expect('foo').to.have.length.within(5,7, 'blah');
}, "blah: expected \'foo\' to have a length within 5..7");
err(function () {
expect([ 1, 2, 3 ]).to.have.length.within(5,7);
}, "expected [ 1, 2, 3 ] to have a length within 5..7");
expect([ 1, 2, 3 ]).to.have.length.within(5,7, 'blah');
}, "blah: expected [ 1, 2, 3 ] to have a length within 5..7");
});
test('above(n)', function(){
@ -168,20 +168,20 @@ suite('expect', function () {
expect([ 1, 2, 3 ]).to.have.length.above(2);
err(function(){
expect(5).to.be.above(6);
}, "expected 5 to be above 6");
expect(5).to.be.above(6, 'blah');
}, "blah: expected 5 to be above 6", 'blah');
err(function(){
expect(10).to.not.be.above(6);
}, "expected 10 to be below 6");
expect(10).to.not.be.above(6, 'blah');
}, "blah: expected 10 to be below 6");
err(function () {
expect('foo').to.have.length.above(4);
}, "expected \'foo\' to have a length above 4 but got 3");
expect('foo').to.have.length.above(4, 'blah');
}, "blah: expected \'foo\' to have a length above 4 but got 3");
err(function () {
expect([ 1, 2, 3 ]).to.have.length.above(4);
}, "expected [ 1, 2, 3 ] to have a length above 4 but got 3");
expect([ 1, 2, 3 ]).to.have.length.above(4, 'blah');
}, "blah: expected [ 1, 2, 3 ] to have a length above 4 but got 3");
});
test('below(n)', function(){
@ -193,20 +193,20 @@ suite('expect', function () {
expect([ 1, 2, 3 ]).to.have.length.below(4);
err(function(){
expect(6).to.be.below(5);
}, "expected 6 to be below 5");
expect(6).to.be.below(5, 'blah');
}, "blah: expected 6 to be below 5");
err(function(){
expect(6).to.not.be.below(10);
}, "expected 6 to be above 10");
expect(6).to.not.be.below(10, 'blah');
}, "blah: expected 6 to be above 10");
err(function () {
expect('foo').to.have.length.below(2);
}, "expected \'foo\' to have a length below 2 but got 3");
expect('foo').to.have.length.below(2, 'blah');
}, "blah: expected \'foo\' to have a length below 2 but got 3");
err(function () {
expect([ 1, 2, 3 ]).to.have.length.below(2);
}, "expected [ 1, 2, 3 ] to have a length below 2 but got 3");
expect([ 1, 2, 3 ]).to.have.length.below(2, 'blah');
}, "blah: expected [ 1, 2, 3 ] to have a length below 2 but got 3");
});
test('match(regexp)', function(){
@ -214,12 +214,12 @@ suite('expect', function () {
expect('foobar').to.not.match(/^bar/)
err(function(){
expect('foobar').to.match(/^bar/i)
}, "expected 'foobar' to match /^bar/i");
expect('foobar').to.match(/^bar/i, 'blah')
}, "blah: expected 'foobar' to match /^bar/i");
err(function(){
expect('foobar').to.not.match(/^foo/i)
}, "expected 'foobar' not to match /^foo/i");
expect('foobar').to.not.match(/^foo/i, 'blah')
}, "blah: expected 'foobar' not to match /^foo/i");
});
test('length(n)', function(){
@ -228,12 +228,12 @@ suite('expect', function () {
expect([1,2,3]).to.have.length(3);
err(function(){
expect(4).to.have.length(3);
}, 'expected 4 to have a property \'length\'');
expect(4).to.have.length(3, 'blah');
}, 'blah: expected 4 to have a property \'length\'');
err(function(){
expect('asd').to.not.have.length(3);
}, "expected 'asd' to not have a length of 3");
expect('asd').to.not.have.length(3, 'blah');
}, "blah: expected 'asd' to not have a length of 3");
});
test('eql(val)', function(){
@ -243,9 +243,8 @@ suite('expect', function () {
expect('4').to.not.eql(4);
err(function(){
expect(4).to.eql(3);
}, 'expected 4 to deeply equal 3');
expect(4).to.eql(3, 'blah');
}, 'blah: expected 4 to deeply equal 3');
});
if ('undefined' !== typeof Buffer) {
@ -263,12 +262,12 @@ suite('expect', function () {
expect(1).to.equal(1);
err(function(){
expect(4).to.equal(3);
}, 'expected 4 to equal 3');
expect(4).to.equal(3, 'blah');
}, 'blah: expected 4 to equal 3');
err(function(){
expect('4').to.equal(4);
}, "expected '4' to equal 4");
expect('4').to.equal(4, 'blah');
}, "blah: expected '4' to equal 4");
});
test('deep.equal(val)', function(){
@ -357,20 +356,20 @@ suite('expect', function () {
expect('asd').to.have.property('constructor', String);
err(function(){
expect('asd').to.have.property('length', 4);
}, "expected 'asd' to have a property 'length' of 4, but got 3");
expect('asd').to.have.property('length', 4, 'blah');
}, "blah: expected 'asd' to have a property 'length' of 4, but got 3");
err(function(){
expect('asd').to.not.have.property('length', 3);
}, "expected 'asd' to not have a property 'length' of 3");
expect('asd').to.not.have.property('length', 3, 'blah');
}, "blah: expected 'asd' to not have a property 'length' of 3");
err(function(){
expect('asd').to.not.have.property('foo', 3);
}, "'asd' has no property 'foo'");
expect('asd').to.not.have.property('foo', 3, 'blah');
}, "blah: 'asd' has no property 'foo'");
err(function(){
expect('asd').to.have.property('constructor', Number);
}, "expected 'asd' to have a property 'constructor' of [Function: Number], but got [Function: String]");
expect('asd').to.have.property('constructor', Number, 'blah');
}, "blah: expected 'asd' to have a property 'constructor' of [Function: Number], but got [Function: String]");
});
test('deep.property(name, val)', function(){
@ -379,16 +378,16 @@ suite('expect', function () {
err(function(){
expect({ foo: { bar: 'baz' } })
.to.have.deep.property('foo.bar', 'quux');
}, "expected { foo: { bar: 'baz' } } to have a deep property 'foo.bar' of 'quux', but got 'baz'");
.to.have.deep.property('foo.bar', 'quux', 'blah');
}, "blah: expected { foo: { bar: 'baz' } } to have a deep property 'foo.bar' of 'quux', but got 'baz'");
err(function(){
expect({ foo: { bar: 'baz' } })
.to.not.have.deep.property('foo.bar', 'baz');
}, "expected { foo: { bar: 'baz' } } to not have a deep property 'foo.bar' of 'baz'");
.to.not.have.deep.property('foo.bar', 'baz', 'blah');
}, "blah: expected { foo: { bar: 'baz' } } to not have a deep property 'foo.bar' of 'baz'");
err(function(){
expect({ foo: 5 })
.to.not.have.deep.property('foo.bar', 'baz');
}, "{ foo: 5 } has no deep property 'foo.bar'");
.to.not.have.deep.property('foo.bar', 'baz', 'blah');
}, "blah: { foo: 5 } has no deep property 'foo.bar'");
});
test('ownProperty(name)', function(){
@ -397,8 +396,8 @@ suite('expect', function () {
expect({ length: 12 }).to.have.ownProperty('length');
err(function(){
expect({ length: 12 }).to.not.have.ownProperty('length');
}, "expected { length: 12 } to not have own property 'length'");
expect({ length: 12 }).to.not.have.ownProperty('length', 'blah');
}, "blah: expected { length: 12 } to not have own property 'length'");
});
test('string()', function(){
@ -411,12 +410,12 @@ suite('expect', function () {
}, "expected 3 to be a string");
err(function(){
expect('foobar').to.have.string('baz');
}, "expected 'foobar' to contain 'baz'");
expect('foobar').to.have.string('baz', 'blah');
}, "blah: expected 'foobar' to contain 'baz'");
err(function(){
expect('foobar').to.not.have.string('bar');
}, "expected 'foobar' to not contain 'bar'");
expect('foobar').to.not.have.string('bar', 'blah');
}, "blah: expected 'foobar' to not contain 'bar'");
});
test('include()', function(){
@ -428,12 +427,12 @@ suite('expect', function () {
expect(['foo', 'bar']).to.not.include(1);
err(function(){
expect(['foo']).to.include('bar');
}, "expected [ 'foo' ] to include 'bar'");
expect(['foo']).to.include('bar', 'blah');
}, "blah: expected [ 'foo' ] to include 'bar'");
err(function(){
expect(['bar', 'foo']).to.not.include('foo');
}, "expected [ 'bar', 'foo' ] to not include 'foo'");
expect(['bar', 'foo']).to.not.include('foo', 'blah');
}, "blah: expected [ 'bar', 'foo' ] to not include 'foo'");
});
test('keys(array)', function(){
@ -573,11 +572,11 @@ suite('expect', function () {
err(function(){
expect(badFn).to.not.throw();
}, "expected [Function] to not throw an error");
}, "expected [Function] to not throw an error but [Error: testing] was thrown");
err(function(){
expect(badFn).to.throw(ReferenceError);
}, "expected [Function] to throw ReferenceError but a Error was thrown");
}, "expected [Function] to throw ReferenceError but [Error: testing] was thrown");
err(function(){
expect(badFn).to.throw(specificError);
@ -585,23 +584,23 @@ suite('expect', function () {
err(function(){
expect(badFn).to.not.throw(Error);
}, "expected [Function] to not throw Error");
}, "expected [Function] to not throw Error but [Error: testing] was thrown");
err(function(){
expect(refErrFn).to.not.throw(ReferenceError);
}, "expected [Function] to not throw ReferenceError");
}, "expected [Function] to not throw ReferenceError but [ReferenceError: hello] was thrown");
err(function(){
expect(badFn).to.throw(PoorlyConstructedError);
}, "expected [Function] to throw PoorlyConstructedError but a Error was thrown");
}, "expected [Function] to throw PoorlyConstructedError but [Error: testing] was thrown");
err(function(){
expect(ickyErrFn).to.not.throw(PoorlyConstructedError);
}, "expected [Function] to not throw PoorlyConstructedError");
}, "expected [Function] to not throw PoorlyConstructedError but { name: 'PoorlyConstructedError' } was thrown");
err(function(){
expect(ickyErrFn).to.throw(ReferenceError);
}, "expected [Function] to throw ReferenceError but a PoorlyConstructedError was thrown");
}, "expected [Function] to throw ReferenceError but { name: 'PoorlyConstructedError' } was thrown");
err(function(){
expect(specificErrFn).to.throw(new ReferenceError('eek'));
@ -620,12 +619,12 @@ suite('expect', function () {
}, "expected [Function] to throw error matching /hello/ but got \'testing\'");
err(function () {
expect(badFn).to.throw(Error, /hello/);
}, "expected [Function] to throw error matching /hello/ but got 'testing'");
expect(badFn).to.throw(Error, /hello/, 'blah');
}, "blah: expected [Function] to throw error matching /hello/ but got 'testing'");
err(function () {
expect(badFn).to.throw(Error, 'hello');
}, "expected [Function] to throw error including 'hello' but got 'testing'");
expect(badFn).to.throw(Error, 'hello', 'blah');
}, "blah: expected [Function] to throw error including 'hello' but got 'testing'");
});
test('respondTo', function(){
@ -644,12 +643,12 @@ suite('expect', function () {
expect(bar).to.respondTo('foo');
err(function(){
expect(Foo).to.respondTo('baz');
}, "expected { [Function: Foo] func: [Function] } to respond to \'baz\'");
expect(Foo).to.respondTo('baz', 'blah');
}, "blah: expected { [Function: Foo] func: [Function] } to respond to \'baz\'");
err(function(){
expect(bar).to.respondTo('baz');
}, "expected { foo: [Function] } to respond to \'baz\'");
expect(bar).to.respondTo('baz', 'blah');
}, "blah: expected { foo: [Function] } to respond to \'baz\'");
});
test('satisfy', function(){
@ -660,8 +659,8 @@ suite('expect', function () {
expect(1).to.satisfy(matcher);
err(function(){
expect(2).to.satisfy(matcher);
}, "expected 2 to satisfy [Function]");
expect(2).to.satisfy(matcher, 'blah');
}, "blah: expected 2 to satisfy [Function]");
});
test('closeTo', function(){
@ -670,11 +669,11 @@ suite('expect', function () {
expect(-10).to.be.closeTo(20, 30);
err(function(){
expect(2).to.be.closeTo(1.0, 0.5);
}, "expected 2 to be close to 1 +/- 0.5");
expect(2).to.be.closeTo(1.0, 0.5, 'blah');
}, "blah: expected 2 to be close to 1 +/- 0.5");
err(function(){
expect(-10).to.be.closeTo(20, 29);
}, "expected -10 to be close to 20 +/- 29");
expect(-10).to.be.closeTo(20, 29, 'blah');
}, "blah: expected -10 to be close to 20 +/- 29");
});
});

View file

@ -32,12 +32,41 @@ suite('should', function() {
should.not.exist(bar);
err(function () {
should.exist(bar);
}, "expected undefined to exist");
should.exist(bar, 'blah');
}, "blah: expected undefined to exist");
err(function () {
should.not.exist(foo);
}, "expected 'foo' to not exist")
should.not.exist(foo, 'blah');
}, "blah: expected 'foo' to not exist")
});
test('root equal', function () {
var value1 = 'value'
, value2 = 'value'
, foo = 'foo';
should.equal(value1, value2);
should.not.equal(value1, foo);
err(function () {
should.equal(value1, foo, 'blah');
}, "blah: expected 'value' to equal 'foo'");
err(function () {
should.not.equal(value1, value2, 'blah');
}, "blah: expected 'value' to not equal 'value'")
});
test('root Throw', function () {
should.Throw(function() { throw new Error('error!') }, Error, 'error!');
should.not.Throw(function () { });
err(function () {
should.Throw(function () { throw new Error('error!') }, Error, 'needed user!', 'blah');
}, "blah: expected [Function] to throw error including 'needed user!' but got 'error!'");
err(function () {
should.not.Throw(function () { throw new Error('error!') }, Error, 'error!', 'blah');
}, "blah: expected [Function] to not throw Error but [Error: error!] was thrown");
});
test('true', function(){
@ -132,8 +161,8 @@ suite('should', function() {
new Foo().should.be.an.instanceof(Foo);
err(function(){
(3).should.an.instanceof(Foo);
}, "expected 3 to be an instance of Foo");
(3).should.an.instanceof(Foo, 'blah');
}, "blah: expected 3 to be an instance of Foo");
});
test('within(start, finish)', function(){
@ -143,12 +172,16 @@ suite('should', function() {
(5).should.not.be.within(1,3);
err(function(){
(5).should.not.be.within(4,6);
}, "expected 5 to not be within 4..6");
(5).should.not.be.within(4,6, 'blah');
}, "blah: expected 5 to not be within 4..6");
err(function(){
(10).should.be.within(50,100);
}, "expected 10 to be within 50..100");
(10).should.be.within(50,100, 'blah');
}, "blah: expected 10 to be within 50..100");
err(function(){
({ foo: 1 }).should.have.length.within(50,100, 'blah');
}, "blah: expected { foo: 1 } to have a property 'length'");
});
test('above(n)', function(){
@ -158,12 +191,16 @@ suite('should', function() {
(5).should.not.be.above(6);
err(function(){
(5).should.be.above(6);
}, "expected 5 to be above 6");
(5).should.be.above(6, 'blah');
}, "blah: expected 5 to be above 6");
err(function(){
(10).should.not.be.above(6);
}, "expected 10 to be below 6");
(10).should.not.be.above(6, 'blah');
}, "blah: expected 10 to be below 6");
err(function(){
({foo: 1}).should.have.length.above(3, 'blah');
}, "blah: expected { foo: 1 } to have a property 'length'");
});
test('below(n)', function(){
@ -173,12 +210,16 @@ suite('should', function() {
(2).should.not.be.below(1);
err(function(){
(6).should.be.below(5);
}, "expected 6 to be below 5");
(6).should.be.below(5, 'blah');
}, "blah: expected 6 to be below 5");
err(function(){
(6).should.not.be.below(10);
}, "expected 6 to be above 10");
(6).should.not.be.below(10, 'blah');
}, "blah: expected 6 to be above 10");
err(function(){
({foo: 1}).should.have.length.below(3, 'blah');
}, "blah: expected { foo: 1 } to have a property 'length'");
});
test('match(regexp)', function(){
@ -186,12 +227,12 @@ suite('should', function() {
'foobar'.should.not.match(/^bar/)
err(function(){
'foobar'.should.match(/^bar/i)
}, "expected 'foobar' to match /^bar/i");
'foobar'.should.match(/^bar/i, 'blah')
}, "blah: expected 'foobar' to match /^bar/i");
err(function(){
'foobar'.should.not.match(/^foo/i)
}, "expected 'foobar' not to match /^foo/i");
'foobar'.should.not.match(/^foo/i, 'blah')
}, "blah: expected 'foobar' not to match /^foo/i");
});
test('length(n)', function(){
@ -200,12 +241,12 @@ suite('should', function() {
[1,2,3].should.have.length(3);
err(function(){
(4).should.have.length(3);
}, 'expected 4 to have a property \'length\'');
(4).should.have.length(3, 'blah');
}, 'blah: expected 4 to have a property \'length\'');
err(function(){
'asd'.should.not.have.length(3);
}, "expected 'asd' to not have a length of 3");
'asd'.should.not.have.length(3, 'blah');
}, "blah: expected 'asd' to not have a length of 3");
});
test('eql(val)', function(){
@ -215,8 +256,8 @@ suite('should', function() {
'4'.should.not.eql(4);
err(function(){
(4).should.eql(3);
}, 'expected 4 to deeply equal 3');
(4).should.eql(3, 'blah');
}, 'blah: expected 4 to deeply equal 3');
});
test('equal(val)', function(){
@ -224,12 +265,12 @@ suite('should', function() {
(1).should.equal(1);
err(function(){
(4).should.equal(3);
}, 'expected 4 to equal 3');
(4).should.equal(3, 'blah');
}, 'blah: expected 4 to equal 3');
err(function(){
'4'.should.equal(4);
}, "expected '4' to equal 4");
'4'.should.equal(4, 'blah');
}, "blah: expected '4' to equal 4");
});
test('empty', function(){
@ -292,20 +333,20 @@ suite('should', function() {
'asd'.should.have.property('constructor', String);
err(function(){
'asd'.should.have.property('length', 4);
}, "expected 'asd' to have a property 'length' of 4, but got 3");
'asd'.should.have.property('length', 4, 'blah');
}, "blah: expected 'asd' to have a property 'length' of 4, but got 3");
err(function(){
'asd'.should.not.have.property('length', 3);
}, "expected 'asd' to not have a property 'length' of 3");
'asd'.should.not.have.property('length', 3, 'blah');
}, "blah: expected 'asd' to not have a property 'length' of 3");
err(function(){
'asd'.should.not.have.property('foo', 3);
}, "'asd' has no property 'foo'");
'asd'.should.not.have.property('foo', 3, 'blah');
}, "blah: 'asd' has no property 'foo'");
err(function(){
'asd'.should.have.property('constructor', Number);
}, "expected 'asd' to have a property 'constructor' of [Function: Number], but got [Function: String]");
'asd'.should.have.property('constructor', Number, 'blah');
}, "blah: expected 'asd' to have a property 'constructor' of [Function: Number], but got [Function: String]");
});
test('ownProperty(name)', function(){
@ -314,8 +355,8 @@ suite('should', function() {
({ length: 12 }).should.have.ownProperty('length');
err(function(){
({ length: 12 }).should.not.have.ownProperty('length');
}, "expected { length: 12 } to not have own property 'length'");
({ length: 12 }).should.not.have.ownProperty('length', 'blah');
}, "blah: expected { length: 12 } to not have own property 'length'");
});
test('string()', function(){
@ -324,16 +365,16 @@ suite('should', function() {
'foobar'.should.not.contain.string('baz');
err(function(){
(3).should.contain.string('baz');
}, "expected 3 to be a string");
(3).should.contain.string('baz', 'blah');
}, "blah: expected 3 to be a string");
err(function(){
'foobar'.should.contain.string('baz');
}, "expected 'foobar' to contain 'baz'");
'foobar'.should.contain.string('baz', 'blah');
}, "blah: expected 'foobar' to contain 'baz'");
err(function(){
'foobar'.should.not.contain.string('bar');
}, "expected 'foobar' to not contain 'bar'");
'foobar'.should.not.contain.string('bar', 'blah');
}, "blah: expected 'foobar' to not contain 'bar'");
});
test('include()', function(){
@ -345,12 +386,12 @@ suite('should', function() {
['foo', 'bar'].should.not.include(1);
err(function(){
['foo'].should.include('bar');
}, "expected [ 'foo' ] to include 'bar'");
['foo'].should.include('bar', 'blah');
}, "blah: expected [ 'foo' ] to include 'bar'");
err(function(){
['bar', 'foo'].should.not.include('foo');
}, "expected [ 'bar', 'foo' ] to not include 'foo'");
['bar', 'foo'].should.not.include('foo', 'blah');
}, "blah: expected [ 'bar', 'foo' ] to not include 'foo'");
});
test('keys(array)', function(){
@ -489,11 +530,11 @@ suite('should', function() {
err(function(){
(badFn).should.not.throw();
}, "expected [Function] to not throw an error");
}, "expected [Function] to not throw an error but [Error: testing] was thrown");
err(function(){
(badFn).should.throw(ReferenceError);
}, "expected [Function] to throw ReferenceError but a Error was thrown");
}, "expected [Function] to throw ReferenceError but [Error: testing] was thrown");
err(function(){
(badFn).should.throw(specificError);
@ -501,23 +542,23 @@ suite('should', function() {
err(function(){
(badFn).should.not.throw(Error);
}, "expected [Function] to not throw Error");
}, "expected [Function] to not throw Error but [Error: testing] was thrown");
err(function(){
(refErrFn).should.not.throw(ReferenceError);
}, "expected [Function] to not throw ReferenceError");
}, "expected [Function] to not throw ReferenceError but [ReferenceError: hello] was thrown");
err(function(){
(badFn).should.throw(PoorlyConstructedError);
}, "expected [Function] to throw PoorlyConstructedError but a Error was thrown");
}, "expected [Function] to throw PoorlyConstructedError but [Error: testing] was thrown");
err(function(){
(ickyErrFn).should.not.throw(PoorlyConstructedError);
}, "expected [Function] to not throw PoorlyConstructedError");
}, "expected [Function] to not throw PoorlyConstructedError but { name: 'PoorlyConstructedError' } was thrown");
err(function(){
(ickyErrFn).should.throw(ReferenceError);
}, "expected [Function] to throw ReferenceError but a PoorlyConstructedError was thrown");
}, "expected [Function] to throw ReferenceError but { name: 'PoorlyConstructedError' } was thrown");
err(function(){
(specificErrFn).should.throw(new ReferenceError('eek'));
@ -536,12 +577,12 @@ suite('should', function() {
}, "expected [Function] to throw error matching /hello/ but got \'testing\'");
err(function () {
(badFn).should.throw(Error, /hello/);
}, "expected [Function] to throw error matching /hello/ but got 'testing'");
(badFn).should.throw(Error, /hello/, 'blah');
}, "blah: expected [Function] to throw error matching /hello/ but got 'testing'");
err(function () {
(badFn).should.throw(Error, 'hello');
}, "expected [Function] to throw error including 'hello' but got 'testing'");
(badFn).should.throw(Error, 'hello', 'blah');
}, "blah: expected [Function] to throw error including 'hello' but got 'testing'");
});
test('respondTo', function(){
@ -560,12 +601,12 @@ suite('should', function() {
bar.should.respondTo('foo');
err(function(){
Foo.should.respondTo('baz');
}, "expected { [Function: Foo] func: [Function] } to respond to \'baz\'");
Foo.should.respondTo('baz', 'blah');
}, "blah: expected { [Function: Foo] func: [Function] } to respond to \'baz\'");
err(function(){
bar.should.respondTo('baz');
}, "expected { foo: [Function] } to respond to \'baz\'");
bar.should.respondTo('baz', 'blah');
}, "blah: expected { foo: [Function] } to respond to \'baz\'");
});
test('satisfy', function(){
@ -576,15 +617,15 @@ suite('should', function() {
(1).should.satisfy(matcher);
err(function(){
(2).should.satisfy(matcher);
}, "expected 2 to satisfy [Function]");
(2).should.satisfy(matcher, 'blah');
}, "blah: expected 2 to satisfy [Function]");
});
test('closeTo', function(){
(1.5).should.be.closeTo(1.0, 0.5);
err(function(){
(2).should.be.closeTo(1.0, 0.5);
}, "expected 2 to be close to 1 +/- 0.5");
(2).should.be.closeTo(1.0, 0.5, 'blah');
}, "blah: expected 2 to be close to 1 +/- 0.5");
});
});