Skip to content

Commit 5dadb8a

Browse files
committed
tests: more API tests
1 parent a9b7dcc commit 5dadb8a

File tree

6 files changed

+138
-0
lines changed

6 files changed

+138
-0
lines changed

spec/api/v3/load_spec.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require("compat53")
2+
3+
describe("teal.load", function()
4+
5+
describe("loading Teal code from Lua", function()
6+
it("works", function()
7+
local lua_code = [[
8+
-- test.lua
9+
local teal = require('teal.init')
10+
11+
local program, err = teal.load('local a: string = "hey"; return a')
12+
return program()
13+
]]
14+
local lua_chunk = load(lua_code)
15+
local result = lua_chunk()
16+
assert.same(result, "hey")
17+
end)
18+
19+
it("can produce type checking errors when using 'c'", function()
20+
local lua_code = [[
21+
-- test.lua
22+
local teal = require('teal.init')
23+
24+
local program, err = teal.load('local a: string = 123; return a', 'code.tl', 'ct')
25+
assert(program == nil)
26+
return err
27+
]]
28+
local lua_chunk = load(lua_code)
29+
local result = lua_chunk()
30+
assert.match(result, "code.tl:1:19: in local declaration: a: got integer, expected string")
31+
end)
32+
33+
it("can run even with type check errors if not using 'c'", function()
34+
local lua_code = [[
35+
-- test.lua
36+
local teal = require('teal.init')
37+
38+
local program, err = teal.load('local a: string = 123; return a', 'code.tl')
39+
return program()
40+
]]
41+
local lua_chunk = load(lua_code)
42+
local result = lua_chunk()
43+
assert.same(result, 123)
44+
end)
45+
end)
46+
end)

spec/api/v3/loader_spec.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local util = require("spec.util")
2+
3+
describe("teal.loader", function()
4+
setup(util.chdir_setup)
5+
teardown(util.chdir_teardown)
6+
it("loads and reports filename correctly in debug info (#508)", function()
7+
local dir_name = util.write_tmp_dir(finally, {
8+
["file1.tl"] = [[
9+
return {
10+
get_src = function()
11+
return debug.getinfo(1, "S").source
12+
end
13+
}
14+
]],
15+
["main.lua"] = [[
16+
local teal = require("teal.init")
17+
teal.loader()
18+
file1 = require("file1")
19+
print(file1.get_src())
20+
]]
21+
})
22+
local pd, output
23+
util.do_in(dir_name, function()
24+
pd = io.popen(util.lua_cmd("main.lua"), "r")
25+
output = pd:read("*a")
26+
end)
27+
util.assert_popen_close(0, pd:close())
28+
util.assert_line_by_line([[
29+
@.]] .. util.os_sep .. [[file1.tl
30+
]], output)
31+
end)
32+
end)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
local util = require("spec.util")
2+
local teal = require("teal")
3+
4+
describe("teal.runtime_target", function()
5+
it("reports the set of warnings", function()
6+
local target = teal.runtime_target()
7+
assert.matches("5.%d", target)
8+
end)
9+
end)

spec/api/v3/search_module_spec.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
local util = require("spec.util")
2+
local teal = require("teal")
3+
4+
describe("teal.search_module", function()
5+
it("returns a search error when a module is not found", function()
6+
local filename, search_err = teal.search_module("bar.foooo")
7+
assert.same(nil, filename)
8+
assert.is_table(search_err)
9+
assert.match("no file '.*bar[/\\]foooo.tl'", search_err[1])
10+
end)
11+
12+
it("processes a module file given a Lua module name", function()
13+
util.mock_io(finally, {
14+
["bar/foo.tl"] = [[
15+
local record Foo<T>
16+
bar: T
17+
end
18+
local type FooInteger = Foo<integer>
19+
return FooInteger
20+
]],
21+
})
22+
23+
local filename, search_err = teal.search_module("bar.foo")
24+
assert.matches("^.[/\\]bar[/\\]foo.tl$", filename)
25+
assert.same(nil, search_err)
26+
end)
27+
end)

spec/api/v3/version_spec.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
local util = require("spec.util")
2+
local teal = require("teal")
3+
4+
describe("teal.version", function()
5+
it("reports a version string", function()
6+
local v = teal.version()
7+
assert.is_string(v)
8+
end)
9+
end)

spec/api/v3/warning_set_spec.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
local util = require("spec.util")
2+
local teal = require("teal")
3+
4+
describe("teal.warning_set", function()
5+
it("reports the set of warnings", function()
6+
local set = teal.warning_set()
7+
local n = 0
8+
for k, v in pairs(set) do
9+
n = n + 1
10+
assert.is_string(k)
11+
assert.same(true, v)
12+
end
13+
assert(n > 0)
14+
end)
15+
end)

0 commit comments

Comments
 (0)