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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-collection.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,128 @@
     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 +"use strict";
     1.8 +
     1.9 +const collection = require("sdk/util/collection");
    1.10 +
    1.11 +exports.testAddRemove = function (assert) {
    1.12 +  let coll = new collection.Collection();
    1.13 +  compare(assert, coll, []);
    1.14 +  addRemove(assert, coll, [], false);
    1.15 +};
    1.16 +
    1.17 +exports.testAddRemoveBackingArray = function (assert) {
    1.18 +  let items = ["foo"];
    1.19 +  let coll = new collection.Collection(items);
    1.20 +  compare(assert, coll, items);
    1.21 +  addRemove(assert, coll, items, true);
    1.22 +
    1.23 +  items = ["foo", "bar"];
    1.24 +  coll = new collection.Collection(items);
    1.25 +  compare(assert, coll, items);
    1.26 +  addRemove(assert, coll, items, true);
    1.27 +};
    1.28 +
    1.29 +exports.testProperty = function (assert) {
    1.30 +  let obj = makeObjWithCollProp();
    1.31 +  compare(assert, obj.coll, []);
    1.32 +  addRemove(assert, obj.coll, [], false);
    1.33 +
    1.34 +  // Test single-value set.
    1.35 +  let items = ["foo"];
    1.36 +  obj.coll = items[0];
    1.37 +  compare(assert, obj.coll, items);
    1.38 +  addRemove(assert, obj.coll, items, false);
    1.39 +
    1.40 +  // Test array set.
    1.41 +  items = ["foo", "bar"];
    1.42 +  obj.coll = items;
    1.43 +  compare(assert, obj.coll, items);
    1.44 +  addRemove(assert, obj.coll, items, false);
    1.45 +};
    1.46 +
    1.47 +exports.testPropertyBackingArray = function (assert) {
    1.48 +  let items = ["foo"];
    1.49 +  let obj = makeObjWithCollProp(items);
    1.50 +  compare(assert, obj.coll, items);
    1.51 +  addRemove(assert, obj.coll, items, true);
    1.52 +
    1.53 +  items = ["foo", "bar"];
    1.54 +  obj = makeObjWithCollProp(items);
    1.55 +  compare(assert, obj.coll, items);
    1.56 +  addRemove(assert, obj.coll, items, true);
    1.57 +};
    1.58 +
    1.59 +// Adds some values to coll and then removes them.  initialItems is an array
    1.60 +// containing coll's initial items.  isBacking is true if initialItems is coll's
    1.61 +// backing array; the point is that updates to coll should affect initialItems
    1.62 +// if that's the case.
    1.63 +function addRemove(assert, coll, initialItems, isBacking) {
    1.64 +  let items = isBacking ? initialItems : initialItems.slice(0);
    1.65 +  let numInitialItems = items.length;
    1.66 +
    1.67 +  // Test add(val).
    1.68 +  let numInsertions = 5;
    1.69 +  for (let i = 0; i < numInsertions; i++) {
    1.70 +    compare(assert, coll, items);
    1.71 +    coll.add(i);
    1.72 +    if (!isBacking)
    1.73 +      items.push(i);
    1.74 +  }
    1.75 +  compare(assert, coll, items);
    1.76 +
    1.77 +  // Add the items we just added to make sure duplicates aren't added.
    1.78 +  for (let i = 0; i < numInsertions; i++)
    1.79 +    coll.add(i);
    1.80 +  compare(assert, coll, items);
    1.81 +
    1.82 +  // Test remove(val).  Do a kind of shuffled remove.  Remove item 1, then
    1.83 +  // item 0, 3, 2, 5, 4, ...
    1.84 +  for (let i = 0; i < numInsertions; i++) {
    1.85 +    let val = i % 2 ? i - 1 :
    1.86 +              i === numInsertions - 1 ? i : i + 1;
    1.87 +    coll.remove(val);
    1.88 +    if (!isBacking)
    1.89 +      items.splice(items.indexOf(val), 1);
    1.90 +    compare(assert, coll, items);
    1.91 +  }
    1.92 +  assert.equal(coll.length, numInitialItems,
    1.93 +               "All inserted items should be removed");
    1.94 +
    1.95 +  // Remove the items we just removed.  coll should be unchanged.
    1.96 +  for (let i = 0; i < numInsertions; i++)
    1.97 +    coll.remove(i);
    1.98 +  compare(assert, coll, items);
    1.99 +
   1.100 +  // Test add and remove([val1, val2]).
   1.101 +  let newItems = [0, 1];
   1.102 +  coll.add(newItems);
   1.103 +  compare(assert, coll, isBacking ? items : items.concat(newItems));
   1.104 +  coll.remove(newItems);
   1.105 +  compare(assert, coll, items);
   1.106 +  assert.equal(coll.length, numInitialItems,
   1.107 +               "All inserted items should be removed");
   1.108 +}
   1.109 +
   1.110 +// Asserts that the items in coll are the items of array.
   1.111 +function compare(assert, coll, array) {
   1.112 +  assert.equal(coll.length, array.length,
   1.113 +               "Collection length should be correct");
   1.114 +  let numItems = 0;
   1.115 +  for (let item in coll) {
   1.116 +    assert.equal(item, array[numItems], "Items should be equal");
   1.117 +    numItems++;
   1.118 +  }
   1.119 +  assert.equal(numItems, array.length,
   1.120 +               "Number of items in iteration should be correct");
   1.121 +}
   1.122 +
   1.123 +// Returns a new object with a collection property named "coll".  backingArray,
   1.124 +// if defined, will create the collection with that backing array.
   1.125 +function makeObjWithCollProp(backingArray) {
   1.126 +  let obj = {};
   1.127 +  collection.addCollectionProperty(obj, "coll", backingArray);
   1.128 +  return obj;
   1.129 +}
   1.130 +
   1.131 +require("sdk/test").run(exports);

mercurial