| 1 |
// Tests for the assertEqual function in jit-test/lib/asserts.js |
| 2 |
|
| 3 |
load(libdir + "asserts.js"); |
| 4 |
|
| 5 |
function assertNotDeepEq(a, b, options) { |
| 6 |
assertThrowsInstanceOf(() => assertDeepEq(a, b, options), Error); |
| 7 |
} |
| 8 |
|
| 9 |
// primitives |
| 10 |
assertDeepEq(undefined, undefined); |
| 11 |
assertDeepEq("1", "1"); |
| 12 |
assertNotDeepEq(1, "1"); |
| 13 |
assertNotDeepEq(undefined, null); |
| 14 |
assertNotDeepEq({}, null); |
| 15 |
|
| 16 |
// symbols |
| 17 |
assertDeepEq(Symbol(), Symbol()); |
| 18 |
assertNotDeepEq(Symbol(), Symbol("")); |
| 19 |
assertDeepEq(Symbol("tweedledum"), Symbol("tweedledum")); |
| 20 |
assertNotDeepEq(Symbol("tweedledum"), Symbol("alice")); |
| 21 |
assertNotDeepEq(Symbol("what-its-called"), Symbol.for("what-its-called")); |
| 22 |
assertNotDeepEq(Symbol.iterator, Symbol.for("Symbol.iterator")); |
| 23 |
assertDeepEq([Symbol(), Symbol(), Symbol()], |
| 24 |
[Symbol(), Symbol(), Symbol()]); |
| 25 |
var sym = Symbol(); |
| 26 |
assertDeepEq([sym, sym], [sym, sym]); |
| 27 |
assertNotDeepEq([sym, sym], [Symbol(), Symbol()]); |
| 28 |
assertNotDeepEq([sym, sym], [Symbol(), sym]); |
| 29 |
var obj1 = {}, obj2 = {}; |
| 30 |
obj1[Symbol("x")] = "y"; |
| 31 |
obj2[Symbol("x")] = "y"; |
| 32 |
assertDeepEq(obj1, obj2); |
| 33 |
|
| 34 |
// objects |
| 35 |
assertDeepEq({}, {}); |
| 36 |
assertDeepEq({one: 1, two: 2}, {one: 1, two: 2}); |
| 37 |
assertNotDeepEq(Object.freeze({}), {}); |
| 38 |
assertDeepEq(Object.create(null), Object.create(null)); |
| 39 |
assertNotDeepEq(Object.create(null, {a: {configurable: false, value: 3}}), |
| 40 |
Object.create(null, {a: {configurable: true, value: 3}})); |
| 41 |
assertNotDeepEq({one: 1}, {one: 1, two: 2}); |
| 42 |
assertNotDeepEq({yes: true}, {oui: true}); |
| 43 |
assertNotDeepEq({zero: 0}, {zero: "0"}); |
| 44 |
|
| 45 |
// test the comment |
| 46 |
var x = {}, y = {}, ax = [x]; |
| 47 |
assertDeepEq([ax, x], [ax, y]); // passes (bogusly) |
| 48 |
assertNotDeepEq([ax, x], [ax, y], {strictEquivalence: true}); |
| 49 |
assertDeepEq([x, ax], [y, ax]); // passes (bogusly) |
| 50 |
assertNotDeepEq([x, ax], [y, ax], {strictEquivalence: true}); |
| 51 |
|
| 52 |
// object identity |
| 53 |
assertNotDeepEq([x, y], [x, x]); |
| 54 |
assertDeepEq([x, y], [x, y]); |
| 55 |
assertDeepEq([y, x], [x, y]); |
| 56 |
|
| 57 |
// proto chain |
| 58 |
var x = {}; |
| 59 |
assertDeepEq(Object.create(x), Object.create(x)); |
| 60 |
assertDeepEq(Object.create({}), Object.create({})); // equivalent but not identical proto objects |
| 61 |
|
| 62 |
// arrays |
| 63 |
assertDeepEq([], []); |
| 64 |
assertNotDeepEq([], [1]); |
| 65 |
assertDeepEq([1], [1]); |
| 66 |
assertNotDeepEq([0], [1]); |
| 67 |
assertDeepEq([1, 2, 3], [1, 2, 3]); |
| 68 |
assertNotDeepEq([1, , 3], [1, undefined, 3]); |
| 69 |
var p = [], q = []; |
| 70 |
p.prop = 1; |
| 71 |
assertNotDeepEq(p, q); |
| 72 |
assertNotDeepEq(q, p); |
| 73 |
q.prop = 1; |
| 74 |
assertDeepEq(q, p); |
| 75 |
|
| 76 |
// functions |
| 77 |
assertNotDeepEq(() => 1, () => 2); |
| 78 |
assertNotDeepEq((...x) => 1, x => 1); |
| 79 |
assertNotDeepEq(function f(){}, function g(){}); |
| 80 |
// Avoid setting name property. |
| 81 |
var [f1, f2] = [function () {}, function () {}]; |
| 82 |
assertDeepEq(f1, f1); |
| 83 |
assertDeepEq(f1, f2); // same text, close enough |
| 84 |
f1.prop = 1; |
| 85 |
assertNotDeepEq(f1, f2); |
| 86 |
f2.prop = 1; |
| 87 |
assertDeepEq(f1, f2); |
| 88 |
|
| 89 |
// recursion |
| 90 |
var a = [], b = []; |
| 91 |
a[0] = a; |
| 92 |
b[0] = b; |
| 93 |
assertDeepEq(a, b); |
| 94 |
a[0] = b; |
| 95 |
assertNotDeepEq(a, b); // [#1=[#1#]] is not structurally equivalent to #1=[[#1#]] |
| 96 |
b[0] = a; |
| 97 |
assertDeepEq(a, b); |
| 98 |
b[0] = [a]; // a[0] === b, b[0] === c, c[0] === a |
| 99 |
assertDeepEq(a, b); |
| 100 |
|
| 101 |
// objects that merge |
| 102 |
var x = {}; |
| 103 |
assertDeepEq({x: x}, {x: x}); |
| 104 |
var y = [x]; |
| 105 |
assertDeepEq([y], [y]); |
| 106 |
|
| 107 |
// cross-compartment |
| 108 |
var g1 = newGlobal({newCompartment: true}), g2 = newGlobal({newCompartment: true}); |
| 109 |
assertDeepEq(g1, g2); |
| 110 |
assertDeepEq(g1, g2, {strictEquivalence: true}); |
| 111 |
Object.preventExtensions(g2.Math.abs); // make some miniscule change |
| 112 |
assertNotDeepEq(g1, g2); |