summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_ObjectUtils.js
blob: ce40a87007c8a018cad5b00cd443e0ae1e280fed (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const { ObjectUtils } = ChromeUtils.import(
  "resource://gre/modules/ObjectUtils.jsm"
);

add_task(async function test_deepEqual() {
  let deepEqual = ObjectUtils.deepEqual.bind(ObjectUtils);
  // CommonJS 7.2
  Assert.ok(
    deepEqual(new Date(2000, 3, 14), new Date(2000, 3, 14)),
    "deepEqual date"
  );
  Assert.ok(deepEqual(new Date(NaN), new Date(NaN)), "deepEqual invalid dates");

  Assert.ok(!deepEqual(new Date(), new Date(2000, 3, 14)), "deepEqual date");

  let now = Date.now();
  Assert.ok(
    !deepEqual(new Date(now), now),
    "Dates and times should not be equal"
  );
  Assert.ok(
    !deepEqual(now, new Date(now)),
    "Times and dates should not be equal"
  );

  Assert.ok(!deepEqual("", {}), "Objects and primitives should not be equal");
  Assert.ok(!deepEqual(/a/, "a"), "RegExps and strings should not be equal");
  Assert.ok(!deepEqual("a", /a/), "Strings and RegExps should not be equal");

  // 7.3
  Assert.ok(deepEqual(/a/, /a/));
  Assert.ok(deepEqual(/a/g, /a/g));
  Assert.ok(deepEqual(/a/i, /a/i));
  Assert.ok(deepEqual(/a/m, /a/m));
  Assert.ok(deepEqual(/a/gim, /a/gim));
  Assert.ok(!deepEqual(/ab/, /a/));
  Assert.ok(!deepEqual(/a/g, /a/));
  Assert.ok(!deepEqual(/a/i, /a/));
  Assert.ok(!deepEqual(/a/m, /a/));
  Assert.ok(!deepEqual(/a/gim, /a/im));

  let re1 = /a/;
  re1.lastIndex = 3;
  Assert.ok(!deepEqual(re1, /a/));

  // 7.4
  Assert.ok(deepEqual(4, "4"), "deepEqual == check");
  Assert.ok(deepEqual(true, 1), "deepEqual == check");
  Assert.ok(!deepEqual(4, "5"), "deepEqual == check");

  // 7.5
  // having the same number of owned properties && the same set of keys
  Assert.ok(deepEqual({ a: 4 }, { a: 4 }));
  Assert.ok(deepEqual({ a: 4, b: "2" }, { a: 4, b: "2" }));
  Assert.ok(deepEqual([4], ["4"]));
  Assert.ok(!deepEqual({ a: 4 }, { a: 4, b: true }));
  Assert.ok(deepEqual(["a"], { 0: "a" }));

  let a1 = [1, 2, 3];
  let a2 = [1, 2, 3];
  a1.a = "test";
  a1.b = true;
  a2.b = true;
  a2.a = "test";
  Assert.ok(!deepEqual(Object.keys(a1), Object.keys(a2)));
  Assert.ok(deepEqual(a1, a2));

  let nbRoot = {
    toString() {
      return this.first + " " + this.last;
    },
  };

  function nameBuilder(first, last) {
    this.first = first;
    this.last = last;
    return this;
  }
  nameBuilder.prototype = nbRoot;

  function nameBuilder2(first, last) {
    this.first = first;
    this.last = last;
    return this;
  }
  nameBuilder2.prototype = nbRoot;

  let nb1 = new nameBuilder("Ryan", "Dahl");
  let nb2 = new nameBuilder2("Ryan", "Dahl");

  Assert.ok(deepEqual(nb1, nb2));

  nameBuilder2.prototype = Object;
  nb2 = new nameBuilder2("Ryan", "Dahl");
  Assert.ok(!deepEqual(nb1, nb2));

  // String literal + object
  Assert.ok(!deepEqual("a", {}));

  // Make sure deepEqual doesn't loop forever on circular refs

  let b = {};
  b.b = b;

  let c = {};
  c.b = c;

  try {
    Assert.ok(!deepEqual(b, c));
  } catch (e) {
    Assert.ok(true, "Didn't recurse infinitely.");
  }

  let e = { a: 3, b: 4 };
  let f = { b: 4, a: 3 };

  function checkEquiv() {
    return arguments;
  }

  Assert.ok(deepEqual(checkEquiv(e, f), checkEquiv(f, e)));
});

add_task(async function test_isEmpty() {
  Assert.ok(ObjectUtils.isEmpty(""), "Empty strings should be empty");
  Assert.ok(ObjectUtils.isEmpty(0), "0 should be empty");
  Assert.ok(ObjectUtils.isEmpty(NaN), "NaN should be empty");
  Assert.ok(ObjectUtils.isEmpty(), "Undefined should be empty");
  Assert.ok(ObjectUtils.isEmpty(null), "Null should be empty");
  Assert.ok(ObjectUtils.isEmpty(false), "False should be empty");

  Assert.ok(
    ObjectUtils.isEmpty({}),
    "Objects without properties should be empty"
  );
  Assert.ok(
    ObjectUtils.isEmpty(
      Object.defineProperty({}, "a", {
        value: 1,
        enumerable: false,
        configurable: true,
        writable: true,
      })
    ),
    "Objects without enumerable properties should be empty"
  );
  Assert.ok(ObjectUtils.isEmpty([]), "Arrays without elements should be empty");

  Assert.ok(
    !ObjectUtils.isEmpty([1]),
    "Arrays with elements should not be empty"
  );
  Assert.ok(
    !ObjectUtils.isEmpty({ a: 1 }),
    "Objects with properties should not be empty"
  );

  function A() {}
  A.prototype.b = 2;
  Assert.ok(
    !ObjectUtils.isEmpty(new A()),
    "Objects with inherited enumerable properties should not be empty"
  );
});