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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-list.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,58 @@
     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 { List, addListItem, removeListItem } = require('sdk/util/list');
    1.10 +const { Class } = require('sdk/core/heritage');
    1.11 +
    1.12 +exports.testList = function(assert) {
    1.13 +  let list = List();
    1.14 +  addListItem(list, 1);
    1.15 +
    1.16 +  for (let key in list) {
    1.17 +    assert.equal(key, 0, 'key is correct');
    1.18 +    assert.equal(list[key], 1, 'value is correct');
    1.19 +  }
    1.20 +
    1.21 +  let count = 0;
    1.22 +  for each (let ele in list) {
    1.23 +    assert.equal(ele, 1, 'ele is correct');
    1.24 +    assert.equal(++count, 1, 'count is correct');
    1.25 +  }
    1.26 +
    1.27 +  count = 0;
    1.28 +  for (let ele of list) {
    1.29 +    assert.equal(ele, 1, 'ele is correct');
    1.30 +    assert.equal(++count, 1, 'count is correct');
    1.31 +  }
    1.32 +
    1.33 +  removeListItem(list, 1);
    1.34 +  assert.equal(list.length, 0, 'remove worked');
    1.35 +};
    1.36 +
    1.37 +exports.testImplementsList = function(assert) {
    1.38 +  let List2 = Class({
    1.39 +    implements: [List],
    1.40 +    initialize: function() {
    1.41 +      List.prototype.initialize.apply(this, [0, 1, 2]);
    1.42 +    }
    1.43 +  });
    1.44 +  let list2 = List2();
    1.45 +  let count = 0;
    1.46 +
    1.47 +  for each (let ele in list2) {
    1.48 +    assert.equal(ele, count++, 'ele is correct');
    1.49 +  }
    1.50 +
    1.51 +  count = 0;
    1.52 +  for (let ele of list2) {
    1.53 +    assert.equal(ele, count++, 'ele is correct');
    1.54 +  }
    1.55 +
    1.56 +  addListItem(list2, 3);
    1.57 +  assert.equal(list2.length, 4, '3 was added');
    1.58 +  assert.equal(list2[list2.length-1], 3, '3 was added');
    1.59 +}
    1.60 +
    1.61 +require('sdk/test').run(exports);

mercurial