JavascriptParserTests.cs 6.7 KB

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