@@ -113,6 +113,56 @@ describe("Titan type checker", function()
113
113
assert .truthy (ok )
114
114
end )
115
115
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
+
116
166
it (" type-checks 'for' with a step" , function ()
117
167
local code = [[
118
168
function fn(x: integer): integer
@@ -144,6 +194,21 @@ describe("Titan type checker", function()
144
194
assert .match (" 'for' start expression" , err )
145
195
end )
146
196
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
+
147
212
it (" catches 'for' errors in the finish expression" , function ()
148
213
local code = [[
149
214
function fn(x: integer, s: string): integer
0 commit comments