addon-sdk/source/test/test-array.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 'use strict'
michael@0 5
michael@0 6 const array = require('sdk/util/array');
michael@0 7
michael@0 8 exports.testHas = function(assert) {
michael@0 9 var testAry = [1, 2, 3];
michael@0 10 assert.equal(array.has([1, 2, 3], 1), true);
michael@0 11 assert.equal(testAry.length, 3);
michael@0 12 assert.equal(testAry[0], 1);
michael@0 13 assert.equal(testAry[1], 2);
michael@0 14 assert.equal(testAry[2], 3);
michael@0 15 assert.equal(array.has(testAry, 2), true);
michael@0 16 assert.equal(array.has(testAry, 3), true);
michael@0 17 assert.equal(array.has(testAry, 4), false);
michael@0 18 assert.equal(array.has(testAry, '1'), false);
michael@0 19 };
michael@0 20 exports.testHasAny = function(assert) {
michael@0 21 var testAry = [1, 2, 3];
michael@0 22 assert.equal(array.hasAny([1, 2, 3], [1]), true);
michael@0 23 assert.equal(array.hasAny([1, 2, 3], [1, 5]), true);
michael@0 24 assert.equal(array.hasAny([1, 2, 3], [5, 1]), true);
michael@0 25 assert.equal(array.hasAny([1, 2, 3], [5, 2]), true);
michael@0 26 assert.equal(array.hasAny([1, 2, 3], [5, 3]), true);
michael@0 27 assert.equal(array.hasAny([1, 2, 3], [5, 4]), false);
michael@0 28 assert.equal(testAry.length, 3);
michael@0 29 assert.equal(testAry[0], 1);
michael@0 30 assert.equal(testAry[1], 2);
michael@0 31 assert.equal(testAry[2], 3);
michael@0 32 assert.equal(array.hasAny(testAry, [2]), true);
michael@0 33 assert.equal(array.hasAny(testAry, [3]), true);
michael@0 34 assert.equal(array.hasAny(testAry, [4]), false);
michael@0 35 assert.equal(array.hasAny(testAry), false);
michael@0 36 assert.equal(array.hasAny(testAry, '1'), false);
michael@0 37 };
michael@0 38
michael@0 39 exports.testAdd = function(assert) {
michael@0 40 var testAry = [1];
michael@0 41 assert.equal(array.add(testAry, 1), false);
michael@0 42 assert.equal(testAry.length, 1);
michael@0 43 assert.equal(testAry[0], 1);
michael@0 44 assert.equal(array.add(testAry, 2), true);
michael@0 45 assert.equal(testAry.length, 2);
michael@0 46 assert.equal(testAry[0], 1);
michael@0 47 assert.equal(testAry[1], 2);
michael@0 48 };
michael@0 49
michael@0 50 exports.testRemove = function(assert) {
michael@0 51 var testAry = [1, 2];
michael@0 52 assert.equal(array.remove(testAry, 3), false);
michael@0 53 assert.equal(testAry.length, 2);
michael@0 54 assert.equal(testAry[0], 1);
michael@0 55 assert.equal(testAry[1], 2);
michael@0 56 assert.equal(array.remove(testAry, 2), true);
michael@0 57 assert.equal(testAry.length, 1);
michael@0 58 assert.equal(testAry[0], 1);
michael@0 59 };
michael@0 60
michael@0 61 exports.testFlatten = function(assert) {
michael@0 62 assert.equal(array.flatten([1, 2, 3]).length, 3);
michael@0 63 assert.equal(array.flatten([1, [2, 3]]).length, 3);
michael@0 64 assert.equal(array.flatten([1, [2, [3]]]).length, 3);
michael@0 65 assert.equal(array.flatten([[1], [[2, [3]]]]).length, 3);
michael@0 66 };
michael@0 67
michael@0 68 exports.testUnique = function(assert) {
michael@0 69 var Class = function () {};
michael@0 70 var A = {};
michael@0 71 var B = new Class();
michael@0 72 var C = [ 1, 2, 3 ];
michael@0 73 var D = {};
michael@0 74 var E = new Class();
michael@0 75
michael@0 76 assert.deepEqual(array.unique([1,2,3,1,2]), [1,2,3]);
michael@0 77 assert.deepEqual(array.unique([1,1,1,4,9,5,5]), [1,4,9,5]);
michael@0 78 assert.deepEqual(array.unique([A, A, A, B, B, D]), [A,B,D]);
michael@0 79 assert.deepEqual(array.unique([A, D, A, E, E, D, A, A, C]), [A, D, E, C])
michael@0 80 };
michael@0 81
michael@0 82 exports.testUnion = function(assert) {
michael@0 83 var Class = function () {};
michael@0 84 var A = {};
michael@0 85 var B = new Class();
michael@0 86 var C = [ 1, 2, 3 ];
michael@0 87 var D = {};
michael@0 88 var E = new Class();
michael@0 89
michael@0 90 assert.deepEqual(array.union([1, 2, 3],[7, 1, 2]), [1, 2, 3, 7]);
michael@0 91 assert.deepEqual(array.union([1, 1, 1, 4, 9, 5, 5], [10, 1, 5]), [1, 4, 9, 5, 10]);
michael@0 92 assert.deepEqual(array.union([A, B], [A, D]), [A, B, D]);
michael@0 93 assert.deepEqual(array.union([A, D], [A, E], [E, D, A], [A, C]), [A, D, E, C]);
michael@0 94 };
michael@0 95
michael@0 96 exports.testFind = function(assert) {
michael@0 97 let isOdd = (x) => x % 2;
michael@0 98 assert.equal(array.find([2, 4, 5, 7, 8, 9], isOdd), 5);
michael@0 99 assert.equal(array.find([2, 4, 6, 8], isOdd), undefined);
michael@0 100 assert.equal(array.find([2, 4, 6, 8], isOdd, null), null);
michael@0 101 };
michael@0 102
michael@0 103 require('test').run(exports);

mercurial