• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3require('../common');
4const assert = require('assert');
5const util = require('util');
6const { AssertionError } = assert;
7const defaultMsgStart = 'Expected values to be strictly deep-equal:\n';
8const defaultMsgStartFull = `${defaultMsgStart}+ actual - expected`;
9
10// Disable colored output to prevent color codes from breaking assertion
11// message comparisons. This should only be an issue when process.stdout
12// is a TTY.
13if (process.stdout.isTTY)
14  process.env.NODE_DISABLE_COLORS = '1';
15
16// Template tag function turning an error message into a RegExp
17// for assert.throws()
18function re(literals, ...values) {
19  let result = 'Expected values to be loosely deep-equal:\n\n';
20  for (const [i, value] of values.entries()) {
21    const str = util.inspect(value, {
22      compact: false,
23      depth: 1000,
24      customInspect: false,
25      maxArrayLength: Infinity,
26      breakLength: Infinity,
27      sorted: true,
28      getters: true
29    });
30    // Need to escape special characters.
31    result += `${str}${literals[i + 1]}`;
32  }
33  return {
34    code: 'ERR_ASSERTION',
35    message: result
36  };
37}
38
39// The following deepEqual tests might seem very weird.
40// They just describe what it is now.
41// That is why we discourage using deepEqual in our own tests.
42
43// Turn off no-restricted-properties because we are testing deepEqual!
44/* eslint-disable no-restricted-properties */
45
46const arr = new Uint8Array([120, 121, 122, 10]);
47const buf = Buffer.from(arr);
48// They have different [[Prototype]]
49assert.throws(
50  () => assert.deepStrictEqual(arr, buf),
51  {
52    code: 'ERR_ASSERTION',
53    message: `${defaultMsgStartFull} ... Lines skipped\n\n` +
54             '+ Uint8Array(4) [\n' +
55             '- Buffer(4) [Uint8Array] [\n    120,\n...\n    122,\n    10\n  ]'
56  }
57);
58assert.deepEqual(arr, buf);
59
60{
61  const buf2 = Buffer.from(arr);
62  buf2.prop = 1;
63
64  assert.throws(
65    () => assert.deepStrictEqual(buf2, buf),
66    {
67      code: 'ERR_ASSERTION',
68      message: `${defaultMsgStartFull}\n\n` +
69               '  Buffer(4) [Uint8Array] [\n' +
70               '    120,\n' +
71               '    121,\n' +
72               '    122,\n' +
73               '    10,\n' +
74               '+   prop: 1\n' +
75               '  ]'
76    }
77  );
78  assert.notDeepEqual(buf2, buf);
79}
80
81{
82  const arr2 = new Uint8Array([120, 121, 122, 10]);
83  arr2.prop = 5;
84  assert.throws(
85    () => assert.deepStrictEqual(arr, arr2),
86    {
87      code: 'ERR_ASSERTION',
88      message: `${defaultMsgStartFull}\n\n` +
89               '  Uint8Array(4) [\n' +
90               '    120,\n' +
91               '    121,\n' +
92               '    122,\n' +
93               '    10,\n' +
94               '-   prop: 5\n' +
95               '  ]'
96    }
97  );
98  assert.notDeepEqual(arr, arr2);
99}
100
101const date = new Date('2016');
102
103class MyDate extends Date {
104  constructor(...args) {
105    super(...args);
106    this[0] = '1';
107  }
108}
109
110const date2 = new MyDate('2016');
111
112assertNotDeepOrStrict(date, date2);
113assert.throws(
114  () => assert.deepStrictEqual(date, date2),
115  {
116    code: 'ERR_ASSERTION',
117    message: `${defaultMsgStartFull}\n\n` +
118             '+ 2016-01-01T00:00:00.000Z\n- MyDate 2016-01-01T00:00:00.000Z' +
119             " {\n-   '0': '1'\n- }"
120  }
121);
122assert.throws(
123  () => assert.deepStrictEqual(date2, date),
124  {
125    code: 'ERR_ASSERTION',
126    message: `${defaultMsgStartFull}\n\n` +
127             '+ MyDate 2016-01-01T00:00:00.000Z {\n' +
128             "+   '0': '1'\n+ }\n- 2016-01-01T00:00:00.000Z"
129  }
130);
131
132class MyRegExp extends RegExp {
133  constructor(...args) {
134    super(...args);
135    this[0] = '1';
136  }
137}
138
139const re1 = new RegExp('test');
140const re2 = new MyRegExp('test');
141
142assertNotDeepOrStrict(re1, re2);
143assert.throws(
144  () => assert.deepStrictEqual(re1, re2),
145  {
146    code: 'ERR_ASSERTION',
147    message: `${defaultMsgStartFull}\n\n` +
148             "+ /test/\n- MyRegExp /test/ {\n-   '0': '1'\n- }"
149  }
150);
151
152// For these weird cases, deepEqual should pass (at least for now),
153// but deepStrictEqual should throw.
154{
155  const similar = new Set([
156    { 0: 1 },  // Object
157    new String('1'),  // Object
158    [1],  // Array
159    date2,  // Date with this[0] = '1'
160    re2,  // RegExp with this[0] = '1'
161    new Int8Array([1]), // Int8Array
162    new Int16Array([1]), // Int16Array
163    new Uint16Array([1]), // Uint16Array
164    new Int32Array([1]), // Int32Array
165    new Uint32Array([1]), // Uint32Array
166    Buffer.from([1]), // Uint8Array
167    (function() { return arguments; })(1),
168  ]);
169
170  for (const a of similar) {
171    for (const b of similar) {
172      if (a !== b) {
173        assert.notDeepEqual(a, b);
174        assert.throws(
175          () => assert.deepStrictEqual(a, b),
176          { code: 'ERR_ASSERTION' }
177        );
178      }
179    }
180  }
181}
182
183function assertDeepAndStrictEqual(a, b) {
184  assert.deepEqual(a, b);
185  assert.deepStrictEqual(a, b);
186
187  assert.deepEqual(b, a);
188  assert.deepStrictEqual(b, a);
189}
190
191function assertNotDeepOrStrict(a, b, err) {
192  assert.throws(
193    () => assert.deepEqual(a, b),
194    err || re`${a}\n\nshould loosely deep-equal\n\n${b}`
195  );
196  assert.throws(
197    () => assert.deepStrictEqual(a, b),
198    err || { code: 'ERR_ASSERTION' }
199  );
200
201  assert.throws(
202    () => assert.deepEqual(b, a),
203    err || re`${b}\n\nshould loosely deep-equal\n\n${a}`
204  );
205  assert.throws(
206    () => assert.deepStrictEqual(b, a),
207    err || { code: 'ERR_ASSERTION' }
208  );
209}
210
211function assertOnlyDeepEqual(a, b, err) {
212  assert.deepEqual(a, b);
213  assert.throws(
214    () => assert.deepStrictEqual(a, b),
215    err || { code: 'ERR_ASSERTION' }
216  );
217
218  assert.deepEqual(b, a);
219  assert.throws(
220    () => assert.deepStrictEqual(b, a),
221    err || { code: 'ERR_ASSERTION' }
222  );
223}
224
225// es6 Maps and Sets
226assertDeepAndStrictEqual(new Set(), new Set());
227assertDeepAndStrictEqual(new Map(), new Map());
228
229assertDeepAndStrictEqual(new Set([1, 2, 3]), new Set([1, 2, 3]));
230assertNotDeepOrStrict(new Set([1, 2, 3]), new Set([1, 2, 3, 4]));
231assertNotDeepOrStrict(new Set([1, 2, 3, 4]), new Set([1, 2, 3]));
232assertDeepAndStrictEqual(new Set(['1', '2', '3']), new Set(['1', '2', '3']));
233assertDeepAndStrictEqual(new Set([[1, 2], [3, 4]]), new Set([[3, 4], [1, 2]]));
234assertNotDeepOrStrict(new Set([{ a: 0 }]), new Set([{ a: 1 }]));
235assertNotDeepOrStrict(new Set([Symbol()]), new Set([Symbol()]));
236
237{
238  const a = [ 1, 2 ];
239  const b = [ 3, 4 ];
240  const c = [ 1, 2 ];
241  const d = [ 3, 4 ];
242
243  assertDeepAndStrictEqual(
244    { a: a, b: b, s: new Set([a, b]) },
245    { a: c, b: d, s: new Set([d, c]) }
246  );
247}
248
249assertDeepAndStrictEqual(new Map([[1, 1], [2, 2]]), new Map([[1, 1], [2, 2]]));
250assertDeepAndStrictEqual(new Map([[1, 1], [2, 2]]), new Map([[2, 2], [1, 1]]));
251assertNotDeepOrStrict(new Map([[1, 1], [2, 2]]), new Map([[1, 2], [2, 1]]));
252assertNotDeepOrStrict(
253  new Map([[[1], 1], [{}, 2]]),
254  new Map([[[1], 2], [{}, 1]])
255);
256
257assertNotDeepOrStrict(new Set([1]), [1]);
258assertNotDeepOrStrict(new Set(), []);
259assertNotDeepOrStrict(new Set(), {});
260
261assertNotDeepOrStrict(new Map([['a', 1]]), { a: 1 });
262assertNotDeepOrStrict(new Map(), []);
263assertNotDeepOrStrict(new Map(), {});
264
265assertOnlyDeepEqual(new Set(['1']), new Set([1]));
266
267assertOnlyDeepEqual(new Map([['1', 'a']]), new Map([[1, 'a']]));
268assertOnlyDeepEqual(new Map([['a', '1']]), new Map([['a', 1]]));
269assertNotDeepOrStrict(new Map([['a', '1']]), new Map([['a', 2]]));
270
271assertDeepAndStrictEqual(new Set([{}]), new Set([{}]));
272
273// Ref: https://github.com/nodejs/node/issues/13347
274assertNotDeepOrStrict(
275  new Set([{ a: 1 }, { a: 1 }]),
276  new Set([{ a: 1 }, { a: 2 }])
277);
278assertNotDeepOrStrict(
279  new Set([{ a: 1 }, { a: 1 }, { a: 2 }]),
280  new Set([{ a: 1 }, { a: 2 }, { a: 2 }])
281);
282assertNotDeepOrStrict(
283  new Map([[{ x: 1 }, 5], [{ x: 1 }, 5]]),
284  new Map([[{ x: 1 }, 5], [{ x: 2 }, 5]])
285);
286
287assertNotDeepOrStrict(new Set([3, '3']), new Set([3, 4]));
288assertNotDeepOrStrict(new Map([[3, 0], ['3', 0]]), new Map([[3, 0], [4, 0]]));
289
290assertNotDeepOrStrict(
291  new Set([{ a: 1 }, { a: 1 }, { a: 2 }]),
292  new Set([{ a: 1 }, { a: 2 }, { a: 2 }])
293);
294
295// Mixed primitive and object keys
296assertDeepAndStrictEqual(
297  new Map([[1, 'a'], [{}, 'a']]),
298  new Map([[1, 'a'], [{}, 'a']])
299);
300assertDeepAndStrictEqual(
301  new Set([1, 'a', [{}, 'a']]),
302  new Set([1, 'a', [{}, 'a']])
303);
304
305// This is an awful case, where a map contains multiple equivalent keys:
306assertOnlyDeepEqual(
307  new Map([[1, 'a'], ['1', 'b']]),
308  new Map([['1', 'a'], [true, 'b']])
309);
310assertNotDeepOrStrict(
311  new Set(['a']),
312  new Set(['b'])
313);
314assertDeepAndStrictEqual(
315  new Map([[{}, 'a'], [{}, 'b']]),
316  new Map([[{}, 'b'], [{}, 'a']])
317);
318assertOnlyDeepEqual(
319  new Map([[true, 'a'], ['1', 'b'], [1, 'a']]),
320  new Map([['1', 'a'], [1, 'b'], [true, 'a']])
321);
322assertNotDeepOrStrict(
323  new Map([[true, 'a'], ['1', 'b'], [1, 'c']]),
324  new Map([['1', 'a'], [1, 'b'], [true, 'a']])
325);
326
327// Similar object keys
328assertNotDeepOrStrict(
329  new Set([{}, {}]),
330  new Set([{}, 1])
331);
332assertNotDeepOrStrict(
333  new Set([[{}, 1], [{}, 1]]),
334  new Set([[{}, 1], [1, 1]])
335);
336assertNotDeepOrStrict(
337  new Map([[{}, 1], [{}, 1]]),
338  new Map([[{}, 1], [1, 1]])
339);
340assertOnlyDeepEqual(
341  new Map([[{}, 1], [true, 1]]),
342  new Map([[{}, 1], [1, 1]])
343);
344
345// Similar primitive key / values
346assertNotDeepOrStrict(
347  new Set([1, true, false]),
348  new Set(['1', 0, '0'])
349);
350assertNotDeepOrStrict(
351  new Map([[1, 5], [true, 5], [false, 5]]),
352  new Map([['1', 5], [0, 5], ['0', 5]])
353);
354
355// Undefined value in Map
356assertDeepAndStrictEqual(
357  new Map([[1, undefined]]),
358  new Map([[1, undefined]])
359);
360assertOnlyDeepEqual(
361  new Map([[1, null], ['', '0']]),
362  new Map([['1', undefined], [false, 0]])
363);
364assertNotDeepOrStrict(
365  new Map([[1, undefined]]),
366  new Map([[2, undefined]])
367);
368
369// null as key
370assertDeepAndStrictEqual(
371  new Map([[null, 3]]),
372  new Map([[null, 3]])
373);
374assertOnlyDeepEqual(
375  new Map([[undefined, null], ['+000', 2n]]),
376  new Map([[null, undefined], [false, '2']]),
377);
378
379assertOnlyDeepEqual(
380  new Set([null, '', 1n, 5, 2n, false]),
381  new Set([undefined, 0, 5n, true, '2', '-000'])
382);
383assertNotDeepOrStrict(
384  new Set(['']),
385  new Set(['0'])
386);
387assertOnlyDeepEqual(
388  new Map([[1, {}]]),
389  new Map([[true, {}]])
390);
391assertOnlyDeepEqual(
392  new Map([[undefined, true]]),
393  new Map([[null, true]])
394);
395assertNotDeepOrStrict(
396  new Map([[undefined, true]]),
397  new Map([[true, true]])
398);
399
400// GH-6416. Make sure circular refs don't throw.
401{
402  const b = {};
403  b.b = b;
404  const c = {};
405  c.b = c;
406
407  assertDeepAndStrictEqual(b, c);
408
409  const d = {};
410  d.a = 1;
411  d.b = d;
412  const e = {};
413  e.a = 1;
414  e.b = {};
415
416  assertNotDeepOrStrict(d, e);
417}
418
419// GH-14441. Circular structures should be consistent
420{
421  const a = {};
422  const b = {};
423  a.a = a;
424  b.a = {};
425  b.a.a = a;
426  assertDeepAndStrictEqual(a, b);
427}
428
429{
430  const a = new Set();
431  const b = new Set();
432  const c = new Set();
433  a.add(a);
434  b.add(b);
435  c.add(a);
436  assertDeepAndStrictEqual(b, c);
437}
438
439// https://github.com/nodejs/node-v0.x-archive/pull/7178
440// Ensure reflexivity of deepEqual with `arguments` objects.
441{
442  const args = (function() { return arguments; })();
443  assertNotDeepOrStrict([], args);
444}
445
446// More checking that arguments objects are handled correctly
447{
448  // eslint-disable-next-line func-style
449  const returnArguments = function() { return arguments; };
450
451  const someArgs = returnArguments('a');
452  const sameArgs = returnArguments('a');
453  const diffArgs = returnArguments('b');
454
455  assertNotDeepOrStrict(someArgs, ['a']);
456  assertNotDeepOrStrict(someArgs, { '0': 'a' });
457  assertNotDeepOrStrict(someArgs, diffArgs);
458  assertDeepAndStrictEqual(someArgs, sameArgs);
459}
460
461{
462  const values = [
463    123,
464    Infinity,
465    0,
466    null,
467    undefined,
468    false,
469    true,
470    {},
471    [],
472    () => {},
473  ];
474  assertDeepAndStrictEqual(new Set(values), new Set(values));
475  assertDeepAndStrictEqual(new Set(values), new Set(values.reverse()));
476
477  const mapValues = values.map((v) => [v, { a: 5 }]);
478  assertDeepAndStrictEqual(new Map(mapValues), new Map(mapValues));
479  assertDeepAndStrictEqual(new Map(mapValues), new Map(mapValues.reverse()));
480}
481
482{
483  const s1 = new Set();
484  const s2 = new Set();
485  s1.add(1);
486  s1.add(2);
487  s2.add(2);
488  s2.add(1);
489  assertDeepAndStrictEqual(s1, s2);
490}
491
492{
493  const m1 = new Map();
494  const m2 = new Map();
495  const obj = { a: 5, b: 6 };
496  m1.set(1, obj);
497  m1.set(2, 'hi');
498  m1.set(3, [1, 2, 3]);
499
500  m2.set(2, 'hi'); // different order
501  m2.set(1, obj);
502  m2.set(3, [1, 2, 3]); // Deep equal, but not reference equal.
503
504  assertDeepAndStrictEqual(m1, m2);
505}
506
507{
508  const m1 = new Map();
509  const m2 = new Map();
510
511  // m1 contains itself.
512  m1.set(1, m1);
513  m2.set(1, new Map());
514
515  assertNotDeepOrStrict(m1, m2);
516}
517
518{
519  const map1 = new Map([[1, 1]]);
520  const map2 = new Map([[1, '1']]);
521  assert.deepEqual(map1, map2);
522  assert.throws(
523    () => assert.deepStrictEqual(map1, map2),
524    {
525      code: 'ERR_ASSERTION',
526      message: `${defaultMsgStartFull}\n\n` +
527               "  Map(1) {\n+   1 => 1\n-   1 => '1'\n  }"
528    }
529  );
530}
531
532{
533  // Two equivalent sets / maps with different key/values applied shouldn't be
534  // the same. This is a terrible idea to do in practice, but deepEqual should
535  // still check for it.
536  const s1 = new Set();
537  const s2 = new Set();
538  s1.x = 5;
539  assertNotDeepOrStrict(s1, s2);
540
541  const m1 = new Map();
542  const m2 = new Map();
543  m1.x = 5;
544  assertNotDeepOrStrict(m1, m2);
545}
546
547{
548  // Circular references.
549  const s1 = new Set();
550  s1.add(s1);
551  const s2 = new Set();
552  s2.add(s2);
553  assertDeepAndStrictEqual(s1, s2);
554
555  const m1 = new Map();
556  m1.set(2, m1);
557  const m2 = new Map();
558  m2.set(2, m2);
559  assertDeepAndStrictEqual(m1, m2);
560
561  const m3 = new Map();
562  m3.set(m3, 2);
563  const m4 = new Map();
564  m4.set(m4, 2);
565  assertDeepAndStrictEqual(m3, m4);
566}
567
568// Handle sparse arrays.
569{
570  assertDeepAndStrictEqual([1, , , 3], [1, , , 3]);
571  assertNotDeepOrStrict([1, , , 3], [1, , , 3, , , ]);
572  const a = new Array(3);
573  const b = new Array(3);
574  a[2] = true;
575  b[1] = true;
576  assertNotDeepOrStrict(a, b);
577  b[2] = true;
578  assertNotDeepOrStrict(a, b);
579  a[0] = true;
580  assertNotDeepOrStrict(a, b);
581}
582
583// Handle different error messages
584{
585  const err1 = new Error('foo1');
586  assertNotDeepOrStrict(err1, new Error('foo2'), assert.AssertionError);
587  assertNotDeepOrStrict(err1, new TypeError('foo1'), assert.AssertionError);
588  assertDeepAndStrictEqual(err1, new Error('foo1'));
589  assertNotDeepOrStrict(err1, {}, AssertionError);
590}
591
592// Handle NaN
593assertDeepAndStrictEqual(NaN, NaN);
594assertDeepAndStrictEqual({ a: NaN }, { a: NaN });
595assertDeepAndStrictEqual([ 1, 2, NaN, 4 ], [ 1, 2, NaN, 4 ]);
596
597// Handle boxed primitives
598{
599  const boxedString = new String('test');
600  const boxedSymbol = Object(Symbol());
601
602  const fakeBoxedSymbol = {};
603  Object.setPrototypeOf(fakeBoxedSymbol, Symbol.prototype);
604  Object.defineProperty(
605    fakeBoxedSymbol,
606    Symbol.toStringTag,
607    { enumerable: false, value: 'Symbol' }
608  );
609
610  assertNotDeepOrStrict(new Boolean(true), Object(false));
611  assertNotDeepOrStrict(Object(true), new Number(1));
612  assertNotDeepOrStrict(new Number(2), new Number(1));
613  assertNotDeepOrStrict(boxedSymbol, Object(Symbol()));
614  assertNotDeepOrStrict(boxedSymbol, {});
615  assertNotDeepOrStrict(boxedSymbol, fakeBoxedSymbol);
616  assertDeepAndStrictEqual(boxedSymbol, boxedSymbol);
617  assertDeepAndStrictEqual(Object(true), Object(true));
618  assertDeepAndStrictEqual(Object(2), Object(2));
619  assertDeepAndStrictEqual(boxedString, Object('test'));
620  boxedString.slow = true;
621  assertNotDeepOrStrict(boxedString, Object('test'));
622  boxedSymbol.slow = true;
623  assertNotDeepOrStrict(boxedSymbol, {});
624  assertNotDeepOrStrict(boxedSymbol, fakeBoxedSymbol);
625}
626
627// Minus zero
628assertOnlyDeepEqual(0, -0);
629assertDeepAndStrictEqual(-0, -0);
630
631// Handle symbols (enumerable only)
632{
633  const symbol1 = Symbol();
634  const obj1 = { [symbol1]: 1 };
635  const obj2 = { [symbol1]: 1 };
636  const obj3 = { [Symbol()]: 1 };
637  // Add a non enumerable symbol as well. It is going to be ignored!
638  Object.defineProperty(obj2, Symbol(), { value: 1 });
639  assertOnlyDeepEqual(obj1, obj3);
640  assertDeepAndStrictEqual(obj1, obj2);
641  obj2[Symbol()] = true;
642  assertOnlyDeepEqual(obj1, obj2);
643  // TypedArrays have a fast path. Test for this as well.
644  const a = new Uint8Array(4);
645  const b = new Uint8Array(4);
646  a[symbol1] = true;
647  b[symbol1] = false;
648  assertOnlyDeepEqual(a, b);
649  b[symbol1] = true;
650  assertDeepAndStrictEqual(a, b);
651  // The same as TypedArrays is valid for boxed primitives
652  const boxedStringA = new String('test');
653  const boxedStringB = new String('test');
654  boxedStringA[symbol1] = true;
655  assertOnlyDeepEqual(boxedStringA, boxedStringB);
656  boxedStringA[symbol1] = true;
657  assertDeepAndStrictEqual(a, b);
658  // Loose equal arrays should not compare symbols.
659  const arr = [1];
660  const arr2 = [1];
661  arr[symbol1] = true;
662  assertOnlyDeepEqual(arr, arr2);
663  arr2[symbol1] = false;
664  assertOnlyDeepEqual(arr, arr2);
665}
666
667assert.throws(
668  () => assert.notDeepEqual(1, true),
669  {
670    message: /1\n\nshould not loosely deep-equal\n\ntrue/
671  }
672);
673
674assert.throws(
675  () => assert.notDeepEqual(1, 1),
676  {
677    message: /Expected "actual" not to be loosely deep-equal to:\n\n1/
678  }
679);
680
681assertDeepAndStrictEqual(new Date(2000, 3, 14), new Date(2000, 3, 14));
682
683assert.throws(() => { assert.deepEqual(new Date(), new Date(2000, 3, 14)); },
684              AssertionError,
685              'deepEqual(new Date(), new Date(2000, 3, 14))');
686
687assert.throws(
688  () => { assert.notDeepEqual(new Date(2000, 3, 14), new Date(2000, 3, 14)); },
689  AssertionError,
690  'notDeepEqual(new Date(2000, 3, 14), new Date(2000, 3, 14))'
691);
692
693assert.throws(
694  () => { assert.notDeepEqual('a'.repeat(1024), 'a'.repeat(1024)); },
695  AssertionError,
696  'notDeepEqual("a".repeat(1024), "a".repeat(1024))'
697);
698
699assertNotDeepOrStrict(new Date(), new Date(2000, 3, 14));
700
701assertDeepAndStrictEqual(/a/, /a/);
702assertDeepAndStrictEqual(/a/g, /a/g);
703assertDeepAndStrictEqual(/a/i, /a/i);
704assertDeepAndStrictEqual(/a/m, /a/m);
705assertDeepAndStrictEqual(/a/igm, /a/igm);
706assertNotDeepOrStrict(/ab/, /a/);
707assertNotDeepOrStrict(/a/g, /a/);
708assertNotDeepOrStrict(/a/i, /a/);
709assertNotDeepOrStrict(/a/m, /a/);
710assertNotDeepOrStrict(/a/igm, /a/im);
711
712{
713  const re1 = /a/g;
714  re1.lastIndex = 3;
715  assert.deepEqual(re1, /a/g);
716}
717
718assert.deepEqual(4, '4');
719assert.deepEqual(true, 1);
720assert.throws(() => assert.deepEqual(4, '5'),
721              AssertionError,
722              'deepEqual( 4, \'5\')');
723
724// Having the same number of owned properties && the same set of keys.
725assert.deepEqual({ a: 4 }, { a: 4 });
726assert.deepEqual({ a: 4, b: '2' }, { a: 4, b: '2' });
727assert.deepEqual([4], ['4']);
728assert.throws(
729  () => assert.deepEqual({ a: 4 }, { a: 4, b: true }), AssertionError);
730assert.notDeepEqual(['a'], { 0: 'a' });
731assert.deepEqual({ a: 4, b: '1' }, { b: '1', a: 4 });
732const a1 = [1, 2, 3];
733const a2 = [1, 2, 3];
734a1.a = 'test';
735a1.b = true;
736a2.b = true;
737a2.a = 'test';
738assert.throws(() => assert.deepEqual(Object.keys(a1), Object.keys(a2)),
739              AssertionError);
740assertDeepAndStrictEqual(a1, a2);
741
742// Having an identical prototype property.
743const nbRoot = {
744  toString() { return `${this.first} ${this.last}`; }
745};
746
747function nameBuilder(first, last) {
748  this.first = first;
749  this.last = last;
750  return this;
751}
752nameBuilder.prototype = nbRoot;
753
754function nameBuilder2(first, last) {
755  this.first = first;
756  this.last = last;
757  return this;
758}
759nameBuilder2.prototype = nbRoot;
760
761const nb1 = new nameBuilder('Ryan', 'Dahl');
762let nb2 = new nameBuilder2('Ryan', 'Dahl');
763
764assert.deepEqual(nb1, nb2);
765
766nameBuilder2.prototype = Object;
767nb2 = new nameBuilder2('Ryan', 'Dahl');
768assert.deepEqual(nb1, nb2);
769
770// Primitives
771assertNotDeepOrStrict(null, {});
772assertNotDeepOrStrict(undefined, {});
773assertNotDeepOrStrict('a', ['a']);
774assertNotDeepOrStrict('a', { 0: 'a' });
775assertNotDeepOrStrict(1, {});
776assertNotDeepOrStrict(true, {});
777assertNotDeepOrStrict(Symbol(), {});
778assertNotDeepOrStrict(Symbol(), Symbol());
779
780assertOnlyDeepEqual(4, '4');
781assertOnlyDeepEqual(true, 1);
782
783{
784  const s = Symbol();
785  assertDeepAndStrictEqual(s, s);
786}
787
788// Primitive wrappers and object.
789assertNotDeepOrStrict(new String('a'), ['a']);
790assertNotDeepOrStrict(new String('a'), { 0: 'a' });
791assertNotDeepOrStrict(new Number(1), {});
792assertNotDeepOrStrict(new Boolean(true), {});
793
794// Same number of keys but different key names.
795assertNotDeepOrStrict({ a: 1 }, { b: 1 });
796
797assert.deepStrictEqual(new Date(2000, 3, 14), new Date(2000, 3, 14));
798
799assert.throws(
800  () => assert.deepStrictEqual(new Date(), new Date(2000, 3, 14)),
801  AssertionError,
802  'deepStrictEqual(new Date(), new Date(2000, 3, 14))'
803);
804
805assert.throws(
806  () => assert.notDeepStrictEqual(new Date(2000, 3, 14), new Date(2000, 3, 14)),
807  {
808    name: 'AssertionError',
809    message: 'Expected "actual" not to be strictly deep-equal to:\n\n' +
810             util.inspect(new Date(2000, 3, 14))
811  }
812);
813
814assert.notDeepStrictEqual(new Date(), new Date(2000, 3, 14));
815
816assert.throws(
817  () => assert.deepStrictEqual(/ab/, /a/),
818  {
819    code: 'ERR_ASSERTION',
820    name: 'AssertionError',
821    message: `${defaultMsgStartFull}\n\n+ /ab/\n- /a/`
822  });
823assert.throws(
824  () => assert.deepStrictEqual(/a/g, /a/),
825  {
826    code: 'ERR_ASSERTION',
827    name: 'AssertionError',
828    message: `${defaultMsgStartFull}\n\n+ /a/g\n- /a/`
829  });
830assert.throws(
831  () => assert.deepStrictEqual(/a/i, /a/),
832  {
833    code: 'ERR_ASSERTION',
834    name: 'AssertionError',
835    message: `${defaultMsgStartFull}\n\n+ /a/i\n- /a/`
836  });
837assert.throws(
838  () => assert.deepStrictEqual(/a/m, /a/),
839  {
840    code: 'ERR_ASSERTION',
841    name: 'AssertionError',
842    message: `${defaultMsgStartFull}\n\n+ /a/m\n- /a/`
843  });
844assert.throws(
845  () => assert.deepStrictEqual(/aa/igm, /aa/im),
846  {
847    code: 'ERR_ASSERTION',
848    name: 'AssertionError',
849    message: `${defaultMsgStartFull}\n\n+ /aa/gim\n- /aa/im\n      ^`
850  });
851
852{
853  const re1 = /a/;
854  re1.lastIndex = 3;
855  assert.deepStrictEqual(re1, /a/);
856}
857
858assert.throws(
859  () => assert.deepStrictEqual(4, '4'),
860  { message: `${defaultMsgStart}\n4 !== '4'\n` }
861);
862
863assert.throws(
864  () => assert.deepStrictEqual(true, 1),
865  { message: `${defaultMsgStart}\ntrue !== 1\n` }
866);
867
868// Having the same number of owned properties && the same set of keys.
869assert.deepStrictEqual({ a: 4 }, { a: 4 });
870assert.deepStrictEqual({ a: 4, b: '2' }, { a: 4, b: '2' });
871assert.throws(() => assert.deepStrictEqual([4], ['4']),
872              {
873                code: 'ERR_ASSERTION',
874                name: 'AssertionError',
875                message: `${defaultMsgStartFull}\n\n  [\n+   4\n-   '4'\n  ]`
876              });
877assert.throws(
878  () => assert.deepStrictEqual({ a: 4 }, { a: 4, b: true }),
879  {
880    code: 'ERR_ASSERTION',
881    name: 'AssertionError',
882    message: `${defaultMsgStartFull}\n\n  ` +
883             '{\n    a: 4,\n-   b: true\n  }'
884  });
885assert.throws(
886  () => assert.deepStrictEqual(['a'], { 0: 'a' }),
887  {
888    code: 'ERR_ASSERTION',
889    name: 'AssertionError',
890    message: `${defaultMsgStartFull}\n\n` +
891             "+ [\n+   'a'\n+ ]\n- {\n-   '0': 'a'\n- }"
892  });
893
894/* eslint-enable */
895
896assertDeepAndStrictEqual({ a: 4, b: '1' }, { b: '1', a: 4 });
897
898assert.throws(
899  () => assert.deepStrictEqual([0, 1, 2, 'a', 'b'], [0, 1, 2, 'b', 'a']),
900  AssertionError);
901
902// Prototype check.
903function Constructor1(first, last) {
904  this.first = first;
905  this.last = last;
906}
907
908function Constructor2(first, last) {
909  this.first = first;
910  this.last = last;
911}
912
913const obj1 = new Constructor1('Ryan', 'Dahl');
914let obj2 = new Constructor2('Ryan', 'Dahl');
915
916assert.throws(() => assert.deepStrictEqual(obj1, obj2), AssertionError);
917
918Constructor2.prototype = Constructor1.prototype;
919obj2 = new Constructor2('Ryan', 'Dahl');
920
921assertDeepAndStrictEqual(obj1, obj2);
922
923// Check extra properties on errors.
924{
925  const a = new TypeError('foo');
926  const b = new TypeError('foo');
927  a.foo = 'bar';
928  b.foo = 'baz.';
929
930  assert.throws(
931    () => assert.throws(
932      () => assert.deepStrictEqual(a, b),
933      {
934        operator: 'throws',
935        message: `${defaultMsgStartFull}\n\n` +
936                '  [TypeError: foo] {\n+   foo: \'bar\'\n-   foo: \'baz\'\n  }',
937      }
938    ),
939    {
940      message: 'Expected values to be strictly deep-equal:\n' +
941        '+ actual - expected ... Lines skipped\n' +
942        '\n' +
943        '  Comparison {\n' +
944        "    message: 'Expected values to be strictly deep-equal:\\n' +\n" +
945        '...\n' +
946        "      '  [TypeError: foo] {\\n' +\n" +
947        "      \"+   foo: 'bar'\\n\" +\n" +
948        "+     \"-   foo: 'baz.'\\n\" +\n" +
949        "-     \"-   foo: 'baz'\\n\" +\n" +
950        "      '  }',\n" +
951        "+   operator: 'deepStrictEqual'\n" +
952        "-   operator: 'throws'\n" +
953        '  }'
954    }
955  );
956}
957
958// Check proxies.
959{
960  const arrProxy = new Proxy([1, 2], {});
961  assert.deepStrictEqual(arrProxy, [1, 2]);
962  const tmp = util.inspect.defaultOptions;
963  util.inspect.defaultOptions = { showProxy: true };
964  assert.throws(
965    () => assert.deepStrictEqual(arrProxy, [1, 2, 3]),
966    { message: `${defaultMsgStartFull}\n\n` +
967               '  [\n    1,\n    2,\n-   3\n  ]' }
968  );
969  util.inspect.defaultOptions = tmp;
970
971  const invalidTrap = new Proxy([1, 2, 3], {
972    ownKeys() { return []; }
973  });
974  assert.throws(
975    () => assert.deepStrictEqual(invalidTrap, [1, 2, 3]),
976    {
977      name: 'TypeError',
978      message: "'ownKeys' on proxy: trap result did not include 'length'"
979    }
980  );
981}
982
983// Strict equal with identical objects that are not identical
984// by reference and longer than 50 elements
985// E.g., assert.deepStrictEqual({ a: Symbol() }, { a: Symbol() })
986{
987  const a = {};
988  const b = {};
989  for (let i = 0; i < 55; i++) {
990    a[`symbol${i}`] = Symbol();
991    b[`symbol${i}`] = Symbol();
992  }
993
994  assert.throws(
995    () => assert.deepStrictEqual(a, b),
996    {
997      code: 'ERR_ASSERTION',
998      name: 'AssertionError',
999      message: /\.\.\./g
1000    }
1001  );
1002}
1003
1004// Basic valueOf check.
1005{
1006  const a = new String(1);
1007  a.valueOf = undefined;
1008  assertNotDeepOrStrict(a, new String(1));
1009}
1010
1011// Basic array out of bounds check.
1012{
1013  const arr = [1, 2, 3];
1014  arr[2 ** 32] = true;
1015  assertNotDeepOrStrict(arr, [1, 2, 3]);
1016}
1017
1018assert.throws(
1019  () => assert.deepStrictEqual([1, 2, 3], [1, 2]),
1020  {
1021    code: 'ERR_ASSERTION',
1022    name: 'AssertionError',
1023    message: `${defaultMsgStartFull}\n\n` +
1024            '  [\n' +
1025            '    1,\n' +
1026            '    2,\n' +
1027            '+   3\n' +
1028            '  ]'
1029  }
1030);
1031
1032// Verify that manipulating the `getTime()` function has no impact on the time
1033// verification.
1034{
1035  const a = new Date('2000');
1036  const b = new Date('2000');
1037  Object.defineProperty(a, 'getTime', {
1038    value: () => 5
1039  });
1040  assertDeepAndStrictEqual(a, b);
1041}
1042
1043// Verify that an array and the equivalent fake array object
1044// are correctly compared
1045{
1046  const a = [1, 2, 3];
1047  const o = {
1048    __proto__: Array.prototype,
1049    0: 1,
1050    1: 2,
1051    2: 3,
1052    length: 3,
1053  };
1054  Object.defineProperty(o, 'length', { enumerable: false });
1055  assertNotDeepOrStrict(o, a);
1056}
1057
1058// Verify that extra keys will be tested for when using fake arrays.
1059{
1060  const a = {
1061    0: 1,
1062    1: 1,
1063    2: 'broken'
1064  };
1065  Object.setPrototypeOf(a, Object.getPrototypeOf([]));
1066  Object.defineProperty(a, Symbol.toStringTag, {
1067    value: 'Array',
1068  });
1069  Object.defineProperty(a, 'length', {
1070    value: 2
1071  });
1072  assertNotDeepOrStrict(a, [1, 1]);
1073}
1074
1075// Verify that changed tags will still check for the error message.
1076{
1077  const err = new Error('foo');
1078  err[Symbol.toStringTag] = 'Foobar';
1079  const err2 = new Error('bar');
1080  err2[Symbol.toStringTag] = 'Foobar';
1081  assertNotDeepOrStrict(err, err2, AssertionError);
1082}
1083
1084// Check for non-native errors.
1085{
1086  const source = new Error('abc');
1087  const err = Object.create(
1088    Object.getPrototypeOf(source), Object.getOwnPropertyDescriptors(source));
1089  Object.defineProperty(err, 'message', { value: 'foo' });
1090  const err2 = Object.create(
1091    Object.getPrototypeOf(source), Object.getOwnPropertyDescriptors(source));
1092  Object.defineProperty(err2, 'message', { value: 'bar' });
1093  err[Symbol.toStringTag] = 'Foo';
1094  err2[Symbol.toStringTag] = 'Foo';
1095  assert.notDeepStrictEqual(err, err2);
1096}
1097
1098// Verify that `valueOf` is not called for boxed primitives.
1099{
1100  const a = new Number(5);
1101  const b = new Number(5);
1102  Object.defineProperty(a, 'valueOf', {
1103    value: () => { throw new Error('failed'); }
1104  });
1105  Object.defineProperty(b, 'valueOf', {
1106    value: () => { throw new Error('failed'); }
1107  });
1108  assertDeepAndStrictEqual(a, b);
1109}
1110
1111// Check getters.
1112{
1113  const a = {
1114    get a() { return 5; }
1115  };
1116  const b = {
1117    get a() { return 6; }
1118  };
1119  assert.throws(
1120    () => assert.deepStrictEqual(a, b),
1121    {
1122      code: 'ERR_ASSERTION',
1123      name: 'AssertionError',
1124      message: /a: \[Getter: 5]\n-   a: \[Getter: 6]\n  /
1125    }
1126  );
1127
1128  // The descriptor is not compared.
1129  assertDeepAndStrictEqual(a, { a: 5 });
1130}
1131
1132// Verify object types being identical on both sides.
1133{
1134  let a = Buffer.from('test');
1135  let b = Object.create(
1136    Object.getPrototypeOf(a),
1137    Object.getOwnPropertyDescriptors(a)
1138  );
1139  Object.defineProperty(b, Symbol.toStringTag, {
1140    value: 'Uint8Array'
1141  });
1142  assertNotDeepOrStrict(a, b);
1143
1144  a = new Uint8Array(10);
1145  b = new Int8Array(10);
1146  Object.defineProperty(b, Symbol.toStringTag, {
1147    value: 'Uint8Array'
1148  });
1149  Object.setPrototypeOf(b, Uint8Array.prototype);
1150  assertNotDeepOrStrict(a, b);
1151
1152  a = [1, 2, 3];
1153  b = { 0: 1, 1: 2, 2: 3 };
1154  Object.setPrototypeOf(b, Array.prototype);
1155  Object.defineProperty(b, 'length', { value: 3, enumerable: false });
1156  Object.defineProperty(b, Symbol.toStringTag, {
1157    value: 'Array'
1158  });
1159  assertNotDeepOrStrict(a, b);
1160
1161  a = new Date(2000);
1162  b = Object.create(
1163    Object.getPrototypeOf(a),
1164    Object.getOwnPropertyDescriptors(a)
1165  );
1166  Object.defineProperty(b, Symbol.toStringTag, {
1167    value: 'Date'
1168  });
1169  assertNotDeepOrStrict(a, b);
1170
1171  a = /abc/g;
1172  b = Object.create(
1173    Object.getPrototypeOf(a),
1174    Object.getOwnPropertyDescriptors(a)
1175  );
1176  Object.defineProperty(b, Symbol.toStringTag, {
1177    value: 'RegExp'
1178  });
1179  assertNotDeepOrStrict(a, b);
1180
1181  a = [];
1182  b = /abc/;
1183  Object.setPrototypeOf(b, Array.prototype);
1184  Object.defineProperty(b, Symbol.toStringTag, {
1185    value: 'Array'
1186  });
1187  assertNotDeepOrStrict(a, b);
1188
1189  a = Object.create(null);
1190  b = new RangeError('abc');
1191  Object.defineProperty(a, Symbol.toStringTag, {
1192    value: 'Error'
1193  });
1194  Object.setPrototypeOf(b, null);
1195  assertNotDeepOrStrict(a, b, assert.AssertionError);
1196}
1197
1198{
1199  // Verify commutativity
1200  // Regression test for https://github.com/nodejs/node/issues/37710
1201  const a = { x: 1 };
1202  const b = { y: 1 };
1203  Object.defineProperty(b, 'x', { value: 1 });
1204
1205  assertNotDeepOrStrict(a, b);
1206}
1207