|
| 1 | +local util = require("spec.util") |
| 2 | +local teal = require("teal") |
| 3 | + |
| 4 | +describe("ParseTree.check", function() |
| 5 | + it("can check Teal code", function() |
| 6 | + local tl_code = [[ |
| 7 | + local foo: string = "hello" |
| 8 | + local planet: integer = 3 |
| 9 | + ]] |
| 10 | + |
| 11 | + local compiler = teal.compiler() |
| 12 | + local input = compiler:input(tl_code) |
| 13 | + local parse_tree = input:parse() |
| 14 | + local module, check_err = parse_tree:check() |
| 15 | + |
| 16 | + assert(module) |
| 17 | + assert.same(0, #check_err.syntax_errors) |
| 18 | + assert.same(0, #check_err.type_errors) |
| 19 | + assert.same(2, #check_err.warnings) |
| 20 | + end) |
| 21 | + |
| 22 | + it("reports syntax errors from the lexer", function() |
| 23 | + local tl_code = [[ |
| 24 | + 2.e + 1 |
| 25 | + ]] |
| 26 | + |
| 27 | + local compiler = teal.compiler() |
| 28 | + local input = compiler:input(tl_code) |
| 29 | + local parse_tree = input:parse() |
| 30 | + local module, check_err = parse_tree:check() |
| 31 | + |
| 32 | + assert.is_nil(module) |
| 33 | + assert.same(1, #check_err.syntax_errors) |
| 34 | + assert.same(0, #check_err.type_errors) |
| 35 | + assert.same(0, #check_err.warnings) |
| 36 | + end) |
| 37 | + |
| 38 | + it("reports syntax errors from parsing", function() |
| 39 | + local tl_code = [[ |
| 40 | + if if if |
| 41 | + ]] |
| 42 | + |
| 43 | + local compiler = teal.compiler() |
| 44 | + local input = compiler:input(tl_code) |
| 45 | + local parse_tree = input:parse() |
| 46 | + local module, check_err = parse_tree:check() |
| 47 | + |
| 48 | + assert.is_nil(module) |
| 49 | + assert.same(3, #check_err.syntax_errors) |
| 50 | + assert.same(0, #check_err.type_errors) |
| 51 | + assert.same(0, #check_err.warnings) |
| 52 | + end) |
| 53 | + |
| 54 | + it("reports type errors from checking", function() |
| 55 | + local tl_code = [[ |
| 56 | + local x: number = "oops" |
| 57 | + ]] |
| 58 | + |
| 59 | + local compiler = teal.compiler() |
| 60 | + local input = compiler:input(tl_code) |
| 61 | + local parse_tree = input:parse() |
| 62 | + local module, check_err = parse_tree:check() |
| 63 | + |
| 64 | + assert(module) |
| 65 | + assert.same(0, #check_err.syntax_errors) |
| 66 | + assert.same(1, #check_err.type_errors) |
| 67 | + assert.same(1, #check_err.warnings) |
| 68 | + end) |
| 69 | + |
| 70 | + it("when type reporting on, return both module and type errors from checking", function() |
| 71 | + local tl_code = [[ |
| 72 | + local x: number = "oops" |
| 73 | + ]] |
| 74 | + |
| 75 | + local compiler = teal.compiler() |
| 76 | + compiler:enable_type_reporting(true) |
| 77 | + local input = compiler:input(tl_code) |
| 78 | + local parse_tree = input:parse() |
| 79 | + local module, check_err = parse_tree:check() |
| 80 | + |
| 81 | + assert(module) |
| 82 | + assert.same(0, #check_err.syntax_errors) |
| 83 | + assert.same(1, #check_err.type_errors) |
| 84 | + assert.same(1, #check_err.warnings) |
| 85 | + end) |
| 86 | +end) |
0 commit comments