From 6956d88d0ca5a1d963c7d38eaaa2f3848a483479 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 19 Jan 2018 11:03:02 +0100 Subject: [PATCH 1/5] doc: document asserts Weak(Map|Set) behavior Right now it is not documentated that WeakMap entries are not compared. This might result in some confusion. This adds a note about the behavior in `assert.deepStrictEqual`. This documentation is also references in `util.isDeepStrictEqual`, so we do not have to document it again for that function as the underlying algorithm is the same. --- doc/api/assert.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/doc/api/assert.md b/doc/api/assert.md index c3735440932e25..da8510f6440f07 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -213,6 +213,7 @@ are recursively evaluated also by the following rules. * Map keys and Set items are compared unordered. * Recursion stops when both sides differ or both sides encounter a circular reference. +* `WeakMap` and `WeakSet` will return true, no matter what values they contain. ```js const assert = require('assert').strict; @@ -254,6 +255,16 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 }); // OK, because it is the same symbol on both objects. assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 }); // Fails because symbol1 !== symbol2! + +const weakMap1 = new WeakMap(); +const weakMap2 = new WeakMap([1, 1]); +const weakMap3 = new WeakMap(); +weakMap3.unequal = true; + +assert.deepStrictEqual(weakMap1, weakMap2); +// OK, because it is impossible to compare the entries +assert.deepStrictEqual(weakMap1, weakMap3); +// Fails because weakMap3 has a property that weakMap1 does not contain! ``` If the values are not equal, an `AssertionError` is thrown with a `message` @@ -870,6 +881,8 @@ second argument. This might lead to difficult-to-spot errors. [`Set`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Set [`Symbol`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Symbol [`TypeError`]: errors.html#errors_class_typeerror +[`WeakMap`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeapMap +[`WeakSet`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeakSet [`assert.deepEqual()`]: #assert_assert_deepequal_actual_expected_message [`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_message [`assert.notDeepStrictEqual()`]: #assert_assert_notdeepstrictequal_actual_expected_message From ea14fb0997f886116cd42fe73e7b5a08c30d04c7 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 19 Jan 2018 12:14:17 +0100 Subject: [PATCH 2/5] fixup address comments --- doc/api/assert.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/api/assert.md b/doc/api/assert.md index da8510f6440f07..eecd1ac9d5f8b6 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -213,7 +213,8 @@ are recursively evaluated also by the following rules. * Map keys and Set items are compared unordered. * Recursion stops when both sides differ or both sides encounter a circular reference. -* `WeakMap` and `WeakSet` will return true, no matter what values they contain. +* [`WeakMap`][] and [`WeakSet`][] will return true, no matter what values they + contain. ```js const assert = require('assert').strict; @@ -257,7 +258,7 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 }); // Fails because symbol1 !== symbol2! const weakMap1 = new WeakMap(); -const weakMap2 = new WeakMap([1, 1]); +const weakMap2 = new WeakMap([[{}, {}]]); const weakMap3 = new WeakMap(); weakMap3.unequal = true; From 63f2b54c8b8d2ebb012993b57fc75403c22a3e7b Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 24 Jan 2018 13:21:20 +0100 Subject: [PATCH 3/5] fixup --- doc/api/assert.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/assert.md b/doc/api/assert.md index eecd1ac9d5f8b6..4c2848ec1cde04 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -882,7 +882,7 @@ second argument. This might lead to difficult-to-spot errors. [`Set`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Set [`Symbol`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Symbol [`TypeError`]: errors.html#errors_class_typeerror -[`WeakMap`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeapMap +[`WeakMap`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeakMap [`WeakSet`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeakSet [`assert.deepEqual()`]: #assert_assert_deepequal_actual_expected_message [`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_message From 86470fca10e9c94978a153a2c92f8e1d9a5b66e3 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 24 Jan 2018 13:23:21 +0100 Subject: [PATCH 4/5] fixup2 --- doc/api/assert.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/assert.md b/doc/api/assert.md index 4c2848ec1cde04..711c99a1d4c183 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -213,8 +213,8 @@ are recursively evaluated also by the following rules. * Map keys and Set items are compared unordered. * Recursion stops when both sides differ or both sides encounter a circular reference. -* [`WeakMap`][] and [`WeakSet`][] will return true, no matter what values they - contain. +* ['WeakMap'][] and ['WeakSet'][] comparison does not rely on their values. See + below for further details. ```js const assert = require('assert').strict; From 68dbc6e9beeb4e102c88055b84ad179c51ec8bc0 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 24 Jan 2018 13:39:45 +0100 Subject: [PATCH 5/5] fixup ... --- doc/api/assert.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/assert.md b/doc/api/assert.md index 711c99a1d4c183..e3c513befdf72b 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -213,7 +213,7 @@ are recursively evaluated also by the following rules. * Map keys and Set items are compared unordered. * Recursion stops when both sides differ or both sides encounter a circular reference. -* ['WeakMap'][] and ['WeakSet'][] comparison does not rely on their values. See +* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values. See below for further details. ```js