1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-deprecated-list.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,200 @@ 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 } = require('sdk/deprecated/list'); 1.10 + 1.11 +function assertList(assert, array, list) { 1.12 + for (let i = 0, l = array.length; i < l; i++) { 1.13 + assert.equal( 1.14 + array.length, 1.15 + list.length, 1.16 + 'list must contain same amount of elements as array' 1.17 + ); 1.18 + assert.equal( 1.19 + 'List(' + array + ')', 1.20 + list + '', 1.21 + 'toString must output array like result' 1.22 + ); 1.23 + assert.ok(i in list, 'must contain element with index: ' + i); 1.24 + assert.equal( 1.25 + array[i], 1.26 + list[i], 1.27 + 'element with index: ' + i + ' should match' 1.28 + ); 1.29 + } 1.30 +} 1.31 + 1.32 +exports['test:test for'] = function(assert) { 1.33 + let fixture = List(3, 2, 1); 1.34 + 1.35 + assert.equal(3, fixture.length, 'length is 3'); 1.36 + let i = 0; 1.37 + for (let key in fixture) { 1.38 + assert.equal(i++, key, 'key should match'); 1.39 + } 1.40 +}; 1.41 + 1.42 +exports['test:test for each'] = function(assert) { 1.43 + let fixture = new List(3, 2, 1); 1.44 + 1.45 + assert.equal(3, fixture.length, 'length is 3'); 1.46 + let i = 3; 1.47 + for (let value of fixture) { 1.48 + assert.equal(i--, value, 'value should match'); 1.49 + } 1.50 +}; 1.51 + 1.52 +exports['test:test for of'] = function(assert) { 1.53 + let fixture = new List(3, 2, 1); 1.54 + 1.55 + assert.equal(3, fixture.length, 'length is 3'); 1.56 + let i = 3; 1.57 + for (let value of fixture) { 1.58 + assert.equal(i--, value, 'value should match'); 1.59 + } 1.60 +}; 1.61 + 1.62 +exports['test:test toString'] = function(assert) { 1.63 + let fixture = List(3, 2, 1); 1.64 + 1.65 + assert.equal( 1.66 + 'List(3,2,1)', 1.67 + fixture + '', 1.68 + 'toString must output array like result' 1.69 + ) 1.70 +}; 1.71 + 1.72 +exports['test:test constructor with apply'] = function(assert) { 1.73 + let array = ['a', 'b', 'c']; 1.74 + let fixture = List.apply(null, array); 1.75 + 1.76 + assert.equal( 1.77 + 3, 1.78 + fixture.length, 1.79 + 'should have applied arguments' 1.80 + ); 1.81 +}; 1.82 + 1.83 +exports['test:direct element access'] = function(assert) { 1.84 + let array = [1, 'foo', 2, 'bar', {}, 'bar', function a() {}, assert, 1]; 1.85 + let fixture = List.apply(null, array); 1.86 + array.splice(5, 1); 1.87 + array.splice(7, 1); 1.88 + 1.89 + assert.equal( 1.90 + array.length, 1.91 + fixture.length, 1.92 + 'list should omit duplicate elements' 1.93 + ); 1.94 + 1.95 + assert.equal( 1.96 + 'List(' + array + ')', 1.97 + fixture.toString(), 1.98 + 'elements should not be rearranged' 1.99 + ); 1.100 + 1.101 + for (let key in array) { 1.102 + assert.ok(key in fixture,'should contain key for index:' + key); 1.103 + assert.equal(array[key], fixture[key], 'values should match for: ' + key); 1.104 + } 1.105 +}; 1.106 + 1.107 +exports['test:removing adding elements'] = function(assert) { 1.108 + let array = [1, 'foo', 2, 'bar', {}, 'bar', function a() {}, assert, 1]; 1.109 + let fixture = List.compose({ 1.110 + add: function() this._add.apply(this, arguments), 1.111 + remove: function() this._remove.apply(this, arguments), 1.112 + clear: function() this._clear() 1.113 + }).apply(null, array); 1.114 + array.splice(5, 1); 1.115 + array.splice(7, 1); 1.116 + 1.117 + assertList(assert, array, fixture); 1.118 + 1.119 + array.splice(array.indexOf(2), 1); 1.120 + fixture.remove(2); 1.121 + assertList(assert, array, fixture); 1.122 + 1.123 + array.splice(array.indexOf('foo'), 1); 1.124 + fixture.remove('foo'); 1.125 + array.splice(array.indexOf(1), 1); 1.126 + fixture.remove(1); 1.127 + array.push('foo'); 1.128 + fixture.add('foo'); 1.129 + assertList(assert, array, fixture); 1.130 + 1.131 + array.splice(0); 1.132 + fixture.clear(0); 1.133 + assertList(assert, array, fixture); 1.134 + 1.135 + array.push(1, 'foo', 2, 'bar', 3); 1.136 + fixture.add(1); 1.137 + fixture.add('foo'); 1.138 + fixture.add(2); 1.139 + fixture.add('bar'); 1.140 + fixture.add(3); 1.141 + 1.142 + assertList(assert, array, fixture); 1.143 +}; 1.144 + 1.145 +exports['test: remove does not leave invalid numerical properties'] = function(assert) { 1.146 + let fixture = List.compose({ 1.147 + remove: function() this._remove.apply(this, arguments), 1.148 + }).apply(null, [1, 2, 3]); 1.149 + 1.150 + fixture.remove(1); 1.151 + assert.equal(fixture[fixture.length], undefined); 1.152 +} 1.153 + 1.154 +exports['test:add list item from Iterator'] = function(assert) { 1.155 + let array = [1, 2, 3, 4], sum = 0, added = false; 1.156 + 1.157 + let fixture = List.compose({ 1.158 + add: function() this._add.apply(this, arguments), 1.159 + }).apply(null, array); 1.160 + 1.161 + for (let item of fixture) { 1.162 + sum += item; 1.163 + 1.164 + if (!added) { 1.165 + fixture.add(5); 1.166 + added = true; 1.167 + } 1.168 + } 1.169 + 1.170 + assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4'); 1.171 +}; 1.172 + 1.173 +exports['test:remove list item from Iterator'] = function(assert) { 1.174 + let array = [1, 2, 3, 4], sum = 0; 1.175 + 1.176 + let fixture = List.compose({ 1.177 + remove: function() this._remove.apply(this, arguments), 1.178 + }).apply(null, array); 1.179 + 1.180 + for (let item of fixture) { 1.181 + sum += item; 1.182 + fixture.remove(item); 1.183 + } 1.184 + 1.185 + assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4'); 1.186 +}; 1.187 + 1.188 +exports['test:clear list from Iterator'] = function(assert) { 1.189 + let array = [1, 2, 3, 4], sum = 0; 1.190 + 1.191 + let fixture = List.compose({ 1.192 + clear: function() this._clear() 1.193 + }).apply(null, array); 1.194 + 1.195 + for (let item of fixture) { 1.196 + sum += item; 1.197 + fixture.clear(); 1.198 + } 1.199 + 1.200 + assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4'); 1.201 +}; 1.202 + 1.203 +require('sdk/test').run(exports);