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