JavascriptParserTests.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. namespace Jint.Tests.Parsing;
  2. public class JavascriptParserTests
  3. {
  4. [Fact]
  5. public void ShouldParseThis()
  6. {
  7. var program = new Parser().ParseScript("this");
  8. var body = program.Body;
  9. Assert.Single(body);
  10. Assert.Equal(NodeType.ThisExpression, body.First().As<ExpressionStatement>().Expression.Type);
  11. }
  12. [Fact]
  13. public void ShouldParseNull()
  14. {
  15. var program = new Parser().ParseScript("null");
  16. var body = program.Body;
  17. Assert.Single(body);
  18. Assert.Equal(NodeType.Literal, body.First().As<ExpressionStatement>().Expression.Type);
  19. Assert.Equal(null, body.First().As<ExpressionStatement>().Expression.As<Literal>().Value);
  20. Assert.Equal("null", body.First().As<ExpressionStatement>().Expression.As<Literal>().Raw);
  21. }
  22. [Fact]
  23. public void ShouldParseNumeric()
  24. {
  25. var code = @"
  26. 42
  27. ";
  28. var program = new Parser().ParseScript(code);
  29. var body = program.Body;
  30. Assert.Single(body);
  31. Assert.Equal(NodeType.Literal, body.First().As<ExpressionStatement>().Expression.Type);
  32. Assert.Equal(42d, body.First().As<ExpressionStatement>().Expression.As<Literal>().Value);
  33. Assert.Equal("42", body.First().As<ExpressionStatement>().Expression.As<Literal>().Raw);
  34. }
  35. [Fact]
  36. public void ShouldParseBinaryExpression()
  37. {
  38. BinaryExpression binary;
  39. var program = new Parser().ParseScript("(1 + 2 ) * 3");
  40. var body = program.Body;
  41. Assert.Single(body);
  42. Assert.NotNull(binary = body.First().As<ExpressionStatement>().Expression.As<BinaryExpression>());
  43. Assert.Equal(3d, binary.Right.As<Literal>().Value);
  44. Assert.Equal(Operator.Multiplication, binary.Operator);
  45. Assert.Equal(1d, binary.Left.As<BinaryExpression>().Left.As<Literal>().Value);
  46. Assert.Equal(2d, binary.Left.As<BinaryExpression>().Right.As<Literal>().Value);
  47. Assert.Equal(Operator.Addition, binary.Left.As<BinaryExpression>().Operator);
  48. }
  49. [Theory]
  50. [InlineData(0, "0")]
  51. [InlineData(42, "42")]
  52. [InlineData(0.14, "0.14")]
  53. [InlineData(3.14159, "3.14159")]
  54. [InlineData(6.02214179e+23, "6.02214179e+23")]
  55. [InlineData(1.492417830e-10, "1.492417830e-10")]
  56. [InlineData(0, "0x0")]
  57. [InlineData(0, "0x0;")]
  58. [InlineData(0xabc, "0xabc")]
  59. [InlineData(0xdef, "0xdef")]
  60. [InlineData(0X1A, "0X1A")]
  61. [InlineData(0x10, "0x10")]
  62. [InlineData(0x100, "0x100")]
  63. [InlineData(0X04, "0X04")]
  64. [InlineData(02, "02")]
  65. [InlineData(10, "012")]
  66. [InlineData(10, "0012")]
  67. [InlineData(1.189008226412092e+38, "0x5973772948c653ac1971f1576e03c4d4")]
  68. [InlineData(18446744073709552000d, "0xffffffffffffffff")]
  69. public void ShouldParseNumericLiterals(object expected, string code)
  70. {
  71. Literal literal;
  72. var program = new Parser().ParseScript(code);
  73. var body = program.Body;
  74. Assert.Single(body);
  75. Assert.NotNull(literal = body.First().As<ExpressionStatement>().Expression.As<Literal>());
  76. Assert.Equal(Convert.ToDouble(expected), Convert.ToDouble(literal.Value));
  77. }
  78. [Theory]
  79. [InlineData("Hello", @"'Hello'")]
  80. [InlineData("\n\r\t\v\b\f\\\'\"\0", @"'\n\r\t\v\b\f\\\'\""\0'")]
  81. [InlineData("\u0061", @"'\u0061'")]
  82. [InlineData("\x61", @"'\x61'")]
  83. [InlineData("Hello\nworld", @"'Hello\nworld'")]
  84. [InlineData("Hello\\\nworld", @"'Hello\\\nworld'")]
  85. public void ShouldParseStringLiterals(string expected, string code)
  86. {
  87. Literal literal;
  88. var program = new Parser().ParseScript(code);
  89. var body = program.Body;
  90. Assert.Single(body);
  91. Assert.NotNull(literal = body.First().As<ExpressionStatement>().Expression.As<Literal>());
  92. Assert.Equal(expected, literal.Value);
  93. }
  94. [Theory]
  95. [InlineData(@"{ x
  96. ++y }")]
  97. [InlineData(@"{ x
  98. --y }")]
  99. [InlineData(@"var x /* comment */;
  100. { var x = 14, y = 3
  101. z; }")]
  102. [InlineData(@"while (true) { continue
  103. there; }")]
  104. [InlineData(@"while (true) { continue // Comment
  105. there; }")]
  106. [InlineData(@"while (true) { continue /* Multiline
  107. Comment */there; }")]
  108. [InlineData(@"while (true) { break
  109. there; }")]
  110. [InlineData(@"while (true) { break // Comment
  111. there; }")]
  112. [InlineData(@"while (true) { break /* Multiline
  113. Comment */there; }")]
  114. [InlineData(@"(function(){ return
  115. x; })")]
  116. [InlineData(@"(function(){ return // Comment
  117. x; })")]
  118. [InlineData(@"(function(){ return/* Multiline
  119. Comment */x; })")]
  120. [InlineData(@"{ throw error
  121. error; }")]
  122. [InlineData(@"{ throw error// Comment
  123. error; }")]
  124. [InlineData(@"{ throw error/* Multiline
  125. Comment */error; }")]
  126. public void ShouldInsertSemicolons(string code)
  127. {
  128. new Parser().ParseScript(code);
  129. }
  130. [Fact]
  131. public void ShouldProvideLocationForMultiLinesStringLiterals()
  132. {
  133. const string Code = @"'\
  134. \
  135. '
  136. ";
  137. var program = new Parser().ParseScript(Code);
  138. var expr = program.Body.First().As<ExpressionStatement>().Expression;
  139. Assert.Equal(1, expr.Location.Start.Line);
  140. Assert.Equal(0, expr.Location.Start.Column);
  141. Assert.Equal(3, expr.Location.End.Line);
  142. Assert.Equal(1, expr.Location.End.Column);
  143. }
  144. [Fact]
  145. public void ShouldThrowErrorForInvalidLeftHandOperation()
  146. {
  147. Assert.Throws<SyntaxErrorException>(() => new Engine().Execute("~ (WE0=1)--- l('1');"));
  148. }
  149. [Theory]
  150. [InlineData("....")]
  151. [InlineData("while")]
  152. [InlineData("var")]
  153. [InlineData("-.-")]
  154. public void ShouldThrowParseErrorExceptionForInvalidCode(string code)
  155. {
  156. Assert.Throws<SyntaxErrorException>(() => new Parser().ParseScript(code));
  157. }
  158. }