JavascriptParserTests.cs 7.1 KB

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