use omnilua::{Lua, LuaVersion};
fn eval_bool(version: LuaVersion, code: &str) -> bool {
let lua = Lua::new_versioned(version);
lua.load(code)
.eval()
.unwrap_or_else(|e| panic!("eval of `{code}` failed under {version:?}: {e:?}"))
}
fn tostring_honors_name(version: LuaVersion) -> bool {
eval_bool(
version,
r#"return tostring(setmetatable({}, {__name = "ZZname"})):sub(1, 6) == "ZZname""#,
)
}
fn typeerror_honors_name(version: LuaVersion) -> bool {
eval_bool(
version,
r#"local ok, e = pcall(math.abs, setmetatable({}, {__name = "ZZname"}))
return (not ok) and e:find("ZZname") ~= nil"#,
)
}
#[test]
fn name_metafield_in_tostring_is_5_3_plus_only() {
assert!(!tostring_honors_name(LuaVersion::V51), "5.1 must ignore __name in tostring");
assert!(!tostring_honors_name(LuaVersion::V52), "5.2 must ignore __name in tostring");
assert!(tostring_honors_name(LuaVersion::V53), "5.3 must honor __name in tostring");
assert!(tostring_honors_name(LuaVersion::V54), "5.4 must honor __name in tostring");
assert!(tostring_honors_name(LuaVersion::V55), "5.5 must honor __name in tostring");
}
#[test]
fn name_metafield_in_typeerror_is_5_3_plus_only() {
assert!(!typeerror_honors_name(LuaVersion::V51), "5.1 type error must say 'table', not __name");
assert!(!typeerror_honors_name(LuaVersion::V52), "5.2 type error must say 'table', not __name");
assert!(typeerror_honors_name(LuaVersion::V53), "5.3 type error must use __name");
assert!(typeerror_honors_name(LuaVersion::V54), "5.4 type error must use __name");
assert!(typeerror_honors_name(LuaVersion::V55), "5.5 type error must use __name");
}