1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/sync/tests/unit/test_utils_deepEquals.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,44 @@ 1.4 +_("Make sure Utils.deepEquals correctly finds items that are deeply equal"); 1.5 +Cu.import("resource://services-sync/util.js"); 1.6 + 1.7 +function run_test() { 1.8 + let data = '[NaN, undefined, null, true, false, Infinity, 0, 1, "a", "b", {a: 1}, {a: "a"}, [{a: 1}], [{a: true}], {a: 1, b: 2}, [1, 2], [1, 2, 3]]'; 1.9 + _("Generating two copies of data:", data); 1.10 + let d1 = eval(data); 1.11 + let d2 = eval(data); 1.12 + 1.13 + d1.forEach(function(a) { 1.14 + _("Testing", a, typeof a, JSON.stringify([a])); 1.15 + let numMatch = 0; 1.16 + 1.17 + d2.forEach(function(b) { 1.18 + if (Utils.deepEquals(a, b)) { 1.19 + numMatch++; 1.20 + _("Found a match", b, typeof b, JSON.stringify([b])); 1.21 + } 1.22 + }); 1.23 + 1.24 + let expect = 1; 1.25 + if (isNaN(a) && typeof a == "number") { 1.26 + expect = 0; 1.27 + _("Checking NaN should result in no matches"); 1.28 + } 1.29 + 1.30 + _("Making sure we found the correct # match:", expect); 1.31 + _("Actual matches:", numMatch); 1.32 + do_check_eq(numMatch, expect); 1.33 + }); 1.34 + 1.35 + _("Make sure adding undefined properties doesn't affect equalness"); 1.36 + let a = {}; 1.37 + let b = { a: undefined }; 1.38 + do_check_true(Utils.deepEquals(a, b)); 1.39 + a.b = 5; 1.40 + do_check_false(Utils.deepEquals(a, b)); 1.41 + b.b = 5; 1.42 + do_check_true(Utils.deepEquals(a, b)); 1.43 + a.c = undefined; 1.44 + do_check_true(Utils.deepEquals(a, b)); 1.45 + b.d = undefined; 1.46 + do_check_true(Utils.deepEquals(a, b)); 1.47 +}