From af39796da3d27c92a90499c487348895f08f45d5 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Gerardy Date: Sun, 1 Jan 2017 10:59:40 +0100 Subject: [PATCH 1/4] [ospec:tests] Add assertions for array deepEquals corner cases --- ospec/tests/test-ospec.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ospec/tests/test-ospec.js b/ospec/tests/test-ospec.js index a1433d26..8896c4a0 100644 --- a/ospec/tests/test-ospec.js +++ b/ospec/tests/test-ospec.js @@ -37,6 +37,13 @@ o.spec("ospec", function() { o({a: [1, 2], b: 3}).deepEquals({a: [1, 2], b: 3}) o([{a: 1, b: 2}, {c: 3}]).deepEquals([{a: 1, b: 2}, {c: 3}]) + var sparse1 = [void 1, void 2, void 3] + delete sparse1[0] + var sparse2 = [void 1, void 2, void 3] + delete sparse2[1] + + o(sparse1).notDeepEquals(sparse2) + var monkeypatch1 = [1, 2] monkeypatch1.field = 3 var monkeypatch2 = [1, 2] @@ -45,6 +52,14 @@ o.spec("ospec", function() { o(monkeypatch1).notDeepEquals([1, 2]) o(monkeypatch1).notDeepEquals(monkeypatch2) + monkeypatch2.field = 3 + o(monkeypatch1).deepEquals(monkeypatch2) + + monkeypatch1.undef = undefined + monkeypatch2.UNDEF = undefined + + o(monkeypatch1).notDeepEquals(monkeypatch2) + var values = ["a", "", 1, 0, true, false, null, undefined, Date(0), ["a"], [], function() {return arguments}.call(), new Uint8Array(), {a: 1}, {}] for (var i = 0; i < values.length; i++) { for (var j = 0; j < values.length; j++) { From 02545a8a98eefd6d9867ec7bd0c006c1ee8cbe2b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Gerardy Date: Sun, 1 Jan 2017 15:08:46 +0100 Subject: [PATCH 2/4] [ospec] Fix array deepEquals corner cases --- ospec/ospec.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ospec/ospec.js b/ospec/ospec.js index 24495b87..9db636b4 100644 --- a/ospec/ospec.js +++ b/ospec/ospec.js @@ -145,9 +145,8 @@ module.exports = new function init() { if (a.length === b.length && (a instanceof Array && b instanceof Array || aIsArgs && bIsArgs)) { var aKeys = Object.getOwnPropertyNames(a), bKeys = Object.getOwnPropertyNames(b) if (aKeys.length !== bKeys.length) return false - var larger = aKeys.length < bKeys.length ? bKeys : aKeys - for (var i = 0; i < larger.length; i++) { - if (!deepEqual(a[larger[i]], b[larger[i]])) return false + for (var i = 0; i < aKeys.length; i++) { + if (!b.hasOwnProperty(aKeys[i]) || !deepEqual(a[aKeys[i]], b[aKeys[i]])) return false } return true } From 027a2207799efd2ff41f7b83a82e16101d447507 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Gerardy Date: Sun, 1 Jan 2017 16:09:15 +0100 Subject: [PATCH 3/4] [ospec:tests] Add assertion for object with undefined properties vs none --- ospec/tests/test-ospec.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ospec/tests/test-ospec.js b/ospec/tests/test-ospec.js index 8896c4a0..b3878f7a 100644 --- a/ospec/tests/test-ospec.js +++ b/ospec/tests/test-ospec.js @@ -36,6 +36,13 @@ o.spec("ospec", function() { o(a).notEquals(2) o({a: [1, 2], b: 3}).deepEquals({a: [1, 2], b: 3}) o([{a: 1, b: 2}, {c: 3}]).deepEquals([{a: 1, b: 2}, {c: 3}]) + + var undef1 = {undef: void 0} + var undef2 = {UNDEF: void 0} + + o(undef1).notDeepEquals(undef2) + o(undef1).notDeepEquals({}) + o({}).notDeepEquals(undef1) var sparse1 = [void 1, void 2, void 3] delete sparse1[0] From df1e19b86cd00b3504796cab1c138230e7734e1b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Gerardy Date: Sun, 1 Jan 2017 16:13:11 +0100 Subject: [PATCH 4/4] [ospec] Fix for objects with undefined properties --- ospec/ospec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ospec/ospec.js b/ospec/ospec.js index 9db636b4..56111b66 100644 --- a/ospec/ospec.js +++ b/ospec/ospec.js @@ -135,7 +135,7 @@ module.exports = new function init() { var aIsArgs = isArguments(a), bIsArgs = isArguments(b) if (a.constructor === Object && b.constructor === Object && !aIsArgs && !bIsArgs) { for (var i in a) { - if (!deepEqual(a[i], b[i])) return false + if ((!(i in b)) || !deepEqual(a[i], b[i])) return false } for (var i in b) { if (!(i in a)) return false