|
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 } = require('sdk/deprecated/list'); |
|
7 |
|
8 function assertList(assert, array, list) { |
|
9 for (let i = 0, l = array.length; i < l; i++) { |
|
10 assert.equal( |
|
11 array.length, |
|
12 list.length, |
|
13 'list must contain same amount of elements as array' |
|
14 ); |
|
15 assert.equal( |
|
16 'List(' + array + ')', |
|
17 list + '', |
|
18 'toString must output array like result' |
|
19 ); |
|
20 assert.ok(i in list, 'must contain element with index: ' + i); |
|
21 assert.equal( |
|
22 array[i], |
|
23 list[i], |
|
24 'element with index: ' + i + ' should match' |
|
25 ); |
|
26 } |
|
27 } |
|
28 |
|
29 exports['test:test for'] = function(assert) { |
|
30 let fixture = List(3, 2, 1); |
|
31 |
|
32 assert.equal(3, fixture.length, 'length is 3'); |
|
33 let i = 0; |
|
34 for (let key in fixture) { |
|
35 assert.equal(i++, key, 'key should match'); |
|
36 } |
|
37 }; |
|
38 |
|
39 exports['test:test for each'] = function(assert) { |
|
40 let fixture = new List(3, 2, 1); |
|
41 |
|
42 assert.equal(3, fixture.length, 'length is 3'); |
|
43 let i = 3; |
|
44 for (let value of fixture) { |
|
45 assert.equal(i--, value, 'value should match'); |
|
46 } |
|
47 }; |
|
48 |
|
49 exports['test:test for of'] = function(assert) { |
|
50 let fixture = new List(3, 2, 1); |
|
51 |
|
52 assert.equal(3, fixture.length, 'length is 3'); |
|
53 let i = 3; |
|
54 for (let value of fixture) { |
|
55 assert.equal(i--, value, 'value should match'); |
|
56 } |
|
57 }; |
|
58 |
|
59 exports['test:test toString'] = function(assert) { |
|
60 let fixture = List(3, 2, 1); |
|
61 |
|
62 assert.equal( |
|
63 'List(3,2,1)', |
|
64 fixture + '', |
|
65 'toString must output array like result' |
|
66 ) |
|
67 }; |
|
68 |
|
69 exports['test:test constructor with apply'] = function(assert) { |
|
70 let array = ['a', 'b', 'c']; |
|
71 let fixture = List.apply(null, array); |
|
72 |
|
73 assert.equal( |
|
74 3, |
|
75 fixture.length, |
|
76 'should have applied arguments' |
|
77 ); |
|
78 }; |
|
79 |
|
80 exports['test:direct element access'] = function(assert) { |
|
81 let array = [1, 'foo', 2, 'bar', {}, 'bar', function a() {}, assert, 1]; |
|
82 let fixture = List.apply(null, array); |
|
83 array.splice(5, 1); |
|
84 array.splice(7, 1); |
|
85 |
|
86 assert.equal( |
|
87 array.length, |
|
88 fixture.length, |
|
89 'list should omit duplicate elements' |
|
90 ); |
|
91 |
|
92 assert.equal( |
|
93 'List(' + array + ')', |
|
94 fixture.toString(), |
|
95 'elements should not be rearranged' |
|
96 ); |
|
97 |
|
98 for (let key in array) { |
|
99 assert.ok(key in fixture,'should contain key for index:' + key); |
|
100 assert.equal(array[key], fixture[key], 'values should match for: ' + key); |
|
101 } |
|
102 }; |
|
103 |
|
104 exports['test:removing adding elements'] = function(assert) { |
|
105 let array = [1, 'foo', 2, 'bar', {}, 'bar', function a() {}, assert, 1]; |
|
106 let fixture = List.compose({ |
|
107 add: function() this._add.apply(this, arguments), |
|
108 remove: function() this._remove.apply(this, arguments), |
|
109 clear: function() this._clear() |
|
110 }).apply(null, array); |
|
111 array.splice(5, 1); |
|
112 array.splice(7, 1); |
|
113 |
|
114 assertList(assert, array, fixture); |
|
115 |
|
116 array.splice(array.indexOf(2), 1); |
|
117 fixture.remove(2); |
|
118 assertList(assert, array, fixture); |
|
119 |
|
120 array.splice(array.indexOf('foo'), 1); |
|
121 fixture.remove('foo'); |
|
122 array.splice(array.indexOf(1), 1); |
|
123 fixture.remove(1); |
|
124 array.push('foo'); |
|
125 fixture.add('foo'); |
|
126 assertList(assert, array, fixture); |
|
127 |
|
128 array.splice(0); |
|
129 fixture.clear(0); |
|
130 assertList(assert, array, fixture); |
|
131 |
|
132 array.push(1, 'foo', 2, 'bar', 3); |
|
133 fixture.add(1); |
|
134 fixture.add('foo'); |
|
135 fixture.add(2); |
|
136 fixture.add('bar'); |
|
137 fixture.add(3); |
|
138 |
|
139 assertList(assert, array, fixture); |
|
140 }; |
|
141 |
|
142 exports['test: remove does not leave invalid numerical properties'] = function(assert) { |
|
143 let fixture = List.compose({ |
|
144 remove: function() this._remove.apply(this, arguments), |
|
145 }).apply(null, [1, 2, 3]); |
|
146 |
|
147 fixture.remove(1); |
|
148 assert.equal(fixture[fixture.length], undefined); |
|
149 } |
|
150 |
|
151 exports['test:add list item from Iterator'] = function(assert) { |
|
152 let array = [1, 2, 3, 4], sum = 0, added = false; |
|
153 |
|
154 let fixture = List.compose({ |
|
155 add: function() this._add.apply(this, arguments), |
|
156 }).apply(null, array); |
|
157 |
|
158 for (let item of fixture) { |
|
159 sum += item; |
|
160 |
|
161 if (!added) { |
|
162 fixture.add(5); |
|
163 added = true; |
|
164 } |
|
165 } |
|
166 |
|
167 assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4'); |
|
168 }; |
|
169 |
|
170 exports['test:remove list item from Iterator'] = function(assert) { |
|
171 let array = [1, 2, 3, 4], sum = 0; |
|
172 |
|
173 let fixture = List.compose({ |
|
174 remove: function() this._remove.apply(this, arguments), |
|
175 }).apply(null, array); |
|
176 |
|
177 for (let item of fixture) { |
|
178 sum += item; |
|
179 fixture.remove(item); |
|
180 } |
|
181 |
|
182 assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4'); |
|
183 }; |
|
184 |
|
185 exports['test:clear list from Iterator'] = function(assert) { |
|
186 let array = [1, 2, 3, 4], sum = 0; |
|
187 |
|
188 let fixture = List.compose({ |
|
189 clear: function() this._clear() |
|
190 }).apply(null, array); |
|
191 |
|
192 for (let item of fixture) { |
|
193 sum += item; |
|
194 fixture.clear(); |
|
195 } |
|
196 |
|
197 assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4'); |
|
198 }; |
|
199 |
|
200 require('sdk/test').run(exports); |