weakset_basic.js 7.1 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. // Basic WeakSet tests -- verifies the API shape
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. var tests = [
  8. {
  9. name: "WeakSet is a constructor on the global object",
  10. body: function () {
  11. assert.isTrue(WeakSet !== undefined, "WeakSet should be defined");
  12. var ws1 = new WeakSet();
  13. // WeakSet is no longer allowed to be called as a function unless the object it is given
  14. // for its this argument already has the [[WeakSetData]] property on it.
  15. // TODO: When we implement @@create support, update this test to reflect it.
  16. //var ws2 = WeakSet();
  17. assert.isTrue(ws1 instanceof WeakSet, "'new WeakSet()' should create a WeakSet object");
  18. //assert.isTrue(ws2 instanceof WeakSet, "'WeakSet()' should also create a WeakSet object");
  19. //assert.isTrue(ws1 !== ws2, "Should be two different WeakSet objects");
  20. assert.areEqual(0, WeakSet.length, "WeakSet takes one optional argument and spec says length must be 0");
  21. }
  22. },
  23. {
  24. name: "WeakSet.prototype should have spec defined built-ins",
  25. body: function () {
  26. assert.isTrue(WeakSet.prototype.constructor === WeakSet, "WeakSet.prototype should have a constructor property set to WeakSet");
  27. assert.isTrue(WeakSet.prototype.hasOwnProperty('add'), "WeakSet.prototype should have a add method");
  28. assert.isTrue(WeakSet.prototype.hasOwnProperty('delete'), "WeakSet.prototype should have a delete method");
  29. assert.isTrue(WeakSet.prototype.hasOwnProperty('has'), "WeakSet.prototype should have a has method");
  30. assert.isTrue(WeakSet.prototype.add.length === 1, "add method takes two arguments");
  31. assert.isTrue(WeakSet.prototype.delete.length === 1, "delete method takes one argument");
  32. assert.isTrue(WeakSet.prototype.has.length === 1, "has method takes one argument");
  33. }
  34. },
  35. {
  36. name: "WeakSet objects' prototype should be WeakSet.prototype",
  37. body: function() {
  38. var ws1 = new WeakSet();
  39. // WeakSet is no longer allowed to be called as a function unless the object it is given
  40. // for its this argument already has the [[WeakSetData]] property on it.
  41. // TODO: When we implement @@create support, update this test to reflect it.
  42. //var ws2 = WeakSet();
  43. assert.isTrue(Object.getPrototypeOf(ws1) === WeakSet.prototype, "'new WeakSet()' should set the prototype of the returned object to WeakSet.prototype");
  44. //assert.isTrue(Object.getPrototypeOf(ws2) === WeakSet.prototype, "'WeakSet()' should set the prototype of the returned object to WeakSet.prototype");
  45. }
  46. },
  47. {
  48. name: "toString of a WeakSet object returns [object WeakSet]",
  49. body: function () {
  50. var ws = new WeakSet();
  51. assert.areEqual("[object WeakSet]", '' + ws, "toString() of map returns [object WeakSet]");
  52. }
  53. },
  54. {
  55. name: "WeakSet objects are normal extensible dynamic objects",
  56. body: function () {
  57. function countEnumerableProperties(o) {
  58. var count = 0;
  59. for (p in o) {
  60. count += 1;
  61. }
  62. return count;
  63. }
  64. var ws = new WeakSet();
  65. assert.isTrue(countEnumerableProperties(WeakSet.prototype) == 0, "Built-in methods should not be enumerable on the prototype object");
  66. assert.isTrue(countEnumerableProperties(ws) == 0, "Built-in methods should not be enumerable on an instance object");
  67. ws.foo = 10;
  68. ws.bar = 'hello';
  69. assert.isTrue(countEnumerableProperties(ws) == 2, "Should be able to add user properties");
  70. assert.isTrue(ws.foo === 10, "Property value should be set and retrieved correctly");
  71. assert.isTrue(ws.bar === 'hello', "Property value should be set and retrieved correctly");
  72. delete ws.foo;
  73. assert.isTrue(countEnumerableProperties(ws) == 1, "Should be able to delete user properties");
  74. assert.isTrue(ws.foo === undefined, "Should be able to delete user properties");
  75. }
  76. },
  77. {
  78. name: "WeakSet is subclassable",
  79. body: function () {
  80. // WeakSet is no longer allowed to be called as a function unless the object it is given
  81. // for its this argument already has the [[WeakSetData]] property on it.
  82. // TODO: When we implement @@create support, update this test to reflect it.
  83. //
  84. // For IE11 we simply throw if WeakSet() is called as a function instead of in a new expression
  85. assert.throws(function () { WeakSet.call(); }, TypeError, "WeakSet.call() throws TypeError");
  86. assert.throws(function () { WeakSet.call({ }); }, TypeError, "WeakSet.call() throws TypeError given an object");
  87. assert.throws(function () { WeakSet.call(123); }, TypeError, "WeakSet.call() throws TypeError given a number");
  88. assert.throws(function () { WeakSet.call("hello"); }, TypeError, "WeakSet.call() throws TypeError given a string");
  89. function MyWeakSet() {
  90. WeakSet.call(this);
  91. }
  92. MyWeakSet.prototype = new WeakSet();
  93. MyWeakSet.prototype.constructor = MyWeakSet;
  94. assert.throws(function () { var mymap = new MyWeakSet(); }, TypeError, "WeakSet.call(this) throws TypeError when used in the old subclassing pattern");
  95. /*
  96. function MyWeakSet() {
  97. WeakSet.call(this);
  98. }
  99. MyWeakSet.prototype = new WeakSet();
  100. MyWeakSet.prototype.constructor = MyWeakSet();
  101. var myWeakSet = new MyWeakSet();
  102. assert.isTrue(myWeakSet instanceof MyWeakSet, "Should be a MyWeakSet object");
  103. assert.isTrue(myWeakSet instanceof WeakSet, "Should also be a WeakSet object");
  104. assert.isTrue(Object.getPrototypeOf(myWeakSet) === MyWeakSet.prototype, "Should have MyWeakSet prototype");
  105. assert.isTrue(Object.getPrototypeOf(myWeakSet) !== WeakSet.prototype, "Should be distinct from WeakSet prototype");
  106. assert.isTrue(myWeakSet.delete instanceof Function, "Should have a delete method");
  107. assert.isTrue(myWeakSet.get instanceof Function, "Should have a get method");
  108. assert.isTrue(myWeakSet.has instanceof Function, "Should have a has method");
  109. assert.isTrue(myWeakSet.set instanceof Function, "Should have a set method");
  110. */
  111. }
  112. },
  113. ];
  114. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });