JavascriptParserTests.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. [Trait("Category", "Common Libraries")]
  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. [Trait("Category", "Primary Expression")]
  38. public void ShouldParseThis()
  39. {
  40. var program = _parser.Parse("this");
  41. var body = program.Body;
  42. Assert.NotNull(body);
  43. Assert.Equal(1, body.Count());
  44. Assert.Equal(SyntaxNodes.ThisExpression, body.First().As<ExpressionStatement>().Expression.Type);
  45. }
  46. [Fact]
  47. [Trait("Category", "Primary Expression")]
  48. public void ShouldParseNull()
  49. {
  50. var program = _parser.Parse("null");
  51. var body = program.Body;
  52. Assert.NotNull(body);
  53. Assert.Equal(1, body.Count());
  54. Assert.Equal(SyntaxNodes.Literal, body.First().As<ExpressionStatement>().Expression.Type);
  55. Assert.Equal(null, body.First().As<ExpressionStatement>().Expression.As<Literal>().Value);
  56. Assert.Equal("null", body.First().As<ExpressionStatement>().Expression.As<Literal>().Raw);
  57. }
  58. [Fact]
  59. [Trait("Category", "Primary Expression")]
  60. public void ShouldParseNumeric()
  61. {
  62. var program = _parser.Parse(
  63. @"
  64. 42
  65. ");
  66. var body = program.Body;
  67. Assert.NotNull(body);
  68. Assert.Equal(1, body.Count());
  69. Assert.Equal(SyntaxNodes.Literal, body.First().As<ExpressionStatement>().Expression.Type);
  70. Assert.Equal(42d, body.First().As<ExpressionStatement>().Expression.As<Literal>().Value);
  71. Assert.Equal("42", body.First().As<ExpressionStatement>().Expression.As<Literal>().Raw);
  72. }
  73. [Fact]
  74. [Trait("Category", "Primary Expression")]
  75. public void ShouldParseBinaryExpression()
  76. {
  77. BinaryExpression binary;
  78. var program = _parser.Parse("(1 + 2 ) * 3");
  79. var body = program.Body;
  80. Assert.NotNull(body);
  81. Assert.Equal(1, body.Count());
  82. Assert.NotNull(binary = body.First().As<ExpressionStatement>().Expression.As<BinaryExpression>());
  83. Assert.Equal(3d, binary.Right.As<Literal>().Value);
  84. Assert.Equal("*", binary.Operator);
  85. Assert.Equal(1d, binary.Left.As<BinaryExpression>().Left.As<Literal>().Value);
  86. Assert.Equal(2d, binary.Left.As<BinaryExpression>().Right.As<Literal>().Value);
  87. Assert.Equal("+", binary.Left.As<BinaryExpression>().Operator);
  88. }
  89. [Theory]
  90. [Trait("Category", "Numeric Literals")]
  91. [InlineData(0, "0")]
  92. [InlineData(42, "42")]
  93. [InlineData(0.14, "0.14")]
  94. [InlineData(3.14159, "3.14159")]
  95. [InlineData(6.02214179e+23, "6.02214179e+23")]
  96. [InlineData(1.492417830e-10, "1.492417830e-10")]
  97. [InlineData(0, "0x0")]
  98. [InlineData(0, "0x0;")]
  99. [InlineData(0xabc, "0xabc")]
  100. [InlineData(0xdef, "0xdef")]
  101. [InlineData(0X1A, "0X1A")]
  102. [InlineData(0x10, "0x10")]
  103. [InlineData(0x100, "0x100")]
  104. [InlineData(0X04, "0X04")]
  105. [InlineData(02, "02")]
  106. [InlineData(10, "012")]
  107. [InlineData(10, "0012")]
  108. public void ShouldParseNumericLiterals(object expected, string source)
  109. {
  110. Literal literal;
  111. var program = _parser.Parse(source);
  112. var body = program.Body;
  113. Assert.NotNull(body);
  114. Assert.Equal(1, body.Count());
  115. Assert.NotNull(literal = body.First().As<ExpressionStatement>().Expression.As<Literal>());
  116. Assert.Equal(Convert.ToDouble(expected), Convert.ToDouble(literal.Value));
  117. }
  118. [Theory]
  119. [Trait("Category", "String Literals")]
  120. [InlineData("Hello", @"'Hello'")]
  121. [InlineData("\n\r\t\v\b\f\\\'\"\0", @"'\n\r\t\v\b\f\\\'\""\0'")]
  122. [InlineData("\u0061", @"'\u0061'")]
  123. [InlineData("\x61", @"'\x61'")]
  124. [InlineData("Hello\nworld", @"'Hello\nworld'")]
  125. [InlineData("Hello\nworld", "'Hello\\\nworld'")]
  126. public void ShouldParseStringLiterals(string expected, string source)
  127. {
  128. Literal literal;
  129. var program = _parser.Parse(source);
  130. var body = program.Body;
  131. Assert.NotNull(body);
  132. Assert.Equal(1, body.Count());
  133. Assert.NotNull(literal = body.First().As<ExpressionStatement>().Expression.As<Literal>());
  134. Assert.Equal(expected, literal.Value);
  135. }
  136. [Theory]
  137. [Trait("Category", "String Literals")]
  138. [InlineData("Hello", @"'Hello'")]
  139. [InlineData("\n\r\t\v\b\f\\\'\"\0", @"'\n\r\t\v\b\f\\\'\""\0'")]
  140. [InlineData("\u0061", @"'\u0061'")]
  141. [InlineData("\x61", @"'\x61'")]
  142. [InlineData("Hello\nworld", @"'Hello\nworld'")]
  143. [InlineData("Hello\nworld", "'Hello\\\nworld'")]
  144. public void ShouldParseRegularExpressions(string expected, string source)
  145. {
  146. Literal literal;
  147. var program = _parser.Parse(source);
  148. var body = program.Body;
  149. Assert.NotNull(body);
  150. Assert.Equal(1, body.Count());
  151. Assert.NotNull(literal = body.First().As<ExpressionStatement>().Expression.As<Literal>());
  152. Assert.Equal(expected, literal.Value);
  153. }
  154. [Theory]
  155. [Trait("Category", "Automatic semicolon insertion")]
  156. [InlineData(@"{ x
  157. ++y }")]
  158. [InlineData(@"{ x
  159. --y }")]
  160. [InlineData(@"var x /* comment */;
  161. { var x = 14, y = 3
  162. z; }")]
  163. [InlineData(@"while (true) { continue
  164. there; }")]
  165. [InlineData(@"while (true) { continue // Comment
  166. there; }")]
  167. [InlineData(@"while (true) { continue /* Multiline
  168. Comment */there; }")]
  169. [InlineData(@"while (true) { break
  170. there; }")]
  171. [InlineData(@"while (true) { break // Comment
  172. there; }")]
  173. [InlineData(@"while (true) { break /* Multiline
  174. Comment */there; }")]
  175. [InlineData(@"(function(){ return
  176. x; })")]
  177. [InlineData(@"(function(){ return // Comment
  178. x; })")]
  179. [InlineData(@"(function(){ return/* Multiline
  180. Comment */x; })")]
  181. [InlineData(@"{ throw error
  182. error; }")]
  183. [InlineData(@"{ throw error// Comment
  184. error; }")]
  185. [InlineData(@"{ throw error/* Multiline
  186. Comment */error; }")]
  187. public void ShouldInsertSemicolons(string source)
  188. {
  189. var program = _parser.Parse(source);
  190. var body = program.Body;
  191. Assert.NotNull(body);
  192. }
  193. }
  194. }