From 07de33e2f39cdf3b03fd13c1e675bb6752815fc1 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Thu, 22 Jun 2017 00:54:44 +0300 Subject: [PATCH 1/7] fix error messages tests --- test/assert.js | 16 ++++++++-------- test/expect.js | 18 +++++++++--------- test/should.js | 8 ++++---- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/test/assert.js b/test/assert.js index 9a36f59dd..aa251b19e 100644 --- a/test/assert.js +++ b/test/assert.js @@ -653,19 +653,19 @@ describe('assert', function () { err(function(){ assert.include(true, true, 'blah'); - }, "blah: object tested must be an array, an object, or a string, but boolean given"); + }, "blah: object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but boolean given"); err(function () { assert.include(42, 'bar'); - }, "object tested must be an array, an object, or a string, but number given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but number given"); err(function(){ assert.include(null, 42); - }, "object tested must be an array, an object, or a string, but null given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but null given"); err(function () { assert.include(undefined, 'bar'); - }, "object tested must be an array, an object, or a string, but undefined given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but undefined given"); }); it('notInclude', function () { @@ -699,19 +699,19 @@ describe('assert', function () { err(function(){ assert.notInclude(true, true, 'blah'); - }, "blah: object tested must be an array, an object, or a string, but boolean given"); + }, "blah: object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but boolean given"); err(function () { assert.notInclude(42, 'bar'); - }, "object tested must be an array, an object, or a string, but number given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but number given"); err(function(){ assert.notInclude(null, 42); - }, "object tested must be an array, an object, or a string, but null given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but null given"); err(function () { assert.notInclude(undefined, 'bar'); - }, "object tested must be an array, an object, or a string, but undefined given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but undefined given"); err(function () { assert.notInclude('foobar', 'bar'); diff --git a/test/expect.js b/test/expect.js index 6f3f45d33..f66dfc3ed 100644 --- a/test/expect.js +++ b/test/expect.js @@ -1748,39 +1748,39 @@ describe('expect', function () { err(function(){ expect(true).to.include(true, 'blah'); - }, "blah: object tested must be an array, an object, or a string, but boolean given"); + }, "blah: object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but boolean given"); err(function(){ expect(true, 'blah').to.include(true); - }, "blah: object tested must be an array, an object, or a string, but boolean given"); + }, "blah: object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but boolean given"); err(function(){ expect(42.0).to.include(42); - }, "object tested must be an array, an object, or a string, but number given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but number given"); err(function(){ expect(null).to.include(42); - }, "object tested must be an array, an object, or a string, but null given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but null given"); err(function(){ expect(undefined).to.include(42); - }, "object tested must be an array, an object, or a string, but undefined given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but undefined given"); err(function(){ expect(true).to.not.include(true); - }, "object tested must be an array, an object, or a string, but boolean given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but boolean given"); err(function(){ expect(42.0).to.not.include(42); - }, "object tested must be an array, an object, or a string, but number given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but number given"); err(function(){ expect(null).to.not.include(42); - }, "object tested must be an array, an object, or a string, but null given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but null given"); err(function(){ expect(undefined).to.not.include(42); - }, "object tested must be an array, an object, or a string, but undefined given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but undefined given"); }); it('deep.include()', function () { diff --git a/test/should.js b/test/should.js index e10a39b24..ecedc8fc4 100644 --- a/test/should.js +++ b/test/should.js @@ -1385,19 +1385,19 @@ describe('should', function() { err(function(){ (true).should.include(true, 'blah'); - }, "blah: object tested must be an array, an object, or a string, but boolean given"); + }, "blah: object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but boolean given"); err(function(){ (42).should.include(4); - }, "object tested must be an array, an object, or a string, but number given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but number given"); err(function(){ (true).should.not.include(true); - }, "object tested must be an array, an object, or a string, but boolean given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but boolean given"); err(function(){ (42).should.not.include(4); - }, "object tested must be an array, an object, or a string, but number given"); + }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but number given"); }); it('deep.include()', function () { From 86a9ea369206cb14464ab80149e24f252153bfd4 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Thu, 22 Jun 2017 02:20:08 +0300 Subject: [PATCH 2/7] add tests --- test/assert.js | 103 +++++++++++++++++++++++++++++++++++++++++++++++++ test/expect.js | 70 +++++++++++++++++++++++++++++++++ test/should.js | 70 +++++++++++++++++++++++++++++++++ 3 files changed, 243 insertions(+) diff --git a/test/assert.js b/test/assert.js index aa251b19e..db1a3b63d 100644 --- a/test/assert.js +++ b/test/assert.js @@ -633,6 +633,42 @@ describe('assert', function () { assert.include({foo: obj1, bar: obj2}, {foo: obj1}); assert.include({foo: obj1, bar: obj2}, {foo: obj1, bar: obj2}); + if (typeof Map === 'function') { + var map = new Map(); + var val = [{a: 1}]; + map.set('a', val); + map.set('b', 2); + + assert.include(map, val); + assert.include(map, 2); + } + + if (typeof Set === 'function') { + var set = new Set(); + var val = [{a: 1}]; + set.add(val); + set.add(2); + + assert.include(set, val); + assert.include(set, 2); + } + + if (typeof WeakMap === 'function') { + var wm = new WeakMap(); + var val = [{a: 1}]; + wm.set(val, 1); + + assert.include(wm, val); + } + + if (typeof WeakSet === 'function') { + var ws = new WeakSet(); + var val = [{a: 1}]; + ws.add(val); + + assert.include(ws, val); + } + if (typeof Symbol === 'function') { var sym1 = Symbol() , sym2 = Symbol(); @@ -678,6 +714,47 @@ describe('assert', function () { assert.notInclude({foo: obj1, bar: obj2}, {foo: {a: 1}}); assert.notInclude({foo: obj1, bar: obj2}, {foo: obj1, bar: {b: 2}}); + if (typeof Map === 'function') { + var map = new Map(); + var val = [{a: 1}]; + map.set('a', val); + map.set('b', 2); + + assert.notInclude(map, [{a: 1}]); + assert.notInclude(map, 3); + } + + if (typeof Set === 'function') { + var set = new Set(); + var val = [{a: 1}]; + set.add(val); + set.add(2); + + assert.include(set, val); + assert.include(set, 2); + + assert.notInclude(set, [{a: 1}]); + assert.notInclude(set, 3); + } + + if (typeof WeakMap === 'function') { + var wm = new WeakMap(); + var val = [{a: 1}]; + wm.set(val, 1); + + assert.notInclude(wm, [{a: 1}]); + assert.notInclude(wm, {}); + } + + if (typeof WeakSet === 'function') { + var ws = new WeakSet(); + var val = [{a: 1}]; + ws.add(val); + + assert.notInclude(ws, [{a: 1}]); + assert.notInclude(ws, {}); + } + if (typeof Symbol === 'function') { var sym1 = Symbol() , sym2 = Symbol() @@ -731,6 +808,32 @@ describe('assert', function () { assert.notDeepInclude({foo: obj1, bar: obj2}, {baz: {a: 1}}); assert.notDeepInclude({foo: obj1, bar: obj2}, {foo: {a: 1}, bar: {b: 9}}); + if (typeof Map === 'function') { + var map = new Map(); + map.set(1, [{a: 1}]); + + assert.deepInclude(map, [{a: 1}]); + } + + if (typeof Set === 'function') { + var set = new Set(); + set.add([{a: 1}]); + + assert.deepInclude(set, [{a: 1}]); + } + + if (typeof WeakMap === 'function') { + err(function() { + assert.deepInclude(new WeakMap(), {}, 'foo'); + }, 'foo: unable to use .deep.include with weak collection'); + } + + if (typeof WeakSet === 'function') { + err(function() { + assert.deepInclude(new WeakSet(), {}); + }, 'unable to use .deep.include with weak collection'); + } + err(function () { assert.deepInclude([obj1, obj2], {a: 9}, 'blah'); }, "blah: expected [ { a: 1 }, { b: 2 } ] to deep include { a: 9 }"); diff --git a/test/expect.js b/test/expect.js index f66dfc3ed..22a09417f 100644 --- a/test/expect.js +++ b/test/expect.js @@ -1694,6 +1694,50 @@ describe('expect', function () { expect({foo: obj1, bar: obj2}).to.not.include({foo: {a: 1}}); expect({foo: obj1, bar: obj2}).to.not.include({foo: obj1, bar: {b: 2}}); + if (typeof Map === 'function') { + var map = new Map(); + var val = [{a: 1}]; + map.set('a', val); + map.set('b', 2); + + expect(map).to.include(val); + expect(map).to.not.include([{a: 1}]); + expect(map).to.include(2); + expect(map).to.not.include(3); + } + + if (typeof Set === 'function') { + var set = new Set(); + var val = [{a: 1}]; + set.add(val); + set.add(2); + + expect(set).to.include(val); + expect(set).to.not.include([{a: 1}]); + expect(set).to.include(2); + expect(set).to.not.include(3); + } + + if (typeof WeakMap === 'function') { + var wm = new WeakMap(); + var val = [{a: 1}]; + wm.set(val, 1); + + expect(wm).to.include(val); + expect(wm).to.not.include([{a: 1}]); + expect(wm).to.not.include({}); + } + + if (typeof WeakSet === 'function') { + var ws = new WeakSet(); + var val = [{a: 1}]; + ws.add(val); + + expect(ws).to.include(val); + expect(ws).to.not.include([{a: 1}]); + expect(ws).to.not.include({}); + } + if (typeof Symbol === 'function') { var sym1 = Symbol() , sym2 = Symbol() @@ -1796,6 +1840,32 @@ describe('expect', function () { expect({foo: obj1, bar: obj2}).to.not.deep.include({baz: {a: 1}}); expect({foo: obj1, bar: obj2}).to.not.deep.include({foo: {a: 1}, bar: {b: 9}}); + if (typeof Map === 'function') { + var map = new Map(); + map.set(1, [{a: 1}]); + + expect(map).to.deep.include([{a: 1}]); + } + + if (typeof Set === 'function') { + var set = new Set(); + set.add([{a: 1}]); + + expect(set).to.deep.include([{a: 1}]); + } + + if (typeof WeakMap === 'function') { + err(function() { + expect(new WeakMap()).to.deep.include({}, 'foo'); + }, 'foo: unable to use .deep.include with weak collection'); + } + + if (typeof WeakSet === 'function') { + err(function() { + expect(new WeakSet()).to.deep.include({}); + }, 'unable to use .deep.include with weak collection'); + } + err(function () { expect([obj1, obj2]).to.deep.include({a: 9}, 'blah'); }, "blah: expected [ { a: 1 }, { b: 2 } ] to deep include { a: 9 }"); diff --git a/test/should.js b/test/should.js index ecedc8fc4..f98ad2a3e 100644 --- a/test/should.js +++ b/test/should.js @@ -1343,6 +1343,50 @@ describe('should', function() { ({foo: obj1, bar: obj2}).should.not.include({foo: {a: 1}}); ({foo: obj1, bar: obj2}).should.not.include({foo: obj1, bar: {b: 2}}); + if (typeof Map === 'function') { + var map = new Map(); + var val = [{a: 1}]; + map.set('a', val); + map.set('b', 2); + + map.should.include(val); + map.should.not.include([{a: 1}]); + map.should.include(2); + map.should.not.include(3); + } + + if (typeof Set === 'function') { + var set = new Set(); + var val = [{a: 1}]; + set.add(val); + set.add(2); + + set.should.include(val); + set.should.not.include([{a: 1}]); + set.should.include(2); + set.should.not.include(3); + } + + if (typeof WeakMap === 'function') { + var wm = new WeakMap(); + var val = [{a: 1}]; + wm.set(val, 1); + + wm.should.include(val); + wm.should.not.include([{a: 1}]); + wm.should.not.include({}); + } + + if (typeof WeakSet === 'function') { + var ws = new WeakSet(); + var val = [{a: 1}]; + ws.add(val); + + ws.should.include(val); + ws.should.not.include([{a: 1}]); + ws.should.not.include({}); + } + if (typeof Symbol === 'function') { var sym1 = Symbol() , sym2 = Symbol() @@ -1413,6 +1457,32 @@ describe('should', function() { ({foo: obj1, bar: obj2}).should.not.deep.include({baz: {a: 1}}); ({foo: obj1, bar: obj2}).should.not.deep.include({foo: {a: 1}, bar: {b: 9}}); + if (typeof Map === 'function') { + var map = new Map(); + + map.set(1, [{a: 1}]); + map.should.deep.include([{a: 1}]); + } + + if (typeof Set === 'function') { + var set = new Set(); + + set.add([{a: 1}]); + set.should.deep.include([{a: 1}]); + } + + if (typeof WeakMap === 'function') { + err(function() { + new WeakMap().should.deep.include({}, 'foo'); + }, 'foo: unable to use .deep.include with weak collection'); + } + + if (typeof WeakSet === 'function') { + err(function() { + new WeakSet().should.deep.include({}); + }, 'unable to use .deep.include with weak collection'); + } + err(function () { [obj1, obj2].should.deep.include({a: 9}, 'blah'); }, "blah: expected [ { a: 1 }, { b: 2 } ] to deep include { a: 9 }"); From 4d56c3df23773b98d1e3f6692d25a6d5cf7c845e Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Thu, 22 Jun 2017 02:20:14 +0300 Subject: [PATCH 3/7] add implementation --- lib/chai/core/assertions.js | 53 ++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index d1330e49f..683bdafdc 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -453,21 +453,16 @@ module.exports = function (chai, _) { flag(this, 'contains', true); } - function isDeepIncluded (arr, val) { - return arr.some(function (arrVal) { - return _.eql(arrVal, val); - }); - } - function include (val, msg) { if (msg) flag(this, 'message', msg); - _.expectTypes(this, ['array', 'object', 'string']); + _.expectTypes(this, [ + 'array', 'object', 'string', + 'map', 'set', 'weakmap', 'weakset', + ]); var obj = flag(this, 'object') - , objType = _.type(obj).toLowerCase() - , isDeep = flag(this, 'deep') - , descriptor = isDeep ? 'deep ' : ''; + , objType = _.type(obj).toLowerCase(); // This block is for asserting a subset of properties in an object. if (objType === 'object') { @@ -504,10 +499,42 @@ module.exports = function (chai, _) { return; } - // Assert inclusion in an array or substring in a string. + var isDeep = flag(this, 'deep') + , descriptor = isDeep ? 'deep ' : '' + , included = false; + + switch (objType) { + case 'string': + included = obj.indexOf(val) !== -1; + break; + + case 'weakmap': + case 'weakset': + if (isDeep) { + var flagMsg = flag(this, 'message') + , ssfi = flag(this, 'ssfi'); + flagMsg = flagMsg ? flagMsg + ': ' : ''; + + throw new AssertionError( + flagMsg + 'unable to use .deep.include with weak collection', + undefined, + ssfi + ); + } + + included = obj.has(val); + break; + + default: + var isEql = isDeep ? _.eql : function(a, b) { return a === b; }; + obj.forEach(function (item) { + included = included || isEql(item, val); + }); + } + + // Assert inclusion in collection or substring in a string. this.assert( - objType === 'string' || !isDeep ? ~obj.indexOf(val) - : isDeepIncluded(obj, val) + included , 'expected #{this} to ' + descriptor + 'include ' + _.inspect(val) , 'expected #{this} to not ' + descriptor + 'include ' + _.inspect(val)); } From 0e809c3f3e831d5bab93c10f19cc8cdf85441db6 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Thu, 22 Jun 2017 16:54:38 +0300 Subject: [PATCH 4/7] performance tweaks --- lib/chai/core/assertions.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index 683bdafdc..54d690649 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -449,6 +449,10 @@ module.exports = function (chai, _) { * @api public */ + function SameValueZero(a, b) { + return (_.isNaN(a) && _.isNaN(b)) || a === b; + } + function includeChainingBehavior () { flag(this, 'contains', true); } @@ -525,11 +529,32 @@ module.exports = function (chai, _) { included = obj.has(val); break; - default: - var isEql = isDeep ? _.eql : function(a, b) { return a === b; }; + case 'map': + var isEql = isDeep ? _.eql : SameValueZero; obj.forEach(function (item) { included = included || isEql(item, val); }); + break; + + case 'set': + if (isDeep) { + obj.forEach(function (item) { + included = included || _.eql(item, val); + }); + } else { + included = obj.has(val); + } + break; + + case 'array': + if (isDeep) { + included = obj.some(function (item) { + return _.eql(item, val); + }) + } else { + included = obj.indexOf(val) !== -1; + } + break; } // Assert inclusion in collection or substring in a string. From defc699b5f358c2de4f987d6833d7c06b9a8486d Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Thu, 22 Jun 2017 16:54:46 +0300 Subject: [PATCH 5/7] add tests for SameValueZero --- test/assert.js | 8 ++++++++ test/expect.js | 8 ++++++++ test/should.js | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/test/assert.js b/test/assert.js index db1a3b63d..73da60473 100644 --- a/test/assert.js +++ b/test/assert.js @@ -638,9 +638,13 @@ describe('assert', function () { var val = [{a: 1}]; map.set('a', val); map.set('b', 2); + map.set('c', -0); + map.set('d', NaN); assert.include(map, val); assert.include(map, 2); + assert.include(map, 0); + assert.include(map, NaN); } if (typeof Set === 'function') { @@ -648,9 +652,13 @@ describe('assert', function () { var val = [{a: 1}]; set.add(val); set.add(2); + set.add(-0); + set.add(NaN); assert.include(set, val); assert.include(set, 2); + assert.include(set, 0); + assert.include(set, NaN); } if (typeof WeakMap === 'function') { diff --git a/test/expect.js b/test/expect.js index 22a09417f..2b4997b43 100644 --- a/test/expect.js +++ b/test/expect.js @@ -1699,11 +1699,15 @@ describe('expect', function () { var val = [{a: 1}]; map.set('a', val); map.set('b', 2); + map.set('c', -0); + map.set('d', NaN); expect(map).to.include(val); expect(map).to.not.include([{a: 1}]); expect(map).to.include(2); expect(map).to.not.include(3); + expect(map).to.include(0); + expect(map).to.include(NaN); } if (typeof Set === 'function') { @@ -1711,11 +1715,15 @@ describe('expect', function () { var val = [{a: 1}]; set.add(val); set.add(2); + set.add(-0); + set.add(NaN); expect(set).to.include(val); expect(set).to.not.include([{a: 1}]); expect(set).to.include(2); expect(set).to.not.include(3); + expect(set).to.include(0); + expect(set).to.include(NaN); } if (typeof WeakMap === 'function') { diff --git a/test/should.js b/test/should.js index f98ad2a3e..44169743a 100644 --- a/test/should.js +++ b/test/should.js @@ -1348,11 +1348,15 @@ describe('should', function() { var val = [{a: 1}]; map.set('a', val); map.set('b', 2); + map.set('c', -0); + map.set('d', NaN); map.should.include(val); map.should.not.include([{a: 1}]); map.should.include(2); map.should.not.include(3); + map.should.include(0); + map.should.include(NaN); } if (typeof Set === 'function') { @@ -1360,11 +1364,15 @@ describe('should', function() { var val = [{a: 1}]; set.add(val); set.add(2); + set.add(-0); + set.add(NaN); set.should.include(val); set.should.not.include([{a: 1}]); set.should.include(2); set.should.not.include(3); + set.should.include(0); + set.should.include(NaN); } if (typeof WeakMap === 'function') { From 039564dd8916110556a40d19d9030ecb09b3bd25 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Fri, 23 Jun 2017 04:39:03 +0300 Subject: [PATCH 6/7] drop weakmap support --- lib/chai/core/assertions.js | 5 ++--- test/assert.js | 43 +++++++++---------------------------- test/expect.js | 38 ++++++++++---------------------- test/should.js | 28 ++++++------------------ 4 files changed, 29 insertions(+), 85 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index 54d690649..dc62750da 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -462,7 +462,7 @@ module.exports = function (chai, _) { _.expectTypes(this, [ 'array', 'object', 'string', - 'map', 'set', 'weakmap', 'weakset', + 'map', 'set', 'weakset', ]); var obj = flag(this, 'object') @@ -512,7 +512,6 @@ module.exports = function (chai, _) { included = obj.indexOf(val) !== -1; break; - case 'weakmap': case 'weakset': if (isDeep) { var flagMsg = flag(this, 'message') @@ -520,7 +519,7 @@ module.exports = function (chai, _) { flagMsg = flagMsg ? flagMsg + ': ' : ''; throw new AssertionError( - flagMsg + 'unable to use .deep.include with weak collection', + flagMsg + 'unable to use .deep.include with WeakSet', undefined, ssfi ); diff --git a/test/assert.js b/test/assert.js index 73da60473..722bbb560 100644 --- a/test/assert.js +++ b/test/assert.js @@ -661,14 +661,6 @@ describe('assert', function () { assert.include(set, NaN); } - if (typeof WeakMap === 'function') { - var wm = new WeakMap(); - var val = [{a: 1}]; - wm.set(val, 1); - - assert.include(wm, val); - } - if (typeof WeakSet === 'function') { var ws = new WeakSet(); var val = [{a: 1}]; @@ -697,19 +689,19 @@ describe('assert', function () { err(function(){ assert.include(true, true, 'blah'); - }, "blah: object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but boolean given"); + }, "blah: object tested must be an array, a map, an object, a set, a string, or a weakset, but boolean given"); err(function () { assert.include(42, 'bar'); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but number given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but number given"); err(function(){ assert.include(null, 42); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but null given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but null given"); err(function () { assert.include(undefined, 'bar'); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but undefined given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but undefined given"); }); it('notInclude', function () { @@ -745,15 +737,6 @@ describe('assert', function () { assert.notInclude(set, 3); } - if (typeof WeakMap === 'function') { - var wm = new WeakMap(); - var val = [{a: 1}]; - wm.set(val, 1); - - assert.notInclude(wm, [{a: 1}]); - assert.notInclude(wm, {}); - } - if (typeof WeakSet === 'function') { var ws = new WeakSet(); var val = [{a: 1}]; @@ -784,19 +767,19 @@ describe('assert', function () { err(function(){ assert.notInclude(true, true, 'blah'); - }, "blah: object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but boolean given"); + }, "blah: object tested must be an array, a map, an object, a set, a string, or a weakset, but boolean given"); err(function () { assert.notInclude(42, 'bar'); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but number given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but number given"); err(function(){ assert.notInclude(null, 42); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but null given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but null given"); err(function () { assert.notInclude(undefined, 'bar'); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but undefined given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but undefined given"); err(function () { assert.notInclude('foobar', 'bar'); @@ -830,16 +813,10 @@ describe('assert', function () { assert.deepInclude(set, [{a: 1}]); } - if (typeof WeakMap === 'function') { - err(function() { - assert.deepInclude(new WeakMap(), {}, 'foo'); - }, 'foo: unable to use .deep.include with weak collection'); - } - if (typeof WeakSet === 'function') { err(function() { - assert.deepInclude(new WeakSet(), {}); - }, 'unable to use .deep.include with weak collection'); + assert.deepInclude(new WeakSet(), {}, 'foo'); + }, 'foo: unable to use .deep.include with WeakSet'); } err(function () { diff --git a/test/expect.js b/test/expect.js index 2b4997b43..6e2911cee 100644 --- a/test/expect.js +++ b/test/expect.js @@ -1726,16 +1726,6 @@ describe('expect', function () { expect(set).to.include(NaN); } - if (typeof WeakMap === 'function') { - var wm = new WeakMap(); - var val = [{a: 1}]; - wm.set(val, 1); - - expect(wm).to.include(val); - expect(wm).to.not.include([{a: 1}]); - expect(wm).to.not.include({}); - } - if (typeof WeakSet === 'function') { var ws = new WeakSet(); var val = [{a: 1}]; @@ -1800,39 +1790,39 @@ describe('expect', function () { err(function(){ expect(true).to.include(true, 'blah'); - }, "blah: object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but boolean given"); + }, "blah: object tested must be an array, a map, an object, a set, a string, or a weakset, but boolean given"); err(function(){ expect(true, 'blah').to.include(true); - }, "blah: object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but boolean given"); + }, "blah: object tested must be an array, a map, an object, a set, a string, or a weakset, but boolean given"); err(function(){ expect(42.0).to.include(42); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but number given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but number given"); err(function(){ expect(null).to.include(42); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but null given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but null given"); err(function(){ expect(undefined).to.include(42); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but undefined given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but undefined given"); err(function(){ expect(true).to.not.include(true); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but boolean given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but boolean given"); err(function(){ expect(42.0).to.not.include(42); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but number given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but number given"); err(function(){ expect(null).to.not.include(42); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but null given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but null given"); err(function(){ expect(undefined).to.not.include(42); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but undefined given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but undefined given"); }); it('deep.include()', function () { @@ -1862,16 +1852,10 @@ describe('expect', function () { expect(set).to.deep.include([{a: 1}]); } - if (typeof WeakMap === 'function') { - err(function() { - expect(new WeakMap()).to.deep.include({}, 'foo'); - }, 'foo: unable to use .deep.include with weak collection'); - } - if (typeof WeakSet === 'function') { err(function() { - expect(new WeakSet()).to.deep.include({}); - }, 'unable to use .deep.include with weak collection'); + expect(new WeakSet()).to.deep.include({}, 'foo'); + }, 'foo: unable to use .deep.include with WeakSet'); } err(function () { diff --git a/test/should.js b/test/should.js index 44169743a..c093c5eda 100644 --- a/test/should.js +++ b/test/should.js @@ -1375,16 +1375,6 @@ describe('should', function() { set.should.include(NaN); } - if (typeof WeakMap === 'function') { - var wm = new WeakMap(); - var val = [{a: 1}]; - wm.set(val, 1); - - wm.should.include(val); - wm.should.not.include([{a: 1}]); - wm.should.not.include({}); - } - if (typeof WeakSet === 'function') { var ws = new WeakSet(); var val = [{a: 1}]; @@ -1437,19 +1427,19 @@ describe('should', function() { err(function(){ (true).should.include(true, 'blah'); - }, "blah: object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but boolean given"); + }, "blah: object tested must be an array, a map, an object, a set, a string, or a weakset, but boolean given"); err(function(){ (42).should.include(4); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but number given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but number given"); err(function(){ (true).should.not.include(true); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but boolean given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but boolean given"); err(function(){ (42).should.not.include(4); - }, "object tested must be an array, a map, an object, a set, a string, a weakmap, or a weakset, but number given"); + }, "object tested must be an array, a map, an object, a set, a string, or a weakset, but number given"); }); it('deep.include()', function () { @@ -1479,16 +1469,10 @@ describe('should', function() { set.should.deep.include([{a: 1}]); } - if (typeof WeakMap === 'function') { - err(function() { - new WeakMap().should.deep.include({}, 'foo'); - }, 'foo: unable to use .deep.include with weak collection'); - } - if (typeof WeakSet === 'function') { err(function() { - new WeakSet().should.deep.include({}); - }, 'unable to use .deep.include with weak collection'); + new WeakSet().should.deep.include({}, 'foo'); + }, 'foo: unable to use .deep.include with WeakSet'); } err(function () { From df6baddb7fee78c940f6720d97c7d0bd9a5d83be Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Fri, 23 Jun 2017 04:39:08 +0300 Subject: [PATCH 7/7] update docs --- lib/chai/core/assertions.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index dc62750da..6cb87b182 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -330,6 +330,16 @@ module.exports = function (chai, _) { * * expect({a: 1, b: 2, c: 3}).to.include({a: 1, b: 2}); * + * When the target is a Set or WeakSet, `.include` asserts that the given `val` is a + * member of the target. SameValueZero equality algorithm is used. + * + * expect(new Set([1, 2])).to.include(2); + * + * When the target is a Map, `.include` asserts that the given `val` is one of + * the values of the target. SameValueZero equality algorithm is used. + * + * expect(new Map([['a', 1], ['b', 2]])).to.include(2); + * * Because `.include` does different things based on the target's type, it's * important to check the target's type before using `.include`. See the `.a` * doc for info on testing a target's type. @@ -338,8 +348,8 @@ module.exports = function (chai, _) { * * By default, strict (`===`) equality is used to compare array members and * object properties. Add `.deep` earlier in the chain to use deep equality - * instead. See the `deep-eql` project page for info on the deep equality - * algorithm: https://github.com/chaijs/deep-eql. + * instead (WeakSet targets are not supported). See the `deep-eql` project + * page for info on the deep equality algorithm: https://github.com/chaijs/deep-eql. * * // Target array deeply (but not strictly) includes `{a: 1}` * expect([{a: 1}]).to.deep.include({a: 1});