use lua_rs_runtime::{Lua, LuaVersion, Value};
#[test]
fn v55_reports_its_version() {
let lua = Lua::new_versioned(LuaVersion::V55);
assert_eq!(lua.version(), LuaVersion::V55);
let from_global: String = lua.globals().get("_VERSION").unwrap();
assert_eq!(from_global, "Lua 5.5");
let from_script: String = lua.load("return _VERSION").eval().unwrap();
assert_eq!(from_script, "Lua 5.5");
}
#[test]
fn v55_runs_a_trivial_script() {
let lua = Lua::new_versioned(LuaVersion::V55);
let sum: i64 = lua
.load("local t = 0; for i = 1, 10 do t = t + i end; return t")
.eval()
.unwrap();
assert_eq!(sum, 55);
}
#[test]
fn v55_global_decl_statement_parses() {
let lua = Lua::new_versioned(LuaVersion::V55);
let ok: i64 = lua
.load("global x; x = 41; return x + 1")
.eval()
.unwrap();
assert_eq!(ok, 42);
lua.load("global a, b <const>, c = 1, 2, 3")
.exec()
.expect("5.5 global name-list with attribute should parse");
lua.load("global *").exec().expect("5.5 `global *` should parse");
lua.load("global <const> *")
.exec()
.expect("5.5 `global <const> *` should parse");
}
#[test]
fn global_is_an_ordinary_identifier_under_54() {
let lua54 = Lua::new_versioned(LuaVersion::V54);
let v: i64 = lua54
.load("local global = 7; return global * 6")
.eval()
.unwrap();
assert_eq!(v, 42);
}
#[test]
fn table_create_present_under_55_absent_under_54() {
let lua54 = Lua::new_versioned(LuaVersion::V54);
let absent: bool = lua54.load("return table.create == nil").eval().unwrap();
assert!(absent, "table.create must be absent under 5.4");
let lua55 = Lua::new_versioned(LuaVersion::V55);
let present: Value = lua55.load("return table.create").eval().unwrap();
assert!(
!matches!(present, Value::Nil),
"table.create must exist under 5.5"
);
let len: i64 = lua55
.load("local t = table.create(8); t[1] = 'a'; t[2] = 'b'; return #t")
.eval()
.unwrap();
assert_eq!(len, 2);
let empty: i64 = lua55
.load("return #table.create(4, 2)")
.eval()
.unwrap();
assert_eq!(empty, 0);
}