supersyntax.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // Using super is invalid in many places.
  6. // These tests are generated and are intended to use to verify the restriction is done properly in various contexts
  7. // Here is the C# code used the generate these test cases
  8. /*
  9. namespace ManagedWorkspace
  10. {
  11. using System;
  12. using System.Collections.Generic;
  13. // state = 0 -> not allowed
  14. // state = 1 -> all allowed
  15. // state = 2 -> only member allowed
  16. internal static class Program
  17. {
  18. private static void Main(string[] args)
  19. {
  20. LeafNode n = new LeafNode();
  21. BranchNode rootNode = new BranchNode();
  22. BranchNode childNode = new BranchNode();
  23. BranchOut(rootNode, n);
  24. BranchOut(rootNode, childNode);
  25. BranchOut(childNode, n);
  26. int testCaseNumber = 1;
  27. foreach (var code in rootNode.Generate(0))
  28. {
  29. Console.WriteLine(GenerateTest(code, testCaseNumber));
  30. testCaseNumber++;
  31. }
  32. }
  33. private static string GenerateTest(Tuple<string, bool> testCase, int testCaseNumber)
  34. {
  35. string goodFormatString = @"assert.doesNotThrow(function () {{ WScript.LoadScript("","samethread").WScript.LoadScript('{0}'); }}, ""generated test #{1}"");";
  36. string badFormatString = @"assert.throws(() => {{ WScript.LoadScript("","samethread").WScript.LoadScript('{0}'); }}, SyntaxError, ""generatedTest #{1}"", ""Invalid use of the 'super' keyword"");";
  37. if (testCase.Item2)
  38. {
  39. return string.Format(goodFormatString, testCase.Item1, testCaseNumber);
  40. }
  41. else
  42. {
  43. return string.Format(badFormatString, testCase.Item1, testCaseNumber);
  44. }
  45. }
  46. private static void BranchOut(BranchNode rootNode, Node n)
  47. {
  48. rootNode.branches.Add(new Branch("class X { constructor(){+} }", n, (oldState) => 1));
  49. rootNode.branches.Add(new Branch("class X { x(){+} }", n, (oldState) => 2));
  50. rootNode.branches.Add(new Branch("function x(){+}", n, (oldState) => 0));
  51. rootNode.branches.Add(new Branch("() => {+}", n, (oldState) => oldState));
  52. }
  53. }
  54. class Branch
  55. {
  56. public Branch(string userFormatString, Node branchTarget, Func<int, int> statePropagator)
  57. {
  58. this.BranchTarget = branchTarget;
  59. this.FormatString = ToFormatString(userFormatString);
  60. this.StatePropagator = statePropagator;
  61. }
  62. public Node BranchTarget { get; set; }
  63. public string FormatString { get; set; }
  64. public Func<int, int> StatePropagator { get; set; }
  65. private string ToFormatString(string p)
  66. {
  67. return p.Replace("{", "{{").Replace("}", "}}").Replace("+", "{0}");
  68. }
  69. }
  70. abstract class Node
  71. {
  72. public abstract IEnumerable<Tuple<string, bool>> Generate(int state);
  73. }
  74. class LeafNode : Node
  75. {
  76. public override IEnumerable<Tuple<string, bool>> Generate(int state)
  77. {
  78. yield return Tuple.Create("super();", state == 1);
  79. yield return Tuple.Create("super.x;", state > 0);
  80. }
  81. }
  82. class BranchNode : Node
  83. {
  84. public List<Branch> branches = new List<Branch>();
  85. public override IEnumerable<Tuple<string, bool>> Generate(int state)
  86. {
  87. foreach (var branch in branches)
  88. {
  89. Node branchTarget = branch.BranchTarget;
  90. string branchFormatString = branch.FormatString;
  91. foreach (var childItem in branchTarget.Generate(branch.StatePropagator(state)))
  92. {
  93. string childString = childItem.Item1;
  94. bool childResult = childItem.Item2;
  95. yield return Tuple.Create(string.Format(branchFormatString, childString), childResult);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. */
  102. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  103. var tests = [
  104. {
  105. name: "Generated tests",
  106. body: function () {
  107. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){super();} }'); }, "generated test #1");
  108. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){super.x;} }'); }, "generated test #2");
  109. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){super();} }'); }, SyntaxError, "generatedTest #3", "Invalid use of the 'super' keyword: generatedTest #3");
  110. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){super.x;} }'); }, "generated test #4");
  111. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){super();}'); }, SyntaxError, "generatedTest #5", "Invalid use of the 'super' keyword");
  112. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){super.x;}'); }, SyntaxError, "generatedTest #6", "Invalid use of the 'super' keyword");
  113. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('() => {super();}'); }, SyntaxError, "generatedTest #7", "Invalid use of the 'super' keyword");
  114. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('() => {super.x;}'); }, SyntaxError, "generatedTest #8", "Invalid use of the 'super' keyword");
  115. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){class X { constructor(){super();} }} }'); }, "generated test #9");
  116. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){class X { constructor(){super.x;} }} }'); }, "generated test #10");
  117. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){class X { x(){super();} }} }'); }, SyntaxError, "generatedTest #11", "Invalid use of the 'super' keyword");
  118. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){class X { x(){super.x;} }} }'); }, "generated test #12");
  119. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){function x(){super();}} }'); }, SyntaxError, "generatedTest #13", "Invalid use of the 'super' keyword");
  120. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){function x(){super.x;}} }'); }, SyntaxError, "generatedTest #14", "Invalid use of the 'super' keyword");
  121. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){() => {super();}} }'); }, "generated test #15");
  122. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){() => {super.x;}} }'); }, "generated test #16");
  123. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){class X { constructor(){super();} }} }'); }, "generated test #17");
  124. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){class X { constructor(){super.x;} }} }'); }, "generated test #18");
  125. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){class X { x(){super();} }} }'); }, SyntaxError, "generatedTest #19", "Invalid use of the 'super' keyword");
  126. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){class X { x(){super.x;} }} }'); }, "generated test #20");
  127. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){function x(){super();}} }'); }, SyntaxError, "generatedTest #21", "Invalid use of the 'super' keyword");
  128. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){function x(){super.x;}} }'); }, SyntaxError, "generatedTest #22", "Invalid use of the 'super' keyword");
  129. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){() => {super();}} }'); }, SyntaxError, "generatedTest #23", "Invalid use of the 'super' keyword");
  130. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){() => {super.x;}} }'); }, "generated test #24");
  131. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){class X { constructor(){super();} }}'); }, "generated test #25");
  132. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){class X { constructor(){super.x;} }}'); }, "generated test #26");
  133. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){class X { x(){super();} }}'); }, SyntaxError, "generatedTest #27", "Invalid use of the 'super' keyword");
  134. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){class X { x(){super.x;} }}'); }, "generated test #28");
  135. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){function x(){super();}}'); }, SyntaxError, "generatedTest #29", "Invalid use of the 'super' keyword");
  136. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){function x(){super.x;}}'); }, SyntaxError, "generatedTest #30", "Invalid use of the 'super' keyword");
  137. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){() => {super();}}'); }, SyntaxError, "generatedTest #31", "Invalid use of the 'super' keyword");
  138. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){() => {super.x;}}'); }, SyntaxError, "generatedTest #32", "Invalid use of the 'super' keyword");
  139. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('() => {class X { constructor(){super();} }}'); }, "generated test #33");
  140. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('() => {class X { constructor(){super.x;} }}'); }, "generated test #34");
  141. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('() => {class X { x(){super();} }}'); }, SyntaxError, "generatedTest #35", "Invalid use of the 'super' keyword");
  142. assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('() => {class X { x(){super.x;} }}'); }, "generated test #36");
  143. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('() => {function x(){super();}}'); }, SyntaxError, "generatedTest #37", "Invalid use of the 'super' keyword");
  144. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('() => {function x(){super.x;}}'); }, SyntaxError, "generatedTest #38", "Invalid use of the 'super' keyword");
  145. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('() => {() => {super();}}'); }, SyntaxError, "generatedTest #39", "Invalid use of the 'super' keyword");
  146. assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('() => {() => {super.x;}}'); }, SyntaxError, "generatedTest #40", "Invalid use of the 'super' keyword");
  147. }
  148. }
  149. ];
  150. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });