From e383a6ece82e4dcff9b8f1eb2045b285227f8a62 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Tue, 4 Sep 2012 23:09:40 -0700 Subject: [PATCH 1/5] Should syntax: piping message through --- lib/chai/core/assertions.js | 89 ++++++++++-------- lib/chai/interface/should.js | 24 ++--- test/should.js | 169 ++++++++++++++++++++++------------- 3 files changed, 171 insertions(+), 111 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index f54b33c..add6b5a 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -99,7 +99,8 @@ module.exports = function (chai, _) { * @api public */ - function an(type) { + function an(type, msg) { + if (msg != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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,25 @@ module.exports = function (chai, _) { * @api public */ - function assertThrows (constructor, msg) { + function assertThrows (constructor, errMsg, msg) { + if (msg != null) { 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; 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 { @@ -912,22 +928,22 @@ module.exports = function (chai, _) { err instanceof constructor , 'expected #{this} to throw ' + name + ' but a ' + err.name + ' was thrown' , 'expected #{this} to not throw ' + name ); - if (!msg) return this; + 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; @@ -969,7 +985,8 @@ module.exports = function (chai, _) { * @api public */ - Assertion.addMethod('respondTo', function (method) { + Assertion.addMethod('respondTo', function (method, msg) { + if (msg != null) { flag(this, 'message', msg); } var obj = flag(this, 'object') , itself = flag(this, 'itself') , context = ('function' === typeof obj && !itself) @@ -1015,7 +1032,8 @@ module.exports = function (chai, _) { * @api public */ - Assertion.addMethod('satisfy', function (matcher) { + Assertion.addMethod('satisfy', function (matcher, msg) { + if (msg != null) { flag(this, 'message', msg); } var obj = flag(this, 'object'); this.assert( matcher(obj) @@ -1039,7 +1057,8 @@ module.exports = function (chai, _) { * @api public */ - Assertion.addMethod('closeTo', function (expected, delta) { + Assertion.addMethod('closeTo', function (expected, delta, msg) { + if (msg != null) { flag(this, 'message', msg); } var obj = flag(this, 'object'); this.assert( Math.abs(obj - expected) <= delta diff --git a/lib/chai/interface/should.js b/lib/chai/interface/should.js index b60f5c4..df1217b 100644 --- a/lib/chai/interface/should.js +++ b/lib/chai/interface/should.js @@ -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']; diff --git a/test/should.js b/test/should.js index 4750c03..f346a39 100644 --- a/test/should.js +++ b/test/should.js @@ -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(){ @@ -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"); }); }); From 941f992a263fd97566c692b5afe1e8a1a84f4cc7 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Tue, 4 Sep 2012 23:13:44 -0700 Subject: [PATCH 2/5] Increasing Throws error message verbosity 'cause we actually want to know what was thrown. --- lib/chai/core/assertions.js | 16 +++++++++++----- test/assert.js | 2 +- test/expect.js | 14 +++++++------- test/should.js | 14 +++++++------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index add6b5a..dcaa21e 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -892,7 +892,8 @@ module.exports = function (chai, _) { var thrown = false , desiredError = null - , name = null; + , name = null + , thrownError = null; if (arguments.length === 0) { errMsg = null; @@ -926,8 +927,8 @@ 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 ); + , '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 @@ -949,15 +950,20 @@ module.exports = function (chai, _) { 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 ); }; diff --git a/test/assert.js b/test/assert.js index 839d0a1..d4ac281 100644 --- a/test/assert.js +++ b/test/assert.js @@ -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() { diff --git a/test/expect.js b/test/expect.js index 552e675..8ca7d17 100644 --- a/test/expect.js +++ b/test/expect.js @@ -562,11 +562,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); @@ -574,23 +574,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')); diff --git a/test/should.js b/test/should.js index f346a39..4f58615 100644 --- a/test/should.js +++ b/test/should.js @@ -530,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); @@ -542,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')); From 57a6e04df6df8950423bb6e4d269f7ea048064ab Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Wed, 5 Sep 2012 02:19:04 -0700 Subject: [PATCH 3/5] =?UTF-8?q?packaging=20up=20browser-side=20changes?= =?UTF-8?q?=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (tests passing in Chrome 21.0.1180.89 and Firefox 15. Safari 6.0 (7536.25) passes all tests except for the three includeStack tests. --- chai.js | 145 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 92 insertions(+), 53 deletions(-) diff --git a/chai.js b/chai.js index 03bd688..ed4b0e9 100644 --- a/chai.js +++ b/chai.js @@ -396,7 +396,8 @@ * @api public */ - function an(type) { + function an(type, msg) { + if (msg != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { 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 != null) { flag(this, 'message', msg); } var obj = flag(this, 'object'); this.assert( Math.abs(obj - expected) <= delta @@ -2364,7 +2389,21 @@ function loadShould () { // modify Object.prototype to have `should` Object.defineProperty(Object.prototype, 'should', - { set: function () {} + { + set: function (value) { + // See https://github.com/chaijs/chai/issues/86: this makes + // `whatever.should = someValue` actually set `someValue`, which is + // especially useful for `global.should = require('chai').should()`. + // + // Note that we have to use [[DefineProperty]] instead of [[Put]] + // since otherwise we would trigger this very setter! + Object.defineProperty(this, 'should', { + value: value, + enumerable: true, + configurable: true, + writable: true + }); + } , get: function(){ if (this instanceof String || this instanceof Number) { return new Assertion(this.constructor(this)); @@ -2378,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']; From d6bbc5fddd2692f3e4ca4c8f7e4b293701acd26d Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Wed, 5 Sep 2012 11:13:14 -0700 Subject: [PATCH 4/5] expect tests now include message pass-through --- test/expect.js | 160 ++++++++++++++++++++++++------------------------- 1 file changed, 80 insertions(+), 80 deletions(-) diff --git a/test/expect.js b/test/expect.js index 8ca7d17..f78f6a3 100644 --- a/test/expect.js +++ b/test/expect.js @@ -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,8 +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'); }); test('equal(val)', function(){ @@ -252,12 +252,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(){ @@ -346,20 +346,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(){ @@ -368,16 +368,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(){ @@ -386,8 +386,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(){ @@ -400,12 +400,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(){ @@ -417,12 +417,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(){ @@ -609,12 +609,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(){ @@ -633,12 +633,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(){ @@ -649,8 +649,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(){ @@ -659,11 +659,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"); }); }); From 00b2ed38404b38e16dab31cfb97f47a24dcd5749 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Wed, 5 Sep 2012 11:23:10 -0700 Subject: [PATCH 5/5] =?UTF-8?q?Cleaning=20up=20the=20js=20style=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I've been writing too much coffeescript! --- chai.js | 34 +++++++++++++++++----------------- lib/chai/core/assertions.js | 34 +++++++++++++++++----------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/chai.js b/chai.js index ed4b0e9..9cb328e 100644 --- a/chai.js +++ b/chai.js @@ -397,7 +397,7 @@ */ function an(type, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object') , klassStart = type.charAt(0).toUpperCase() , klass = klassStart + type.slice(1) @@ -436,7 +436,7 @@ } function include (val, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object') this.assert( ~obj.indexOf(val) @@ -658,7 +658,7 @@ */ function assertEqual (val, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); if (flag(this, 'deep')) { return this.eql(val); @@ -690,7 +690,7 @@ */ Assertion.addMethod('eql', function (obj, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); this.assert( _.eql(obj, flag(this, 'object')) , 'expected #{this} to deeply equal #{exp}' @@ -722,7 +722,7 @@ */ function assertAbove (n, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); if (flag(this, 'doLength')) { new Assertion(obj, msg).to.have.property('length'); @@ -770,7 +770,7 @@ */ function assertBelow (n, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); if (flag(this, 'doLength')) { new Assertion(obj, msg).to.have.property('length'); @@ -817,7 +817,7 @@ */ Assertion.addMethod('within', function (start, finish, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object') , range = start + '..' + finish; if (flag(this, 'doLength')) { @@ -855,7 +855,7 @@ */ function assertInstanceOf (constructor, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var name = _.getName(constructor); this.assert( flag(this, 'object') instanceof constructor @@ -926,7 +926,7 @@ */ Assertion.addMethod('property', function (name, val, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var descriptor = flag(this, 'deep') ? 'deep property ' : 'property ' , negate = flag(this, 'negate') @@ -975,7 +975,7 @@ */ function assertOwnProperty (name, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); this.assert( obj.hasOwnProperty(name) @@ -1017,7 +1017,7 @@ } function assertLength (n, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); new Assertion(obj, msg).to.have.property('length'); var len = obj.length; @@ -1047,7 +1047,7 @@ */ Assertion.addMethod('match', function (re, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); this.assert( re.exec(obj) @@ -1069,7 +1069,7 @@ */ Assertion.addMethod('string', function (str, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); new Assertion(obj, msg).is.a('string'); @@ -1183,7 +1183,7 @@ */ function assertThrows (constructor, errMsg, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); new Assertion(obj, msg).is.a('function'); @@ -1289,7 +1289,7 @@ */ Assertion.addMethod('respondTo', function (method, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object') , itself = flag(this, 'itself') , context = ('function' === typeof obj && !itself) @@ -1336,7 +1336,7 @@ */ Assertion.addMethod('satisfy', function (matcher, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); this.assert( matcher(obj) @@ -1361,7 +1361,7 @@ */ Assertion.addMethod('closeTo', function (expected, delta, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); this.assert( Math.abs(obj - expected) <= delta diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index dcaa21e..96a7109 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -100,7 +100,7 @@ module.exports = function (chai, _) { */ function an(type, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object') , klassStart = type.charAt(0).toUpperCase() , klass = klassStart + type.slice(1) @@ -139,7 +139,7 @@ module.exports = function (chai, _) { } function include (val, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object') this.assert( ~obj.indexOf(val) @@ -361,7 +361,7 @@ module.exports = function (chai, _) { */ function assertEqual (val, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); if (flag(this, 'deep')) { return this.eql(val); @@ -393,7 +393,7 @@ module.exports = function (chai, _) { */ Assertion.addMethod('eql', function (obj, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); this.assert( _.eql(obj, flag(this, 'object')) , 'expected #{this} to deeply equal #{exp}' @@ -425,7 +425,7 @@ module.exports = function (chai, _) { */ function assertAbove (n, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); if (flag(this, 'doLength')) { new Assertion(obj, msg).to.have.property('length'); @@ -473,7 +473,7 @@ module.exports = function (chai, _) { */ function assertBelow (n, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); if (flag(this, 'doLength')) { new Assertion(obj, msg).to.have.property('length'); @@ -520,7 +520,7 @@ module.exports = function (chai, _) { */ Assertion.addMethod('within', function (start, finish, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object') , range = start + '..' + finish; if (flag(this, 'doLength')) { @@ -558,7 +558,7 @@ module.exports = function (chai, _) { */ function assertInstanceOf (constructor, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var name = _.getName(constructor); this.assert( flag(this, 'object') instanceof constructor @@ -629,7 +629,7 @@ module.exports = function (chai, _) { */ Assertion.addMethod('property', function (name, val, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var descriptor = flag(this, 'deep') ? 'deep property ' : 'property ' , negate = flag(this, 'negate') @@ -678,7 +678,7 @@ module.exports = function (chai, _) { */ function assertOwnProperty (name, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); this.assert( obj.hasOwnProperty(name) @@ -720,7 +720,7 @@ module.exports = function (chai, _) { } function assertLength (n, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); new Assertion(obj, msg).to.have.property('length'); var len = obj.length; @@ -750,7 +750,7 @@ module.exports = function (chai, _) { */ Assertion.addMethod('match', function (re, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); this.assert( re.exec(obj) @@ -772,7 +772,7 @@ module.exports = function (chai, _) { */ Assertion.addMethod('string', function (str, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); new Assertion(obj, msg).is.a('string'); @@ -886,7 +886,7 @@ module.exports = function (chai, _) { */ function assertThrows (constructor, errMsg, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); new Assertion(obj, msg).is.a('function'); @@ -992,7 +992,7 @@ module.exports = function (chai, _) { */ Assertion.addMethod('respondTo', function (method, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object') , itself = flag(this, 'itself') , context = ('function' === typeof obj && !itself) @@ -1039,7 +1039,7 @@ module.exports = function (chai, _) { */ Assertion.addMethod('satisfy', function (matcher, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); this.assert( matcher(obj) @@ -1064,7 +1064,7 @@ module.exports = function (chai, _) { */ Assertion.addMethod('closeTo', function (expected, delta, msg) { - if (msg != null) { flag(this, 'message', msg); } + if (msg) flag(this, 'message', msg); var obj = flag(this, 'object'); this.assert( Math.abs(obj - expected) <= delta