test-array.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 array = require('sdk/util/array');
  6. exports.testHas = function(assert) {
  7. var testAry = [1, 2, 3];
  8. assert.equal(array.has([1, 2, 3], 1), true);
  9. assert.equal(testAry.length, 3);
  10. assert.equal(testAry[0], 1);
  11. assert.equal(testAry[1], 2);
  12. assert.equal(testAry[2], 3);
  13. assert.equal(array.has(testAry, 2), true);
  14. assert.equal(array.has(testAry, 3), true);
  15. assert.equal(array.has(testAry, 4), false);
  16. assert.equal(array.has(testAry, '1'), false);
  17. };
  18. exports.testHasAny = function(assert) {
  19. var testAry = [1, 2, 3];
  20. assert.equal(array.hasAny([1, 2, 3], [1]), true);
  21. assert.equal(array.hasAny([1, 2, 3], [1, 5]), true);
  22. assert.equal(array.hasAny([1, 2, 3], [5, 1]), true);
  23. assert.equal(array.hasAny([1, 2, 3], [5, 2]), true);
  24. assert.equal(array.hasAny([1, 2, 3], [5, 3]), true);
  25. assert.equal(array.hasAny([1, 2, 3], [5, 4]), false);
  26. assert.equal(testAry.length, 3);
  27. assert.equal(testAry[0], 1);
  28. assert.equal(testAry[1], 2);
  29. assert.equal(testAry[2], 3);
  30. assert.equal(array.hasAny(testAry, [2]), true);
  31. assert.equal(array.hasAny(testAry, [3]), true);
  32. assert.equal(array.hasAny(testAry, [4]), false);
  33. assert.equal(array.hasAny(testAry), false);
  34. assert.equal(array.hasAny(testAry, '1'), false);
  35. };
  36. exports.testAdd = function(assert) {
  37. var testAry = [1];
  38. assert.equal(array.add(testAry, 1), false);
  39. assert.equal(testAry.length, 1);
  40. assert.equal(testAry[0], 1);
  41. assert.equal(array.add(testAry, 2), true);
  42. assert.equal(testAry.length, 2);
  43. assert.equal(testAry[0], 1);
  44. assert.equal(testAry[1], 2);
  45. };
  46. exports.testRemove = function(assert) {
  47. var testAry = [1, 2];
  48. assert.equal(array.remove(testAry, 3), false);
  49. assert.equal(testAry.length, 2);
  50. assert.equal(testAry[0], 1);
  51. assert.equal(testAry[1], 2);
  52. assert.equal(array.remove(testAry, 2), true);
  53. assert.equal(testAry.length, 1);
  54. assert.equal(testAry[0], 1);
  55. };
  56. exports.testFlatten = function(assert) {
  57. assert.equal(array.flatten([1, 2, 3]).length, 3);
  58. assert.equal(array.flatten([1, [2, 3]]).length, 3);
  59. assert.equal(array.flatten([1, [2, [3]]]).length, 3);
  60. assert.equal(array.flatten([[1], [[2, [3]]]]).length, 3);
  61. };
  62. exports.testUnique = function(assert) {
  63. var Class = function () {};
  64. var A = {};
  65. var B = new Class();
  66. var C = [ 1, 2, 3 ];
  67. var D = {};
  68. var E = new Class();
  69. assert.deepEqual(array.unique([1,2,3,1,2]), [1,2,3]);
  70. assert.deepEqual(array.unique([1,1,1,4,9,5,5]), [1,4,9,5]);
  71. assert.deepEqual(array.unique([A, A, A, B, B, D]), [A,B,D]);
  72. assert.deepEqual(array.unique([A, D, A, E, E, D, A, A, C]), [A, D, E, C])
  73. };
  74. exports.testUnion = function(assert) {
  75. var Class = function () {};
  76. var A = {};
  77. var B = new Class();
  78. var C = [ 1, 2, 3 ];
  79. var D = {};
  80. var E = new Class();
  81. assert.deepEqual(array.union([1, 2, 3],[7, 1, 2]), [1, 2, 3, 7]);
  82. assert.deepEqual(array.union([1, 1, 1, 4, 9, 5, 5], [10, 1, 5]), [1, 4, 9, 5, 10]);
  83. assert.deepEqual(array.union([A, B], [A, D]), [A, B, D]);
  84. assert.deepEqual(array.union([A, D], [A, E], [E, D, A], [A, C]), [A, D, E, C]);
  85. };
  86. exports.testFind = function(assert) {
  87. let isOdd = (x) => x % 2;
  88. assert.equal(array.find([2, 4, 5, 7, 8, 9], isOdd), 5);
  89. assert.equal(array.find([2, 4, 6, 8], isOdd), undefined);
  90. assert.equal(array.find([2, 4, 6, 8], isOdd, null), null);
  91. };
  92. require('test').run(exports);