test-deprecated-list.js 4.8 KB

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