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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial