ES6Proto.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. // Tests for Object.setPrototypeOf and Object#__proto__ ES6 behavior
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. var pd = Object.getOwnPropertyDescriptor(Object.prototype, '__proto__');
  8. var __proto__set = pd.set;
  9. var __proto__get = pd.get;
  10. var tests = [
  11. {
  12. name: "Sanity base cases",
  13. body: function() {
  14. assert.areEqual('function', typeof __proto__set, "Object#__proto__ is an accessor property with set method");
  15. assert.areEqual('function', typeof __proto__get, "Object#__proto__ is an accessor property with get method");
  16. }
  17. },
  18. {
  19. name: "Error conditions for Object#__proto__",
  20. body: function () {
  21. assert.throws(function() { __proto__set.call(); }, TypeError, "set Object#__proto__ throws if this argument is not passed", "Object.prototype.__proto__: 'this' is not an Object");
  22. assert.throws(function() { __proto__set.call(undefined); }, TypeError, "set Object#__proto__ throws if this argument is undefined", "Object.prototype.__proto__: 'this' is not an Object");
  23. assert.throws(function() { __proto__set.call(null); }, TypeError, "set Object#__proto__ throws if this argument is null", "Object.prototype.__proto__: 'this' is not an Object");
  24. assert.throws(function() { __proto__get.call(); }, TypeError, "get Object#__proto__ throws if this argument is not passed", "Object.prototype.__proto__: 'this' is not an Object");
  25. assert.throws(function() { __proto__get.call(undefined); }, TypeError, "get Object#__proto__ throws if this argument is undefined", "Object.prototype.__proto__: 'this' is not an Object");
  26. assert.throws(function() { __proto__get.call(null); }, TypeError, "get Object#__proto__ throws if this argument is null", "Object.prototype.__proto__: 'this' is not an Object");
  27. }
  28. },
  29. {
  30. name: "Cases where Object#__proto__ shouldn't change [[Prototype]]",
  31. body: function () {
  32. var p = {};
  33. var o = Object.create(p);
  34. assert.areEqual(undefined, __proto__set.call(o), "set Object#__proto__ returns undefined if the proto argument is not passed");
  35. assert.areEqual(p, __proto__get.call(o), "[[Prototype]] slot of o was not changed");
  36. assert.areEqual(undefined, __proto__set.call(o, undefined), "set Object#__proto__ returns undefined if the proto argument is undefined");
  37. assert.areEqual(p, __proto__get.call(o), "[[Prototype]] slot of o was not changed");
  38. assert.areEqual(undefined, __proto__set.call(o, 5), "set Object#__proto__ returns undefined if the proto argument is non-object");
  39. assert.areEqual(p, __proto__get.call(o), "[[Prototype]] slot of o was not changed");
  40. var n = 5;
  41. assert.areEqual(undefined, __proto__set.call(n, {}), "set Object#__proto__ returns undefined if the this argument is non-object when proto argument is supplied");
  42. assert.areEqual(Number.prototype, __proto__get.call(n), "[[Prototype]] slot of n was not changed");
  43. }
  44. },
  45. {
  46. name: "Simple validation of Object#__proto__",
  47. body: function () {
  48. var p = {};
  49. var o = Object.create(p);
  50. assert.areEqual(undefined, __proto__set.call(o, null), "set Object#__proto__ returns undefined if the proto argument is null");
  51. assert.areEqual(null, __proto__get.call(o), "[[Prototype]] slot of o was changed to null");
  52. assert.areEqual(undefined, __proto__set.call(o, p), "set Object#__proto__ returns undefined if the proto argument is object");
  53. assert.areEqual(p, __proto__get.call(o), "[[Prototype]] slot of o was changed to p");
  54. }
  55. },
  56. {
  57. name: "Error conditions for Object.setPrototypeOf/getPrototypeOf",
  58. body: function () {
  59. assert.throws(function() { Object.setPrototypeOf(); }, TypeError, "Object.setPrototypeOf throws when called with no arguments", "Object.setPrototypeOf: argument is not an Object");
  60. assert.throws(function() { Object.setPrototypeOf(undefined); }, TypeError, "Object.setPrototypeOf throws when object argument is undefined", "Object.setPrototypeOf: argument is not an Object");
  61. assert.throws(function() { Object.setPrototypeOf(null); }, TypeError, "Object.setPrototypeOf throws when object argument is null", "Object.setPrototypeOf: argument is not an Object");
  62. assert.throws(function() { Object.setPrototypeOf({}); }, TypeError, "Object.setPrototypeOf throws when proto is not passed", "Object.setPrototypeOf: argument is not an Object and is not null");
  63. assert.throws(function() { Object.setPrototypeOf({}, undefined); }, TypeError, "Object.setPrototypeOf throws when proto is undefined", "Object.setPrototypeOf: argument is not an Object and is not null");
  64. assert.throws(function() { Object.setPrototypeOf({}, 5); }, TypeError, "Object.setPrototypeOf throws when proto is not object", "Object.setPrototypeOf: argument is not an Object and is not null");
  65. assert.throws(function() { Object.getPrototypeOf(); }, TypeError, "Object.getPrototypeOf throws when argument is not passed", "Object.getPrototypeOf: argument is not an Object");
  66. assert.throws(function() { Object.getPrototypeOf(undefined); }, TypeError, "Object.getPrototypeOf throws when argument is undefined", "Object.getPrototypeOf: argument is not an Object");
  67. assert.throws(function() { Object.getPrototypeOf(null); }, TypeError, "Object.getPrototypeOf throws when argument is null", "Object.getPrototypeOf: argument is not an Object");
  68. }
  69. },
  70. {
  71. name: "Object.setPrototypeOf used on non-object argument doesn't change [[Prototype]]",
  72. body: function () {
  73. var n = 5;
  74. assert.areEqual(5, Object.setPrototypeOf(n, {}), "Object.setPrototypeOf returns first argument if argument is non-object when proto argument is supplied");
  75. assert.areEqual(Number.prototype, Object.getPrototypeOf(n), "[[Prototype]] slot of n was not changed");
  76. }
  77. },
  78. {
  79. name: "Simple validation of Object.setPrototypeOf",
  80. body: function () {
  81. var p = {};
  82. var o = Object.create(p);
  83. assert.areEqual(o, Object.setPrototypeOf(o, null), "Object.setPrototypeOf returns o if the proto argument is null");
  84. assert.areEqual(null, Object.getPrototypeOf(o), "[[Prototype]] slot of o was changed to null");
  85. assert.areEqual(o, Object.setPrototypeOf(o, p), "Object.setPrototypeOf returns o if the proto argument is object");
  86. assert.areEqual(p, Object.getPrototypeOf(o), "[[Prototype]] slot of o was changed to p");
  87. }
  88. },
  89. ];
  90. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });