|
| 1 | +yue = require "yue" |
| 2 | + |
| 3 | +describe "yue.to_ast", -> |
| 4 | + it "should return AST with end position for simple expression", -> |
| 5 | + ast = yue.to_ast "x = 1" |
| 6 | + assert.is_not_nil ast |
| 7 | + -- ast format: [name, begin_line, begin_col, end_line, end_col, ...children] |
| 8 | + assert.same ast[1], "File" |
| 9 | + assert.same ast[2], 1 -- begin line |
| 10 | + assert.same ast[3], 1 -- begin col |
| 11 | + assert.is_number ast[4] -- end line |
| 12 | + assert.is_number ast[5] -- end col |
| 13 | + |
| 14 | + it "should have correct end position for leaf nodes", -> |
| 15 | + ast = yue.to_ast "1" |
| 16 | + assert.is_not_nil ast |
| 17 | + -- Leaf node with no children should have format: [name, begin_line, begin_col, end_line, end_col, value] |
| 18 | + assert.same ast[1], "File" |
| 19 | + assert.same ast[2], 1 |
| 20 | + assert.same ast[3], 1 |
| 21 | + assert.is_number ast[4] |
| 22 | + assert.is_number ast[5] |
| 23 | + |
| 24 | + it "should have end position for multi-line code", -> |
| 25 | + code = [[ |
| 26 | +x = 1 |
| 27 | +y = 2]] |
| 28 | + ast = yue.to_ast code |
| 29 | + assert.is_not_nil ast |
| 30 | + assert.same ast[1], "File" |
| 31 | + assert.same ast[2], 1 |
| 32 | + assert.same ast[3], 1 |
| 33 | + -- End position should be on line 2 |
| 34 | + assert.is_true ast[4] >= 2 |
| 35 | + |
| 36 | + it "should have end position for function definition", -> |
| 37 | + code = [[ |
| 38 | +add = (a, b) -> |
| 39 | + a + b]] |
| 40 | + ast = yue.to_ast code |
| 41 | + assert.is_not_nil ast |
| 42 | + assert.same ast[1], "File" |
| 43 | + assert.same ast[2], 1 |
| 44 | + assert.same ast[3], 1 |
| 45 | + assert.is_number ast[4] |
| 46 | + assert.is_number ast[5] |
| 47 | + |
| 48 | + it "should have end position for table literal", -> |
| 49 | + ast = yue.to_ast "{a: 1, b: 2}" |
| 50 | + assert.is_not_nil ast |
| 51 | + assert.same ast[1], "File" |
| 52 | + assert.same ast[2], 1 |
| 53 | + assert.same ast[3], 1 |
| 54 | + assert.is_number ast[4] |
| 55 | + assert.is_number ast[5] |
| 56 | + |
| 57 | + it "should have end position for class definition", -> |
| 58 | + code = [[ |
| 59 | +class Person |
| 60 | + new: (@name) => |
| 61 | + getName: => @name]] |
| 62 | + ast = yue.to_ast code |
| 63 | + assert.is_not_nil ast |
| 64 | + assert.same ast[1], "File" |
| 65 | + assert.same ast[2], 1 |
| 66 | + assert.same ast[3], 1 |
| 67 | + assert.is_true ast[4] >= 3 |
| 68 | + |
| 69 | + it "should return nil and error message for invalid syntax", -> |
| 70 | + ast, err = yue.to_ast "if then else" |
| 71 | + assert.is_nil ast |
| 72 | + assert.is_string err |
| 73 | + |
| 74 | + it "should support flatten level parameter", -> |
| 75 | + ast = yue.to_ast "x = 1", 0 |
| 76 | + assert.is_not_nil ast |
| 77 | + assert.same ast[1], "File" |
| 78 | + assert.is_number ast[4] |
| 79 | + assert.is_number ast[5] |
| 80 | + |
| 81 | + it "should have end position in nested structures", -> |
| 82 | + code = "x = [i for i = 1, 10]" |
| 83 | + ast = yue.to_ast code |
| 84 | + assert.is_not_nil ast |
| 85 | + assert.same ast[1], "File" |
| 86 | + assert.same ast[2], 1 |
| 87 | + assert.same ast[3], 1 |
| 88 | + assert.is_number ast[4] |
| 89 | + assert.is_number ast[5] |
| 90 | + -- End column should reflect the end of the expression |
| 91 | + assert.is_true ast[5] > 1 |
0 commit comments