array_flat.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  6. function testFlat(input, output, depth)
  7. {
  8. assert.areEqual(output, Array.prototype.flat.call(input, depth));
  9. }
  10. function testFlatMap(input, output, mappingFunction, thisArg)
  11. {
  12. assert.areEqual(output, Array.prototype.flatMap.call(input, mappingFunction, thisArg));
  13. }
  14. const tests = [
  15. {
  16. name : "properties",
  17. body : function ()
  18. {
  19. assert.areEqual("flat", Array.prototype.flat.name, "Array.prototype.flat name should be flat");
  20. assert.areEqual("flatMap", Array.prototype.flatMap.name, "Array.prototype.flatMap name should be flatMap");
  21. assert.areEqual(0, Array.prototype.flat.length, "Array.prototype.flat length should be 0");
  22. assert.areEqual(1, Array.prototype.flatMap.length, "Array.prototype.flatMap length should be 1");
  23. }
  24. },
  25. {
  26. name : "flatten arrays",
  27. body : function ()
  28. {
  29. testFlat([2, 3, [4, 5]], [2, 3, 4, 5]);
  30. testFlat([2, 3, [4, [5, 6]]], [2, 3, 4, [5, 6]]);
  31. testFlat([2, 3, [4, [5, 6]]], [2, 3, 4, 5, 6], 2);
  32. testFlat([], []);
  33. testFlat([[], [], 1], [1]);
  34. const typedArr = new Int32Array(3);
  35. const typedArr2 = new Int32Array(3);
  36. typedArr[0] = 5;
  37. typedArr[1] = 6;
  38. typedArr[2] = 3;
  39. typedArr2[0] = 5;
  40. typedArr2[1] = 6;
  41. typedArr2[2] = 3;
  42. testFlat(typedArr, typedArr2);
  43. }
  44. },
  45. {
  46. name : "flatMap arrays",
  47. body : function ()
  48. {
  49. testFlatMap([2, 3, 4, 5], [2, 4, 3, 6, 4, 8, 5, 10], function (a) { return [a, a * 2]});
  50. const thisArg = { count : 0 };
  51. testFlatMap([2, 3, 4], [2, 3, 3, 4, 4, 5], function (a) { this.count += a; return [ a, a + 1]}, thisArg);
  52. testFlatMap([2, 3, 4], [[2], [3], [4]], function (a) { return [[a]]});
  53. assert.areEqual(9, thisArg.count);
  54. assert.throws(()=>{[2, 3].flatMap("Not Callable")});
  55. assert.throws(()=>{[2, 3].flatMap(class NotCallable {})});
  56. }
  57. },
  58. {
  59. name : "flatMap abnormal this",
  60. body : function ()
  61. {
  62. "use strict";
  63. testFlatMap([2, 3], [null, null], function () { return [this]}, null);
  64. testFlatMap([2, 3], [undefined, undefined], function () { return [this]}, undefined);
  65. testFlatMap([2, 3], [undefined, undefined], function () { return [this]});
  66. testFlatMap([2, 3], ["", ""], function () { return [this]}, "");
  67. testFlatMap([2, 3], ["Test", "Test"], function () { return [this]}, "Test");
  68. const boo = {};
  69. testFlatMap([2, 3], [boo, boo], function () { return [this]}, boo);
  70. }
  71. },
  72. {
  73. name : "Proxied Array",
  74. body : function ()
  75. {
  76. let getCount = 0, hasCount = 0;
  77. const handler = {
  78. get : function (t, p, r) { ++getCount; return Reflect.get(t, p, r); },
  79. has : function (t, p, r) { ++hasCount; return Reflect.has(t, p, r); }
  80. }
  81. const prox = new Proxy ([2, [3, 5]], handler);
  82. testFlat(prox, [2, 3, 5]);
  83. assert.areEqual(4, getCount); // length and constructor are also accessed hence count 2 higher than length
  84. assert.areEqual(2, hasCount);
  85. const prox2 = new Proxy ([2, 3, 5], handler);
  86. testFlatMap(prox2, [2, 4, 3, 6, 5, 10], function (a) { return [a, a * 2]});
  87. assert.areEqual(9, getCount); // length and constructor are also accessed hence count 2 higher than length
  88. assert.areEqual(5, hasCount);
  89. }
  90. },
  91. {
  92. name : "Invalid object",
  93. body : function ()
  94. {
  95. assert.throws(() => {Array.prototype.flat.call(null)}, TypeError);
  96. assert.throws(() => {Array.prototype.flat.call(undefined)}, TypeError);
  97. assert.throws(() => {Array.prototype.flatMap.call(null)}, TypeError);
  98. assert.throws(() => {Array.prototype.flatMap.call(undefined)}, TypeError);
  99. }
  100. }
  101. ];
  102. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });