ie12ScriptFunctionToStrings.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. eval("function \n\t\r foo() {var a = 5;}");
  14. assert.areEqual("function foo() {var a = 5;}", foo.toString(), "toString should remove all extra whitespace, new lines, tabs and carriage return before the open (");
  15. }
  16. },
  17. {
  18. name: "function assignment test",
  19. body: function ()
  20. {
  21. eval("var a = function \t\n\r\t foo() {var a = 5;}");
  22. assert.areEqual("function foo() {var a = 5;}", a.toString(), "toString should remove all extra whitespace, new lines, tabs and carriage return before the open (");
  23. a = function(i) {i++;}
  24. assert.areEqual("function (i) {i++;}", a.toString(), "toString should add a space if one does not exist");
  25. }
  26. },
  27. {
  28. name: "generator function declaration test",
  29. body: function ()
  30. {
  31. eval("function* \t\r\n foo() {var a = 5;}");
  32. assert.areEqual("function* foo() {var a = 5;}", foo.toString(), "toString should remove all extra whitespace, new lines, tabs and carriage return before the open (");
  33. eval("function \t\r\n*\n\r\n \t foo() {var a = 5;}");
  34. assert.areEqual("function* foo() {var a = 5;}", foo.toString(), "toString should remove all extra whitespace, new lines, tabs and carriage return before the open (");
  35. }
  36. },
  37. {
  38. name: "generator function assignment test",
  39. body: function ()
  40. {
  41. eval("var a = function* \t\n\r \t foo() {var a = 5;}");
  42. assert.areEqual("function* foo() {var a = 5;}", a.toString(), "toString should remove all extra whitespace, new lines, tabs and carriage return before the open (");
  43. eval("var a = function \t\n\r * \t\n foo() {var a = 5;}");
  44. assert.areEqual("function* foo() {var a = 5;}", a.toString(), "toString should remove all extra whitespace, new lines, tabs and carriage return before the open (");
  45. }
  46. },
  47. {
  48. name: "Named function expression tests",
  49. body: function ()
  50. {
  51. eval("var o = { foo : function \n\t bar \t () {}}");
  52. eval("o.e = function \t qui \t () {}");
  53. assert.areEqual("function bar() {}",o.foo.toString(),"confirm that the foo identifier does not override the name bar ");
  54. assert.areEqual("function qui() {}",o.e.toString(),"confirm that the foo identifier does not override the name qui");
  55. }
  56. },
  57. {
  58. name: "function expression tests without names",
  59. body: function ()
  60. {
  61. eval("var o = { foo : function \n\t \t () {}}");
  62. eval("o.e = function \t \t () {}");
  63. assert.areEqual("function () {}",o.foo.toString(),"confirm that the foo identifier does not override the name bar ");
  64. assert.areEqual("function () {}",o.e.toString(),"confirm that the foo identifier does not override the name qui");
  65. }
  66. },
  67. {
  68. name: "internal function test",
  69. body: function ()
  70. {
  71. eval("function foo() { return foo.toString(); }");
  72. var a = foo;
  73. assert.areEqual("function foo() { return foo.toString(); }", a(),"confirm that even if we call toString internally it has no effect on the name")
  74. }
  75. },
  76. {
  77. name: "class method test",
  78. body: function ()
  79. {
  80. eval("var qux = class { constructor(){} static func(){} method(){} get getter(){} set setter(v){}}");
  81. var quxObj = new qux();
  82. assert.areEqual("constructor(){}",quxObj.constructor.toString(),"The constructor should have the toString with name constructor");
  83. assert.areEqual("func(){}",qux.func.toString(),"the name should be func")
  84. assert.areEqual("method(){}",quxObj.method.toString(),"the name should be method")
  85. var oGet = Object.getOwnPropertyDescriptor(qux.prototype,"getter");
  86. var oSet = Object.getOwnPropertyDescriptor(qux.prototype,"setter");
  87. assert.areEqual("getter(){}", oGet.get.toString(), "the name should be getter");
  88. assert.areEqual("setter(v){}", oSet.set.toString(), "the name should be setter");
  89. var qux = class {};
  90. var quxObj = new qux();
  91. // I left a space between closing ellipse ) and the opening bracket { because that's how all browsers do toString on runtime functions
  92. // Should this not be how other browsers do default constructor go to RuntimeCommon.h and change JS_DEFAULT_CTOR_DISPLAY_STRING
  93. assert.areEqual("constructor() {}",quxObj.constructor.toString(),"The constructor should have the toString with name constructor")
  94. }
  95. },
  96. {
  97. name: "shorthand method function test",
  98. body: function ()
  99. {
  100. var o = {['f']() {},g () {}};
  101. assert.areEqual("f() {}",o.f.toString());
  102. }
  103. },
  104. {
  105. name: "arrow function Test",
  106. body: function ()
  107. {
  108. var arrowDecl = () => {};
  109. assert.areEqual("() => {}",arrowDecl.toString(),"Make sure arrow functions remain unaffected by ie12 formatting");
  110. }
  111. }
  112. ];
  113. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });