array_map.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. if (this.WScript && this.WScript.LoadScriptFile) { // Check for running in ch
  6. this.WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. }
  8. var tests = [
  9. {
  10. name: "22.1.3.16: Array.prototype.map basic case",
  11. body: function () {
  12. const a = [5, 6, 7];
  13. let transform = function (value, index, obj) {
  14. assert.areEqual(obj[index], value);
  15. return value * index;
  16. };
  17. const b = a.map(transform);
  18. assert.areEqual("0,6,14", b.join(","), "transformed array");
  19. }
  20. },
  21. {
  22. name: "22.1.3.16: Array.prototype.map should skip missing items",
  23. body: function () {
  24. const a = [1, 2, 3];
  25. delete a[1];
  26. let callCount = 0;
  27. let transform = function (value, index, obj) {
  28. assert.areEqual(obj[index], value);
  29. callCount += 1;
  30. return value * 2;
  31. };
  32. let b = a.map(transform);
  33. assert.areEqual(2, callCount, "visited two items only");
  34. assert.areEqual("2,,6", b.join(","), "transformed array");
  35. }
  36. },
  37. {
  38. name: "22.1.3.16: mutating array after Array.prototype.map has started",
  39. body: function () {
  40. let a = [1, 2, 3];
  41. let callCount = 0;
  42. let transform = function (value, index, obj) {
  43. assert.areEqual(obj[index], value);
  44. callCount += 1;
  45. if (index === 0) {
  46. delete a[1]; // should be skipped
  47. a[2] = 4; // new value should be used
  48. a[4] = 5; // added items shouldn't be visited
  49. }
  50. return value * index;
  51. };
  52. let b = a.map(transform);
  53. assert.areEqual(2, callCount, "visited two items only");
  54. assert.areEqual("0,,8", b.join(","), "transformed array");
  55. assert.areEqual("1,,4,,5", a.join(","), "mutated array");
  56. }
  57. },
  58. {
  59. name: "22.1.3.16: Array.prototype.map should call ArraySpeciesCreate which relies on 'constructor' property",
  60. body: function () {
  61. const a = [1, 2, 3];
  62. Object.defineProperty(a, 'constructor', {
  63. get: function () {
  64. throw new Error("13");
  65. }
  66. });
  67. assert.throws(function () { a.map(function () { }); }, Error, "Should throw from constructor", "13");
  68. }
  69. },
  70. {
  71. name: "22.1.3.16: Array.prototype.map might provide 'this' argument to the callback",
  72. body: function () {
  73. const a = [5, 6, 7];
  74. let that = {calls: 0};
  75. let transform = function (value, index, obj) {
  76. this.calls++;
  77. return 0;
  78. };
  79. a.map(transform, that);
  80. assert.areEqual(3, that.calls, "context's 'calls' property");
  81. }
  82. },
  83. {
  84. name: "22.1.3.16: Array.prototype.map is generic and can be applied to other objects",
  85. body: function () {
  86. let a = { 0: "a", 1: "bc", 2: "" }
  87. a.length = 3;
  88. let transform = function (value, index, obj) {
  89. assert.areEqual(obj[index], value);
  90. return value + "!";
  91. };
  92. const b = Array.prototype.map.call(a, transform);
  93. assert.areEqual("a!,bc!,!", b.join(","), "transformed object");
  94. }
  95. }
  96. ];
  97. testRunner.runTests(tests, { verbose: false /*so no need to provide baseline*/ });