set_basic.js 7.8 KB

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