DeferParseMethods.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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: "Various object literal method deferral",
  9. body: function () {
  10. var sym = Symbol();
  11. let out = 'nothing';
  12. var obj = {
  13. get a() { return 'get a'; },
  14. set a(v) { out = 'set a'; },
  15. b() { return 'b'; },
  16. ['c']() { return 'c'; },
  17. [sym]() { return 'sym'; },
  18. async d() { return 'd'; },
  19. *e() { yield 'e'; },
  20. get ['f']() { return 'get f'; },
  21. set ['f'](v) { out = 'set f'; },
  22. async ['g']() { return 'g'; },
  23. *['h']() { yield 'h'; },
  24. async async() { return 'async async'; },
  25. }
  26. var obj2 = {
  27. async() { return 'async'; }
  28. }
  29. var obj3 = {
  30. get async() { return 'get async'; },
  31. set async(v) { out = 'set async'; }
  32. }
  33. var obj4 = {
  34. *async() { yield 'generator async'; }
  35. }
  36. assert.areEqual('get a', obj.a, "Simple named getter");
  37. obj.a = 123;
  38. assert.areEqual('set a', out, "Simple named setter");
  39. assert.areEqual('b', obj.b(), "Simple method");
  40. assert.areEqual('c', obj.c(), "Method with computed property name");
  41. assert.areEqual('sym', obj[sym](), "Method with computed property name (key is not string)");
  42. assert.isTrue(obj.d() instanceof Promise, "Async method");
  43. assert.areEqual('e', obj.e().next().value, "Generator method");
  44. assert.areEqual('get f', obj.f, "Getter method with computed name");
  45. obj.f = 123;
  46. assert.areEqual('set f', out, "Setter method with computed name");
  47. assert.isTrue(obj.g() instanceof Promise, "Async method with computed name");
  48. assert.areEqual('h', obj.h().next().value, "Generator method with computed name");
  49. assert.isTrue(obj.async() instanceof Promise, "Async method named async");
  50. assert.areEqual('async', obj2.async(), "Method named async");
  51. assert.areEqual('get async', obj3.async, "Getter named async");
  52. obj3.async = 123;
  53. assert.areEqual('set async', out, "Setter named async");
  54. assert.areEqual('generator async', obj4.async().next().value, "Generator method named async");
  55. }
  56. },
  57. {
  58. name: "Uncommon object literal method name types",
  59. body: function() {
  60. var out = 'nothing';
  61. var obj = {
  62. "s1"() { return "s1"; },
  63. async "s2"() { return "s2"; },
  64. * "s3"() { return "s3"; },
  65. get "s4"() { return "s4"; },
  66. set "s4"(v) { out = "s4"; },
  67. 0.1() { return 0.1; },
  68. async 0.2() { return 0.2; },
  69. * 0.3() { return 0.3; },
  70. get 0.4() { return 0.4; },
  71. set 0.4(v) { out = 0.4; },
  72. 123() { return 123; },
  73. async 456() { return 456; },
  74. * 789() { yield 789; },
  75. get 123456() { return 123456; },
  76. set 123456(v) { out = 123456; },
  77. while() { return "while"; },
  78. async else() { return "else"; },
  79. * if() { return "if"; },
  80. get catch() { return "catch"; },
  81. set catch(v) { out = "catch"; },
  82. }
  83. assert.areEqual('s1', obj.s1(), "Method with string name");
  84. assert.areEqual(0.1, obj[0.1](), "Method with float name");
  85. assert.areEqual(123, obj[123](), "Method with numeric name");
  86. assert.areEqual('while', obj.while(), "Method with keyword name");
  87. assert.isTrue(obj.s2() instanceof Promise, "Async method with string name");
  88. assert.isTrue(obj[0.2]() instanceof Promise, "Async method with float name");
  89. assert.isTrue(obj[456]() instanceof Promise, "Async method with numeric name");
  90. assert.isTrue(obj.else() instanceof Promise, "Async method with keyword name");
  91. assert.areEqual('s3', obj.s3().next().value, "Generator method with string name");
  92. assert.areEqual(0.3, obj[0.3]().next().value, "Generator method with float name");
  93. assert.areEqual(789, obj[789]().next().value, "Generator method with numeric name");
  94. assert.areEqual('if', obj.if().next().value, "Generator method with keyword name");
  95. assert.areEqual('s4', obj.s4, "Getter method with string name");
  96. assert.areEqual(0.4, obj[0.4], "Getter method with float name");
  97. assert.areEqual(123456, obj[123456], "Getter method with numeric name");
  98. assert.areEqual('catch', obj.catch, "Getter method with keyword name");
  99. obj.s4 = 123
  100. assert.areEqual('s4', out, "Setter method with string name");
  101. obj[0.4] = 123
  102. assert.areEqual(0.4, out, "Setter method with float name");
  103. obj[123456] = 123
  104. assert.areEqual(123456, out, "Setter method with numeric name");
  105. obj.catch = 123
  106. assert.areEqual('catch', out, "Setter method with keyword name");
  107. }
  108. },
  109. {
  110. name: "Regular function nested in a deferred method",
  111. body: function() {
  112. var obj = {
  113. m() {
  114. function foo() { return 'foo'; }
  115. return foo();
  116. }
  117. }
  118. assert.areEqual('foo', obj.m(), "Regular function nested in a deferred method should not be parsed as a method");
  119. }
  120. },
  121. {
  122. name: "Method with 'super' capture",
  123. body: function() {
  124. var obj = {
  125. m() { return () => super.bar(); }
  126. }
  127. Object.setPrototypeOf(obj, { bar() { return this; } });
  128. assert.areEqual(obj, obj.m()(), "Method should call lambda should call super method should return this captured from obj.m");
  129. }
  130. },
  131. {
  132. name: "Async lambda with parens",
  133. body: function() {
  134. var a = async() => { };
  135. assert.isTrue(a() instanceof Promise, "Async lambda with parens around formal parameters")
  136. }
  137. }
  138. ]
  139. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });