ReflectApiTests.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. var tests = [
  7. {
  8. name: "Reflect.Set",
  9. body: function () {
  10. var receiver = {};
  11. var fn = function () { };
  12. Object.defineProperty(receiver, 'p', { set: fn });
  13. assert.isFalse(Reflect.set({}, 'p', 42, receiver), "If the receiver object already has an accessor property then [[Set]] will return false");
  14. assert.isFalse(Reflect.set({ p: 43 }, 'p', 42, receiver), "For existing accessor property on receiver [[Set]] returns false even when an own descriptor exists on the target");
  15. }
  16. },
  17. {
  18. name: "Reflect.apply API",
  19. body: function () {
  20. function foo() {}
  21. assert.throws(function() { Reflect.apply() }, TypeError, "", "Reflect.apply: argument is not a Function object");
  22. assert.throws(function() { Reflect.apply(foo) }, TypeError, "argumentsList default == undefined", "Reflect.apply: argument is not an array or array-like object");
  23. assert.throws(function() { Reflect.apply(foo, undefined) }, TypeError, "argumentsList default == undefined", "Reflect.apply: argument is not an array or array-like object");
  24. assert.throws(function() { Reflect.apply(foo, undefined, undefined) }, TypeError, "argumentsList == undefined", "Reflect.apply: argument is not an array or array-like object");
  25. assert.throws(function() { Reflect.apply(foo, undefined, null) }, TypeError, "argumentsList == null", "Reflect.apply: argument is not an array or array-like object");
  26. assert.throws(function() { Reflect.apply(foo, undefined, false) }, TypeError, "argumentsList == false", "Reflect.apply: argument is not an array or array-like object");
  27. assert.throws(function() { Reflect.apply(foo, undefined, 1) }, TypeError, "argumentsList == 1", "Reflect.apply: argument is not an array or array-like object");
  28. assert.throws(function() { Reflect.apply(foo, undefined, Number(42)) }, TypeError, "argumentsList == Number(42)", "Reflect.apply: argument is not an array or array-like object");
  29. assert.doesNotThrow(function() { Reflect.apply(foo, undefined, {}) });
  30. assert.doesNotThrow(function() { Reflect.apply(foo, undefined, []) });
  31. assert.doesNotThrow(function() { Reflect.apply(foo, undefined, new Int32Array()) }, "TypedArray should be a valid value for arguments");
  32. assert.doesNotThrow(function() { Reflect.apply(foo, undefined, new Error()) }, "Error should be a valid value for arguments");
  33. assert.doesNotThrow(function() { Reflect.apply(foo, undefined, new Proxy({}, {})) }, "Proxy should be a valid value for arguments");
  34. assert.doesNotThrow(function() { Reflect.apply(foo, undefined, new Promise(r => r())) }, "Promise should be a valid value for arguments");
  35. assert.doesNotThrow(function() { Reflect.apply(foo, undefined, new Number(42)) }, "a Number object should be a valid value for arguments");
  36. assert.doesNotThrow(function() { Reflect.apply(foo, undefined, Function("return 123")) }, "a Function object should be a valid value for arguments");
  37. assert.doesNotThrow(function() { Reflect.apply(foo, undefined, new Function("return 123")) }, "a Function object should be a valid value for arguments");
  38. }
  39. },
  40. {
  41. name: "Reflect.construct API",
  42. body: function () {
  43. function foo() {}
  44. assert.throws(function() { Reflect.construct() }, TypeError, "" ,"'target' is not a constructor");
  45. assert.throws(function() { Reflect.construct(foo) }, TypeError, "argumentsList default == undefined" ,"Reflect.construct: argument is not an array or array-like object");
  46. assert.throws(function() { Reflect.construct(foo, undefined) }, TypeError, "argumentsList == undefined" ,"Reflect.construct: argument is not an array or array-like object");
  47. assert.throws(function() { Reflect.construct(foo, null) }, TypeError, "argumentsList == null" ,"Reflect.construct: argument is not an array or array-like object");
  48. assert.throws(function() { Reflect.construct(foo, false) }, TypeError, "argumentsList == false" ,"Reflect.construct: argument is not an array or array-like object");
  49. assert.throws(function() { Reflect.construct(foo, 1) }, TypeError, "argumentsList == 1" ,"Reflect.construct: argument is not an array or array-like object");
  50. assert.throws(function() { Reflect.construct(foo, Number(42)) }, TypeError, "argumentsList == Number(42)" ,"Reflect.construct: argument is not an array or array-like object");
  51. assert.doesNotThrow(function() { Reflect.construct(foo, {}) });
  52. assert.doesNotThrow(function() { Reflect.construct(foo, []) });
  53. assert.doesNotThrow(function() { Reflect.construct(foo, [], Array) });
  54. assert.doesNotThrow(function() { Reflect.construct(foo, new Int32Array()) }, "TypedArray should be a valid value for arguments");
  55. assert.doesNotThrow(function() { Reflect.construct(foo, new Error()) }, "Error should be a valid value for arguments");
  56. assert.doesNotThrow(function() { Reflect.construct(foo, new Proxy({}, {})) }, "Proxy should be a valid value for arguments");
  57. assert.doesNotThrow(function() { Reflect.construct(foo, new Promise(r => r())) }, "Promise should be a valid value for arguments");
  58. assert.doesNotThrow(function() { Reflect.construct(foo, new Number(42)) }, "a Number object should be a valid value for arguments");
  59. assert.doesNotThrow(function() { Reflect.construct(foo, Function("return 123")) }, "a Function object should be a valid value for arguments");
  60. assert.doesNotThrow(function() { Reflect.construct(foo, new Function("return 123")) }, "a Function object should be a valid value for arguments");
  61. assert.throws(function() { Reflect.construct(Reflect.apply, undefined) }, TypeError, "" ,"'target' is not a constructor");
  62. }
  63. },
  64. {
  65. name: "Reflect.construct",
  66. body: function () {
  67. var o = {};
  68. var internPrototype;
  69. function fn() {
  70. assert.areEqual(new.target, Array, "The new.target for the invocation should be passed in as Array");
  71. this.o = o;
  72. internPrototype = Object.getPrototypeOf(this);
  73. }
  74. var result = Reflect.construct(fn, [], Array);
  75. assert.areEqual(Object.getPrototypeOf(result), Array.prototype, "Internal proto of the result object should be set according the newTarget");
  76. assert.areEqual(internPrototype, Array.prototype, "This object of the result is created from newTarget parameter and the internal proto is set from it");
  77. assert.areEqual(result.o, o, "Property set to this object during object construction should be retained");
  78. class A1 {
  79. constructor() {
  80. assert.areEqual(new.target, Array, "The new.target for the base class should be passed in as Array");
  81. assert.isTrue(this instanceof Array, "The internal proto of the base class's this should be set to the newTarget's prototype");
  82. }
  83. }
  84. var result = Reflect.construct(A1, [], Array);
  85. class A2 {}
  86. class B2 extends A2 {
  87. constructor() {
  88. assert.areEqual(new.target, Array, "The new.target for the derived class should be passed in as Array");
  89. super(); /* Initialize this */
  90. assert.isTrue(this instanceof Array, "The internal proto of the derived class's this should be set to the newTarget's prototype");
  91. }
  92. }
  93. var result = Reflect.construct(B2, [], Array);
  94. class A3 {}
  95. class B3 extends A3 {
  96. constructor() {
  97. assert.areEqual(new.target, B3, "The new.target for the derived class should be the same as the newTarget passed in to the construct call");
  98. super();
  99. assert.isTrue(this instanceof A3, "This should be properly initialized with the right base class");
  100. assert.isTrue(this instanceof B3, "This should be properly instantiated using the derived class object");
  101. }
  102. }
  103. var result = Reflect.construct(B3, [], B3);
  104. }
  105. }
  106. ];
  107. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });