Use assert for type checking

This commit is contained in:
2025-06-19 21:38:34 +10:00
parent 1c821d8804
commit 1ebeaa9e95
4 changed files with 23 additions and 31 deletions

View File

@@ -81,7 +81,7 @@ fn local_fn() {
fn check(self: string, arg: number) {}
inner
}),
r#"function()local function check(self,arg)do if type(self)=="number"then self=tostring(self);elseif type(self)~="string"then return error("string expected in \'self\', got "..type(self));end;end;do local _arg=arg;arg=tonumber(arg);if arg==nil then return error("number expected in \'arg\', got "..type(_arg));end;end;end;return inner;end"#
r#"function()local function check(self,arg)do if type(self)=="number"then self=tostring(self);else assert(type(self)=="string","string expected in \'self\', got "..type(self));end;end;do local _arg=arg;arg=tonumber(arg);assert(arg~=nil,"number expected in \'arg\', got "..type(_arg));end;end;return inner;end"#
);
}
@@ -212,19 +212,19 @@ fn type_checks() {
assert_eq!(luaify!(|s| {}), r#"function(s)end"#);
assert_eq!(
luaify!(|s: table| {}),
r#"function(s)do if type(s)~="table"then return error("table expected in \'s\', got "..type(s));end;end;end"#
r#"function(s)do assert(type(s)=="table","table expected in \'s\', got "..type(s));end;end"#
);
assert_eq!(
luaify!(|s| { s as string }),
r#"function(s)do if type(s)=="number"then s=tostring(s);elseif type(s)~="string"then return error("string expected in \'s\', got "..type(s));end;end;end"#
r#"function(s)do if type(s)=="number"then s=tostring(s);else assert(type(s)=="string","string expected in \'s\', got "..type(s));end;end;end"#
);
assert_eq!(
luaify!(|s| { s as number }),
r#"function(s)do local _s=s;s=tonumber(s);if s==nil then return error("number expected in \'s\', got "..type(_s));end;end;end"#
r#"function(s)do local _s=s;s=tonumber(s);assert(s~=nil,"number expected in \'s\', got "..type(_s));end;end"#
);
assert_eq!(
luaify!(|s| { s as nil }),
r#"function(s)do if s~=nil then return error("nil expected in \'s\', got "..type(s));end;end;end"#
r#"function(s)do assert(s==nil,"nil expected in \'s\', got "..type(s));end;end"#
);
assert_eq!(luaify!(|s| { s as any }), r#"function(s)do end;end"#);
@@ -234,7 +234,7 @@ fn type_checks() {
ok as boolean;
res as nil;
}),
r#"function(s)local ok,res=coroutine.yield(thread);do if type(ok)~="boolean"then return error("boolean expected in \'ok\', got "..type(ok));end;end;do if res~=nil then return error("nil expected in \'res\', got "..type(res));end;end;end"#
r#"function(s)local ok,res=coroutine.yield(thread);do assert(type(ok)=="boolean","boolean expected in \'ok\', got "..type(ok));end;do assert(res==nil,"nil expected in \'res\', got "..type(res));end;end"#
);
}