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

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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';
     6 const { List } = require('sdk/deprecated/list');
     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 }
    29 exports['test:test for'] = function(assert) {
    30   let fixture = List(3, 2, 1);
    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 };
    39 exports['test:test for each'] = function(assert) {
    40   let fixture = new List(3, 2, 1);
    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 };
    49 exports['test:test for of'] = function(assert) {
    50   let fixture = new List(3, 2, 1);
    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 };
    59 exports['test:test toString'] = function(assert) {
    60   let fixture = List(3, 2, 1);
    62   assert.equal(
    63     'List(3,2,1)',
    64     fixture + '',
    65     'toString must output array like result'
    66   )
    67 };
    69 exports['test:test constructor with apply'] = function(assert) {
    70   let array = ['a', 'b', 'c'];
    71   let fixture = List.apply(null, array);
    73   assert.equal(
    74     3,
    75     fixture.length,
    76     'should have applied arguments'
    77   );
    78 };
    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);
    86   assert.equal(
    87     array.length,
    88     fixture.length,
    89     'list should omit duplicate elements'
    90   );
    92   assert.equal(
    93     'List(' + array + ')',
    94     fixture.toString(),
    95     'elements should not be rearranged'
    96   );
    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 };
   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);
   114   assertList(assert, array, fixture);
   116   array.splice(array.indexOf(2), 1);
   117   fixture.remove(2);
   118   assertList(assert, array, fixture);
   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);
   128   array.splice(0);
   129   fixture.clear(0);
   130   assertList(assert, array, fixture);
   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);
   139   assertList(assert, array, fixture);
   140 };
   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]);
   147     fixture.remove(1);
   148     assert.equal(fixture[fixture.length], undefined);
   149 }
   151 exports['test:add list item from Iterator'] = function(assert) {
   152   let array = [1, 2, 3, 4], sum = 0, added = false;
   154   let fixture = List.compose({
   155     add: function() this._add.apply(this, arguments),
   156   }).apply(null, array);
   158   for (let item of fixture) {
   159     sum += item;
   161     if (!added) {
   162       fixture.add(5);
   163       added = true;
   164     }
   165   }
   167   assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4');
   168 };
   170 exports['test:remove list item from Iterator'] = function(assert) {
   171   let array = [1, 2, 3, 4], sum = 0;
   173   let fixture = List.compose({
   174     remove: function() this._remove.apply(this, arguments),
   175   }).apply(null, array);
   177   for (let item of fixture) {
   178     sum += item;
   179     fixture.remove(item);
   180   }
   182   assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4');
   183 };
   185 exports['test:clear list from Iterator'] = function(assert) {
   186   let array = [1, 2, 3, 4], sum = 0;
   188   let fixture = List.compose({
   189     clear: function() this._clear()
   190   }).apply(null, array);
   192   for (let item of fixture) {
   193     sum += item;
   194     fixture.clear();
   195   }
   197   assert.equal(sum, 1 + 2 + 3 + 4, 'sum = 1 + 2 + 3 + 4');
   198 };
   200 require('sdk/test').run(exports);

mercurial