1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/traits/assert.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,98 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +var BaseAssert = require("sdk/test/assert").Assert; 1.11 + 1.12 +/** 1.13 + * Whether or not given property descriptors are equivalent. They are 1.14 + * equivalent either if both are marked as "conflict" or "required" property 1.15 + * or if all the properties of descriptors are equal. 1.16 + * @param {Object} actual 1.17 + * @param {Object} expected 1.18 + */ 1.19 +function equivalentDescriptors(actual, expected) { 1.20 + return (actual.conflict && expected.conflict) || 1.21 + (actual.required && expected.required) || 1.22 + equalDescriptors(actual, expected); 1.23 +} 1.24 + 1.25 +function equalDescriptors(actual, expected) { 1.26 + return actual.get === expected.get && 1.27 + actual.set === expected.set && 1.28 + actual.value === expected.value && 1.29 + !!actual.enumerable === !!expected.enumerable && 1.30 + !!actual.configurable === !!expected.configurable && 1.31 + !!actual.writable === !!expected.writable; 1.32 +} 1.33 + 1.34 +/** 1.35 + * Whether or not given `target` array contains all the element 1.36 + * from a given `source` array. 1.37 + */ 1.38 +function containsSet(source, target) { 1.39 + return source.some(function(element) { 1.40 + return 0 > target.indexOf(element); 1.41 + }); 1.42 +} 1.43 + 1.44 +/** 1.45 + * Whether or not given two arrays contain all elements from another. 1.46 + */ 1.47 +function equivalentSets(source, target) { 1.48 + return containsSet(source, target) && containsSet(target, source); 1.49 +} 1.50 + 1.51 +/** 1.52 + * Finds name of the property from `source` property descriptor map, that 1.53 + * is not equivalent of the name named property in the `target` property 1.54 + * descriptor map. If not found `null` is returned instead. 1.55 + */ 1.56 +function findNonEquivalentPropertyName(source, target) { 1.57 + var value = null; 1.58 + Object.getOwnPropertyNames(source).some(function(key) { 1.59 + var areEquivalent = false; 1.60 + if (!equivalentDescriptors(source[key], target[key])) { 1.61 + value = key; 1.62 + areEquivalent = true; 1.63 + } 1.64 + return areEquivalent; 1.65 + }); 1.66 + return value; 1.67 +} 1.68 + 1.69 +var AssertDescriptor = { 1.70 + equalTraits: { 1.71 + value: function equivalentTraits(actual, expected, message) { 1.72 + var difference; 1.73 + var actualKeys = Object.getOwnPropertyNames(actual); 1.74 + var expectedKeys = Object.getOwnPropertyNames(expected); 1.75 + 1.76 + if (equivalentSets(actualKeys, expectedKeys)) { 1.77 + this.fail({ 1.78 + operator: "equalTraits", 1.79 + message: "Traits define different properties", 1.80 + actual: actualKeys.sort().join(","), 1.81 + expected: expectedKeys.sort().join(","), 1.82 + }); 1.83 + } 1.84 + else if ((difference = findNonEquivalentPropertyName(actual, expected))) { 1.85 + this.fail({ 1.86 + operator: "equalTraits", 1.87 + message: "Traits define non-equivalent property `" + difference + "`", 1.88 + actual: actual[difference], 1.89 + expected: expected[difference] 1.90 + }); 1.91 + } 1.92 + else { 1.93 + this.pass(message || "Traits are equivalent."); 1.94 + } 1.95 + } 1.96 + } 1.97 +}; 1.98 + 1.99 +exports.Assert = function Assert() { 1.100 + return Object.create(BaseAssert.apply(null, arguments), AssertDescriptor); 1.101 +};