michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: const { List } = require('sdk/deprecated/list'); michael@0: michael@0: function assertList(assert, array, list) { michael@0: for (let i = 0, l = array.length; i < l; i++) { michael@0: assert.equal( michael@0: array.length, michael@0: list.length, michael@0: 'list must contain same amount of elements as array' michael@0: ); michael@0: assert.equal( michael@0: 'List(' + array + ')', michael@0: list + '', michael@0: 'toString must output array like result' michael@0: ); michael@0: assert.ok(i in list, 'must contain element with index: ' + i); michael@0: assert.equal( michael@0: array[i], michael@0: list[i], michael@0: 'element with index: ' + i + ' should match' michael@0: ); michael@0: } michael@0: } michael@0: michael@0: exports['test:test for'] = function(assert) { michael@0: let fixture = List(3, 2, 1); michael@0: michael@0: assert.equal(3, fixture.length, 'length is 3'); michael@0: let i = 0; michael@0: for (let key in fixture) { michael@0: assert.equal(i++, key, 'key should match'); michael@0: } michael@0: }; michael@0: michael@0: exports['test:test for each'] = function(assert) { michael@0: let fixture = new List(3, 2, 1); michael@0: michael@0: assert.equal(3, fixture.length, 'length is 3'); michael@0: let i = 3; michael@0: for (let value of fixture) { michael@0: assert.equal(i--, value, 'value should match'); michael@0: } michael@0: }; michael@0: michael@0: exports['test:test for of'] = function(assert) { michael@0: let fixture = new List(3, 2, 1); michael@0: michael@0: assert.equal(3, fixture.length, 'length is 3'); michael@0: let i = 3; michael@0: for (let value of fixture) { michael@0: assert.equal(i--, value, 'value should match'); michael@0: } michael@0: }; michael@0: michael@0: exports['test:test toString'] = function(assert) { michael@0: let fixture = List(3, 2, 1); michael@0: michael@0: assert.equal( michael@0: 'List(3,2,1)', michael@0: fixture + '', michael@0: 'toString must output array like result' michael@0: ) michael@0: }; michael@0: michael@0: exports['test:test constructor with apply'] = function(assert) { michael@0: let array = ['a', 'b', 'c']; michael@0: let fixture = List.apply(null, array); michael@0: michael@0: assert.equal( michael@0: 3, michael@0: fixture.length, michael@0: 'should have applied arguments' michael@0: ); michael@0: }; michael@0: michael@0: exports['test:direct element access'] = function(assert) { michael@0: let array = [1, 'foo', 2, 'bar', {}, 'bar', function a() {}, assert, 1]; michael@0: let fixture = List.apply(null, array); michael@0: array.splice(5, 1); michael@0: array.splice(7, 1); michael@0: michael@0: assert.equal( michael@0: array.length, michael@0: fixture.length, michael@0: 'list should omit duplicate elements' michael@0: ); michael@0: michael@0: assert.equal( michael@0: 'List(' + array + ')', michael@0: fixture.toString(), michael@0: 'elements should not be rearranged' michael@0: ); michael@0: michael@0: for (let key in array) { michael@0: assert.ok(key in fixture,'should contain key for index:' + key); michael@0: assert.equal(array[key], fixture[key], 'values should match for: ' + key); michael@0: } michael@0: }; michael@0: michael@0: exports['test:removing adding elements'] = function(assert) { michael@0: let array = [1, 'foo', 2, 'bar', {}, 'bar', function a() {}, assert, 1]; michael@0: let fixture = List.compose({ michael@0: add: function() this._add.apply(this, arguments), michael@0: remove: function() this._remove.apply(this, arguments), michael@0: clear: function() this._clear() michael@0: }).apply(null, array); michael@0: array.splice(5, 1); michael@0: array.splice(7, 1); michael@0: michael@0: assertList(assert, array, fixture); michael@0: michael@0: array.splice(array.indexOf(2), 1); michael@0: fixture.remove(2); michael@0: assertList(assert, array, fixture); michael@0: michael@0: array.splice(array.indexOf('foo'), 1); michael@0: fixture.remove('foo'); michael@0: array.splice(array.indexOf(1), 1); michael@0: fixture.remove(1); michael@0: array.push('foo'); michael@0: fixture.add('foo'); michael@0: assertList(assert, array, fixture); michael@0: michael@0: array.splice(0); michael@0: fixture.clear(0); michael@0: assertList(assert, array, fixture); michael@0: michael@0: array.push(1, 'foo', 2, 'bar', 3); michael@0: fixture.add(1); michael@0: fixture.add('foo'); michael@0: fixture.add(2); michael@0: fixture.add('bar'); michael@0: fixture.add(3); michael@0: michael@0: assertList(assert, array, fixture); michael@0: }; michael@0: michael@0: exports['test: remove does not leave invalid numerical properties'] = function(assert) { michael@0: let fixture = List.compose({ michael@0: remove: function() this._remove.apply(this, arguments), michael@0: }).apply(null, [1, 2, 3]); michael@0: michael@0: fixture.remove(1); michael@0: assert.equal(fixture[fixture.length], undefined); michael@0: } michael@0: michael@0: exports['test:add list item from Iterator'] = function(assert) { michael@0: let array = [1, 2, 3, 4], sum = 0, added = false; michael@0: michael@0: let fixture = List.compose({ michael@0: add: function() this._add.apply(this, arguments), michael@0: }).apply(null, array); michael@0: michael@0: for (let item of fixture) { michael@0: sum += item; michael@0: michael@0: if (!added) { michael@0: fixture.add(5); michael@0: added = true; michael@0: } michael@0: } michael@0: michael@0: assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4'); michael@0: }; michael@0: michael@0: exports['test:remove list item from Iterator'] = function(assert) { michael@0: let array = [1, 2, 3, 4], sum = 0; michael@0: michael@0: let fixture = List.compose({ michael@0: remove: function() this._remove.apply(this, arguments), michael@0: }).apply(null, array); michael@0: michael@0: for (let item of fixture) { michael@0: sum += item; michael@0: fixture.remove(item); michael@0: } michael@0: michael@0: assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4'); michael@0: }; michael@0: michael@0: exports['test:clear list from Iterator'] = function(assert) { michael@0: let array = [1, 2, 3, 4], sum = 0; michael@0: michael@0: let fixture = List.compose({ michael@0: clear: function() this._clear() michael@0: }).apply(null, array); michael@0: michael@0: for (let item of fixture) { michael@0: sum += item; michael@0: fixture.clear(); michael@0: } michael@0: michael@0: assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4'); michael@0: }; michael@0: michael@0: require('sdk/test').run(exports);