/test/Esprima.Tests/ParserTests.cs

https://github.com/sebastienros/esprima-dotnet · C# · 191 lines · 158 code · 33 blank · 0 comment · 0 complexity · b3ec38ba19002bc8eb27f40f11933350 MD5 · raw file

  1. using System.Linq;
  2. using Esprima.Ast;
  3. using Xunit;
  4. namespace Esprima.Tests
  5. {
  6. public class ParserTests
  7. {
  8. [Fact]
  9. public void ProgramShouldBeStrict()
  10. {
  11. var parser = new JavaScriptParser("'use strict'; function p() {}");
  12. var program = parser.ParseScript();
  13. Assert.True(program.Strict);
  14. }
  15. [Fact]
  16. public void ProgramShouldNotBeStrict()
  17. {
  18. var parser = new JavaScriptParser("function p() {}");
  19. var program = parser.ParseScript();
  20. Assert.False(program.Strict);
  21. }
  22. [Fact]
  23. public void FunctionShouldNotBeStrict()
  24. {
  25. var parser = new JavaScriptParser("function p() {}");
  26. var program = parser.ParseScript();
  27. var function = program.Body.First().As<FunctionDeclaration>();
  28. Assert.False(function.Strict);
  29. }
  30. [Fact]
  31. public void FunctionWithUseStrictShouldBeStrict()
  32. {
  33. var parser = new JavaScriptParser("function p() { 'use strict'; }");
  34. var program = parser.ParseScript();
  35. var function = program.Body.First().As<FunctionDeclaration>();
  36. Assert.True(function.Strict);
  37. }
  38. [Fact]
  39. public void FunctionShouldBeStrictInProgramStrict()
  40. {
  41. var parser = new JavaScriptParser("'use strict'; function p() {}");
  42. var program = parser.ParseScript();
  43. var function = program.Body.Skip(1).First().As<FunctionDeclaration>();
  44. Assert.True(function.Strict);
  45. }
  46. [Fact]
  47. public void FunctionShouldBeStrict()
  48. {
  49. var parser = new JavaScriptParser("function p() {'use strict'; return false;}");
  50. var program = parser.ParseScript();
  51. var function = program.Body.First().As<FunctionDeclaration>();
  52. Assert.True(function.Strict);
  53. }
  54. [Fact]
  55. public void FunctionShouldBeStrictInStrictFunction()
  56. {
  57. var parser = new JavaScriptParser("function p() {'use strict'; function q() { return; } return; }");
  58. var program = parser.ParseScript();
  59. var p = program.Body.First().As<FunctionDeclaration>();
  60. var q = p.Body.As<BlockStatement>().Body.Skip(1).First().As<FunctionDeclaration>();
  61. Assert.Equal("p", p.Id.Name);
  62. Assert.Equal("q", q.Id.Name);
  63. Assert.True(p.Strict);
  64. Assert.True(q.Strict);
  65. }
  66. [Fact]
  67. public void LabelSetShouldPointToStatement()
  68. {
  69. var parser = new JavaScriptParser("here: Hello();");
  70. var program = parser.ParseScript();
  71. var labeledStatement = program.Body.First().As<LabeledStatement>();
  72. var body = labeledStatement.Body;
  73. Assert.Equal(labeledStatement.Label, body.LabelSet);
  74. }
  75. [Theory]
  76. [InlineData(1.189008226412092e+38, "0x5973772948c653ac1971f1576e03c4d4")]
  77. [InlineData(18446744073709552000d, "0xffffffffffffffff")]
  78. public void ShouldParseNumericLiterals(object expected, string source)
  79. {
  80. var parser = new JavaScriptParser(source);
  81. var expression = parser.ParseExpression();
  82. var literal = expression as Literal;
  83. Assert.NotNull(literal);
  84. Assert.Equal(expected, literal.NumericValue);
  85. }
  86. [Fact]
  87. public void ShouldParseClassInheritance()
  88. {
  89. var parser = new JavaScriptParser("class Rectangle extends aggregation(Shape, Colored, ZCoord) { }");
  90. var program = parser.ParseScript();
  91. }
  92. [Fact]
  93. public void ShouldSymbolPropertyKey()
  94. {
  95. var parser = new JavaScriptParser("var a = { [Symbol.iterator]: undefined }");
  96. var program = parser.ParseScript();
  97. }
  98. [Fact]
  99. public void ShouldParseLocation()
  100. {
  101. var options = new ParserOptions
  102. {
  103. Loc = true
  104. };
  105. var parser = new JavaScriptParser("// End on second line\r\n", options);
  106. var program = parser.ParseScript();
  107. }
  108. [Fact]
  109. public void ShouldParseArrayPattern()
  110. {
  111. var parser = new JavaScriptParser(@"
  112. var values = [1, 2, 3];
  113. var callCount = 0;
  114. var f;
  115. f = ([...[...x]]) => {
  116. callCount = callCount + 1;
  117. };
  118. f(values);
  119. ");
  120. var program = parser.ParseScript();
  121. }
  122. [Fact]
  123. public void CanParseInvalidCurly()
  124. {
  125. var parser = new JavaScriptParser("if (1}=1) eval('1');");
  126. Assert.Throws<ParserException>(() => parser.ParseScript());
  127. }
  128. [Fact]
  129. public void CanReportProblemWithLargeNumber()
  130. {
  131. Assert.Throws<ParserException>(() => new JavaScriptParser("066666666666666666666666666666"));
  132. }
  133. [Theory]
  134. [InlineData(".")]
  135. [InlineData("..")]
  136. [InlineData("...")]
  137. public void CanParseDot(string script)
  138. {
  139. var parser = new JavaScriptParser(script);
  140. Assert.Throws<ParserException>(() => parser.ParseScript());
  141. }
  142. [Fact]
  143. public void ThrowsErrorForInvalidRegExFlags()
  144. {
  145. var parser = new JavaScriptParser("/'/o//'///C//ÿ");
  146. Assert.Throws<ParserException>(() => parser.ParseScript());
  147. }
  148. [Fact]
  149. public void ThrowsErrorForDeepRecursionParsing()
  150. {
  151. var parser = new JavaScriptParser("if ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((true)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) { } ");
  152. Assert.Throws<ParserException>(() => parser.ParseScript());
  153. }
  154. }
  155. }