ScriptFunctionToStrings.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. if (this.WScript && this.WScript.LoadScriptFile) { // Check for running in ch
  6. this.WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. }
  8. var tests = [
  9. {
  10. name: "function declaration test",
  11. body: function ()
  12. {
  13. var s = "function \n\t\r foo() {var a = 5;}";
  14. eval(s);
  15. assert.areEqual(s, foo.toString(), "Function.toString should preserve original formatting");
  16. }
  17. },
  18. {
  19. name: "function assignment test",
  20. body: function ()
  21. {
  22. var s = "function \t\n\r\t foo() {var a = 5;}";
  23. eval("var a = " + s);
  24. assert.areEqual(s, a.toString(), "Function.toString should preserve original formatting");
  25. a = function (i) { i++; }
  26. assert.areEqual("function (i) { i++; }", a.toString(), "toString should add a space if one does not exist");
  27. }
  28. },
  29. {
  30. name: "generator function declaration test",
  31. body: function ()
  32. {
  33. var s = "function* \t\r\n foo() {var a = 5;}";
  34. eval(s);
  35. assert.areEqual(s, foo.toString(), "Function.toString should preserve original formatting");
  36. s = "function \t\r\n*\n\r\n \t foo() {var a = 5;}";
  37. eval(s);
  38. assert.areEqual(s, foo.toString(), "Function.toString should preserve original formatting");
  39. }
  40. },
  41. {
  42. name: "generator function assignment test",
  43. body: function ()
  44. {
  45. var s = "function* \t\n\r \t foo() {var a = 5;}";
  46. eval("var a = " + s);
  47. assert.areEqual(s, a.toString(), "Function.toString should preserve original formatting");
  48. s = "function \t\n\r * \t\n foo() {var a = 5;}";
  49. eval("var a = " + s);
  50. assert.areEqual(s, a.toString(), "Function.toString should preserve original formatting");
  51. }
  52. },
  53. {
  54. name: "Named function expression tests",
  55. body: function ()
  56. {
  57. var s1 = "function \n\t bar \t () {}";
  58. var s2 = "function \t qui \t () {}";
  59. eval("var o = { foo : " + s1 + "}");
  60. eval("o.e = " + s2);
  61. assert.areEqual(s1, o.foo.toString(), "confirm that the foo identifier does not override the name bar ");
  62. assert.areEqual(s2, o.e.toString(), "confirm that the foo identifier does not override the name qui");
  63. }
  64. },
  65. {
  66. name: "function expression tests without names",
  67. body: function ()
  68. {
  69. var s1 = "function \n\t \t () {}";
  70. var s2 = "function \t \t () {}";
  71. eval("var o = { foo : " + s1 + "}");
  72. eval("o.e = " + s2);
  73. assert.areEqual(s1, o.foo.toString(), "confirm that the foo identifier does not override the name bar ");
  74. assert.areEqual(s2, o.e.toString(), "confirm that the foo identifier does not override the name qui");
  75. }
  76. },
  77. {
  78. name: "internal function test",
  79. body: function ()
  80. {
  81. var s = "function foo() { return foo.toString(); }";
  82. eval(s);
  83. var a = foo;
  84. assert.areEqual(s, a(), "confirm that even if we call toString internally it has no effect on the name")
  85. }
  86. },
  87. {
  88. name: "class method test",
  89. body: function ()
  90. {
  91. eval("var qux = class { constructor(){} static func(){} method(){} get getter(){} set setter(v){}}");
  92. var quxObj = new qux();
  93. assert.areEqual("func(){}", qux.func.toString(), "the name should be func")
  94. assert.areEqual("method(){}", quxObj.method.toString(), "the name should be method")
  95. var oGet = Object.getOwnPropertyDescriptor(qux.prototype, "getter");
  96. var oSet = Object.getOwnPropertyDescriptor(qux.prototype, "setter");
  97. assert.areEqual("getter(){}", oGet.get.toString(), "the name should be getter");
  98. assert.areEqual("setter(v){}", oSet.set.toString(), "the name should be setter");
  99. }
  100. },
  101. {
  102. name: "class constructor test",
  103. body: function ()
  104. {
  105. // [19.2.3.5] Function.prototype.toString()
  106. // The string representation must have the syntax of a FunctionDeclaration, FunctionExpression, GeneratorDeclaration,
  107. // GeneratorExpression, AsyncFunctionDeclaration, AsyncFunctionExpression, ClassDeclaration, ClassExpression, ArrowFunction,
  108. // AsyncArrowFunction, or MethodDefinition depending upon the actual characteristics of the object.
  109. eval("var qux = class { constructor(){} static func(){} method(){} get getter(){} set setter(v){}}");
  110. var quxObj = new qux();
  111. assert.areEqual("class { constructor(){} static func(){} method(){} get getter(){} set setter(v){}}",
  112. quxObj.constructor.toString(), "The string representation must have the syntax of a ClassExpression");
  113. var qux = class { };
  114. var quxObj = new qux();
  115. assert.areEqual("class { }", quxObj.constructor.toString(), "The string representation must have the syntax of a ClassDeclaration")
  116. }
  117. },
  118. {
  119. name: "shorthand method function test",
  120. body: function ()
  121. {
  122. var o = { ['f']() { }, g() { } };
  123. assert.areEqual("['f']() { }", o.f.toString());
  124. }
  125. },
  126. {
  127. name: "arrow function Test",
  128. body: function ()
  129. {
  130. var arrowDecl = () => { };
  131. assert.areEqual("() => { }", arrowDecl.toString(), "Make sure arrow functions remain unaffected by ie12 formatting");
  132. }
  133. }
  134. ];
  135. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });