GeezerVison is showing github.com — assert: allow circular references · nodejs/node@76c9ab5
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/assert.js +17-4Lines changed: 17 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -143,7 +143,7 @@ assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) { | 143 | 143 | } | 144 | 144 | }; | 145 | 145 |
| 146 | | -function _deepEqual(actual, expected, strict) { | | 146 | +function _deepEqual(actual, expected, strict, memos) { | 147 | 147 | // 7.1. All identical values are equivalent, as determined by ===. | 148 | 148 | if (actual === expected) { | 149 | 149 | return true; | @@ -191,15 +191,27 @@ function _deepEqual(actual, expected, strict) { | 191 | 191 | // corresponding key, and an identical 'prototype' property. Note: this | 192 | 192 | // accounts for both named and indexed properties on Arrays. | 193 | 193 | } else { | 194 | | - return objEquiv(actual, expected, strict); | | 194 | + memos = memos || {actual: [], expected: []}; | | 195 | + | | 196 | + const actualIndex = memos.actual.indexOf(actual); | | 197 | + if (actualIndex !== -1) { | | 198 | + if (actualIndex === memos.expected.indexOf(expected)) { | | 199 | + return true; | | 200 | + } | | 201 | + } | | 202 | + | | 203 | + memos.actual.push(actual); | | 204 | + memos.expected.push(expected); | | 205 | + | | 206 | + return objEquiv(actual, expected, strict, memos); | 195 | 207 | } | 196 | 208 | } | 197 | 209 |
| 198 | 210 | function isArguments(object) { | 199 | 211 | return Object.prototype.toString.call(object) == '[object Arguments]'; | 200 | 212 | } | 201 | 213 |
| 202 | | -function objEquiv(a, b, strict) { | | 214 | +function objEquiv(a, b, strict, actualVisitedObjects) { | 203 | 215 | if (a === null || a === undefined || b === null || b === undefined) | 204 | 216 | return false; | 205 | 217 | // if one is a primitive, the other must be same | @@ -235,7 +247,8 @@ function objEquiv(a, b, strict) { | 235 | 247 | //~~~possibly expensive deep test | 236 | 248 | for (i = ka.length - 1; i >= 0; i--) { | 237 | 249 | key = ka[i]; | 238 | | - if (!_deepEqual(a[key], b[key], strict)) return false; | | 250 | + if (!_deepEqual(a[key], b[key], strict, actualVisitedObjects)) | | 251 | + return false; | 239 | 252 | } | 240 | 253 | return true; | 241 | 254 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-assert.js +20-12Lines changed: 20 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -381,25 +381,33 @@ try { | 381 | 381 |
| 382 | 382 | assert.ok(threw); | 383 | 383 |
| 384 | | -// GH-207. Make sure deepEqual doesn't loop forever on circular refs | 385 | | -var b = {}; | 386 | | -b.b = b; | | 384 | +// https://github.com/nodejs/node/issues/6416 | | 385 | +// Make sure circular refs don't throw. | | 386 | +{ | | 387 | + const b = {}; | | 388 | + b.b = b; | 387 | 389 |
| 388 | | -var c = {}; | 389 | | -c.b = c; | | 390 | + const c = {}; | | 391 | + c.b = c; | 390 | 392 |
| 391 | | -var gotError = false; | 392 | | -try { | 393 | | - assert.deepEqual(b, c); | 394 | | -} catch (e) { | 395 | | - gotError = true; | 396 | | -} | | 393 | + a.doesNotThrow(makeBlock(a.deepEqual, b, c)); | | 394 | + a.doesNotThrow(makeBlock(a.deepStrictEqual, b, c)); | 397 | 395 |
| | 396 | + const d = {}; | | 397 | + d.a = 1; | | 398 | + d.b = d; | | 399 | + | | 400 | + const e = {}; | | 401 | + e.a = 1; | | 402 | + e.b = e.a; | | 403 | + | | 404 | + a.throws(makeBlock(a.deepEqual, d, e), /AssertionError/); | | 405 | + a.throws(makeBlock(a.deepStrictEqual, d, e), /AssertionError/); | | 406 | +} | 398 | 407 | // GH-7178. Ensure reflexivity of deepEqual with `arguments` objects. | 399 | 408 | var args = (function() { return arguments; })(); | 400 | 409 | a.throws(makeBlock(a.deepEqual, [], args)); | 401 | 410 | a.throws(makeBlock(a.deepEqual, args, [])); | 402 | | -assert.ok(gotError); | 403 | 411 |
| 404 | 412 |
| 405 | 413 | var circular = {y: 1}; | |
|