local yue = require("yue") return describe("yue.to_ast", function() it("should return AST with end position for simple expression", function() local ast = yue.to_ast("x = 1") assert.is_not_nil(ast) assert.same(ast[1], "File") assert.same(ast[2], 1) assert.same(ast[3], 1) assert.is_number(ast[4]) return assert.is_number(ast[5]) end) it("should have correct end position for leaf nodes", function() local ast = yue.to_ast("1") assert.is_not_nil(ast) assert.same(ast[1], "File") assert.same(ast[2], 1) assert.same(ast[3], 1) assert.is_number(ast[4]) return assert.is_number(ast[5]) end) it("should have end position for multi-line code", function() local code = [[x = 1 y = 2]] local ast = yue.to_ast(code) assert.is_not_nil(ast) assert.same(ast[1], "File") assert.same(ast[2], 1) assert.same(ast[3], 1) return assert.is_true(ast[4] >= 2) end) it("should have end position for function definition", function() local code = [[add = (a, b) -> a + b]] local ast = yue.to_ast(code) assert.is_not_nil(ast) assert.same(ast[1], "File") assert.same(ast[2], 1) assert.same(ast[3], 1) assert.is_number(ast[4]) return assert.is_number(ast[5]) end) it("should have end position for table literal", function() local ast = yue.to_ast("{a: 1, b: 2}") assert.is_not_nil(ast) assert.same(ast[1], "File") assert.same(ast[2], 1) assert.same(ast[3], 1) assert.is_number(ast[4]) return assert.is_number(ast[5]) end) it("should have end position for class definition", function() local code = [[class Person new: (@name) => getName: => @name]] local ast = yue.to_ast(code) assert.is_not_nil(ast) assert.same(ast[1], "File") assert.same(ast[2], 1) assert.same(ast[3], 1) return assert.is_true(ast[4] >= 3) end) it("should return nil and error message for invalid syntax", function() local ast, err = yue.to_ast("if then else") assert.is_nil(ast) return assert.is_string(err) end) it("should support flatten level parameter", function() local ast = yue.to_ast("x = 1", 0) assert.is_not_nil(ast) assert.same(ast[1], "File") assert.is_number(ast[4]) return assert.is_number(ast[5]) end) return it("should have end position in nested structures", function() local code = "x = [i for i = 1, 10]" local ast = yue.to_ast(code) assert.is_not_nil(ast) assert.same(ast[1], "File") assert.same(ast[2], 1) assert.same(ast[3], 1) assert.is_number(ast[4]) assert.is_number(ast[5]) return assert.is_true(ast[5] > 1) end) end)