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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 collection = require("sdk/util/collection");
michael@0 7
michael@0 8 exports.testAddRemove = function (assert) {
michael@0 9 let coll = new collection.Collection();
michael@0 10 compare(assert, coll, []);
michael@0 11 addRemove(assert, coll, [], false);
michael@0 12 };
michael@0 13
michael@0 14 exports.testAddRemoveBackingArray = function (assert) {
michael@0 15 let items = ["foo"];
michael@0 16 let coll = new collection.Collection(items);
michael@0 17 compare(assert, coll, items);
michael@0 18 addRemove(assert, coll, items, true);
michael@0 19
michael@0 20 items = ["foo", "bar"];
michael@0 21 coll = new collection.Collection(items);
michael@0 22 compare(assert, coll, items);
michael@0 23 addRemove(assert, coll, items, true);
michael@0 24 };
michael@0 25
michael@0 26 exports.testProperty = function (assert) {
michael@0 27 let obj = makeObjWithCollProp();
michael@0 28 compare(assert, obj.coll, []);
michael@0 29 addRemove(assert, obj.coll, [], false);
michael@0 30
michael@0 31 // Test single-value set.
michael@0 32 let items = ["foo"];
michael@0 33 obj.coll = items[0];
michael@0 34 compare(assert, obj.coll, items);
michael@0 35 addRemove(assert, obj.coll, items, false);
michael@0 36
michael@0 37 // Test array set.
michael@0 38 items = ["foo", "bar"];
michael@0 39 obj.coll = items;
michael@0 40 compare(assert, obj.coll, items);
michael@0 41 addRemove(assert, obj.coll, items, false);
michael@0 42 };
michael@0 43
michael@0 44 exports.testPropertyBackingArray = function (assert) {
michael@0 45 let items = ["foo"];
michael@0 46 let obj = makeObjWithCollProp(items);
michael@0 47 compare(assert, obj.coll, items);
michael@0 48 addRemove(assert, obj.coll, items, true);
michael@0 49
michael@0 50 items = ["foo", "bar"];
michael@0 51 obj = makeObjWithCollProp(items);
michael@0 52 compare(assert, obj.coll, items);
michael@0 53 addRemove(assert, obj.coll, items, true);
michael@0 54 };
michael@0 55
michael@0 56 // Adds some values to coll and then removes them. initialItems is an array
michael@0 57 // containing coll's initial items. isBacking is true if initialItems is coll's
michael@0 58 // backing array; the point is that updates to coll should affect initialItems
michael@0 59 // if that's the case.
michael@0 60 function addRemove(assert, coll, initialItems, isBacking) {
michael@0 61 let items = isBacking ? initialItems : initialItems.slice(0);
michael@0 62 let numInitialItems = items.length;
michael@0 63
michael@0 64 // Test add(val).
michael@0 65 let numInsertions = 5;
michael@0 66 for (let i = 0; i < numInsertions; i++) {
michael@0 67 compare(assert, coll, items);
michael@0 68 coll.add(i);
michael@0 69 if (!isBacking)
michael@0 70 items.push(i);
michael@0 71 }
michael@0 72 compare(assert, coll, items);
michael@0 73
michael@0 74 // Add the items we just added to make sure duplicates aren't added.
michael@0 75 for (let i = 0; i < numInsertions; i++)
michael@0 76 coll.add(i);
michael@0 77 compare(assert, coll, items);
michael@0 78
michael@0 79 // Test remove(val). Do a kind of shuffled remove. Remove item 1, then
michael@0 80 // item 0, 3, 2, 5, 4, ...
michael@0 81 for (let i = 0; i < numInsertions; i++) {
michael@0 82 let val = i % 2 ? i - 1 :
michael@0 83 i === numInsertions - 1 ? i : i + 1;
michael@0 84 coll.remove(val);
michael@0 85 if (!isBacking)
michael@0 86 items.splice(items.indexOf(val), 1);
michael@0 87 compare(assert, coll, items);
michael@0 88 }
michael@0 89 assert.equal(coll.length, numInitialItems,
michael@0 90 "All inserted items should be removed");
michael@0 91
michael@0 92 // Remove the items we just removed. coll should be unchanged.
michael@0 93 for (let i = 0; i < numInsertions; i++)
michael@0 94 coll.remove(i);
michael@0 95 compare(assert, coll, items);
michael@0 96
michael@0 97 // Test add and remove([val1, val2]).
michael@0 98 let newItems = [0, 1];
michael@0 99 coll.add(newItems);
michael@0 100 compare(assert, coll, isBacking ? items : items.concat(newItems));
michael@0 101 coll.remove(newItems);
michael@0 102 compare(assert, coll, items);
michael@0 103 assert.equal(coll.length, numInitialItems,
michael@0 104 "All inserted items should be removed");
michael@0 105 }
michael@0 106
michael@0 107 // Asserts that the items in coll are the items of array.
michael@0 108 function compare(assert, coll, array) {
michael@0 109 assert.equal(coll.length, array.length,
michael@0 110 "Collection length should be correct");
michael@0 111 let numItems = 0;
michael@0 112 for (let item in coll) {
michael@0 113 assert.equal(item, array[numItems], "Items should be equal");
michael@0 114 numItems++;
michael@0 115 }
michael@0 116 assert.equal(numItems, array.length,
michael@0 117 "Number of items in iteration should be correct");
michael@0 118 }
michael@0 119
michael@0 120 // Returns a new object with a collection property named "coll". backingArray,
michael@0 121 // if defined, will create the collection with that backing array.
michael@0 122 function makeObjWithCollProp(backingArray) {
michael@0 123 let obj = {};
michael@0 124 collection.addCollectionProperty(obj, "coll", backingArray);
michael@0 125 return obj;
michael@0 126 }
michael@0 127
michael@0 128 require("sdk/test").run(exports);

mercurial