diff options
Diffstat (limited to 'spec/inputs/test')
| -rw-r--r-- | spec/inputs/test/ast_spec.yue | 91 | ||||
| -rw-r--r-- | spec/inputs/test/format_spec.yue | 4 |
2 files changed, 94 insertions, 1 deletions
diff --git a/spec/inputs/test/ast_spec.yue b/spec/inputs/test/ast_spec.yue new file mode 100644 index 0000000..11efc94 --- /dev/null +++ b/spec/inputs/test/ast_spec.yue | |||
| @@ -0,0 +1,91 @@ | |||
| 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 | ||
diff --git a/spec/inputs/test/format_spec.yue b/spec/inputs/test/format_spec.yue index 8c6096a..310b610 100644 --- a/spec/inputs/test/format_spec.yue +++ b/spec/inputs/test/format_spec.yue | |||
| @@ -165,7 +165,9 @@ import "yue" | |||
| 165 | rewriteLineCol = (item)-> | 165 | rewriteLineCol = (item)-> |
| 166 | item[2] = 0 | 166 | item[2] = 0 |
| 167 | item[3] = 0 | 167 | item[3] = 0 |
| 168 | for i = 4, #item | 168 | item[4] = 0 |
| 169 | item[5] = 0 | ||
| 170 | for i = 6, #item | ||
| 169 | switch type item[i] when "table" | 171 | switch type item[i] when "table" |
| 170 | if item[i][1] == "comment" | 172 | if item[i][1] == "comment" |
| 171 | table.remove item, i | 173 | table.remove item, i |
