From 4b0c0b622231d80a2ae6ae90942a1c7b90d3a41a Mon Sep 17 00:00:00 2001 From: Joshua Perry Date: Mon, 13 Oct 2014 16:31:17 -0600 Subject: [PATCH 1/7] Allows writing lint-friendly tests This makes the final assertion checks noop functions so that they can be formatted like function calls. This keeps linters (JSLint/JSHint) off our back for having an inop expression. Completes #41 --- lib/chai/assertion.js | 4 ++++ lib/chai/core/assertions.js | 18 +++++++++--------- test/expect.js | 24 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/lib/chai/assertion.js b/lib/chai/assertion.js index a3405a832..6ac8db6d5 100644 --- a/lib/chai/assertion.js +++ b/lib/chai/assertion.js @@ -69,6 +69,10 @@ module.exports = function (_chai, util) { util.addChainableMethod(this.prototype, name, fn, chainingBehavior); }; + Assertion.addChainableNoop = function(name, fn) { + util.addChainableMethod(this.prototype, name, function() { }, fn); + }; + Assertion.overwriteProperty = function (name, fn) { util.overwriteProperty(this.prototype, name, fn); }; diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index 97a85d124..cdcd2f5b3 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -190,7 +190,7 @@ module.exports = function (chai, _) { * @api public */ - Assertion.addProperty('ok', function () { + Assertion.addChainableNoop('ok', function () { this.assert( flag(this, 'object') , 'expected #{this} to be truthy' @@ -209,7 +209,7 @@ module.exports = function (chai, _) { * @api public */ - Assertion.addProperty('true', function () { + Assertion.addChainableNoop('true', function () { this.assert( true === flag(this, 'object') , 'expected #{this} to be true' @@ -230,7 +230,7 @@ module.exports = function (chai, _) { * @api public */ - Assertion.addProperty('false', function () { + Assertion.addChainableNoop('false', function () { this.assert( false === flag(this, 'object') , 'expected #{this} to be false' @@ -251,7 +251,7 @@ module.exports = function (chai, _) { * @api public */ - Assertion.addProperty('null', function () { + Assertion.addChainableNoop('null', function () { this.assert( null === flag(this, 'object') , 'expected #{this} to be null' @@ -271,7 +271,7 @@ module.exports = function (chai, _) { * @api public */ - Assertion.addProperty('undefined', function () { + Assertion.addChainableNoop('undefined', function () { this.assert( undefined === flag(this, 'object') , 'expected #{this} to be undefined' @@ -296,7 +296,7 @@ module.exports = function (chai, _) { * @api public */ - Assertion.addProperty('exist', function () { + Assertion.addChainableNoop('exist', function () { this.assert( null != flag(this, 'object') , 'expected #{this} to exist' @@ -320,7 +320,7 @@ module.exports = function (chai, _) { * @api public */ - Assertion.addProperty('empty', function () { + Assertion.addChainableNoop('empty', function () { var obj = flag(this, 'object') , expected = obj; @@ -361,8 +361,8 @@ module.exports = function (chai, _) { ); } - Assertion.addProperty('arguments', checkArguments); - Assertion.addProperty('Arguments', checkArguments); + Assertion.addChainableNoop('arguments', checkArguments); + Assertion.addChainableNoop('Arguments', checkArguments); /** * ### .equal(value) diff --git a/test/expect.js b/test/expect.js index bd03a50c9..6cbf6f854 100644 --- a/test/expect.js +++ b/test/expect.js @@ -15,6 +15,9 @@ describe('expect', function () { expect(false).to.not.be.true; expect(1).to.not.be.true; + expect(true).to.be.true(); + expect(true).to.be.true().and.not.false; + err(function(){ expect('test').to.be.true; }, "expected 'test' to be true") @@ -26,6 +29,9 @@ describe('expect', function () { expect(1).to.be.ok; expect(0).to.not.be.ok; + expect(true).to.be.ok(); + expect(true).to.be.ok().and.not.false; + err(function(){ expect('').to.be.ok; }, "expected '' to be truthy"); @@ -40,6 +46,9 @@ describe('expect', function () { expect(true).to.not.be.false; expect(0).to.not.be.false; + expect(false).to.be.false(); + expect(false).to.be.false().and.not.true; + err(function(){ expect('').to.be.false; }, "expected '' to be false") @@ -49,6 +58,9 @@ describe('expect', function () { expect(null).to.be.null; expect(false).to.not.be.null; + expect(null).to.be.null(); + expect(null).to.be.null().and.not.ok(); + err(function(){ expect('').to.be.null; }, "expected '' to be null") @@ -59,6 +71,9 @@ describe('expect', function () { expect(undefined).to.be.undefined; expect(null).to.not.be.undefined; + expect(undefined).to.be.undefined(); + expect(undefined).to.be.undefined().and.not.ok(); + err(function(){ expect('').to.be.undefined; }, "expected '' to be undefined") @@ -69,6 +84,9 @@ describe('expect', function () { , bar; expect(foo).to.exist; expect(bar).to.not.exist; + + expect(foo).to.exist(); + expect(foo).to.exist().and.contain('bar'); }); it('arguments', function(){ @@ -77,6 +95,9 @@ describe('expect', function () { expect([]).to.not.be.arguments; expect(args).to.be.an('arguments').and.be.arguments; expect([]).to.be.an('array').and.not.be.Arguments; + + expect(args).to.be.Arguments(); + expect(args).to.be.arguments().and.not.empty; }); it('.equal()', function(){ @@ -355,6 +376,9 @@ describe('expect', function () { expect({}).to.be.empty; expect({foo: 'bar'}).not.to.be.empty; + expect('').to.be.empty(); + expect('').to.be.empty().and.exist; + err(function(){ expect('').not.to.be.empty; }, "expected \'\' not to be empty"); From cc1357d344b1afe97029b2c2c17806ccf47bf401 Mon Sep 17 00:00:00 2001 From: Joshua Perry Date: Mon, 13 Oct 2014 18:00:07 -0600 Subject: [PATCH 2/7] Change test to not trigger argument bug --- test/expect.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/expect.js b/test/expect.js index 6cbf6f854..11c042492 100644 --- a/test/expect.js +++ b/test/expect.js @@ -97,7 +97,7 @@ describe('expect', function () { expect([]).to.be.an('array').and.not.be.Arguments; expect(args).to.be.Arguments(); - expect(args).to.be.arguments().and.not.empty; + expect(args).to.be.arguments().and.be.ok; }); it('.equal()', function(){ From 9142ce0cfd919444bf68cd6c20f5e3b7a3b9abb9 Mon Sep 17 00:00:00 2001 From: Joshua Perry Date: Tue, 4 Nov 2014 09:25:31 -0700 Subject: [PATCH 3/7] Make the NOOP function a shared constant --- lib/chai/assertion.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/chai/assertion.js b/lib/chai/assertion.js index 6ac8db6d5..dcded9e80 100644 --- a/lib/chai/assertion.js +++ b/lib/chai/assertion.js @@ -6,6 +6,7 @@ */ var config = require('./config'); +var NOOP = function() { }; module.exports = function (_chai, util) { /*! @@ -70,7 +71,7 @@ module.exports = function (_chai, util) { }; Assertion.addChainableNoop = function(name, fn) { - util.addChainableMethod(this.prototype, name, function() { }, fn); + util.addChainableMethod(this.prototype, name, NOOP, fn); }; Assertion.overwriteProperty = function (name, fn) { From bbd3aafbe59e78415f3782f3f810d4bf83c3c8c4 Mon Sep 17 00:00:00 2001 From: Joshua Perry Date: Tue, 4 Nov 2014 10:24:30 -0700 Subject: [PATCH 4/7] Add docs for function style NOOP asserts --- lib/chai/core/assertions.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index cdcd2f5b3..63e09302a 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -185,7 +185,12 @@ module.exports = function (chai, _) { * expect(false).to.not.be.ok; * expect(undefined).to.not.be.ok; * expect(null).to.not.be.ok; + * expect(null).to.not.be.ok; + * + * Can also be used in function form which will satiate certain linter errors. * + * expect('everthing').to.be.ok(); + * * @name ok * @api public */ @@ -205,6 +210,10 @@ module.exports = function (chai, _) { * expect(true).to.be.true; * expect(1).to.not.be.true; * + * Can also be used in function form which will satiate certain linter errors. + * + * expect(true).to.be.true(); + * * @name true * @api public */ @@ -226,6 +235,10 @@ module.exports = function (chai, _) { * expect(false).to.be.false; * expect(0).to.not.be.false; * + * Can also be used in function form which will satiate certain linter errors. + * + * expect(false).to.be.false(); + * * @name false * @api public */ @@ -247,6 +260,10 @@ module.exports = function (chai, _) { * expect(null).to.be.null; * expect(undefined).not.to.be.null; * + * Can also be used in function form which will satiate certain linter errors. + * + * expect(null).to.be.null(); + * * @name null * @api public */ @@ -267,6 +284,10 @@ module.exports = function (chai, _) { * expect(undefined).to.be.undefined; * expect(null).to.not.be.undefined; * + * Can also be used in function form which will satiate certain linter errors. + * + * expect(undefined).to.be.undefined(); + * * @name undefined * @api public */ @@ -292,6 +313,10 @@ module.exports = function (chai, _) { * expect(bar).to.not.exist; * expect(baz).to.not.exist; * + * Can also be used in function form which will satiate certain linter errors. + * + * expect(foo).to.exist(); + * * @name exist * @api public */ @@ -316,6 +341,10 @@ module.exports = function (chai, _) { * expect('').to.be.empty; * expect({}).to.be.empty; * + * Can also be used in function form which will satiate certain linter errors. + * + * expect([]).to.be.empty(); + * * @name empty * @api public */ @@ -346,6 +375,12 @@ module.exports = function (chai, _) { * expect(arguments).to.be.arguments; * } * + * Can also be used in function form which will satiate certain linter errors. + * + * function test () { + * expect(arguments).to.be.arguments(); + * } + * * @name arguments * @alias Arguments * @api public From c448e1de7c2e3a5af21bc9b203536f3e1db81786 Mon Sep 17 00:00:00 2001 From: Joshua Perry Date: Wed, 5 Nov 2014 07:25:26 -0700 Subject: [PATCH 5/7] Revise documentation wording --- lib/chai/core/assertions.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index 63e09302a..875603df4 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -185,9 +185,8 @@ module.exports = function (chai, _) { * expect(false).to.not.be.ok; * expect(undefined).to.not.be.ok; * expect(null).to.not.be.ok; - * expect(null).to.not.be.ok; * - * Can also be used in function form which will satiate certain linter errors. + * Can also be used as a function, which prevents some linter errors. * * expect('everthing').to.be.ok(); * @@ -210,7 +209,7 @@ module.exports = function (chai, _) { * expect(true).to.be.true; * expect(1).to.not.be.true; * - * Can also be used in function form which will satiate certain linter errors. + * Can also be used as a function, which prevents some linter errors. * * expect(true).to.be.true(); * @@ -235,7 +234,7 @@ module.exports = function (chai, _) { * expect(false).to.be.false; * expect(0).to.not.be.false; * - * Can also be used in function form which will satiate certain linter errors. + * Can also be used as a function, which prevents some linter errors. * * expect(false).to.be.false(); * @@ -260,7 +259,7 @@ module.exports = function (chai, _) { * expect(null).to.be.null; * expect(undefined).not.to.be.null; * - * Can also be used in function form which will satiate certain linter errors. + * Can also be used as a function, which prevents some linter errors. * * expect(null).to.be.null(); * @@ -284,7 +283,7 @@ module.exports = function (chai, _) { * expect(undefined).to.be.undefined; * expect(null).to.not.be.undefined; * - * Can also be used in function form which will satiate certain linter errors. + * Can also be used as a function, which prevents some linter errors. * * expect(undefined).to.be.undefined(); * @@ -313,7 +312,7 @@ module.exports = function (chai, _) { * expect(bar).to.not.exist; * expect(baz).to.not.exist; * - * Can also be used in function form which will satiate certain linter errors. + * Can also be used as a function, which prevents some linter errors. * * expect(foo).to.exist(); * @@ -341,7 +340,7 @@ module.exports = function (chai, _) { * expect('').to.be.empty; * expect({}).to.be.empty; * - * Can also be used in function form which will satiate certain linter errors. + * Can also be used as a function, which prevents some linter errors. * * expect([]).to.be.empty(); * @@ -375,7 +374,7 @@ module.exports = function (chai, _) { * expect(arguments).to.be.arguments; * } * - * Can also be used in function form which will satiate certain linter errors. + * Can also be used as a function, which prevents some linter errors. * * function test () { * expect(arguments).to.be.arguments(); From b4c0424ffb8d7059846ebbd89c56628e32b0d5af Mon Sep 17 00:00:00 2001 From: Joshua Perry Date: Wed, 5 Nov 2014 07:30:45 -0700 Subject: [PATCH 6/7] Add unit tests for chained terminating property asserts --- test/expect.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/expect.js b/test/expect.js index 11c042492..5844400b4 100644 --- a/test/expect.js +++ b/test/expect.js @@ -17,6 +17,7 @@ describe('expect', function () { expect(true).to.be.true(); expect(true).to.be.true().and.not.false; + expect(true).to.be.true.and.not.false(); err(function(){ expect('test').to.be.true; @@ -31,6 +32,7 @@ describe('expect', function () { expect(true).to.be.ok(); expect(true).to.be.ok().and.not.false; + expect(true).to.be.ok.and.not.false(); err(function(){ expect('').to.be.ok; @@ -48,6 +50,7 @@ describe('expect', function () { expect(false).to.be.false(); expect(false).to.be.false().and.not.true; + expect(false).to.be.false.and.not.true(); err(function(){ expect('').to.be.false; @@ -60,6 +63,7 @@ describe('expect', function () { expect(null).to.be.null(); expect(null).to.be.null().and.not.ok(); + expect(null).to.be.null.and.not.ok(); err(function(){ expect('').to.be.null; @@ -73,6 +77,7 @@ describe('expect', function () { expect(undefined).to.be.undefined(); expect(undefined).to.be.undefined().and.not.ok(); + expect(undefined).to.be.undefined.and.not.ok(); err(function(){ expect('').to.be.undefined; @@ -98,6 +103,7 @@ describe('expect', function () { expect(args).to.be.Arguments(); expect(args).to.be.arguments().and.be.ok; + expect(args).to.be.arguments.and.be.ok(); }); it('.equal()', function(){ From 867563b0dbae7801c86495fdcacfba2f63028a6b Mon Sep 17 00:00:00 2001 From: Joshua Perry Date: Wed, 5 Nov 2014 10:14:28 -0700 Subject: [PATCH 7/7] Add a couple more unit tests --- test/expect.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/expect.js b/test/expect.js index 5844400b4..905e2bd8d 100644 --- a/test/expect.js +++ b/test/expect.js @@ -92,6 +92,7 @@ describe('expect', function () { expect(foo).to.exist(); expect(foo).to.exist().and.contain('bar'); + expect(foo).to.exist.and.contain('bar'); }); it('arguments', function(){ @@ -384,6 +385,7 @@ describe('expect', function () { expect('').to.be.empty(); expect('').to.be.empty().and.exist; + expect('').to.be.empty.and.exist; err(function(){ expect('').not.to.be.empty;