| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ca2c73c commit b0d3bec
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -142,6 +142,7 @@ Generally identical to `assert.deepEqual()` with a few exceptions: | |||
| 142 | 142 | the [Strict Equality Comparison][] too. | |
| 143 | 143 | 3. [Type tags][Object.prototype.toString()] of objects should be the same. | |
| 144 | 144 | 4. [Object wrappers][] are compared both as objects and unwrapped values. | |
| 145 | + 5. `0` and `-0` are not considered equal. | ||
| 145 | 146 | ||
| 146 | 147 | ```js | |
| 147 | 148 | const assert = require('assert'); | |
@@ -179,6 +180,11 @@ assert.deepStrictEqual(new Number(1), new Number(2)); | |||
| 179 | 180 | // Fails because the wrapped number is unwrapped and compared as well. | |
| 180 | 181 | assert.deepStrictEqual(new String('foo'), Object('foo')); | |
| 181 | 182 | // OK because the object and the string are identical when unwrapped. | |
| 183 | + | ||
| 184 | + assert.deepStrictEqual(-0, -0); | ||
| 185 | + // OK | ||
| 186 | + assert.deepStrictEqual(0, -0); | ||
| 187 | + // AssertionError: 0 deepStrictEqual -0 | ||
| 182 | 188 | ``` | |
| 183 | 189 | ||
| 184 | 190 | If the values are not equal, an `AssertionError` is thrown with a `message` | |
@@ -438,6 +444,9 @@ parameter is an instance of an `Error` then it will be thrown instead of the | |||
| 438 | 444 | <!-- YAML | |
| 439 | 445 | added: v1.2.0 | |
| 440 | 446 | changes: | |
| 447 | + - version: REPLACEME | ||
| 448 | + pr-url: https://github.com/nodejs/node/pull/REPLACEME | ||
| 449 | + description: -0 and +0 are not considered equal anymore. | ||
| 441 | 450 | - version: REPLACEME | |
| 442 | 451 | pr-url: https://github.com/nodejs/node/pull/15036 | |
| 443 | 452 | description: NaN is now compared using the [SameValueZero][] comparison. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -118,12 +118,12 @@ function areSimilarRegExps(a, b) { | |||
| 118 | 118 | // For small buffers it's faster to compare the buffer in a loop. The c++ | |
| 119 | 119 | // barrier including the Uint8Array operation takes the advantage of the faster | |
| 120 | 120 | // binary compare otherwise. The break even point was at about 300 characters. | |
| 121 | - function areSimilarTypedArrays(a, b) { | ||
| 121 | + function areSimilarTypedArrays(a, b, max) { | ||
| 122 | 122 | const len = a.byteLength; | |
| 123 | 123 | if (len !== b.byteLength) { | |
| 124 | 124 | return false; | |
| 125 | 125 | } | |
| 126 | - if (len < 300) { | ||
| 126 | + if (len < max) { | ||
| 127 | 127 | for (var offset = 0; offset < len; offset++) { | |
| 128 | 128 | if (a[offset] !== b[offset]) { | |
| 129 | 129 | return false; | |
@@ -160,10 +160,7 @@ function isObjectOrArrayTag(tag) { | |||
| 160 | 160 | // reasonable to interpret their underlying memory in the same way, | |
| 161 | 161 | // which is checked by comparing their type tags. | |
| 162 | 162 | // (e.g. a Uint8Array and a Uint16Array with the same memory content | |
| 163 | - // could still be different because they will be interpreted differently) | ||
| 164 | - // Never perform binary comparisons for Float*Arrays, though, | ||
| 165 | - // since e.g. +0 === -0 is true despite the two values' bit patterns | ||
| 166 | - // not being identical. | ||
| 163 | + // could still be different because they will be interpreted differently). | ||
| 167 | 164 | // | |
| 168 | 165 | // For strict comparison, objects should have | |
| 169 | 166 | // a) The same built-in type tags | |
@@ -211,8 +208,9 @@ function strictDeepEqual(actual, expected) { | |||
| 211 | 208 | if (actual.message !== expected.message) { | |
| 212 | 209 | return false; | |
| 213 | 210 | } | |
| 214 | - } else if (!isFloatTypedArrayTag(actualTag) && ArrayBuffer.isView(actual)) { | ||
| 215 | - if (!areSimilarTypedArrays(actual, expected)) { | ||
| 211 | + } else if (ArrayBuffer.isView(actual)) { | ||
| 212 | + if (!areSimilarTypedArrays(actual, expected, | ||
| 213 | + isFloatTypedArrayTag(actualTag) ? 0 : 300)) { | ||
| 216 | 214 | return false; | |
| 217 | 215 | } | |
| 218 | 216 | // Buffer.compare returns true, so actual.length === expected.length | |
@@ -266,9 +264,10 @@ function looseDeepEqual(actual, expected) { | |||
| 266 | 264 | const actualTag = objectToString(actual); | |
| 267 | 265 | const expectedTag = objectToString(expected); | |
| 268 | 266 | if (actualTag === expectedTag) { | |
| 269 | - if (!isObjectOrArrayTag(actualTag) && !isFloatTypedArrayTag(actualTag) && | ||
| 270 | - ArrayBuffer.isView(actual)) { | ||
| 271 | - return areSimilarTypedArrays(actual, expected); | ||
| 267 | + if (!isObjectOrArrayTag(actualTag) && ArrayBuffer.isView(actual)) { | ||
| 268 | + return areSimilarTypedArrays(actual, expected, | ||
| 269 | + isFloatTypedArrayTag(actualTag) ? | ||
| 270 | + Infinity : 300); | ||
| 272 | 271 | } | |
| 273 | 272 | // Ensure reflexivity of deepEqual with `arguments` objects. | |
| 274 | 273 | // See https://github.com/nodejs/node-v0.x-archive/pull/7178 | |
@@ -280,7 +279,9 @@ function looseDeepEqual(actual, expected) { | |||
| 280 | 279 | function innerDeepEqual(actual, expected, strict, memos) { | |
| 281 | 280 | // All identical values are equivalent, as determined by ===. | |
| 282 | 281 | if (actual === expected) { | |
| 283 | - return true; | ||
| 282 | + if (actual !== 0) | ||
| 283 | + return true; | ||
| 284 | + return strict ? Object.is(actual, expected) : true; | ||
| 284 | 285 | } | |
| 285 | 286 | ||
| 286 | 287 | // Returns a boolean if (not) equal and undefined in case we have to check | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -507,5 +507,8 @@ assert.doesNotThrow( | |||
| 507 | 507 | boxedSymbol.slow = true; | |
| 508 | 508 | assertNotDeepOrStrict(boxedSymbol, {}); | |
| 509 | 509 | } | |
| 510 | + // Minus zero | ||
| 511 | + assertOnlyDeepEqual(0, -0); | ||
| 512 | + assertDeepAndStrictEqual(-0, -0); | ||
| 510 | 513 | ||
| 511 | 514 | /* eslint-enable */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,16 +20,20 @@ const equalArrayPairs = [ | |||
| 20 | 20 | [new Int32Array(1e5), new Int32Array(1e5)], | |
| 21 | 21 | [new Float32Array(1e5), new Float32Array(1e5)], | |
| 22 | 22 | [new Float64Array(1e5), new Float64Array(1e5)], | |
| 23 | - [new Int16Array(256), new Uint16Array(256)], | ||
| 24 | - [new Int16Array([256]), new Uint16Array([256])], | ||
| 25 | - [new Float32Array([+0.0]), new Float32Array([-0.0])], | ||
| 26 | - [new Float64Array([+0.0]), new Float32Array([-0.0])], | ||
| 27 | - [new Float64Array([+0.0]), new Float64Array([-0.0])], | ||
| 23 | + [new Float32Array([+0.0]), new Float32Array([+0.0])], | ||
| 28 | 24 | [new Uint8Array([1, 2, 3, 4]).subarray(1), new Uint8Array([2, 3, 4])], | |
| 29 | 25 | [new Uint16Array([1, 2, 3, 4]).subarray(1), new Uint16Array([2, 3, 4])], | |
| 30 | 26 | [new Uint32Array([1, 2, 3, 4]).subarray(1, 3), new Uint32Array([2, 3])] | |
| 31 | 27 | ]; | |
| 32 | 28 | ||
| 29 | + const looseEqualArrayPairs = [ | ||
| 30 | + [new Float64Array([+0.0]), new Float32Array([-0.0])], | ||
| 31 | + [new Int16Array(256), new Uint16Array(256)], | ||
| 32 | + [new Int16Array([256]), new Uint16Array([256])], | ||
| 33 | + [new Float32Array([+0.0]), new Float32Array([-0.0])], | ||
| 34 | + [new Float64Array([+0.0]), new Float64Array([-0.0])] | ||
| 35 | + ]; | ||
| 36 | + | ||
| 33 | 37 | const notEqualArrayPairs = [ | |
| 34 | 38 | [new Uint8Array(2), new Uint8Array(3)], | |
| 35 | 39 | [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])], | |
@@ -46,6 +50,16 @@ const notEqualArrayPairs = [ | |||
| 46 | 50 | equalArrayPairs.forEach((arrayPair) => { | |
| 47 | 51 | // eslint-disable-next-line no-restricted-properties | |
| 48 | 52 | assert.deepEqual(arrayPair[0], arrayPair[1]); | |
| 53 | + assert.deepStrictEqual(arrayPair[0], arrayPair[1]); | ||
| 54 | + }); | ||
| 55 | + | ||
| 56 | + looseEqualArrayPairs.forEach((arrayPair) => { | ||
| 57 | + // eslint-disable-next-line no-restricted-properties | ||
| 58 | + assert.deepEqual(arrayPair[0], arrayPair[1]); | ||
| 59 | + assert.throws( | ||
| 60 | + makeBlock(assert.deepStrictEqual, arrayPair[0], arrayPair[1]), | ||
| 61 | + assert.AssertionError | ||
| 62 | + ); | ||
| 49 | 63 | }); | |
| 50 | 64 | ||
| 51 | 65 | notEqualArrayPairs.forEach((arrayPair) => { | |
@@ -54,4 +68,8 @@ notEqualArrayPairs.forEach((arrayPair) => { | |||
| 54 | 68 | makeBlock(assert.deepEqual, arrayPair[0], arrayPair[1]), | |
| 55 | 69 | assert.AssertionError | |
| 56 | 70 | ); | |
| 71 | + assert.throws( | ||
| 72 | + makeBlock(assert.deepStrictEqual, arrayPair[0], arrayPair[1]), | ||
| 73 | + assert.AssertionError | ||
| 74 | + ); | ||
| 57 | 75 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments