Skip to content

Commit acc1a27

Browse files
committed
tests: more type tests
1 parent 2c5465c commit acc1a27

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

spec/checker_spec.lua

+65
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,56 @@ describe("Titan type checker", function()
113113
assert.truthy(ok)
114114
end)
115115

116+
it("type-checks 'while'", function()
117+
local code = [[
118+
function fn(x: integer): integer
119+
local i: integer = 15
120+
while x < 100 do
121+
x = x + i
122+
end
123+
return x
124+
end
125+
]]
126+
local ast, err = parser.parse(code)
127+
local ok, err = checker.check(ast, code, "test.titan")
128+
assert.truthy(ok)
129+
end)
130+
131+
it("type-checks 'if'", function()
132+
local code = [[
133+
function fn(x: integer): integer
134+
local i: integer = 15
135+
if x < 100 then
136+
x = x + i
137+
elseif x > 100 then
138+
x = x - i
139+
else
140+
x = 100
141+
end
142+
return x
143+
end
144+
]]
145+
local ast, err = parser.parse(code)
146+
local ok, err = checker.check(ast, code, "test.titan")
147+
assert.truthy(ok)
148+
end)
149+
150+
it("checks code inside the 'while' black", function()
151+
local code = [[
152+
function fn(x: integer): integer
153+
local i: integer = 15
154+
while i do
155+
local s: string = i
156+
end
157+
return x
158+
end
159+
]]
160+
local ast, err = parser.parse(code)
161+
local ok, err = checker.check(ast, code, "test.titan")
162+
assert.falsy(ok)
163+
assert.match("expected string but found integer", err)
164+
end)
165+
116166
it("type-checks 'for' with a step", function()
117167
local code = [[
118168
function fn(x: integer): integer
@@ -144,6 +194,21 @@ describe("Titan type checker", function()
144194
assert.match("'for' start expression", err)
145195
end)
146196

197+
it("catches 'for' errors in the control variable", function()
198+
local code = [[
199+
function fn(x: integer, s: string): integer
200+
for i: string = 1, s, 2 do
201+
x = x + i
202+
end
203+
return x
204+
end
205+
]]
206+
local ast, err = parser.parse(code)
207+
local ok, err = checker.check(ast, code, "test.titan")
208+
assert.falsy(ok)
209+
assert.match("control variable", err)
210+
end)
211+
147212
it("catches 'for' errors in the finish expression", function()
148213
local code = [[
149214
function fn(x: integer, s: string): integer

0 commit comments

Comments
 (0)