delete.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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: "Deleting of configurable non-indexed properties on Arrays",
  11. body: function () {
  12. var arr = [1,4,9,16];
  13. arr.non_indexed = 'whatever';
  14. Object.defineProperty(arr, 'with_getter', { get: function() { return 'with getter'; }, configurable: true });
  15. assert.areEqual('whatever', arr.non_indexed, "arr.non_indexed is set to 'whatever'");
  16. assert.areEqual('with getter', arr.with_getter, "arr.with_getter is set to 'with getter'");
  17. assert.areEqual(true, delete arr.non_indexed, "Deleting own property should succeed");
  18. assert.areEqual(undefined, arr.non_indexed, "arr.non_indexed has been deleted");
  19. assert.areEqual(true, delete arr.with_getter, "Deleting own property with a getter should succeed");
  20. assert.areEqual(undefined, arr.with_getter, "arr.with_getter has been deleted");
  21. }
  22. },
  23. {
  24. name: "Deleting of non-configurable non-indexed properties on Arrays",
  25. body: function () {
  26. var arr = [1,4,9,16];
  27. var id = 'id';
  28. Object.defineProperty(arr, id, { value: 17, configurable: false });
  29. assert.areEqual(false, delete arr[id]);
  30. assert.areEqual(17, arr[id], "arr['id'] value after failed delete");
  31. assert.throws(function () { 'use strict'; delete arr[id]; }, TypeError,
  32. "Should throw on delete of non-indexed property in array",
  33. "Calling delete on 'id' is not allowed in strict mode");
  34. }
  35. },
  36. {
  37. name: "Deleting of the 'length' property on Arrays",
  38. body: function () {
  39. var arr = [1,4,9,16];
  40. assert.areEqual(false, delete arr.length, "delete of arr.length should fail (as noop)");
  41. assert.areEqual(4, arr.length, "arr.length after attempting to delete it");
  42. assert.throws(function () { 'use strict'; delete arr.length; }, TypeError,
  43. "Should throw on delete of 'length' property in array",
  44. "Calling delete on 'length' is not allowed in strict mode");
  45. assert.areEqual(4, arr.length, "arr.length after attempting to delete it in strict mode");
  46. }
  47. },
  48. {
  49. name: "Deleting of indexed properties on Arrays",
  50. body: function () {
  51. var arr = [1,4,9,16];
  52. assert.areEqual(true, delete arr[1], "delete of arr[1] should succeed");
  53. assert.areEqual(undefined, arr[1], "arr[1] value after delete should be undefined");
  54. assert.areEqual(4, arr.length, "the array's lenght should not change");
  55. assert.areEqual(true, delete arr[42], "delete of arr[42] (beyond the array bounds) should succeed");
  56. assert.areEqual(4, arr.length, "the array's length is unchanged");
  57. const last = arr.length - 1;
  58. assert.areEqual(true, delete arr[last], "delete of last element should succeed");
  59. assert.areEqual(undefined, arr[last], "arr[last] value after delete should be undefined");
  60. assert.areEqual(4, arr.length, "the array's lenght should not change");
  61. }
  62. },
  63. {
  64. name: "Deleting of indexed properties on Arrays that are also set on prototypes",
  65. body: function () {
  66. Object.prototype[4] = "obj.proto";
  67. Array.prototype[1] = "arr.proto";
  68. var arr = [1,4,9,16,25];
  69. assert.areEqual(true, delete arr[1], "delete of arr[1] should succeed");
  70. assert.areEqual("arr.proto", arr[1], "arr[1] after deleting should be picked up from the Array prototype");
  71. assert.areEqual(true, delete arr[4], "delete of arr[4] should succeed");
  72. assert.areEqual("obj.proto", arr[4], "arr[4] after deleting should be picked up from the Object prototype");
  73. assert.areEqual(5, arr.length, "arr.length after deleting of the last element");
  74. }
  75. },
  76. {
  77. name: "Deleting of properties on frozen Arrays",
  78. body: function () {
  79. var arr = [42];
  80. arr.foo = 'fourty-two';
  81. Object.freeze(arr);
  82. // indexed property
  83. assert.areEqual(false, delete arr[0], "delete arr[0] from frozen array");
  84. assert.throws(function () { 'use strict'; delete arr[0]; }, TypeError,
  85. "Should throw on delete of non-indexed property in array",
  86. "Calling delete on '0' is not allowed in strict mode");
  87. // non-indexed property
  88. assert.areEqual(false, delete arr.foo, "delete arr.foo from frozen array");
  89. assert.throws(function () { 'use strict'; delete arr.foo; }, TypeError,
  90. "Should throw on delete of non-indexed property in array",
  91. "Calling delete on 'foo' is not allowed in strict mode");
  92. }
  93. },
  94. {
  95. name: "Deleting of indexed properties on sealed Arrays",
  96. body: function () {
  97. var arr = [42];
  98. arr.foo = 'fourty-two';
  99. Object.seal(arr);
  100. // indexed property
  101. assert.areEqual(false, delete arr[0], "delete arr[0] from sealed array");
  102. assert.throws(function () { 'use strict'; delete arr[0]; }, TypeError,
  103. "Should throw on delete of non-indexed property in array",
  104. "Calling delete on '0' is not allowed in strict mode");
  105. // non-indexed property
  106. assert.areEqual(false, delete arr.foo, "delete arr.foo from sealed array");
  107. assert.throws(function () { 'use strict'; delete arr.foo; }, TypeError,
  108. "Should throw on delete of non-indexed property in array",
  109. "Calling delete on 'foo' is not allowed in strict mode");
  110. }
  111. },
  112. ];
  113. testRunner.runTests(tests, { verbose: false /*so no need to provide baseline*/ });