array_includes.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // ES6 Array.prototype.includes(x,y) API extension tests -- verifies the API shape and basic functionality
  6. if (this.WScript && this.WScript.LoadScriptFile) { // Check for running in ch
  7. this.WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  8. }
  9. var tests = [
  10. {
  11. name: "Array.prototype.includes range test cases",
  12. body: function () {
  13. var x = [1, 2, 2, 4, 5, 0, NaN, 0, true, true , false, undefined, 1.1, 2.4]
  14. for(var i=0; i < x.length; i++)
  15. {
  16. assert.isTrue(x.includes(x[i]), "includes returns true for valid search values in the array range including NaN and undefined")
  17. }
  18. assert.isTrue(x.includes(-0), "includes treats -0 and +0 as same");
  19. }
  20. },
  21. {
  22. name: "Array.prototype.includes out of range test cases",
  23. body: function () {
  24. var x = [1, 2, 2, 4, 5, 0, NaN, 0, true, true , false, undefined, 1.1, 2.4]
  25. assert.isFalse(x.includes(1, 1), "includes(1, 1): out of range values should return false");
  26. assert.isFalse(x.includes(-0, 10), "includes(-0, 10): out of range values should return false");
  27. assert.isFalse(x.includes(undefined, x.length - 2), "includes(undefined, x.length - 2): out of range values should return false");
  28. assert.isFalse(x.includes(10), "includes(10): out of range values should return false");
  29. assert.isFalse(x.includes(null), "includes(null): out of range values should return false");
  30. }
  31. },
  32. {
  33. name: "Array.prototype.includes works with native arrays",
  34. body: function () {
  35. var x = [1, 2, 2, 4, 5, 0]; //native int array
  36. assert.isTrue(x.includes(2), "includes(2): includes return true for search hits");
  37. assert.isTrue(x.includes(0), "includes(0): includes return true for search hits");
  38. assert.isFalse(x.includes(3), "includes(3): includes return false for search miss");
  39. assert.isFalse(x.includes(1.2), "includes(1.2): includes return false for search miss");
  40. assert.isFalse(x.includes(undefined), "includes(undefined): includes return false for search miss");
  41. assert.isTrue(x.includes(2, -5), "includes(2, -5): includes return true for search hit");
  42. assert.isFalse(x.includes(2, -1), "includes(2, -1): includes return false for search miss");
  43. assert.isTrue(x.includes(-0), "includes(-0): includes return true for search hit");
  44. var x = [1,2, 1.2, 2.3, -2.8, 4, 5, 0]; //native float array
  45. assert.isTrue(x.includes(2.3), "includes(2.3): includes return true for search hits");
  46. assert.isTrue(x.includes(0), "includes(0): includes return true for search hits");
  47. assert.isFalse(x.includes(-2.9), "includes(-2.9): includes return false for search miss");
  48. assert.isTrue(x.includes(1.2), "includes(1.2): includes return false for search miss");
  49. assert.isFalse(x.includes(undefined), "includes(undefined): includes return false for search miss");
  50. assert.isTrue(x.includes(2.3, -5), "includes(2.3, -5): includes return true for search hit");
  51. assert.isFalse(x.includes(2, -1), "includes(2, -1): includes return false for search miss");
  52. assert.isTrue(x.includes(-0), "includes(-0): includes return true for search hit");
  53. assert.isTrue(x.includes(-0, -200), "includes(-0, -200): includes return true for search hit");
  54. assert.isFalse(x.includes(2, 100), "includes(2, 100): includes return true for search hit");
  55. }
  56. },
  57. {
  58. name: "Array.prototype.includes works with missing elements in arrays",
  59. body: function () {
  60. var x = [1, 2, 2, 4, 5, 0]; //native int array
  61. x[1000] = 25;
  62. assert.isTrue(x.includes(undefined), "includes(undefined): includes return true for search hit");
  63. var x = [1,2, 1.2, 2.3, -2.8, 4, 5, 0]; //native float array
  64. x[1000] = 25.5;
  65. assert.isTrue(x.includes(undefined), "includes(undefined): includes return true for search hit");
  66. var x = [ 1, 2, -0, "x"];
  67. x[1000] = 25.5;
  68. assert.isTrue(x.includes(undefined), "includes(undefined): includes return true for search hit");
  69. }
  70. },
  71. {
  72. name: "Array.prototype.includes walks prototype with missing elements in arrays",
  73. body: function () {
  74. //implicit calls
  75. var marker = false;
  76. var arr = [10];
  77. Object.defineProperty(Array.prototype, "4", {configurable : true, get: function(){return 30;}});
  78. arr.length = 6;
  79. assert.isTrue(arr.includes(30), "includes(30): includes successful in searching prototype values");
  80. assert.isTrue(arr.includes(undefined), "includes(undefined): includes return true for search hit invoking prototype");
  81. arr = [10.1];
  82. arr.length = 6;
  83. assert.isTrue(arr.includes(30), "includes(30): includes successful in searching prototype values");
  84. assert.isTrue(arr.includes(undefined), "includes(undefined): includes return true for search hit invoking prototype");
  85. assert.isTrue(arr.includes(30, 2), "includes(30, 2): includes successful in searching prototype values");
  86. assert.isTrue(arr.includes(undefined, 4), "includes(undefined, 4): includes return true for search hit invoking prototype");
  87. arr = ["x"];
  88. arr.length = 6;
  89. assert.isTrue(arr.includes(30), "includes(30): includes successful in searching prototype values");
  90. assert.isTrue(arr.includes(undefined), "includes(undefined): includes return true for search hit invoking prototype");
  91. assert.isTrue(arr.includes(30, -4), "includes(30, -4): includes successful in searching prototype values");
  92. assert.isTrue(arr.includes(undefined, -2), "includes(undefined, -2): includes return true for search hit invoking prototype");
  93. }
  94. },
  95. {
  96. name: "Array.prototype.includes built-in length is 1",
  97. body: function () {
  98. assert.areEqual(1, [].includes.length, "includes built-in length is 1");
  99. }
  100. },
  101. {
  102. name: "Array.prototype.includes built-in works for object",
  103. body: function () {
  104. var b = function(){};
  105. b.prototype = Array.prototype;
  106. var y = new b();
  107. var a = {};
  108. y[0] = "abc";
  109. y[1] = "def";
  110. y[2] = "efg";
  111. y[3] = true;
  112. y[4] = true;
  113. y[5] = false;
  114. y[6] = a;
  115. y[7] = a;
  116. y[8] = null;
  117. y[9] = NaN;
  118. y.length = 11;
  119. assert.isTrue(y.includes("abc"), "includes('abc'): includes return true for search hit");
  120. assert.isFalse(y.includes("abc", 3), "includes('abc', 3): includes return false for search miss");
  121. assert.isFalse(y.includes("abc", 2), "includes('abc', 2): includes return false for search miss");
  122. assert.isFalse(y.includes("abc", -2), "includes('abc', -2): includes return false for search miss");
  123. assert.isFalse(y.includes("xyg"), "includes('xyg'): includes return false for search miss");
  124. assert.isFalse(y.includes("", -2), "includes('', -2): includes return false for search miss");
  125. assert.isFalse(y.includes(new Boolean(true)), "includes(new Boolean(true)): includes return false for search miss");
  126. assert.isTrue(y.includes(NaN), "includes(NaN): includes return true for search hit");
  127. assert.isTrue(y.includes(undefined), "includes(undefined):includes return true for search hit");
  128. }
  129. },
  130. {
  131. name: "Array.prototype.includes with proxy to validate that has is not called",
  132. body: function () {
  133. var calls = 0;
  134. var p = new Proxy({}, {
  135. get : function(_, k) {
  136. if (k == 'length') {
  137. return 4;
  138. }
  139. calls++
  140. return k*2;
  141. }
  142. });
  143. var a = [].includes.call(p, 100);
  144. assert.areEqual(calls, 4, "Even though 'has' is not there get will be called 4 times");
  145. }
  146. },
  147. ];
  148. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });