test-list.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 { List, addListItem, removeListItem } = require('sdk/util/list');
  6. const { Class } = require('sdk/core/heritage');
  7. exports.testList = function(assert) {
  8. let list = List();
  9. addListItem(list, 1);
  10. for (let key in list) {
  11. assert.equal(key, 0, 'key is correct');
  12. assert.equal(list[key], 1, 'value is correct');
  13. }
  14. let count = 0;
  15. for each (let ele in list) {
  16. assert.equal(ele, 1, 'ele is correct');
  17. assert.equal(++count, 1, 'count is correct');
  18. }
  19. count = 0;
  20. for (let ele of list) {
  21. assert.equal(ele, 1, 'ele is correct');
  22. assert.equal(++count, 1, 'count is correct');
  23. }
  24. removeListItem(list, 1);
  25. assert.equal(list.length, 0, 'remove worked');
  26. };
  27. exports.testImplementsList = function(assert) {
  28. let List2 = Class({
  29. implements: [List],
  30. initialize: function() {
  31. List.prototype.initialize.apply(this, [0, 1, 2]);
  32. }
  33. });
  34. let list2 = List2();
  35. let count = 0;
  36. for each (let ele in list2) {
  37. assert.equal(ele, count++, 'ele is correct');
  38. }
  39. count = 0;
  40. for (let ele of list2) {
  41. assert.equal(ele, count++, 'ele is correct');
  42. }
  43. addListItem(list2, 3);
  44. assert.equal(list2.length, 4, '3 was added');
  45. assert.equal(list2[list2.length-1], 3, '3 was added');
  46. }
  47. require('sdk/test').run(exports);