bug_issue_2747.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  6. //
  7. // Check Map
  8. //
  9. var oldSet = Map.prototype.set;
  10. var m;
  11. function constructMap () {
  12. m = new Map([["a", 1], ["b", 2]]);
  13. }
  14. Object.defineProperty(Map.prototype, "set", {
  15. get: Map.prototype.get, // can be any Map.prototype method that depends on `this` being valid
  16. configurable: true
  17. });
  18. assert.throws(function () {
  19. return Map.prototype.set;
  20. }, TypeError, "Getting Map.prototype.set with the altered getter should throw a TypeError");
  21. assert.throws(constructMap, TypeError, "Constructing a Map (uses Map.prototype.set internally) should throw a TypeError");
  22. Object.defineProperty(Map.prototype, "set", {
  23. get: function () { return oldSet; }
  24. });
  25. assert.doesNotThrow(function () {
  26. return Map.prototype.set;
  27. }, "Getting Map.prototype.set with the default set function should not throw");
  28. assert.doesNotThrow(constructMap, "Constructing a Map with the default set function should not throw");
  29. assert.doesNotThrow(function () {
  30. m.set("a", 2);
  31. }, "Inserting a new key/value pair with the default set function should not through");
  32. assert.isTrue(m.get("a") === 2, "Inserting a new key/value pair should actually insert it");
  33. //
  34. // Check Set
  35. //
  36. var oldAdd = Set.prototype.add;
  37. var s;
  38. function constructSet () {
  39. s = new Set([1, 2, 3, 2, 4, 1]);
  40. }
  41. Object.defineProperty(Set.prototype, "add", {
  42. get: Set.prototype.has, // can be any Set.prototype method that depends on `this` being valid
  43. configurable: true
  44. });
  45. assert.throws(function () {
  46. return Set.prototype.add;
  47. }, TypeError, "Getting Set.prototype.add with the altered getter should throw a TypeError");
  48. assert.throws(constructSet, TypeError, "Constructing a Set (uses Set.prototype.add internally) should throw a TypeError");
  49. Object.defineProperty(Set.prototype, "add", {
  50. get: function () { return oldAdd; }
  51. });
  52. assert.doesNotThrow(function () {
  53. return Set.prototype.add;
  54. }, "Getting Set.prototype.add with the default add function should not throw");
  55. assert.doesNotThrow(constructSet, "Constructing a Set with the default add function should not throw");
  56. assert.doesNotThrow(function () {
  57. s.add(6);
  58. }, "Inserting a new item with the default set function should not throw");
  59. assert.isTrue(s.has(6) === true, "Inserting a new item should actually insert it");
  60. WScript.Echo("pass");