2
0

JavascriptParserTests.cs 6.7 KB

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