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