|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Module dependencies. |
| 5 | + */ |
| 6 | +var assert = require('assert'); |
| 7 | + |
| 8 | +/** |
| 9 | + * Test for deep equality between objects that have circular references. |
| 10 | + * |
| 11 | + * @param {Object} actual - The actual object. |
| 12 | + * @param {Object} expected - The expected object. |
| 13 | + */ |
| 14 | +function deepEqualCircular(actual, expected) { |
| 15 | + var IS_VISITED_KEY = '_isVisited'; |
| 16 | + |
| 17 | + var actualKeys = Object.keys(actual).sort(); |
| 18 | + var expectedKeys = Object.keys(expected).sort(); |
| 19 | + |
| 20 | + // remove extraneous properties (if applicable) |
| 21 | + var isVisitedKeyIndex = actualKeys.indexOf(IS_VISITED_KEY); |
| 22 | + if (isVisitedKeyIndex > -1) { |
| 23 | + actualKeys.splice(isVisitedKeyIndex, 1); |
| 24 | + } |
| 25 | + |
| 26 | + // compare object keys (sanity check) |
| 27 | + assert.deepStrictEqual(actualKeys, expectedKeys); |
| 28 | + |
| 29 | + // compare actual against expected |
| 30 | + expectedKeys.forEach(function(key) { |
| 31 | + var actualValue = actual[key]; |
| 32 | + var expectedValue = expected[key]; |
| 33 | + |
| 34 | + if (actualValue !== null && typeof actualValue === 'object') { |
| 35 | + // actual and expected are not the same type |
| 36 | + if (expectedValue !== null && typeof expectedValue.constructor === 'object') { |
| 37 | + throw new Error( |
| 38 | + 'Actual value: ' + util.inspect(actualValue) + |
| 39 | + '\n\nExpected value: ' + |
| 40 | + util.inspect(expectedValue) |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + // no need to revisit an already visited object |
| 45 | + // this is to mitigate exceeding maximum call stack with circular reference |
| 46 | + if (actualValue[IS_VISITED_KEY]) { |
| 47 | + return; |
| 48 | + |
| 49 | + // otherwise, walk through it |
| 50 | + } else { |
| 51 | + // taint the object to denote that it's been visited |
| 52 | + actualValue[IS_VISITED_KEY] = true; |
| 53 | + return deepEqualCircular(actualValue, expectedValue); |
| 54 | + } |
| 55 | + |
| 56 | + // compare the values as is |
| 57 | + } else { |
| 58 | + assert.deepStrictEqual(actualValue, expectedValue); |
| 59 | + } |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Export assert helpers. |
| 65 | + */ |
| 66 | +module.exports = { |
| 67 | + deepEqualCircular: deepEqualCircular |
| 68 | +}; |
0 commit comments