JavascriptParserTests.cs 6.6 KB

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