Skip to content

Commit c263d86

Browse files
committed
tests: Input.gen and Module.gen
1 parent e267da6 commit c263d86

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

spec/api/v3/input/gen_spec.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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)

spec/api/v3/module/gen_spec.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
local util = require("spec.util")
2+
local teal = require("teal")
3+
4+
describe("Module.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 mod, check_err = input:check()
23+
local result_lua_code = mod:gen()
24+
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+
end)

0 commit comments

Comments
 (0)