ES6ArrayUseConstructor_v5.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // Disabling ES6 Array builtins using this['constructor'] property to construct their return values
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. var tests = [
  8. {
  9. name: "Array.prototype.concat",
  10. body: function () {
  11. var arr = ['a','b','c'];
  12. arr['constructor'] = Number;
  13. var out = Array.prototype.concat.call(arr, [1,2,3]);
  14. assert.isTrue(Array.isArray(out), "Return from Array.prototype.concat should be an Array object");
  15. assert.isFalse(out instanceof Number, "Return from Array.prototype.concat should not have been constructed from Number");
  16. assert.areEqual(6, out.length, "Array.prototype.concat sets the length property of returned object");
  17. }
  18. },
  19. {
  20. name: "Array.prototype.filter",
  21. body: function () {
  22. var arr = ['a','b','c'];
  23. arr['constructor'] = Number;
  24. var out = Array.prototype.filter.call(arr, function() { return true; });
  25. assert.isTrue(Array.isArray(out), "Return from Array.prototype.filter should be an Array object");
  26. assert.isFalse(out instanceof Number, "Return from Array.prototype.filter should not have been constructed from Number");
  27. assert.areEqual(3, out.length, "Array.prototype.filter does not set the length property of returned object, but it is Array.");
  28. }
  29. },
  30. {
  31. name: "Array.prototype.map",
  32. body: function () {
  33. var arr = ['a','b','c'];
  34. arr['constructor'] = Number;
  35. var out = Array.prototype.map.call(arr, function(val) { return val; });
  36. assert.isTrue(Array.isArray(out), "Return from Array.prototype.map should be an Array object");
  37. assert.isFalse(out instanceof Number, "Return from Array.prototype.map should not have been constructed from Number");
  38. assert.areEqual(3, out.length, "Array.prototype.map does not set the length property of returned object, but it is Array.");
  39. }
  40. },
  41. {
  42. name: "Array.prototype.slice",
  43. body: function () {
  44. var arr = ['a','b','c'];
  45. arr['constructor'] = Number;
  46. var out = Array.prototype.slice.call(arr);
  47. assert.isTrue(Array.isArray(out), "Return from Array.prototype.slice should be an Array object");
  48. assert.isFalse(out instanceof Number, "Return from Array.prototype.slice should not have been constructed from Number");
  49. assert.areEqual(3, out.length, "Array.prototype.slice sets the length property of returned object");
  50. }
  51. },
  52. {
  53. name: "Array.prototype.splice",
  54. body: function () {
  55. var arr = ['a','b','c','d','e','f'];
  56. arr['constructor'] = Number;
  57. var out = Array.prototype.splice.call(arr, 0, 3);
  58. assert.isTrue(Array.isArray(out), "Return from Array.prototype.splice should be an Array object");
  59. assert.isFalse(out instanceof Number, "Return from Array.prototype.splice should not have been constructed from Number");
  60. assert.areEqual(3, out.length, "Array.prototype.splice sets the length property of returned object");
  61. }
  62. },
  63. ];
  64. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });