LexerTests.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using Lua.CodeAnalysis.Syntax;
  2. namespace Lua.Tests;
  3. public class LexerTests
  4. {
  5. [Test]
  6. [TestCase("0")]
  7. [TestCase("123")]
  8. [TestCase("1234567890")]
  9. public void Test_Numeric_Integer(string x)
  10. {
  11. var expected = new[]
  12. {
  13. SyntaxToken.Number(x, new(1, 0))
  14. };
  15. var actual = GetTokens(x);
  16. CollectionAssert.AreEqual(expected, actual);
  17. }
  18. [Test]
  19. [TestCase("1.2")]
  20. [TestCase("123.45")]
  21. [TestCase("12345.6789")]
  22. public void Test_Numeric_Decimal(string x)
  23. {
  24. var expected = new[]
  25. {
  26. SyntaxToken.Number(x, new(1, 0))
  27. };
  28. var actual = GetTokens(x);
  29. CollectionAssert.AreEqual(expected, actual);
  30. }
  31. [Test]
  32. [TestCase("0x123")]
  33. [TestCase("0x456")]
  34. [TestCase("0x789")]
  35. public void Test_Numeric_Hex(string x)
  36. {
  37. var expected = new[]
  38. {
  39. SyntaxToken.Number(x, new(1, 0))
  40. };
  41. var actual = GetTokens(x);
  42. CollectionAssert.AreEqual(expected, actual);
  43. }
  44. [Test]
  45. [TestCase("12E3")]
  46. [TestCase("45E+6")]
  47. [TestCase("78E-9")]
  48. [TestCase("1e+2")]
  49. [TestCase("3e-4")]
  50. public void Test_Numeric_Exponential(string x)
  51. {
  52. var expected = new[]
  53. {
  54. SyntaxToken.Number(x, new(1, 0))
  55. };
  56. var actual = GetTokens(x);
  57. CollectionAssert.AreEqual(expected, actual);
  58. }
  59. [Test]
  60. [TestCase("\"\"")]
  61. [TestCase("\"hello\"")]
  62. [TestCase("\"1.23\"")]
  63. [TestCase("\"1-2-3-4-5\"")]
  64. [TestCase("\'hello\'")]
  65. public void Test_String(string x)
  66. {
  67. var expected = new[]
  68. {
  69. SyntaxToken.String(x.AsMemory(1, x.Length - 2), new(1, 0))
  70. };
  71. var actual = GetTokens(x);
  72. CollectionAssert.AreEqual(expected, actual);
  73. }
  74. [Test]
  75. [TestCase("foo")]
  76. [TestCase("bar")]
  77. [TestCase("baz")]
  78. public void Test_Identifier(string x)
  79. {
  80. var expected = new[]
  81. {
  82. SyntaxToken.Identifier(x, new(1, 0))
  83. };
  84. var actual = GetTokens(x);
  85. CollectionAssert.AreEqual(expected, actual);
  86. }
  87. [Test]
  88. [TestCase("-- hello!")]
  89. [TestCase("-- how are you?")]
  90. [TestCase("-- goodbye!")]
  91. public void Test_Comment_Line(string code)
  92. {
  93. var expected = Array.Empty<SyntaxToken>();
  94. var actual = GetTokens(code);
  95. CollectionAssert.AreEqual(expected, actual);
  96. }
  97. [Test]
  98. [TestCase(@"--[[
  99. hello!
  100. how are you?
  101. goodbye!
  102. ]]--")]
  103. [TestCase(@"--[[
  104. hello!
  105. how are you?
  106. goodbye!
  107. ]]")]
  108. public void Test_Comment_Block(string code)
  109. {
  110. var expected = Array.Empty<SyntaxToken>();
  111. var actual = GetTokens(code);
  112. CollectionAssert.AreEqual(expected, actual);
  113. }
  114. [Test]
  115. [TestCase("--[[ \n hello")]
  116. [TestCase("--[[ \r hello")]
  117. [TestCase("--[[ \r\n hello")]
  118. public void Test_Comment_Block_Error(string code)
  119. {
  120. Assert.Throws<LuaParseException>(() => GetTokens(code), "main.lua:(1,5): unfinished long comment (starting at line 0)");
  121. }
  122. [Test]
  123. public void Test_Comment_Line_WithCode()
  124. {
  125. var expected = new[]
  126. {
  127. SyntaxToken.Number("10.0", new(1, 0))
  128. };
  129. var actual = GetTokens("10.0 -- this is numeric literal");
  130. CollectionAssert.AreEqual(expected, actual);
  131. }
  132. [Test]
  133. public void Test_Nil()
  134. {
  135. var expected = new[]
  136. {
  137. SyntaxToken.Nil(new(1, 0))
  138. };
  139. var actual = GetTokens("nil");
  140. CollectionAssert.AreEqual(expected, actual);
  141. }
  142. [Test]
  143. public void Test_True()
  144. {
  145. var expected = new[]
  146. {
  147. SyntaxToken.True(new(1, 0))
  148. };
  149. var actual = GetTokens("true");
  150. CollectionAssert.AreEqual(expected, actual);
  151. }
  152. [Test]
  153. public void Test_False()
  154. {
  155. var expected = new[]
  156. {
  157. SyntaxToken.False(new(1, 0))
  158. };
  159. var actual = GetTokens("false");
  160. CollectionAssert.AreEqual(expected, actual);
  161. }
  162. [Test]
  163. public void Test_If()
  164. {
  165. var expected = new[]
  166. {
  167. SyntaxToken.If(new(1, 0)), SyntaxToken.Identifier("x", new(1, 3)), SyntaxToken.Equality(new(1, 5)), SyntaxToken.Number("1.0", new(1, 8)), SyntaxToken.Then(new(1, 12)), SyntaxToken.EndOfLine(new(1, 16)),
  168. SyntaxToken.Return(new(2, 4)), SyntaxToken.Nil(new(2, 11)), SyntaxToken.EndOfLine(new(2, 14)),
  169. SyntaxToken.End(new(3, 0)),
  170. };
  171. var actual = GetTokens(
  172. @"if x == 1.0 then
  173. return nil
  174. end");
  175. CollectionAssert.AreEqual(expected, actual);
  176. }
  177. [Test]
  178. public void Test_If_Else()
  179. {
  180. var expected = new[]
  181. {
  182. SyntaxToken.If(new(1, 0)), SyntaxToken.Identifier("x", new(1, 3)), SyntaxToken.Equality(new(1, 5)), SyntaxToken.Number("1.0", new(1, 8)), SyntaxToken.Then(new(1, 12)), SyntaxToken.EndOfLine(new(1, 16)),
  183. SyntaxToken.Return(new(2, 4)), SyntaxToken.Number("1.0", new(2, 11)), SyntaxToken.EndOfLine(new(2, 14)),
  184. SyntaxToken.Else(new(3, 0)), SyntaxToken.EndOfLine(new(3, 4)),
  185. SyntaxToken.Return(new(4, 4)), SyntaxToken.Number("0.0", new(4, 11)), SyntaxToken.EndOfLine(new(4, 14)),
  186. SyntaxToken.End(new(5, 0)),
  187. };
  188. var actual = GetTokens(
  189. @"if x == 1.0 then
  190. return 1.0
  191. else
  192. return 0.0
  193. end");
  194. CollectionAssert.AreEqual(expected, actual);
  195. }
  196. static SyntaxToken[] GetTokens(string source)
  197. {
  198. var list = new List<SyntaxToken>();
  199. var lexer = new Lexer
  200. {
  201. Source = source.AsMemory(),
  202. ChunkName = "main.lua"
  203. };
  204. while (lexer.MoveNext())
  205. {
  206. list.Add(lexer.Current);
  207. }
  208. return list.ToArray();
  209. }
  210. }