proxyTrapConsumeNewTarget.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. name: "Proxy construct trap consumes new.target",
  8. body: function() {
  9. let result = "";
  10. class A {
  11. constructor() {
  12. assert.areEqual(B, new.target, "The whole point of the test is to make sure new.target flow through proxy!");
  13. result += "A";
  14. }
  15. }
  16. var proxyObject = new Proxy(A, {
  17. construct: function(target, argumentsList, newTarget) {
  18. result += "proxyObject";
  19. assert.areEqual(A, target, "A is the target in this case");
  20. assert.areEqual(0, argumentsList.length, "No arguments are passed");
  21. assert.areEqual(B, newTarget, "B is also the new.target in this case");
  22. return Reflect.construct(target, argumentsList, newTarget);
  23. }
  24. });
  25. class B extends proxyObject {
  26. constructor() {
  27. result += "B";
  28. super();
  29. }
  30. }
  31. new B();
  32. assert.areEqual("BproxyObjectA", result, "Test indeed ran the code I expect it to");
  33. }
  34. }, {
  35. name: "Proxy construct trap consumes overridden new.target",
  36. body: function() {
  37. let testCompleted = false;
  38. function MyNewTarget() {
  39. assert.isTrue(false, "We should not be creating instance of MyNewTarget");
  40. }
  41. function MyConstructor() {
  42. assert.areEqual(MyNewTarget, new.target, "myNewTarget is overridden in this case");
  43. testCompleted = true;
  44. }
  45. Reflect.construct(MyConstructor, [], MyNewTarget);
  46. assert.isTrue(testCompleted, "Test indeed ran the code I expect it to");
  47. }
  48. }, {
  49. name: "Proxy construct trap spread case",
  50. body: function() {
  51. let result = "";
  52. function MyConstructor() {
  53. assert.areEqual(proxyObject, new.target, "myNewTarget is overridden in this case");
  54. result += "MyConstructor";
  55. }
  56. var proxyObject = new Proxy(MyConstructor, {
  57. construct: function(target, argumentsList, newTarget) {
  58. result += "proxyObject";
  59. assert.areEqual(4, argumentsList.length, "spreaded arguments count should be right");
  60. assert.areEqual(1, argumentsList[0], "spreaded arguments[0] should be right");
  61. assert.areEqual(2.25, argumentsList[1], "spreaded arguments[1] should be right");
  62. assert.areEqual(undefined, argumentsList[2], "spreaded arguments[2] should be right");
  63. assert.areEqual('hello', argumentsList[3], "spreaded arguments[3] should be right");
  64. return Reflect.construct(target, argumentsList, newTarget);
  65. }
  66. });
  67. var args = [1, 2.25, undefined, 'hello'];
  68. var newProxyObject = new proxyObject(...args);
  69. assert.areEqual("proxyObjectMyConstructor", result, "Test indeed ran the code I expect it to");
  70. }
  71. }];
  72. testRunner.runTests(tests, {
  73. verbose: WScript.Arguments[0] != "summary"
  74. });