From 5930c0112f9c9154a490851db35247db467db7a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Oliva?= Date: Tue, 12 Mar 2019 16:45:58 +0100 Subject: [PATCH 1/3] test(oneOf): Add failing tests for contain.oneOf --- package-lock.json | 28 +++++++++++++++++++++------- test/expect.js | 6 ++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 625239118..26298c86f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2147,12 +2147,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2167,17 +2169,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -2294,7 +2299,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -2306,6 +2312,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2320,6 +2327,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2327,12 +2335,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -2351,6 +2361,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -2431,7 +2442,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -2443,6 +2455,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -2564,6 +2577,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", diff --git a/test/expect.js b/test/expect.js index 9b4f485b8..74313efb8 100644 --- a/test/expect.js +++ b/test/expect.js @@ -3274,6 +3274,12 @@ describe('expect', function () { var threeFour = [3, [4]]; expect(threeFour).to.be.oneOf([1, 2, threeFour]); + expect([1, 2]).to.contain.oneOf([4,2,5]); + expect([3, 4]).to.not.contain.oneOf([2,1,5]); + + expect('The quick brown fox jumps over the lazy dog').to.contain.oneOf(['cat', 'dog', 'bird']); + expect('The quick brown fox jumps over the lazy dog').to.not.contain.oneOf(['elephant', 'pigeon', 'lynx']); + err(function () { expect(1).to.be.oneOf([2, 3], 'blah'); }, "blah: expected 1 to be one of [ 2, 3 ]"); From 7eaf684b1a8c2e081fa01d91e0655ba988849301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Oliva?= Date: Wed, 26 Jun 2019 23:18:10 +0200 Subject: [PATCH 2/3] feat(oneOf): expect(value).to.contain.oneOf([]) --- lib/chai/core/assertions.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index cabcf622f..d9daff0d5 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -3135,16 +3135,27 @@ module.exports = function (chai, _) { if (msg) flag(this, 'message', msg); var expected = flag(this, 'object') , flagMsg = flag(this, 'message') - , ssfi = flag(this, 'ssfi'); + , ssfi = flag(this, 'ssfi') + , contains = flag(this, 'contains'); new Assertion(list, flagMsg, ssfi, true).to.be.an('array'); - this.assert( + if (contains) { + this.assert( + list.some(possibility => expected.indexOf(possibility) > -1) + , 'expected #{this} to contain one of #{exp}' + , 'expected #{this} to not contain one of #{exp}' + , list + , expected + ); + } else { + this.assert( list.indexOf(expected) > -1 - , 'expected #{this} to be one of #{exp}' - , 'expected #{this} to not be one of #{exp}' - , list - , expected - ); + , 'expected #{this} to be one of #{exp}' + , 'expected #{this} to not be one of #{exp}' + , list + , expected + ); + } } Assertion.addMethod('oneOf', oneOf); From 9d2f6dc989813012a47dc2da8a05b7049c1c59cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Oliva?= Date: Tue, 25 Jun 2019 13:03:01 +0200 Subject: [PATCH 3/3] docs(oneOf): Add contains flag to oneOf documentation --- lib/chai/core/assertions.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index d9daff0d5..f2a74af16 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -3117,6 +3117,14 @@ module.exports = function (chai, _) { * expect(1).to.equal(1); // Recommended * expect(1).to.not.be.oneOf([2, 3, 4]); // Not recommended * + * It can also be chained with `.contain` or `.include`, which will work with + * both arrays and strings: + * + * expect('Today is sunny').to.contain.oneOf(['sunny', 'cloudy']) + * expect('Today is rainy').to.not.contain.oneOf(['sunny', 'cloudy']) + * expect([1,2,3]).to.contain.oneOf([3,4,5]) + * expect([1,2,3]).to.not.contain.oneOf([4,5,6]) + * * `.oneOf` accepts an optional `msg` argument which is a custom error message * to show when the assertion fails. The message can also be given as the * second argument to `expect`.