test-collection.js 4.0 KB

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