|
| 1 | +local util = require("spec.util") |
| 2 | +local teal = require("teal") |
| 3 | + |
| 4 | +describe("Input.gen", function() |
| 5 | + it("can generate Lua from Teal", function() |
| 6 | + local tl_code = [[ |
| 7 | + local function add(x: integer, y: integer): integer |
| 8 | + return x + y |
| 9 | + end |
| 10 | + print(add(1, 2)) |
| 11 | + ]] |
| 12 | + |
| 13 | + local expected_lua_code = [[ |
| 14 | + local function add(x, y) |
| 15 | + return x + y |
| 16 | + end |
| 17 | + print(add(1, 2)) |
| 18 | + ]] |
| 19 | + |
| 20 | + local compiler = teal.compiler() |
| 21 | + local input = compiler:input(tl_code) |
| 22 | + local result_lua_code, mod, check_err = input:gen() |
| 23 | + |
| 24 | + assert(mod) |
| 25 | + assert.same({}, check_err.syntax_errors) |
| 26 | + assert.same({}, check_err.type_errors) |
| 27 | + assert.same({}, check_err.warnings) |
| 28 | + util.assert_line_by_line(expected_lua_code, result_lua_code) |
| 29 | + end) |
| 30 | + |
| 31 | + it("does not generate Lua from invalid Teal", function() |
| 32 | + local tl_code = [[ |
| 33 | + local function add(x: integer, y: integer): integer |
| 34 | + return x + y |
| 35 | + ]] |
| 36 | + |
| 37 | + local compiler = teal.compiler() |
| 38 | + local input = compiler:input(tl_code) |
| 39 | + local result_lua_code, mod, check_err = input:gen() |
| 40 | + |
| 41 | + assert.is_nil(mod) |
| 42 | + assert.same(1, #check_err.syntax_errors) |
| 43 | + assert.same(0, #check_err.type_errors) |
| 44 | + assert.same(0, #check_err.warnings) |
| 45 | + end) |
| 46 | +end) |
0 commit comments