use luaur_rt::{Lua, Result};
fn err_message(lua: &Lua, src: &str, name: &str) -> String {
let e = lua
.load(src)
.set_name(name)
.exec()
.expect_err("chunk was expected to raise a runtime error");
e.to_string()
}
#[test]
fn test_error_call_has_chunk_and_line_prefix() {
let lua = Lua::new();
let msg = err_message(&lua, "\n\nerror('boom')\n", "myChunk");
assert!(msg.contains("boom"), "missing message text: {msg}");
assert!(
msg.contains("myChunk"),
"missing chunk name in position prefix (pusherror chunkid): {msg}"
);
assert!(
msg.contains(":3:"),
"missing line-3 position from pusherror format: {msg}"
);
}
#[test]
fn test_runtime_type_error_has_position() {
let lua = Lua::new();
let msg = err_message(&lua, "local t = nil\nreturn t.x\n", "idxChunk");
assert!(
msg.to_lowercase().contains("index"),
"expected an index error: {msg}"
);
assert!(msg.contains(":2:"), "missing line-2 position: {msg}");
}
#[test]
fn test_error_line_number_is_accurate() {
let lua = Lua::new();
let msg = err_message(&lua, "\n\n\n\n\nerror('deep')\n", "lineChunk");
assert!(msg.contains("deep"), "missing message: {msg}");
assert!(
msg.contains(":6:"),
"expected the fault on line 6 (pusherror line arg): {msg}"
);
}
#[test]
fn test_coroutine_resume_error_has_position() -> Result<()> {
let lua = Lua::new();
let func = lua
.load("\nerror('co-boom')\n") .set_name("coChunk")
.into_function()?;
let thread = lua.create_thread(func)?;
let err = thread
.resume::<()>(())
.expect_err("coroutine was expected to error");
let msg = err.to_string();
assert!(msg.contains("co-boom"), "missing message: {msg}");
assert!(
msg.contains(":2:"),
"missing position from coroutine-resume error path: {msg}"
);
Ok(())
}