JavascriptParserTests.cs 7.4 KB

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