summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/self-test/assertDeepEq.js
blob: 3619a3fcc661f9a7528f11b3dc3978327b628468 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Tests for the assertEqual function in jit-test/lib/asserts.js

load(libdir + "asserts.js");

function assertNotDeepEq(a, b, options) {
    assertThrowsInstanceOf(() => assertDeepEq(a, b, options), Error);
}

// primitives
assertDeepEq(undefined, undefined);
assertDeepEq("1", "1");
assertNotDeepEq(1, "1");
assertNotDeepEq(undefined, null);
assertNotDeepEq({}, null);

// symbols
assertDeepEq(Symbol(), Symbol());
assertNotDeepEq(Symbol(), Symbol(""));
assertDeepEq(Symbol("tweedledum"), Symbol("tweedledum"));
assertNotDeepEq(Symbol("tweedledum"), Symbol("alice"));
assertNotDeepEq(Symbol("what-its-called"), Symbol.for("what-its-called"));
assertNotDeepEq(Symbol.iterator, Symbol.for("Symbol.iterator"));
assertDeepEq([Symbol(), Symbol(), Symbol()],
             [Symbol(), Symbol(), Symbol()]);
var sym = Symbol();
assertDeepEq([sym, sym], [sym, sym]);
assertNotDeepEq([sym, sym], [Symbol(), Symbol()]);
assertNotDeepEq([sym, sym], [Symbol(), sym]);
var obj1 = {}, obj2 = {};
obj1[Symbol("x")] = "y";
obj2[Symbol("x")] = "y";
assertDeepEq(obj1, obj2);

// objects
assertDeepEq({}, {});
assertDeepEq({one: 1, two: 2}, {one: 1, two: 2});
assertNotDeepEq(Object.freeze({}), {});
assertDeepEq(Object.create(null), Object.create(null));
assertNotDeepEq(Object.create(null, {a: {configurable: false, value: 3}}),
               Object.create(null, {a: {configurable: true, value: 3}}));
assertNotDeepEq({one: 1}, {one: 1, two: 2});
assertNotDeepEq({yes: true}, {oui: true});
assertNotDeepEq({zero: 0}, {zero: "0"});

// test the comment
var x = {}, y = {}, ax = [x];
assertDeepEq([ax, x], [ax, y]);  // passes (bogusly)
assertNotDeepEq([ax, x], [ax, y], {strictEquivalence: true});
assertDeepEq([x, ax], [y, ax]);  // passes (bogusly)
assertNotDeepEq([x, ax], [y, ax], {strictEquivalence: true});

// object identity
assertNotDeepEq([x, y], [x, x]);
assertDeepEq([x, y], [x, y]);
assertDeepEq([y, x], [x, y]);

// proto chain
var x = {};
assertDeepEq(Object.create(x), Object.create(x));
assertDeepEq(Object.create({}), Object.create({})); // equivalent but not identical proto objects

// arrays
assertDeepEq([], []);
assertNotDeepEq([], [1]);
assertDeepEq([1], [1]);
assertNotDeepEq([0], [1]);
assertDeepEq([1, 2, 3], [1, 2, 3]);
assertNotDeepEq([1, , 3], [1, undefined, 3]);
var p = [], q = [];
p.prop = 1;
assertNotDeepEq(p, q);
assertNotDeepEq(q, p);
q.prop = 1;
assertDeepEq(q, p);

// functions
assertNotDeepEq(() => 1, () => 2);
assertNotDeepEq((...x) => 1, x => 1);
assertNotDeepEq(function f(){}, function g(){});
// Avoid setting name property.
var [f1, f2] = [function () {}, function () {}];
assertDeepEq(f1, f1);
assertDeepEq(f1, f2);  // same text, close enough
f1.prop = 1;
assertNotDeepEq(f1, f2);
f2.prop = 1;
assertDeepEq(f1, f2);

// recursion
var a = [], b = [];
a[0] = a;
b[0] = b;
assertDeepEq(a, b);
a[0] = b;
assertNotDeepEq(a, b);  // [#1=[#1#]] is not structurally equivalent to #1=[[#1#]]
b[0] = a;
assertDeepEq(a, b);
b[0] = [a];  // a[0] === b, b[0] === c, c[0] === a
assertDeepEq(a, b);

// objects that merge
var x = {};
assertDeepEq({x: x}, {x: x});
var y = [x];
assertDeepEq([y], [y]);

// cross-compartment
var g1 = newGlobal({newCompartment: true}), g2 = newGlobal({newCompartment: true});
assertDeepEq(g1, g2);
assertDeepEq(g1, g2, {strictEquivalence: true});
Object.preventExtensions(g2.Math.abs);  // make some miniscule change
assertNotDeepEq(g1, g2);