gecko-dev/toolkit/modules/tests/xpcshell/test_ObjectUtils.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

138 lines
4.1 KiB
JavaScript

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/igm, /a/igm));
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/igm, /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");
});