map_basic.js 8.1 KB

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