aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/ast_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'spec/outputs/test/ast_spec.lua')
-rw-r--r--spec/outputs/test/ast_spec.lua85
1 files changed, 85 insertions, 0 deletions
diff --git a/spec/outputs/test/ast_spec.lua b/spec/outputs/test/ast_spec.lua
new file mode 100644
index 0000000..bab5b9e
--- /dev/null
+++ b/spec/outputs/test/ast_spec.lua
@@ -0,0 +1,85 @@
1local yue = require("yue")
2return describe("yue.to_ast", function()
3 it("should return AST with end position for simple expression", function()
4 local ast = yue.to_ast("x = 1")
5 assert.is_not_nil(ast)
6 assert.same(ast[1], "File")
7 assert.same(ast[2], 1)
8 assert.same(ast[3], 1)
9 assert.is_number(ast[4])
10 return assert.is_number(ast[5])
11 end)
12 it("should have correct end position for leaf nodes", function()
13 local ast = yue.to_ast("1")
14 assert.is_not_nil(ast)
15 assert.same(ast[1], "File")
16 assert.same(ast[2], 1)
17 assert.same(ast[3], 1)
18 assert.is_number(ast[4])
19 return assert.is_number(ast[5])
20 end)
21 it("should have end position for multi-line code", function()
22 local code = [[x = 1
23y = 2]]
24 local ast = yue.to_ast(code)
25 assert.is_not_nil(ast)
26 assert.same(ast[1], "File")
27 assert.same(ast[2], 1)
28 assert.same(ast[3], 1)
29 return assert.is_true(ast[4] >= 2)
30 end)
31 it("should have end position for function definition", function()
32 local code = [[add = (a, b) ->
33 a + b]]
34 local ast = yue.to_ast(code)
35 assert.is_not_nil(ast)
36 assert.same(ast[1], "File")
37 assert.same(ast[2], 1)
38 assert.same(ast[3], 1)
39 assert.is_number(ast[4])
40 return assert.is_number(ast[5])
41 end)
42 it("should have end position for table literal", function()
43 local ast = yue.to_ast("{a: 1, b: 2}")
44 assert.is_not_nil(ast)
45 assert.same(ast[1], "File")
46 assert.same(ast[2], 1)
47 assert.same(ast[3], 1)
48 assert.is_number(ast[4])
49 return assert.is_number(ast[5])
50 end)
51 it("should have end position for class definition", function()
52 local code = [[class Person
53 new: (@name) =>
54 getName: => @name]]
55 local ast = yue.to_ast(code)
56 assert.is_not_nil(ast)
57 assert.same(ast[1], "File")
58 assert.same(ast[2], 1)
59 assert.same(ast[3], 1)
60 return assert.is_true(ast[4] >= 3)
61 end)
62 it("should return nil and error message for invalid syntax", function()
63 local ast, err = yue.to_ast("if then else")
64 assert.is_nil(ast)
65 return assert.is_string(err)
66 end)
67 it("should support flatten level parameter", function()
68 local ast = yue.to_ast("x = 1", 0)
69 assert.is_not_nil(ast)
70 assert.same(ast[1], "File")
71 assert.is_number(ast[4])
72 return assert.is_number(ast[5])
73 end)
74 return it("should have end position in nested structures", function()
75 local code = "x = [i for i = 1, 10]"
76 local ast = yue.to_ast(code)
77 assert.is_not_nil(ast)
78 assert.same(ast[1], "File")
79 assert.same(ast[2], 1)
80 assert.same(ast[3], 1)
81 assert.is_number(ast[4])
82 assert.is_number(ast[5])
83 return assert.is_true(ast[5] > 1)
84 end)
85end)