FailToSetLength.js 6.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // Some of the Array.prototype built-in functions set the length property of the array object and
  6. // should throw a TypeError if setting the length property fails. Tests in this file verify that
  7. // we throw TypeError when we're supposed to.
  8. // See BLUE: 559834 for more details
  9. if (this.WScript && this.WScript.LoadScriptFile) { // Check for running in ch
  10. this.WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  11. }
  12. var tests = [
  13. {
  14. name: "Array.prototype built-in functions called with object that has length property with no setter",
  15. body: function () {
  16. var obj = { 0: 0, 1: 1, get length() { return 2; }};
  17. assert.throws(function() { Array.prototype.pop.call(obj); }, TypeError, "Array.prototype.pop throws when obj has a getter for length but is missing a setter", "Cannot define property: object is not extensible");
  18. assert.throws(function() { Array.prototype.push.call(obj, 2); }, TypeError, "Array.prototype.push throws when obj has a getter for length but is missing a setter", "Cannot define property: object is not extensible");
  19. assert.throws(function() { Array.prototype.shift.call(obj); }, TypeError, "Array.prototype.shift throws when obj has a getter for length but is missing a setter", "Cannot define property: object is not extensible");
  20. assert.throws(function() { Array.prototype.unshift.call(obj, 2); }, TypeError, "Array.prototype.unshift throws when obj has a getter for length but is missing a setter", "Cannot define property: object is not extensible");
  21. assert.throws(function() { Array.prototype.splice.call(obj, 0, 1); }, TypeError, "Array.prototype.splice throws when obj has a getter for length but is missing a setter", "Cannot define property: object is not extensible");
  22. }
  23. },
  24. {
  25. name: "Array.prototype built-in functions called with object that has length property with no setter and length property has zero value",
  26. body: function () {
  27. var obj = { 0: 0, 1: 1, get length() { return 0; }};
  28. assert.throws(function() { Array.prototype.pop.call(obj); }, TypeError, "Array.prototype.pop throws when obj has a getter for length which returns zero", "Cannot define property: object is not extensible");
  29. assert.throws(function() { Array.prototype.push.call(obj, 2); }, TypeError, "Array.prototype.push throws when obj has a getter for length which returns zero", "Cannot define property: object is not extensible");
  30. assert.throws(function() { Array.prototype.shift.call(obj); }, TypeError, "Array.prototype.shift throws when obj has a getter for length which returns zero", "Cannot define property: object is not extensible");
  31. assert.throws(function() { Array.prototype.unshift.call(obj, 2); }, TypeError, "Array.prototype.unshift throws when obj has a getter for length which returns zero", "Cannot define property: object is not extensible");
  32. assert.throws(function() { Array.prototype.splice.call(obj, 0, 1); }, TypeError, "Array.prototype.splice throws when obj has a getter for length which returns zero", "Cannot define property: object is not extensible");
  33. }
  34. },
  35. {
  36. name: "Array.prototype built-in functions called with object that has length property which is non-configurable and non-writable",
  37. body: function () {
  38. var obj = { 0: 0, 1: 1 };
  39. Object.defineProperty(obj, "length", { value: 2, writable: false, configurable: false });
  40. assert.throws(function() { Array.prototype.pop.call(obj); }, TypeError, "Array.prototype.pop throws when obj has a length property which is not writable and not configurable", "Object doesn't support this action");
  41. assert.throws(function() { Array.prototype.push.call(obj, 2); }, TypeError, "Array.prototype.push throws when obj has a length property which is not writable and not configurable", "Object doesn't support this action");
  42. assert.throws(function() { Array.prototype.shift.call(obj); }, TypeError, "Array.prototype.shift throws when obj has a length property which is not writable and not configurable", "Object doesn't support this action");
  43. assert.throws(function() { Array.prototype.unshift.call(obj, 2); }, TypeError, "Array.prototype.unshift throws when obj has a length property which is not writable and not configurable", "Object doesn't support this action");
  44. assert.throws(function() { Array.prototype.splice.call(obj, 0, 1); }, TypeError, "Array.prototype.splice throws when obj has a length property which is not writable and not configurable", "Object doesn't support this action");
  45. }
  46. },
  47. {
  48. name: "Array.prototype built-in functions called with object that has properties with index we need to set in prototype chain and property is an accessor with no setter",
  49. body: function () {
  50. var proto = {};
  51. var obj = {0:1, 1:1, 2:1, 3:-109, length:4};
  52. obj.__proto__ = proto;
  53. Object.defineProperty(proto, "4", {configurable: true, get: function() { return 31; }});
  54. assert.throws(function() { Array.prototype.unshift.call(obj, 200, 201, 202); }, TypeError, "Array.prototype.unshift throws when obj prototype-chain has a property named one of the indices we need to set which is an accessor with no setter", "Cannot define property: object is not extensible");
  55. assert.throws(function() { Array.prototype.push.call(obj, 200); }, TypeError, "Array.prototype.push throws when obj prototype-chain has a property named one of the indices we need to set which is an accessor with no setter", "Cannot define property: object is not extensible");
  56. }
  57. },
  58. ];
  59. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });