11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_cirequire('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst util = require('util'); 61cb0ef41Sopenharmony_ciconst { AssertionError } = assert; 71cb0ef41Sopenharmony_ciconst defaultMsgStart = 'Expected values to be strictly deep-equal:\n'; 81cb0ef41Sopenharmony_ciconst defaultMsgStartFull = `${defaultMsgStart}+ actual - expected`; 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci// Disable colored output to prevent color codes from breaking assertion 111cb0ef41Sopenharmony_ci// message comparisons. This should only be an issue when process.stdout 121cb0ef41Sopenharmony_ci// is a TTY. 131cb0ef41Sopenharmony_ciif (process.stdout.isTTY) 141cb0ef41Sopenharmony_ci process.env.NODE_DISABLE_COLORS = '1'; 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci// Template tag function turning an error message into a RegExp 171cb0ef41Sopenharmony_ci// for assert.throws() 181cb0ef41Sopenharmony_cifunction re(literals, ...values) { 191cb0ef41Sopenharmony_ci let result = 'Expected values to be loosely deep-equal:\n\n'; 201cb0ef41Sopenharmony_ci for (const [i, value] of values.entries()) { 211cb0ef41Sopenharmony_ci const str = util.inspect(value, { 221cb0ef41Sopenharmony_ci compact: false, 231cb0ef41Sopenharmony_ci depth: 1000, 241cb0ef41Sopenharmony_ci customInspect: false, 251cb0ef41Sopenharmony_ci maxArrayLength: Infinity, 261cb0ef41Sopenharmony_ci breakLength: Infinity, 271cb0ef41Sopenharmony_ci sorted: true, 281cb0ef41Sopenharmony_ci getters: true 291cb0ef41Sopenharmony_ci }); 301cb0ef41Sopenharmony_ci // Need to escape special characters. 311cb0ef41Sopenharmony_ci result += `${str}${literals[i + 1]}`; 321cb0ef41Sopenharmony_ci } 331cb0ef41Sopenharmony_ci return { 341cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 351cb0ef41Sopenharmony_ci message: result 361cb0ef41Sopenharmony_ci }; 371cb0ef41Sopenharmony_ci} 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci// The following deepEqual tests might seem very weird. 401cb0ef41Sopenharmony_ci// They just describe what it is now. 411cb0ef41Sopenharmony_ci// That is why we discourage using deepEqual in our own tests. 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci// Turn off no-restricted-properties because we are testing deepEqual! 441cb0ef41Sopenharmony_ci/* eslint-disable no-restricted-properties */ 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ciconst arr = new Uint8Array([120, 121, 122, 10]); 471cb0ef41Sopenharmony_ciconst buf = Buffer.from(arr); 481cb0ef41Sopenharmony_ci// They have different [[Prototype]] 491cb0ef41Sopenharmony_ciassert.throws( 501cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(arr, buf), 511cb0ef41Sopenharmony_ci { 521cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 531cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull} ... Lines skipped\n\n` + 541cb0ef41Sopenharmony_ci '+ Uint8Array(4) [\n' + 551cb0ef41Sopenharmony_ci '- Buffer(4) [Uint8Array] [\n 120,\n...\n 122,\n 10\n ]' 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci); 581cb0ef41Sopenharmony_ciassert.deepEqual(arr, buf); 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci{ 611cb0ef41Sopenharmony_ci const buf2 = Buffer.from(arr); 621cb0ef41Sopenharmony_ci buf2.prop = 1; 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci assert.throws( 651cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(buf2, buf), 661cb0ef41Sopenharmony_ci { 671cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 681cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n` + 691cb0ef41Sopenharmony_ci ' Buffer(4) [Uint8Array] [\n' + 701cb0ef41Sopenharmony_ci ' 120,\n' + 711cb0ef41Sopenharmony_ci ' 121,\n' + 721cb0ef41Sopenharmony_ci ' 122,\n' + 731cb0ef41Sopenharmony_ci ' 10,\n' + 741cb0ef41Sopenharmony_ci '+ prop: 1\n' + 751cb0ef41Sopenharmony_ci ' ]' 761cb0ef41Sopenharmony_ci } 771cb0ef41Sopenharmony_ci ); 781cb0ef41Sopenharmony_ci assert.notDeepEqual(buf2, buf); 791cb0ef41Sopenharmony_ci} 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci{ 821cb0ef41Sopenharmony_ci const arr2 = new Uint8Array([120, 121, 122, 10]); 831cb0ef41Sopenharmony_ci arr2.prop = 5; 841cb0ef41Sopenharmony_ci assert.throws( 851cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(arr, arr2), 861cb0ef41Sopenharmony_ci { 871cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 881cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n` + 891cb0ef41Sopenharmony_ci ' Uint8Array(4) [\n' + 901cb0ef41Sopenharmony_ci ' 120,\n' + 911cb0ef41Sopenharmony_ci ' 121,\n' + 921cb0ef41Sopenharmony_ci ' 122,\n' + 931cb0ef41Sopenharmony_ci ' 10,\n' + 941cb0ef41Sopenharmony_ci '- prop: 5\n' + 951cb0ef41Sopenharmony_ci ' ]' 961cb0ef41Sopenharmony_ci } 971cb0ef41Sopenharmony_ci ); 981cb0ef41Sopenharmony_ci assert.notDeepEqual(arr, arr2); 991cb0ef41Sopenharmony_ci} 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ciconst date = new Date('2016'); 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ciclass MyDate extends Date { 1041cb0ef41Sopenharmony_ci constructor(...args) { 1051cb0ef41Sopenharmony_ci super(...args); 1061cb0ef41Sopenharmony_ci this[0] = '1'; 1071cb0ef41Sopenharmony_ci } 1081cb0ef41Sopenharmony_ci} 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ciconst date2 = new MyDate('2016'); 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ciassertNotDeepOrStrict(date, date2); 1131cb0ef41Sopenharmony_ciassert.throws( 1141cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(date, date2), 1151cb0ef41Sopenharmony_ci { 1161cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 1171cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n` + 1181cb0ef41Sopenharmony_ci '+ 2016-01-01T00:00:00.000Z\n- MyDate 2016-01-01T00:00:00.000Z' + 1191cb0ef41Sopenharmony_ci " {\n- '0': '1'\n- }" 1201cb0ef41Sopenharmony_ci } 1211cb0ef41Sopenharmony_ci); 1221cb0ef41Sopenharmony_ciassert.throws( 1231cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(date2, date), 1241cb0ef41Sopenharmony_ci { 1251cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 1261cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n` + 1271cb0ef41Sopenharmony_ci '+ MyDate 2016-01-01T00:00:00.000Z {\n' + 1281cb0ef41Sopenharmony_ci "+ '0': '1'\n+ }\n- 2016-01-01T00:00:00.000Z" 1291cb0ef41Sopenharmony_ci } 1301cb0ef41Sopenharmony_ci); 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ciclass MyRegExp extends RegExp { 1331cb0ef41Sopenharmony_ci constructor(...args) { 1341cb0ef41Sopenharmony_ci super(...args); 1351cb0ef41Sopenharmony_ci this[0] = '1'; 1361cb0ef41Sopenharmony_ci } 1371cb0ef41Sopenharmony_ci} 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ciconst re1 = new RegExp('test'); 1401cb0ef41Sopenharmony_ciconst re2 = new MyRegExp('test'); 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ciassertNotDeepOrStrict(re1, re2); 1431cb0ef41Sopenharmony_ciassert.throws( 1441cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(re1, re2), 1451cb0ef41Sopenharmony_ci { 1461cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 1471cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n` + 1481cb0ef41Sopenharmony_ci "+ /test/\n- MyRegExp /test/ {\n- '0': '1'\n- }" 1491cb0ef41Sopenharmony_ci } 1501cb0ef41Sopenharmony_ci); 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci// For these weird cases, deepEqual should pass (at least for now), 1531cb0ef41Sopenharmony_ci// but deepStrictEqual should throw. 1541cb0ef41Sopenharmony_ci{ 1551cb0ef41Sopenharmony_ci const similar = new Set([ 1561cb0ef41Sopenharmony_ci { 0: 1 }, // Object 1571cb0ef41Sopenharmony_ci new String('1'), // Object 1581cb0ef41Sopenharmony_ci [1], // Array 1591cb0ef41Sopenharmony_ci date2, // Date with this[0] = '1' 1601cb0ef41Sopenharmony_ci re2, // RegExp with this[0] = '1' 1611cb0ef41Sopenharmony_ci new Int8Array([1]), // Int8Array 1621cb0ef41Sopenharmony_ci new Int16Array([1]), // Int16Array 1631cb0ef41Sopenharmony_ci new Uint16Array([1]), // Uint16Array 1641cb0ef41Sopenharmony_ci new Int32Array([1]), // Int32Array 1651cb0ef41Sopenharmony_ci new Uint32Array([1]), // Uint32Array 1661cb0ef41Sopenharmony_ci Buffer.from([1]), // Uint8Array 1671cb0ef41Sopenharmony_ci (function() { return arguments; })(1), 1681cb0ef41Sopenharmony_ci ]); 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci for (const a of similar) { 1711cb0ef41Sopenharmony_ci for (const b of similar) { 1721cb0ef41Sopenharmony_ci if (a !== b) { 1731cb0ef41Sopenharmony_ci assert.notDeepEqual(a, b); 1741cb0ef41Sopenharmony_ci assert.throws( 1751cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(a, b), 1761cb0ef41Sopenharmony_ci { code: 'ERR_ASSERTION' } 1771cb0ef41Sopenharmony_ci ); 1781cb0ef41Sopenharmony_ci } 1791cb0ef41Sopenharmony_ci } 1801cb0ef41Sopenharmony_ci } 1811cb0ef41Sopenharmony_ci} 1821cb0ef41Sopenharmony_ci 1831cb0ef41Sopenharmony_cifunction assertDeepAndStrictEqual(a, b) { 1841cb0ef41Sopenharmony_ci assert.deepEqual(a, b); 1851cb0ef41Sopenharmony_ci assert.deepStrictEqual(a, b); 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ci assert.deepEqual(b, a); 1881cb0ef41Sopenharmony_ci assert.deepStrictEqual(b, a); 1891cb0ef41Sopenharmony_ci} 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_cifunction assertNotDeepOrStrict(a, b, err) { 1921cb0ef41Sopenharmony_ci assert.throws( 1931cb0ef41Sopenharmony_ci () => assert.deepEqual(a, b), 1941cb0ef41Sopenharmony_ci err || re`${a}\n\nshould loosely deep-equal\n\n${b}` 1951cb0ef41Sopenharmony_ci ); 1961cb0ef41Sopenharmony_ci assert.throws( 1971cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(a, b), 1981cb0ef41Sopenharmony_ci err || { code: 'ERR_ASSERTION' } 1991cb0ef41Sopenharmony_ci ); 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci assert.throws( 2021cb0ef41Sopenharmony_ci () => assert.deepEqual(b, a), 2031cb0ef41Sopenharmony_ci err || re`${b}\n\nshould loosely deep-equal\n\n${a}` 2041cb0ef41Sopenharmony_ci ); 2051cb0ef41Sopenharmony_ci assert.throws( 2061cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(b, a), 2071cb0ef41Sopenharmony_ci err || { code: 'ERR_ASSERTION' } 2081cb0ef41Sopenharmony_ci ); 2091cb0ef41Sopenharmony_ci} 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_cifunction assertOnlyDeepEqual(a, b, err) { 2121cb0ef41Sopenharmony_ci assert.deepEqual(a, b); 2131cb0ef41Sopenharmony_ci assert.throws( 2141cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(a, b), 2151cb0ef41Sopenharmony_ci err || { code: 'ERR_ASSERTION' } 2161cb0ef41Sopenharmony_ci ); 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci assert.deepEqual(b, a); 2191cb0ef41Sopenharmony_ci assert.throws( 2201cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(b, a), 2211cb0ef41Sopenharmony_ci err || { code: 'ERR_ASSERTION' } 2221cb0ef41Sopenharmony_ci ); 2231cb0ef41Sopenharmony_ci} 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ci// es6 Maps and Sets 2261cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(new Set(), new Set()); 2271cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(new Map(), new Map()); 2281cb0ef41Sopenharmony_ci 2291cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(new Set([1, 2, 3]), new Set([1, 2, 3])); 2301cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Set([1, 2, 3]), new Set([1, 2, 3, 4])); 2311cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Set([1, 2, 3, 4]), new Set([1, 2, 3])); 2321cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(new Set(['1', '2', '3']), new Set(['1', '2', '3'])); 2331cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(new Set([[1, 2], [3, 4]]), new Set([[3, 4], [1, 2]])); 2341cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Set([{ a: 0 }]), new Set([{ a: 1 }])); 2351cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Set([Symbol()]), new Set([Symbol()])); 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ci{ 2381cb0ef41Sopenharmony_ci const a = [ 1, 2 ]; 2391cb0ef41Sopenharmony_ci const b = [ 3, 4 ]; 2401cb0ef41Sopenharmony_ci const c = [ 1, 2 ]; 2411cb0ef41Sopenharmony_ci const d = [ 3, 4 ]; 2421cb0ef41Sopenharmony_ci 2431cb0ef41Sopenharmony_ci assertDeepAndStrictEqual( 2441cb0ef41Sopenharmony_ci { a: a, b: b, s: new Set([a, b]) }, 2451cb0ef41Sopenharmony_ci { a: c, b: d, s: new Set([d, c]) } 2461cb0ef41Sopenharmony_ci ); 2471cb0ef41Sopenharmony_ci} 2481cb0ef41Sopenharmony_ci 2491cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(new Map([[1, 1], [2, 2]]), new Map([[1, 1], [2, 2]])); 2501cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(new Map([[1, 1], [2, 2]]), new Map([[2, 2], [1, 1]])); 2511cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Map([[1, 1], [2, 2]]), new Map([[1, 2], [2, 1]])); 2521cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 2531cb0ef41Sopenharmony_ci new Map([[[1], 1], [{}, 2]]), 2541cb0ef41Sopenharmony_ci new Map([[[1], 2], [{}, 1]]) 2551cb0ef41Sopenharmony_ci); 2561cb0ef41Sopenharmony_ci 2571cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Set([1]), [1]); 2581cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Set(), []); 2591cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Set(), {}); 2601cb0ef41Sopenharmony_ci 2611cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Map([['a', 1]]), { a: 1 }); 2621cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Map(), []); 2631cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Map(), {}); 2641cb0ef41Sopenharmony_ci 2651cb0ef41Sopenharmony_ciassertOnlyDeepEqual(new Set(['1']), new Set([1])); 2661cb0ef41Sopenharmony_ci 2671cb0ef41Sopenharmony_ciassertOnlyDeepEqual(new Map([['1', 'a']]), new Map([[1, 'a']])); 2681cb0ef41Sopenharmony_ciassertOnlyDeepEqual(new Map([['a', '1']]), new Map([['a', 1]])); 2691cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Map([['a', '1']]), new Map([['a', 2]])); 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(new Set([{}]), new Set([{}])); 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_ci// Ref: https://github.com/nodejs/node/issues/13347 2741cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 2751cb0ef41Sopenharmony_ci new Set([{ a: 1 }, { a: 1 }]), 2761cb0ef41Sopenharmony_ci new Set([{ a: 1 }, { a: 2 }]) 2771cb0ef41Sopenharmony_ci); 2781cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 2791cb0ef41Sopenharmony_ci new Set([{ a: 1 }, { a: 1 }, { a: 2 }]), 2801cb0ef41Sopenharmony_ci new Set([{ a: 1 }, { a: 2 }, { a: 2 }]) 2811cb0ef41Sopenharmony_ci); 2821cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 2831cb0ef41Sopenharmony_ci new Map([[{ x: 1 }, 5], [{ x: 1 }, 5]]), 2841cb0ef41Sopenharmony_ci new Map([[{ x: 1 }, 5], [{ x: 2 }, 5]]) 2851cb0ef41Sopenharmony_ci); 2861cb0ef41Sopenharmony_ci 2871cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Set([3, '3']), new Set([3, 4])); 2881cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Map([[3, 0], ['3', 0]]), new Map([[3, 0], [4, 0]])); 2891cb0ef41Sopenharmony_ci 2901cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 2911cb0ef41Sopenharmony_ci new Set([{ a: 1 }, { a: 1 }, { a: 2 }]), 2921cb0ef41Sopenharmony_ci new Set([{ a: 1 }, { a: 2 }, { a: 2 }]) 2931cb0ef41Sopenharmony_ci); 2941cb0ef41Sopenharmony_ci 2951cb0ef41Sopenharmony_ci// Mixed primitive and object keys 2961cb0ef41Sopenharmony_ciassertDeepAndStrictEqual( 2971cb0ef41Sopenharmony_ci new Map([[1, 'a'], [{}, 'a']]), 2981cb0ef41Sopenharmony_ci new Map([[1, 'a'], [{}, 'a']]) 2991cb0ef41Sopenharmony_ci); 3001cb0ef41Sopenharmony_ciassertDeepAndStrictEqual( 3011cb0ef41Sopenharmony_ci new Set([1, 'a', [{}, 'a']]), 3021cb0ef41Sopenharmony_ci new Set([1, 'a', [{}, 'a']]) 3031cb0ef41Sopenharmony_ci); 3041cb0ef41Sopenharmony_ci 3051cb0ef41Sopenharmony_ci// This is an awful case, where a map contains multiple equivalent keys: 3061cb0ef41Sopenharmony_ciassertOnlyDeepEqual( 3071cb0ef41Sopenharmony_ci new Map([[1, 'a'], ['1', 'b']]), 3081cb0ef41Sopenharmony_ci new Map([['1', 'a'], [true, 'b']]) 3091cb0ef41Sopenharmony_ci); 3101cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 3111cb0ef41Sopenharmony_ci new Set(['a']), 3121cb0ef41Sopenharmony_ci new Set(['b']) 3131cb0ef41Sopenharmony_ci); 3141cb0ef41Sopenharmony_ciassertDeepAndStrictEqual( 3151cb0ef41Sopenharmony_ci new Map([[{}, 'a'], [{}, 'b']]), 3161cb0ef41Sopenharmony_ci new Map([[{}, 'b'], [{}, 'a']]) 3171cb0ef41Sopenharmony_ci); 3181cb0ef41Sopenharmony_ciassertOnlyDeepEqual( 3191cb0ef41Sopenharmony_ci new Map([[true, 'a'], ['1', 'b'], [1, 'a']]), 3201cb0ef41Sopenharmony_ci new Map([['1', 'a'], [1, 'b'], [true, 'a']]) 3211cb0ef41Sopenharmony_ci); 3221cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 3231cb0ef41Sopenharmony_ci new Map([[true, 'a'], ['1', 'b'], [1, 'c']]), 3241cb0ef41Sopenharmony_ci new Map([['1', 'a'], [1, 'b'], [true, 'a']]) 3251cb0ef41Sopenharmony_ci); 3261cb0ef41Sopenharmony_ci 3271cb0ef41Sopenharmony_ci// Similar object keys 3281cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 3291cb0ef41Sopenharmony_ci new Set([{}, {}]), 3301cb0ef41Sopenharmony_ci new Set([{}, 1]) 3311cb0ef41Sopenharmony_ci); 3321cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 3331cb0ef41Sopenharmony_ci new Set([[{}, 1], [{}, 1]]), 3341cb0ef41Sopenharmony_ci new Set([[{}, 1], [1, 1]]) 3351cb0ef41Sopenharmony_ci); 3361cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 3371cb0ef41Sopenharmony_ci new Map([[{}, 1], [{}, 1]]), 3381cb0ef41Sopenharmony_ci new Map([[{}, 1], [1, 1]]) 3391cb0ef41Sopenharmony_ci); 3401cb0ef41Sopenharmony_ciassertOnlyDeepEqual( 3411cb0ef41Sopenharmony_ci new Map([[{}, 1], [true, 1]]), 3421cb0ef41Sopenharmony_ci new Map([[{}, 1], [1, 1]]) 3431cb0ef41Sopenharmony_ci); 3441cb0ef41Sopenharmony_ci 3451cb0ef41Sopenharmony_ci// Similar primitive key / values 3461cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 3471cb0ef41Sopenharmony_ci new Set([1, true, false]), 3481cb0ef41Sopenharmony_ci new Set(['1', 0, '0']) 3491cb0ef41Sopenharmony_ci); 3501cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 3511cb0ef41Sopenharmony_ci new Map([[1, 5], [true, 5], [false, 5]]), 3521cb0ef41Sopenharmony_ci new Map([['1', 5], [0, 5], ['0', 5]]) 3531cb0ef41Sopenharmony_ci); 3541cb0ef41Sopenharmony_ci 3551cb0ef41Sopenharmony_ci// Undefined value in Map 3561cb0ef41Sopenharmony_ciassertDeepAndStrictEqual( 3571cb0ef41Sopenharmony_ci new Map([[1, undefined]]), 3581cb0ef41Sopenharmony_ci new Map([[1, undefined]]) 3591cb0ef41Sopenharmony_ci); 3601cb0ef41Sopenharmony_ciassertOnlyDeepEqual( 3611cb0ef41Sopenharmony_ci new Map([[1, null], ['', '0']]), 3621cb0ef41Sopenharmony_ci new Map([['1', undefined], [false, 0]]) 3631cb0ef41Sopenharmony_ci); 3641cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 3651cb0ef41Sopenharmony_ci new Map([[1, undefined]]), 3661cb0ef41Sopenharmony_ci new Map([[2, undefined]]) 3671cb0ef41Sopenharmony_ci); 3681cb0ef41Sopenharmony_ci 3691cb0ef41Sopenharmony_ci// null as key 3701cb0ef41Sopenharmony_ciassertDeepAndStrictEqual( 3711cb0ef41Sopenharmony_ci new Map([[null, 3]]), 3721cb0ef41Sopenharmony_ci new Map([[null, 3]]) 3731cb0ef41Sopenharmony_ci); 3741cb0ef41Sopenharmony_ciassertOnlyDeepEqual( 3751cb0ef41Sopenharmony_ci new Map([[undefined, null], ['+000', 2n]]), 3761cb0ef41Sopenharmony_ci new Map([[null, undefined], [false, '2']]), 3771cb0ef41Sopenharmony_ci); 3781cb0ef41Sopenharmony_ci 3791cb0ef41Sopenharmony_ciassertOnlyDeepEqual( 3801cb0ef41Sopenharmony_ci new Set([null, '', 1n, 5, 2n, false]), 3811cb0ef41Sopenharmony_ci new Set([undefined, 0, 5n, true, '2', '-000']) 3821cb0ef41Sopenharmony_ci); 3831cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 3841cb0ef41Sopenharmony_ci new Set(['']), 3851cb0ef41Sopenharmony_ci new Set(['0']) 3861cb0ef41Sopenharmony_ci); 3871cb0ef41Sopenharmony_ciassertOnlyDeepEqual( 3881cb0ef41Sopenharmony_ci new Map([[1, {}]]), 3891cb0ef41Sopenharmony_ci new Map([[true, {}]]) 3901cb0ef41Sopenharmony_ci); 3911cb0ef41Sopenharmony_ciassertOnlyDeepEqual( 3921cb0ef41Sopenharmony_ci new Map([[undefined, true]]), 3931cb0ef41Sopenharmony_ci new Map([[null, true]]) 3941cb0ef41Sopenharmony_ci); 3951cb0ef41Sopenharmony_ciassertNotDeepOrStrict( 3961cb0ef41Sopenharmony_ci new Map([[undefined, true]]), 3971cb0ef41Sopenharmony_ci new Map([[true, true]]) 3981cb0ef41Sopenharmony_ci); 3991cb0ef41Sopenharmony_ci 4001cb0ef41Sopenharmony_ci// GH-6416. Make sure circular refs don't throw. 4011cb0ef41Sopenharmony_ci{ 4021cb0ef41Sopenharmony_ci const b = {}; 4031cb0ef41Sopenharmony_ci b.b = b; 4041cb0ef41Sopenharmony_ci const c = {}; 4051cb0ef41Sopenharmony_ci c.b = c; 4061cb0ef41Sopenharmony_ci 4071cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(b, c); 4081cb0ef41Sopenharmony_ci 4091cb0ef41Sopenharmony_ci const d = {}; 4101cb0ef41Sopenharmony_ci d.a = 1; 4111cb0ef41Sopenharmony_ci d.b = d; 4121cb0ef41Sopenharmony_ci const e = {}; 4131cb0ef41Sopenharmony_ci e.a = 1; 4141cb0ef41Sopenharmony_ci e.b = {}; 4151cb0ef41Sopenharmony_ci 4161cb0ef41Sopenharmony_ci assertNotDeepOrStrict(d, e); 4171cb0ef41Sopenharmony_ci} 4181cb0ef41Sopenharmony_ci 4191cb0ef41Sopenharmony_ci// GH-14441. Circular structures should be consistent 4201cb0ef41Sopenharmony_ci{ 4211cb0ef41Sopenharmony_ci const a = {}; 4221cb0ef41Sopenharmony_ci const b = {}; 4231cb0ef41Sopenharmony_ci a.a = a; 4241cb0ef41Sopenharmony_ci b.a = {}; 4251cb0ef41Sopenharmony_ci b.a.a = a; 4261cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(a, b); 4271cb0ef41Sopenharmony_ci} 4281cb0ef41Sopenharmony_ci 4291cb0ef41Sopenharmony_ci{ 4301cb0ef41Sopenharmony_ci const a = new Set(); 4311cb0ef41Sopenharmony_ci const b = new Set(); 4321cb0ef41Sopenharmony_ci const c = new Set(); 4331cb0ef41Sopenharmony_ci a.add(a); 4341cb0ef41Sopenharmony_ci b.add(b); 4351cb0ef41Sopenharmony_ci c.add(a); 4361cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(b, c); 4371cb0ef41Sopenharmony_ci} 4381cb0ef41Sopenharmony_ci 4391cb0ef41Sopenharmony_ci// https://github.com/nodejs/node-v0.x-archive/pull/7178 4401cb0ef41Sopenharmony_ci// Ensure reflexivity of deepEqual with `arguments` objects. 4411cb0ef41Sopenharmony_ci{ 4421cb0ef41Sopenharmony_ci const args = (function() { return arguments; })(); 4431cb0ef41Sopenharmony_ci assertNotDeepOrStrict([], args); 4441cb0ef41Sopenharmony_ci} 4451cb0ef41Sopenharmony_ci 4461cb0ef41Sopenharmony_ci// More checking that arguments objects are handled correctly 4471cb0ef41Sopenharmony_ci{ 4481cb0ef41Sopenharmony_ci // eslint-disable-next-line func-style 4491cb0ef41Sopenharmony_ci const returnArguments = function() { return arguments; }; 4501cb0ef41Sopenharmony_ci 4511cb0ef41Sopenharmony_ci const someArgs = returnArguments('a'); 4521cb0ef41Sopenharmony_ci const sameArgs = returnArguments('a'); 4531cb0ef41Sopenharmony_ci const diffArgs = returnArguments('b'); 4541cb0ef41Sopenharmony_ci 4551cb0ef41Sopenharmony_ci assertNotDeepOrStrict(someArgs, ['a']); 4561cb0ef41Sopenharmony_ci assertNotDeepOrStrict(someArgs, { '0': 'a' }); 4571cb0ef41Sopenharmony_ci assertNotDeepOrStrict(someArgs, diffArgs); 4581cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(someArgs, sameArgs); 4591cb0ef41Sopenharmony_ci} 4601cb0ef41Sopenharmony_ci 4611cb0ef41Sopenharmony_ci{ 4621cb0ef41Sopenharmony_ci const values = [ 4631cb0ef41Sopenharmony_ci 123, 4641cb0ef41Sopenharmony_ci Infinity, 4651cb0ef41Sopenharmony_ci 0, 4661cb0ef41Sopenharmony_ci null, 4671cb0ef41Sopenharmony_ci undefined, 4681cb0ef41Sopenharmony_ci false, 4691cb0ef41Sopenharmony_ci true, 4701cb0ef41Sopenharmony_ci {}, 4711cb0ef41Sopenharmony_ci [], 4721cb0ef41Sopenharmony_ci () => {}, 4731cb0ef41Sopenharmony_ci ]; 4741cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(new Set(values), new Set(values)); 4751cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(new Set(values), new Set(values.reverse())); 4761cb0ef41Sopenharmony_ci 4771cb0ef41Sopenharmony_ci const mapValues = values.map((v) => [v, { a: 5 }]); 4781cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(new Map(mapValues), new Map(mapValues)); 4791cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(new Map(mapValues), new Map(mapValues.reverse())); 4801cb0ef41Sopenharmony_ci} 4811cb0ef41Sopenharmony_ci 4821cb0ef41Sopenharmony_ci{ 4831cb0ef41Sopenharmony_ci const s1 = new Set(); 4841cb0ef41Sopenharmony_ci const s2 = new Set(); 4851cb0ef41Sopenharmony_ci s1.add(1); 4861cb0ef41Sopenharmony_ci s1.add(2); 4871cb0ef41Sopenharmony_ci s2.add(2); 4881cb0ef41Sopenharmony_ci s2.add(1); 4891cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(s1, s2); 4901cb0ef41Sopenharmony_ci} 4911cb0ef41Sopenharmony_ci 4921cb0ef41Sopenharmony_ci{ 4931cb0ef41Sopenharmony_ci const m1 = new Map(); 4941cb0ef41Sopenharmony_ci const m2 = new Map(); 4951cb0ef41Sopenharmony_ci const obj = { a: 5, b: 6 }; 4961cb0ef41Sopenharmony_ci m1.set(1, obj); 4971cb0ef41Sopenharmony_ci m1.set(2, 'hi'); 4981cb0ef41Sopenharmony_ci m1.set(3, [1, 2, 3]); 4991cb0ef41Sopenharmony_ci 5001cb0ef41Sopenharmony_ci m2.set(2, 'hi'); // different order 5011cb0ef41Sopenharmony_ci m2.set(1, obj); 5021cb0ef41Sopenharmony_ci m2.set(3, [1, 2, 3]); // Deep equal, but not reference equal. 5031cb0ef41Sopenharmony_ci 5041cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(m1, m2); 5051cb0ef41Sopenharmony_ci} 5061cb0ef41Sopenharmony_ci 5071cb0ef41Sopenharmony_ci{ 5081cb0ef41Sopenharmony_ci const m1 = new Map(); 5091cb0ef41Sopenharmony_ci const m2 = new Map(); 5101cb0ef41Sopenharmony_ci 5111cb0ef41Sopenharmony_ci // m1 contains itself. 5121cb0ef41Sopenharmony_ci m1.set(1, m1); 5131cb0ef41Sopenharmony_ci m2.set(1, new Map()); 5141cb0ef41Sopenharmony_ci 5151cb0ef41Sopenharmony_ci assertNotDeepOrStrict(m1, m2); 5161cb0ef41Sopenharmony_ci} 5171cb0ef41Sopenharmony_ci 5181cb0ef41Sopenharmony_ci{ 5191cb0ef41Sopenharmony_ci const map1 = new Map([[1, 1]]); 5201cb0ef41Sopenharmony_ci const map2 = new Map([[1, '1']]); 5211cb0ef41Sopenharmony_ci assert.deepEqual(map1, map2); 5221cb0ef41Sopenharmony_ci assert.throws( 5231cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(map1, map2), 5241cb0ef41Sopenharmony_ci { 5251cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 5261cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n` + 5271cb0ef41Sopenharmony_ci " Map(1) {\n+ 1 => 1\n- 1 => '1'\n }" 5281cb0ef41Sopenharmony_ci } 5291cb0ef41Sopenharmony_ci ); 5301cb0ef41Sopenharmony_ci} 5311cb0ef41Sopenharmony_ci 5321cb0ef41Sopenharmony_ci{ 5331cb0ef41Sopenharmony_ci // Two equivalent sets / maps with different key/values applied shouldn't be 5341cb0ef41Sopenharmony_ci // the same. This is a terrible idea to do in practice, but deepEqual should 5351cb0ef41Sopenharmony_ci // still check for it. 5361cb0ef41Sopenharmony_ci const s1 = new Set(); 5371cb0ef41Sopenharmony_ci const s2 = new Set(); 5381cb0ef41Sopenharmony_ci s1.x = 5; 5391cb0ef41Sopenharmony_ci assertNotDeepOrStrict(s1, s2); 5401cb0ef41Sopenharmony_ci 5411cb0ef41Sopenharmony_ci const m1 = new Map(); 5421cb0ef41Sopenharmony_ci const m2 = new Map(); 5431cb0ef41Sopenharmony_ci m1.x = 5; 5441cb0ef41Sopenharmony_ci assertNotDeepOrStrict(m1, m2); 5451cb0ef41Sopenharmony_ci} 5461cb0ef41Sopenharmony_ci 5471cb0ef41Sopenharmony_ci{ 5481cb0ef41Sopenharmony_ci // Circular references. 5491cb0ef41Sopenharmony_ci const s1 = new Set(); 5501cb0ef41Sopenharmony_ci s1.add(s1); 5511cb0ef41Sopenharmony_ci const s2 = new Set(); 5521cb0ef41Sopenharmony_ci s2.add(s2); 5531cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(s1, s2); 5541cb0ef41Sopenharmony_ci 5551cb0ef41Sopenharmony_ci const m1 = new Map(); 5561cb0ef41Sopenharmony_ci m1.set(2, m1); 5571cb0ef41Sopenharmony_ci const m2 = new Map(); 5581cb0ef41Sopenharmony_ci m2.set(2, m2); 5591cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(m1, m2); 5601cb0ef41Sopenharmony_ci 5611cb0ef41Sopenharmony_ci const m3 = new Map(); 5621cb0ef41Sopenharmony_ci m3.set(m3, 2); 5631cb0ef41Sopenharmony_ci const m4 = new Map(); 5641cb0ef41Sopenharmony_ci m4.set(m4, 2); 5651cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(m3, m4); 5661cb0ef41Sopenharmony_ci} 5671cb0ef41Sopenharmony_ci 5681cb0ef41Sopenharmony_ci// Handle sparse arrays. 5691cb0ef41Sopenharmony_ci{ 5701cb0ef41Sopenharmony_ci /* eslint-disable no-sparse-arrays */ 5711cb0ef41Sopenharmony_ci assertDeepAndStrictEqual([1, , , 3], [1, , , 3]); 5721cb0ef41Sopenharmony_ci assertNotDeepOrStrict([1, , , 3], [1, , , 3, , , ]); 5731cb0ef41Sopenharmony_ci /* eslint-enable no-sparse-arrays */ 5741cb0ef41Sopenharmony_ci const a = new Array(3); 5751cb0ef41Sopenharmony_ci const b = new Array(3); 5761cb0ef41Sopenharmony_ci a[2] = true; 5771cb0ef41Sopenharmony_ci b[1] = true; 5781cb0ef41Sopenharmony_ci assertNotDeepOrStrict(a, b); 5791cb0ef41Sopenharmony_ci b[2] = true; 5801cb0ef41Sopenharmony_ci assertNotDeepOrStrict(a, b); 5811cb0ef41Sopenharmony_ci a[0] = true; 5821cb0ef41Sopenharmony_ci assertNotDeepOrStrict(a, b); 5831cb0ef41Sopenharmony_ci} 5841cb0ef41Sopenharmony_ci 5851cb0ef41Sopenharmony_ci// Handle different error messages 5861cb0ef41Sopenharmony_ci{ 5871cb0ef41Sopenharmony_ci const err1 = new Error('foo1'); 5881cb0ef41Sopenharmony_ci assertNotDeepOrStrict(err1, new Error('foo2'), assert.AssertionError); 5891cb0ef41Sopenharmony_ci assertNotDeepOrStrict(err1, new TypeError('foo1'), assert.AssertionError); 5901cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(err1, new Error('foo1')); 5911cb0ef41Sopenharmony_ci assertNotDeepOrStrict(err1, {}, AssertionError); 5921cb0ef41Sopenharmony_ci} 5931cb0ef41Sopenharmony_ci 5941cb0ef41Sopenharmony_ci// Handle NaN 5951cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(NaN, NaN); 5961cb0ef41Sopenharmony_ciassertDeepAndStrictEqual({ a: NaN }, { a: NaN }); 5971cb0ef41Sopenharmony_ciassertDeepAndStrictEqual([ 1, 2, NaN, 4 ], [ 1, 2, NaN, 4 ]); 5981cb0ef41Sopenharmony_ci 5991cb0ef41Sopenharmony_ci// Handle boxed primitives 6001cb0ef41Sopenharmony_ci{ 6011cb0ef41Sopenharmony_ci const boxedString = new String('test'); 6021cb0ef41Sopenharmony_ci const boxedSymbol = Object(Symbol()); 6031cb0ef41Sopenharmony_ci 6041cb0ef41Sopenharmony_ci const fakeBoxedSymbol = {}; 6051cb0ef41Sopenharmony_ci Object.setPrototypeOf(fakeBoxedSymbol, Symbol.prototype); 6061cb0ef41Sopenharmony_ci Object.defineProperty( 6071cb0ef41Sopenharmony_ci fakeBoxedSymbol, 6081cb0ef41Sopenharmony_ci Symbol.toStringTag, 6091cb0ef41Sopenharmony_ci { enumerable: false, value: 'Symbol' } 6101cb0ef41Sopenharmony_ci ); 6111cb0ef41Sopenharmony_ci 6121cb0ef41Sopenharmony_ci assertNotDeepOrStrict(new Boolean(true), Object(false)); 6131cb0ef41Sopenharmony_ci assertNotDeepOrStrict(Object(true), new Number(1)); 6141cb0ef41Sopenharmony_ci assertNotDeepOrStrict(new Number(2), new Number(1)); 6151cb0ef41Sopenharmony_ci assertNotDeepOrStrict(boxedSymbol, Object(Symbol())); 6161cb0ef41Sopenharmony_ci assertNotDeepOrStrict(boxedSymbol, {}); 6171cb0ef41Sopenharmony_ci assertNotDeepOrStrict(boxedSymbol, fakeBoxedSymbol); 6181cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(boxedSymbol, boxedSymbol); 6191cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(Object(true), Object(true)); 6201cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(Object(2), Object(2)); 6211cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(boxedString, Object('test')); 6221cb0ef41Sopenharmony_ci boxedString.slow = true; 6231cb0ef41Sopenharmony_ci assertNotDeepOrStrict(boxedString, Object('test')); 6241cb0ef41Sopenharmony_ci boxedSymbol.slow = true; 6251cb0ef41Sopenharmony_ci assertNotDeepOrStrict(boxedSymbol, {}); 6261cb0ef41Sopenharmony_ci assertNotDeepOrStrict(boxedSymbol, fakeBoxedSymbol); 6271cb0ef41Sopenharmony_ci} 6281cb0ef41Sopenharmony_ci 6291cb0ef41Sopenharmony_ci// Minus zero 6301cb0ef41Sopenharmony_ciassertOnlyDeepEqual(0, -0); 6311cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(-0, -0); 6321cb0ef41Sopenharmony_ci 6331cb0ef41Sopenharmony_ci// Handle symbols (enumerable only) 6341cb0ef41Sopenharmony_ci{ 6351cb0ef41Sopenharmony_ci const symbol1 = Symbol(); 6361cb0ef41Sopenharmony_ci const obj1 = { [symbol1]: 1 }; 6371cb0ef41Sopenharmony_ci const obj2 = { [symbol1]: 1 }; 6381cb0ef41Sopenharmony_ci const obj3 = { [Symbol()]: 1 }; 6391cb0ef41Sopenharmony_ci // Add a non enumerable symbol as well. It is going to be ignored! 6401cb0ef41Sopenharmony_ci Object.defineProperty(obj2, Symbol(), { value: 1 }); 6411cb0ef41Sopenharmony_ci assertOnlyDeepEqual(obj1, obj3); 6421cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(obj1, obj2); 6431cb0ef41Sopenharmony_ci obj2[Symbol()] = true; 6441cb0ef41Sopenharmony_ci assertOnlyDeepEqual(obj1, obj2); 6451cb0ef41Sopenharmony_ci // TypedArrays have a fast path. Test for this as well. 6461cb0ef41Sopenharmony_ci const a = new Uint8Array(4); 6471cb0ef41Sopenharmony_ci const b = new Uint8Array(4); 6481cb0ef41Sopenharmony_ci a[symbol1] = true; 6491cb0ef41Sopenharmony_ci b[symbol1] = false; 6501cb0ef41Sopenharmony_ci assertOnlyDeepEqual(a, b); 6511cb0ef41Sopenharmony_ci b[symbol1] = true; 6521cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(a, b); 6531cb0ef41Sopenharmony_ci // The same as TypedArrays is valid for boxed primitives 6541cb0ef41Sopenharmony_ci const boxedStringA = new String('test'); 6551cb0ef41Sopenharmony_ci const boxedStringB = new String('test'); 6561cb0ef41Sopenharmony_ci boxedStringA[symbol1] = true; 6571cb0ef41Sopenharmony_ci assertOnlyDeepEqual(boxedStringA, boxedStringB); 6581cb0ef41Sopenharmony_ci boxedStringA[symbol1] = true; 6591cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(a, b); 6601cb0ef41Sopenharmony_ci // Loose equal arrays should not compare symbols. 6611cb0ef41Sopenharmony_ci const arr = [1]; 6621cb0ef41Sopenharmony_ci const arr2 = [1]; 6631cb0ef41Sopenharmony_ci arr[symbol1] = true; 6641cb0ef41Sopenharmony_ci assertOnlyDeepEqual(arr, arr2); 6651cb0ef41Sopenharmony_ci arr2[symbol1] = false; 6661cb0ef41Sopenharmony_ci assertOnlyDeepEqual(arr, arr2); 6671cb0ef41Sopenharmony_ci} 6681cb0ef41Sopenharmony_ci 6691cb0ef41Sopenharmony_ciassert.throws( 6701cb0ef41Sopenharmony_ci () => assert.notDeepEqual(1, true), 6711cb0ef41Sopenharmony_ci { 6721cb0ef41Sopenharmony_ci message: /1\n\nshould not loosely deep-equal\n\ntrue/ 6731cb0ef41Sopenharmony_ci } 6741cb0ef41Sopenharmony_ci); 6751cb0ef41Sopenharmony_ci 6761cb0ef41Sopenharmony_ciassert.throws( 6771cb0ef41Sopenharmony_ci () => assert.notDeepEqual(1, 1), 6781cb0ef41Sopenharmony_ci { 6791cb0ef41Sopenharmony_ci message: /Expected "actual" not to be loosely deep-equal to:\n\n1/ 6801cb0ef41Sopenharmony_ci } 6811cb0ef41Sopenharmony_ci); 6821cb0ef41Sopenharmony_ci 6831cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(new Date(2000, 3, 14), new Date(2000, 3, 14)); 6841cb0ef41Sopenharmony_ci 6851cb0ef41Sopenharmony_ciassert.throws(() => { assert.deepEqual(new Date(), new Date(2000, 3, 14)); }, 6861cb0ef41Sopenharmony_ci AssertionError, 6871cb0ef41Sopenharmony_ci 'deepEqual(new Date(), new Date(2000, 3, 14))'); 6881cb0ef41Sopenharmony_ci 6891cb0ef41Sopenharmony_ciassert.throws( 6901cb0ef41Sopenharmony_ci () => { assert.notDeepEqual(new Date(2000, 3, 14), new Date(2000, 3, 14)); }, 6911cb0ef41Sopenharmony_ci AssertionError, 6921cb0ef41Sopenharmony_ci 'notDeepEqual(new Date(2000, 3, 14), new Date(2000, 3, 14))' 6931cb0ef41Sopenharmony_ci); 6941cb0ef41Sopenharmony_ci 6951cb0ef41Sopenharmony_ciassert.throws( 6961cb0ef41Sopenharmony_ci () => { assert.notDeepEqual('a'.repeat(1024), 'a'.repeat(1024)); }, 6971cb0ef41Sopenharmony_ci AssertionError, 6981cb0ef41Sopenharmony_ci 'notDeepEqual("a".repeat(1024), "a".repeat(1024))' 6991cb0ef41Sopenharmony_ci); 7001cb0ef41Sopenharmony_ci 7011cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Date(), new Date(2000, 3, 14)); 7021cb0ef41Sopenharmony_ci 7031cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(/a/, /a/); 7041cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(/a/g, /a/g); 7051cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(/a/i, /a/i); 7061cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(/a/m, /a/m); 7071cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(/a/igm, /a/igm); 7081cb0ef41Sopenharmony_ciassertNotDeepOrStrict(/ab/, /a/); 7091cb0ef41Sopenharmony_ciassertNotDeepOrStrict(/a/g, /a/); 7101cb0ef41Sopenharmony_ciassertNotDeepOrStrict(/a/i, /a/); 7111cb0ef41Sopenharmony_ciassertNotDeepOrStrict(/a/m, /a/); 7121cb0ef41Sopenharmony_ciassertNotDeepOrStrict(/a/igm, /a/im); 7131cb0ef41Sopenharmony_ci 7141cb0ef41Sopenharmony_ci{ 7151cb0ef41Sopenharmony_ci const re1 = /a/g; 7161cb0ef41Sopenharmony_ci re1.lastIndex = 3; 7171cb0ef41Sopenharmony_ci assert.notDeepEqual(re1, /a/g); 7181cb0ef41Sopenharmony_ci} 7191cb0ef41Sopenharmony_ci 7201cb0ef41Sopenharmony_ciassert.deepEqual(4, '4'); 7211cb0ef41Sopenharmony_ciassert.deepEqual(true, 1); 7221cb0ef41Sopenharmony_ciassert.throws(() => assert.deepEqual(4, '5'), 7231cb0ef41Sopenharmony_ci AssertionError, 7241cb0ef41Sopenharmony_ci 'deepEqual( 4, \'5\')'); 7251cb0ef41Sopenharmony_ci 7261cb0ef41Sopenharmony_ci// Having the same number of owned properties && the same set of keys. 7271cb0ef41Sopenharmony_ciassert.deepEqual({ a: 4 }, { a: 4 }); 7281cb0ef41Sopenharmony_ciassert.deepEqual({ a: 4, b: '2' }, { a: 4, b: '2' }); 7291cb0ef41Sopenharmony_ciassert.deepEqual([4], ['4']); 7301cb0ef41Sopenharmony_ciassert.throws( 7311cb0ef41Sopenharmony_ci () => assert.deepEqual({ a: 4 }, { a: 4, b: true }), AssertionError); 7321cb0ef41Sopenharmony_ciassert.notDeepEqual(['a'], { 0: 'a' }); 7331cb0ef41Sopenharmony_ciassert.deepEqual({ a: 4, b: '1' }, { b: '1', a: 4 }); 7341cb0ef41Sopenharmony_ciconst a1 = [1, 2, 3]; 7351cb0ef41Sopenharmony_ciconst a2 = [1, 2, 3]; 7361cb0ef41Sopenharmony_cia1.a = 'test'; 7371cb0ef41Sopenharmony_cia1.b = true; 7381cb0ef41Sopenharmony_cia2.b = true; 7391cb0ef41Sopenharmony_cia2.a = 'test'; 7401cb0ef41Sopenharmony_ciassert.throws(() => assert.deepEqual(Object.keys(a1), Object.keys(a2)), 7411cb0ef41Sopenharmony_ci AssertionError); 7421cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(a1, a2); 7431cb0ef41Sopenharmony_ci 7441cb0ef41Sopenharmony_ci// Having an identical prototype property. 7451cb0ef41Sopenharmony_ciconst nbRoot = { 7461cb0ef41Sopenharmony_ci toString() { return `${this.first} ${this.last}`; } 7471cb0ef41Sopenharmony_ci}; 7481cb0ef41Sopenharmony_ci 7491cb0ef41Sopenharmony_cifunction nameBuilder(first, last) { 7501cb0ef41Sopenharmony_ci this.first = first; 7511cb0ef41Sopenharmony_ci this.last = last; 7521cb0ef41Sopenharmony_ci return this; 7531cb0ef41Sopenharmony_ci} 7541cb0ef41Sopenharmony_cinameBuilder.prototype = nbRoot; 7551cb0ef41Sopenharmony_ci 7561cb0ef41Sopenharmony_cifunction nameBuilder2(first, last) { 7571cb0ef41Sopenharmony_ci this.first = first; 7581cb0ef41Sopenharmony_ci this.last = last; 7591cb0ef41Sopenharmony_ci return this; 7601cb0ef41Sopenharmony_ci} 7611cb0ef41Sopenharmony_cinameBuilder2.prototype = nbRoot; 7621cb0ef41Sopenharmony_ci 7631cb0ef41Sopenharmony_ciconst nb1 = new nameBuilder('Ryan', 'Dahl'); 7641cb0ef41Sopenharmony_cilet nb2 = new nameBuilder2('Ryan', 'Dahl'); 7651cb0ef41Sopenharmony_ci 7661cb0ef41Sopenharmony_ciassert.deepEqual(nb1, nb2); 7671cb0ef41Sopenharmony_ci 7681cb0ef41Sopenharmony_cinameBuilder2.prototype = Object; 7691cb0ef41Sopenharmony_cinb2 = new nameBuilder2('Ryan', 'Dahl'); 7701cb0ef41Sopenharmony_ciassert.deepEqual(nb1, nb2); 7711cb0ef41Sopenharmony_ci 7721cb0ef41Sopenharmony_ci// Primitives 7731cb0ef41Sopenharmony_ciassertNotDeepOrStrict(null, {}); 7741cb0ef41Sopenharmony_ciassertNotDeepOrStrict(undefined, {}); 7751cb0ef41Sopenharmony_ciassertNotDeepOrStrict('a', ['a']); 7761cb0ef41Sopenharmony_ciassertNotDeepOrStrict('a', { 0: 'a' }); 7771cb0ef41Sopenharmony_ciassertNotDeepOrStrict(1, {}); 7781cb0ef41Sopenharmony_ciassertNotDeepOrStrict(true, {}); 7791cb0ef41Sopenharmony_ciassertNotDeepOrStrict(Symbol(), {}); 7801cb0ef41Sopenharmony_ciassertNotDeepOrStrict(Symbol(), Symbol()); 7811cb0ef41Sopenharmony_ci 7821cb0ef41Sopenharmony_ciassertOnlyDeepEqual(4, '4'); 7831cb0ef41Sopenharmony_ciassertOnlyDeepEqual(true, 1); 7841cb0ef41Sopenharmony_ci 7851cb0ef41Sopenharmony_ci{ 7861cb0ef41Sopenharmony_ci const s = Symbol(); 7871cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(s, s); 7881cb0ef41Sopenharmony_ci} 7891cb0ef41Sopenharmony_ci 7901cb0ef41Sopenharmony_ci// Primitive wrappers and object. 7911cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new String('a'), ['a']); 7921cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new String('a'), { 0: 'a' }); 7931cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Number(1), {}); 7941cb0ef41Sopenharmony_ciassertNotDeepOrStrict(new Boolean(true), {}); 7951cb0ef41Sopenharmony_ci 7961cb0ef41Sopenharmony_ci// Same number of keys but different key names. 7971cb0ef41Sopenharmony_ciassertNotDeepOrStrict({ a: 1 }, { b: 1 }); 7981cb0ef41Sopenharmony_ci 7991cb0ef41Sopenharmony_ciassert.deepStrictEqual(new Date(2000, 3, 14), new Date(2000, 3, 14)); 8001cb0ef41Sopenharmony_ci 8011cb0ef41Sopenharmony_ciassert.throws( 8021cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(new Date(), new Date(2000, 3, 14)), 8031cb0ef41Sopenharmony_ci AssertionError, 8041cb0ef41Sopenharmony_ci 'deepStrictEqual(new Date(), new Date(2000, 3, 14))' 8051cb0ef41Sopenharmony_ci); 8061cb0ef41Sopenharmony_ci 8071cb0ef41Sopenharmony_ciassert.throws( 8081cb0ef41Sopenharmony_ci () => assert.notDeepStrictEqual(new Date(2000, 3, 14), new Date(2000, 3, 14)), 8091cb0ef41Sopenharmony_ci { 8101cb0ef41Sopenharmony_ci name: 'AssertionError', 8111cb0ef41Sopenharmony_ci message: 'Expected "actual" not to be strictly deep-equal to:\n\n' + 8121cb0ef41Sopenharmony_ci util.inspect(new Date(2000, 3, 14)) 8131cb0ef41Sopenharmony_ci } 8141cb0ef41Sopenharmony_ci); 8151cb0ef41Sopenharmony_ci 8161cb0ef41Sopenharmony_ciassert.notDeepStrictEqual(new Date(), new Date(2000, 3, 14)); 8171cb0ef41Sopenharmony_ci 8181cb0ef41Sopenharmony_ciassert.throws( 8191cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(/ab/, /a/), 8201cb0ef41Sopenharmony_ci { 8211cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 8221cb0ef41Sopenharmony_ci name: 'AssertionError', 8231cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n+ /ab/\n- /a/` 8241cb0ef41Sopenharmony_ci }); 8251cb0ef41Sopenharmony_ciassert.throws( 8261cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(/a/g, /a/), 8271cb0ef41Sopenharmony_ci { 8281cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 8291cb0ef41Sopenharmony_ci name: 'AssertionError', 8301cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n+ /a/g\n- /a/` 8311cb0ef41Sopenharmony_ci }); 8321cb0ef41Sopenharmony_ciassert.throws( 8331cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(/a/i, /a/), 8341cb0ef41Sopenharmony_ci { 8351cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 8361cb0ef41Sopenharmony_ci name: 'AssertionError', 8371cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n+ /a/i\n- /a/` 8381cb0ef41Sopenharmony_ci }); 8391cb0ef41Sopenharmony_ciassert.throws( 8401cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(/a/m, /a/), 8411cb0ef41Sopenharmony_ci { 8421cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 8431cb0ef41Sopenharmony_ci name: 'AssertionError', 8441cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n+ /a/m\n- /a/` 8451cb0ef41Sopenharmony_ci }); 8461cb0ef41Sopenharmony_ciassert.throws( 8471cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(/aa/igm, /aa/im), 8481cb0ef41Sopenharmony_ci { 8491cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 8501cb0ef41Sopenharmony_ci name: 'AssertionError', 8511cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n+ /aa/gim\n- /aa/im\n ^` 8521cb0ef41Sopenharmony_ci }); 8531cb0ef41Sopenharmony_ci 8541cb0ef41Sopenharmony_ci{ 8551cb0ef41Sopenharmony_ci const re1 = /a/; 8561cb0ef41Sopenharmony_ci re1.lastIndex = 3; 8571cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(re1, /a/); 8581cb0ef41Sopenharmony_ci} 8591cb0ef41Sopenharmony_ci 8601cb0ef41Sopenharmony_ciassert.throws( 8611cb0ef41Sopenharmony_ci // eslint-disable-next-line no-restricted-syntax 8621cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(4, '4'), 8631cb0ef41Sopenharmony_ci { message: `${defaultMsgStart}\n4 !== '4'\n` } 8641cb0ef41Sopenharmony_ci); 8651cb0ef41Sopenharmony_ci 8661cb0ef41Sopenharmony_ciassert.throws( 8671cb0ef41Sopenharmony_ci // eslint-disable-next-line no-restricted-syntax 8681cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(true, 1), 8691cb0ef41Sopenharmony_ci { message: `${defaultMsgStart}\ntrue !== 1\n` } 8701cb0ef41Sopenharmony_ci); 8711cb0ef41Sopenharmony_ci 8721cb0ef41Sopenharmony_ci// Having the same number of owned properties && the same set of keys. 8731cb0ef41Sopenharmony_ciassert.deepStrictEqual({ a: 4 }, { a: 4 }); 8741cb0ef41Sopenharmony_ciassert.deepStrictEqual({ a: 4, b: '2' }, { a: 4, b: '2' }); 8751cb0ef41Sopenharmony_ciassert.throws(() => assert.deepStrictEqual([4], ['4']), 8761cb0ef41Sopenharmony_ci { 8771cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 8781cb0ef41Sopenharmony_ci name: 'AssertionError', 8791cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n [\n+ 4\n- '4'\n ]` 8801cb0ef41Sopenharmony_ci }); 8811cb0ef41Sopenharmony_ciassert.throws( 8821cb0ef41Sopenharmony_ci () => assert.deepStrictEqual({ a: 4 }, { a: 4, b: true }), 8831cb0ef41Sopenharmony_ci { 8841cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 8851cb0ef41Sopenharmony_ci name: 'AssertionError', 8861cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n ` + 8871cb0ef41Sopenharmony_ci '{\n a: 4,\n- b: true\n }' 8881cb0ef41Sopenharmony_ci }); 8891cb0ef41Sopenharmony_ciassert.throws( 8901cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(['a'], { 0: 'a' }), 8911cb0ef41Sopenharmony_ci { 8921cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 8931cb0ef41Sopenharmony_ci name: 'AssertionError', 8941cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n` + 8951cb0ef41Sopenharmony_ci "+ [\n+ 'a'\n+ ]\n- {\n- '0': 'a'\n- }" 8961cb0ef41Sopenharmony_ci }); 8971cb0ef41Sopenharmony_ci 8981cb0ef41Sopenharmony_ci/* eslint-enable */ 8991cb0ef41Sopenharmony_ci 9001cb0ef41Sopenharmony_ciassertDeepAndStrictEqual({ a: 4, b: '1' }, { b: '1', a: 4 }); 9011cb0ef41Sopenharmony_ci 9021cb0ef41Sopenharmony_ciassert.throws( 9031cb0ef41Sopenharmony_ci () => assert.deepStrictEqual([0, 1, 2, 'a', 'b'], [0, 1, 2, 'b', 'a']), 9041cb0ef41Sopenharmony_ci AssertionError); 9051cb0ef41Sopenharmony_ci 9061cb0ef41Sopenharmony_ci// Prototype check. 9071cb0ef41Sopenharmony_cifunction Constructor1(first, last) { 9081cb0ef41Sopenharmony_ci this.first = first; 9091cb0ef41Sopenharmony_ci this.last = last; 9101cb0ef41Sopenharmony_ci} 9111cb0ef41Sopenharmony_ci 9121cb0ef41Sopenharmony_cifunction Constructor2(first, last) { 9131cb0ef41Sopenharmony_ci this.first = first; 9141cb0ef41Sopenharmony_ci this.last = last; 9151cb0ef41Sopenharmony_ci} 9161cb0ef41Sopenharmony_ci 9171cb0ef41Sopenharmony_ciconst obj1 = new Constructor1('Ryan', 'Dahl'); 9181cb0ef41Sopenharmony_cilet obj2 = new Constructor2('Ryan', 'Dahl'); 9191cb0ef41Sopenharmony_ci 9201cb0ef41Sopenharmony_ciassert.throws(() => assert.deepStrictEqual(obj1, obj2), AssertionError); 9211cb0ef41Sopenharmony_ci 9221cb0ef41Sopenharmony_ciConstructor2.prototype = Constructor1.prototype; 9231cb0ef41Sopenharmony_ciobj2 = new Constructor2('Ryan', 'Dahl'); 9241cb0ef41Sopenharmony_ci 9251cb0ef41Sopenharmony_ciassertDeepAndStrictEqual(obj1, obj2); 9261cb0ef41Sopenharmony_ci 9271cb0ef41Sopenharmony_ci// Check extra properties on errors. 9281cb0ef41Sopenharmony_ci{ 9291cb0ef41Sopenharmony_ci const a = new TypeError('foo'); 9301cb0ef41Sopenharmony_ci const b = new TypeError('foo'); 9311cb0ef41Sopenharmony_ci a.foo = 'bar'; 9321cb0ef41Sopenharmony_ci b.foo = 'baz.'; 9331cb0ef41Sopenharmony_ci 9341cb0ef41Sopenharmony_ci assert.throws( 9351cb0ef41Sopenharmony_ci () => assert.throws( 9361cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(a, b), 9371cb0ef41Sopenharmony_ci { 9381cb0ef41Sopenharmony_ci operator: 'throws', 9391cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n` + 9401cb0ef41Sopenharmony_ci ' [TypeError: foo] {\n+ foo: \'bar\'\n- foo: \'baz\'\n }', 9411cb0ef41Sopenharmony_ci } 9421cb0ef41Sopenharmony_ci ), 9431cb0ef41Sopenharmony_ci { 9441cb0ef41Sopenharmony_ci message: 'Expected values to be strictly deep-equal:\n' + 9451cb0ef41Sopenharmony_ci '+ actual - expected ... Lines skipped\n' + 9461cb0ef41Sopenharmony_ci '\n' + 9471cb0ef41Sopenharmony_ci ' Comparison {\n' + 9481cb0ef41Sopenharmony_ci " message: 'Expected values to be strictly deep-equal:\\n' +\n" + 9491cb0ef41Sopenharmony_ci '...\n' + 9501cb0ef41Sopenharmony_ci " ' [TypeError: foo] {\\n' +\n" + 9511cb0ef41Sopenharmony_ci " \"+ foo: 'bar'\\n\" +\n" + 9521cb0ef41Sopenharmony_ci "+ \"- foo: 'baz.'\\n\" +\n" + 9531cb0ef41Sopenharmony_ci "- \"- foo: 'baz'\\n\" +\n" + 9541cb0ef41Sopenharmony_ci " ' }',\n" + 9551cb0ef41Sopenharmony_ci "+ operator: 'deepStrictEqual'\n" + 9561cb0ef41Sopenharmony_ci "- operator: 'throws'\n" + 9571cb0ef41Sopenharmony_ci ' }' 9581cb0ef41Sopenharmony_ci } 9591cb0ef41Sopenharmony_ci ); 9601cb0ef41Sopenharmony_ci} 9611cb0ef41Sopenharmony_ci 9621cb0ef41Sopenharmony_ci// Check proxies. 9631cb0ef41Sopenharmony_ci{ 9641cb0ef41Sopenharmony_ci const arrProxy = new Proxy([1, 2], {}); 9651cb0ef41Sopenharmony_ci assert.deepStrictEqual(arrProxy, [1, 2]); 9661cb0ef41Sopenharmony_ci const tmp = util.inspect.defaultOptions; 9671cb0ef41Sopenharmony_ci util.inspect.defaultOptions = { showProxy: true }; 9681cb0ef41Sopenharmony_ci assert.throws( 9691cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(arrProxy, [1, 2, 3]), 9701cb0ef41Sopenharmony_ci { message: `${defaultMsgStartFull}\n\n` + 9711cb0ef41Sopenharmony_ci ' [\n 1,\n 2,\n- 3\n ]' } 9721cb0ef41Sopenharmony_ci ); 9731cb0ef41Sopenharmony_ci util.inspect.defaultOptions = tmp; 9741cb0ef41Sopenharmony_ci 9751cb0ef41Sopenharmony_ci const invalidTrap = new Proxy([1, 2, 3], { 9761cb0ef41Sopenharmony_ci ownKeys() { return []; } 9771cb0ef41Sopenharmony_ci }); 9781cb0ef41Sopenharmony_ci assert.throws( 9791cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(invalidTrap, [1, 2, 3]), 9801cb0ef41Sopenharmony_ci { 9811cb0ef41Sopenharmony_ci name: 'TypeError', 9821cb0ef41Sopenharmony_ci message: "'ownKeys' on proxy: trap result did not include 'length'" 9831cb0ef41Sopenharmony_ci } 9841cb0ef41Sopenharmony_ci ); 9851cb0ef41Sopenharmony_ci} 9861cb0ef41Sopenharmony_ci 9871cb0ef41Sopenharmony_ci// Strict equal with identical objects that are not identical 9881cb0ef41Sopenharmony_ci// by reference and longer than 50 elements 9891cb0ef41Sopenharmony_ci// E.g., assert.deepStrictEqual({ a: Symbol() }, { a: Symbol() }) 9901cb0ef41Sopenharmony_ci{ 9911cb0ef41Sopenharmony_ci const a = {}; 9921cb0ef41Sopenharmony_ci const b = {}; 9931cb0ef41Sopenharmony_ci for (let i = 0; i < 55; i++) { 9941cb0ef41Sopenharmony_ci a[`symbol${i}`] = Symbol(); 9951cb0ef41Sopenharmony_ci b[`symbol${i}`] = Symbol(); 9961cb0ef41Sopenharmony_ci } 9971cb0ef41Sopenharmony_ci 9981cb0ef41Sopenharmony_ci assert.throws( 9991cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(a, b), 10001cb0ef41Sopenharmony_ci { 10011cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 10021cb0ef41Sopenharmony_ci name: 'AssertionError', 10031cb0ef41Sopenharmony_ci message: /\.\.\./g 10041cb0ef41Sopenharmony_ci } 10051cb0ef41Sopenharmony_ci ); 10061cb0ef41Sopenharmony_ci} 10071cb0ef41Sopenharmony_ci 10081cb0ef41Sopenharmony_ci// Basic valueOf check. 10091cb0ef41Sopenharmony_ci{ 10101cb0ef41Sopenharmony_ci const a = new String(1); 10111cb0ef41Sopenharmony_ci a.valueOf = undefined; 10121cb0ef41Sopenharmony_ci assertNotDeepOrStrict(a, new String(1)); 10131cb0ef41Sopenharmony_ci} 10141cb0ef41Sopenharmony_ci 10151cb0ef41Sopenharmony_ci// Basic array out of bounds check. 10161cb0ef41Sopenharmony_ci{ 10171cb0ef41Sopenharmony_ci const arr = [1, 2, 3]; 10181cb0ef41Sopenharmony_ci arr[2 ** 32] = true; 10191cb0ef41Sopenharmony_ci assertNotDeepOrStrict(arr, [1, 2, 3]); 10201cb0ef41Sopenharmony_ci} 10211cb0ef41Sopenharmony_ci 10221cb0ef41Sopenharmony_ciassert.throws( 10231cb0ef41Sopenharmony_ci () => assert.deepStrictEqual([1, 2, 3], [1, 2]), 10241cb0ef41Sopenharmony_ci { 10251cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 10261cb0ef41Sopenharmony_ci name: 'AssertionError', 10271cb0ef41Sopenharmony_ci message: `${defaultMsgStartFull}\n\n` + 10281cb0ef41Sopenharmony_ci ' [\n' + 10291cb0ef41Sopenharmony_ci ' 1,\n' + 10301cb0ef41Sopenharmony_ci ' 2,\n' + 10311cb0ef41Sopenharmony_ci '+ 3\n' + 10321cb0ef41Sopenharmony_ci ' ]' 10331cb0ef41Sopenharmony_ci } 10341cb0ef41Sopenharmony_ci); 10351cb0ef41Sopenharmony_ci 10361cb0ef41Sopenharmony_ci// Verify that manipulating the `getTime()` function has no impact on the time 10371cb0ef41Sopenharmony_ci// verification. 10381cb0ef41Sopenharmony_ci{ 10391cb0ef41Sopenharmony_ci const a = new Date('2000'); 10401cb0ef41Sopenharmony_ci const b = new Date('2000'); 10411cb0ef41Sopenharmony_ci Object.defineProperty(a, 'getTime', { 10421cb0ef41Sopenharmony_ci value: () => 5 10431cb0ef41Sopenharmony_ci }); 10441cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(a, b); 10451cb0ef41Sopenharmony_ci} 10461cb0ef41Sopenharmony_ci 10471cb0ef41Sopenharmony_ci// Verify that an array and the equivalent fake array object 10481cb0ef41Sopenharmony_ci// are correctly compared 10491cb0ef41Sopenharmony_ci{ 10501cb0ef41Sopenharmony_ci const a = [1, 2, 3]; 10511cb0ef41Sopenharmony_ci const o = { 10521cb0ef41Sopenharmony_ci __proto__: Array.prototype, 10531cb0ef41Sopenharmony_ci 0: 1, 10541cb0ef41Sopenharmony_ci 1: 2, 10551cb0ef41Sopenharmony_ci 2: 3, 10561cb0ef41Sopenharmony_ci length: 3, 10571cb0ef41Sopenharmony_ci }; 10581cb0ef41Sopenharmony_ci Object.defineProperty(o, 'length', { enumerable: false }); 10591cb0ef41Sopenharmony_ci assertNotDeepOrStrict(o, a); 10601cb0ef41Sopenharmony_ci} 10611cb0ef41Sopenharmony_ci 10621cb0ef41Sopenharmony_ci// Verify that extra keys will be tested for when using fake arrays. 10631cb0ef41Sopenharmony_ci{ 10641cb0ef41Sopenharmony_ci const a = { 10651cb0ef41Sopenharmony_ci 0: 1, 10661cb0ef41Sopenharmony_ci 1: 1, 10671cb0ef41Sopenharmony_ci 2: 'broken' 10681cb0ef41Sopenharmony_ci }; 10691cb0ef41Sopenharmony_ci Object.setPrototypeOf(a, Object.getPrototypeOf([])); 10701cb0ef41Sopenharmony_ci Object.defineProperty(a, Symbol.toStringTag, { 10711cb0ef41Sopenharmony_ci value: 'Array', 10721cb0ef41Sopenharmony_ci }); 10731cb0ef41Sopenharmony_ci Object.defineProperty(a, 'length', { 10741cb0ef41Sopenharmony_ci value: 2 10751cb0ef41Sopenharmony_ci }); 10761cb0ef41Sopenharmony_ci assertNotDeepOrStrict(a, [1, 1]); 10771cb0ef41Sopenharmony_ci} 10781cb0ef41Sopenharmony_ci 10791cb0ef41Sopenharmony_ci// Verify that changed tags will still check for the error message. 10801cb0ef41Sopenharmony_ci{ 10811cb0ef41Sopenharmony_ci const err = new Error('foo'); 10821cb0ef41Sopenharmony_ci err[Symbol.toStringTag] = 'Foobar'; 10831cb0ef41Sopenharmony_ci const err2 = new Error('bar'); 10841cb0ef41Sopenharmony_ci err2[Symbol.toStringTag] = 'Foobar'; 10851cb0ef41Sopenharmony_ci assertNotDeepOrStrict(err, err2, AssertionError); 10861cb0ef41Sopenharmony_ci} 10871cb0ef41Sopenharmony_ci 10881cb0ef41Sopenharmony_ci// Check for non-native errors. 10891cb0ef41Sopenharmony_ci{ 10901cb0ef41Sopenharmony_ci const source = new Error('abc'); 10911cb0ef41Sopenharmony_ci const err = Object.create( 10921cb0ef41Sopenharmony_ci Object.getPrototypeOf(source), Object.getOwnPropertyDescriptors(source)); 10931cb0ef41Sopenharmony_ci Object.defineProperty(err, 'message', { value: 'foo' }); 10941cb0ef41Sopenharmony_ci const err2 = Object.create( 10951cb0ef41Sopenharmony_ci Object.getPrototypeOf(source), Object.getOwnPropertyDescriptors(source)); 10961cb0ef41Sopenharmony_ci Object.defineProperty(err2, 'message', { value: 'bar' }); 10971cb0ef41Sopenharmony_ci err[Symbol.toStringTag] = 'Foo'; 10981cb0ef41Sopenharmony_ci err2[Symbol.toStringTag] = 'Foo'; 10991cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(err, err2); 11001cb0ef41Sopenharmony_ci} 11011cb0ef41Sopenharmony_ci 11021cb0ef41Sopenharmony_ci// Verify that `valueOf` is not called for boxed primitives. 11031cb0ef41Sopenharmony_ci{ 11041cb0ef41Sopenharmony_ci const a = new Number(5); 11051cb0ef41Sopenharmony_ci const b = new Number(5); 11061cb0ef41Sopenharmony_ci Object.defineProperty(a, 'valueOf', { 11071cb0ef41Sopenharmony_ci value: () => { throw new Error('failed'); } 11081cb0ef41Sopenharmony_ci }); 11091cb0ef41Sopenharmony_ci Object.defineProperty(b, 'valueOf', { 11101cb0ef41Sopenharmony_ci value: () => { throw new Error('failed'); } 11111cb0ef41Sopenharmony_ci }); 11121cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(a, b); 11131cb0ef41Sopenharmony_ci} 11141cb0ef41Sopenharmony_ci 11151cb0ef41Sopenharmony_ci// Check getters. 11161cb0ef41Sopenharmony_ci{ 11171cb0ef41Sopenharmony_ci const a = { 11181cb0ef41Sopenharmony_ci get a() { return 5; } 11191cb0ef41Sopenharmony_ci }; 11201cb0ef41Sopenharmony_ci const b = { 11211cb0ef41Sopenharmony_ci get a() { return 6; } 11221cb0ef41Sopenharmony_ci }; 11231cb0ef41Sopenharmony_ci assert.throws( 11241cb0ef41Sopenharmony_ci () => assert.deepStrictEqual(a, b), 11251cb0ef41Sopenharmony_ci { 11261cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 11271cb0ef41Sopenharmony_ci name: 'AssertionError', 11281cb0ef41Sopenharmony_ci message: /a: \[Getter: 5]\n- {3}a: \[Getter: 6]\n {2}/ 11291cb0ef41Sopenharmony_ci } 11301cb0ef41Sopenharmony_ci ); 11311cb0ef41Sopenharmony_ci 11321cb0ef41Sopenharmony_ci // The descriptor is not compared. 11331cb0ef41Sopenharmony_ci assertDeepAndStrictEqual(a, { a: 5 }); 11341cb0ef41Sopenharmony_ci} 11351cb0ef41Sopenharmony_ci 11361cb0ef41Sopenharmony_ci// Verify object types being identical on both sides. 11371cb0ef41Sopenharmony_ci{ 11381cb0ef41Sopenharmony_ci let a = Buffer.from('test'); 11391cb0ef41Sopenharmony_ci let b = Object.create( 11401cb0ef41Sopenharmony_ci Object.getPrototypeOf(a), 11411cb0ef41Sopenharmony_ci Object.getOwnPropertyDescriptors(a) 11421cb0ef41Sopenharmony_ci ); 11431cb0ef41Sopenharmony_ci Object.defineProperty(b, Symbol.toStringTag, { 11441cb0ef41Sopenharmony_ci value: 'Uint8Array' 11451cb0ef41Sopenharmony_ci }); 11461cb0ef41Sopenharmony_ci assertNotDeepOrStrict(a, b); 11471cb0ef41Sopenharmony_ci 11481cb0ef41Sopenharmony_ci a = new Uint8Array(10); 11491cb0ef41Sopenharmony_ci b = new Int8Array(10); 11501cb0ef41Sopenharmony_ci Object.defineProperty(b, Symbol.toStringTag, { 11511cb0ef41Sopenharmony_ci value: 'Uint8Array' 11521cb0ef41Sopenharmony_ci }); 11531cb0ef41Sopenharmony_ci Object.setPrototypeOf(b, Uint8Array.prototype); 11541cb0ef41Sopenharmony_ci assertNotDeepOrStrict(a, b); 11551cb0ef41Sopenharmony_ci 11561cb0ef41Sopenharmony_ci a = [1, 2, 3]; 11571cb0ef41Sopenharmony_ci b = { 0: 1, 1: 2, 2: 3 }; 11581cb0ef41Sopenharmony_ci Object.setPrototypeOf(b, Array.prototype); 11591cb0ef41Sopenharmony_ci Object.defineProperty(b, 'length', { value: 3, enumerable: false }); 11601cb0ef41Sopenharmony_ci Object.defineProperty(b, Symbol.toStringTag, { 11611cb0ef41Sopenharmony_ci value: 'Array' 11621cb0ef41Sopenharmony_ci }); 11631cb0ef41Sopenharmony_ci assertNotDeepOrStrict(a, b); 11641cb0ef41Sopenharmony_ci 11651cb0ef41Sopenharmony_ci a = new Date(2000); 11661cb0ef41Sopenharmony_ci b = Object.create( 11671cb0ef41Sopenharmony_ci Object.getPrototypeOf(a), 11681cb0ef41Sopenharmony_ci Object.getOwnPropertyDescriptors(a) 11691cb0ef41Sopenharmony_ci ); 11701cb0ef41Sopenharmony_ci Object.defineProperty(b, Symbol.toStringTag, { 11711cb0ef41Sopenharmony_ci value: 'Date' 11721cb0ef41Sopenharmony_ci }); 11731cb0ef41Sopenharmony_ci assertNotDeepOrStrict(a, b); 11741cb0ef41Sopenharmony_ci 11751cb0ef41Sopenharmony_ci a = /abc/g; 11761cb0ef41Sopenharmony_ci b = Object.create( 11771cb0ef41Sopenharmony_ci Object.getPrototypeOf(a), 11781cb0ef41Sopenharmony_ci Object.getOwnPropertyDescriptors(a) 11791cb0ef41Sopenharmony_ci ); 11801cb0ef41Sopenharmony_ci Object.defineProperty(b, Symbol.toStringTag, { 11811cb0ef41Sopenharmony_ci value: 'RegExp' 11821cb0ef41Sopenharmony_ci }); 11831cb0ef41Sopenharmony_ci assertNotDeepOrStrict(a, b); 11841cb0ef41Sopenharmony_ci 11851cb0ef41Sopenharmony_ci a = []; 11861cb0ef41Sopenharmony_ci b = /abc/; 11871cb0ef41Sopenharmony_ci Object.setPrototypeOf(b, Array.prototype); 11881cb0ef41Sopenharmony_ci Object.defineProperty(b, Symbol.toStringTag, { 11891cb0ef41Sopenharmony_ci value: 'Array' 11901cb0ef41Sopenharmony_ci }); 11911cb0ef41Sopenharmony_ci assertNotDeepOrStrict(a, b); 11921cb0ef41Sopenharmony_ci 11931cb0ef41Sopenharmony_ci a = Object.create(null); 11941cb0ef41Sopenharmony_ci b = new RangeError('abc'); 11951cb0ef41Sopenharmony_ci Object.defineProperty(a, Symbol.toStringTag, { 11961cb0ef41Sopenharmony_ci value: 'Error' 11971cb0ef41Sopenharmony_ci }); 11981cb0ef41Sopenharmony_ci Object.setPrototypeOf(b, null); 11991cb0ef41Sopenharmony_ci assertNotDeepOrStrict(a, b, assert.AssertionError); 12001cb0ef41Sopenharmony_ci} 12011cb0ef41Sopenharmony_ci 12021cb0ef41Sopenharmony_ci{ 12031cb0ef41Sopenharmony_ci // Verify commutativity 12041cb0ef41Sopenharmony_ci // Regression test for https://github.com/nodejs/node/issues/37710 12051cb0ef41Sopenharmony_ci const a = { x: 1 }; 12061cb0ef41Sopenharmony_ci const b = { y: 1 }; 12071cb0ef41Sopenharmony_ci Object.defineProperty(b, 'x', { value: 1 }); 12081cb0ef41Sopenharmony_ci 12091cb0ef41Sopenharmony_ci assertNotDeepOrStrict(a, b); 12101cb0ef41Sopenharmony_ci} 1211