use std::io;
use luaur_rt::{Error, Lua, Result};
#[test]
fn test_external_error() {
let runtime_err = Error::runtime("test error");
let converted = Error::external(runtime_err);
assert!(matches!(converted, Error::RuntimeError(ref msg) if msg == "test error"));
let converted = Error::external(io::Error::other("other error"));
assert!(matches!(converted, Error::ExternalError(_)));
assert!(converted.downcast_ref::<io::Error>().is_some());
}
#[test]
fn test_rust_error_surfaces_to_lua() -> Result<()> {
let lua = Lua::new();
let func =
lua.create_function(|_, ()| -> Result<()> { Err(Error::runtime("runtime error")) })?;
lua.globals().set("func", func)?;
let msg = lua
.load("local _, err = pcall(func); return tostring(err)")
.eval::<String>()?;
assert!(msg.contains("runtime error"), "got: {msg}");
Ok(())
}
#[test]
fn test_external_io_error_message_preserved() -> Result<()> {
let lua = Lua::new();
let func = lua.create_function(|_, ()| -> Result<()> {
Err(Error::external(io::Error::other("disk on fire")))
})?;
lua.globals().set("func", func)?;
let msg = lua
.load("local _, err = pcall(func); return tostring(err)")
.eval::<String>()?;
assert!(msg.contains("disk on fire"), "got: {msg}");
Ok(())
}
#[test]
fn test_error_display() {
assert_eq!(Error::runtime("boom").to_string(), "runtime error: boom");
let conv = Error::FromLuaConversionError {
from: "nil",
to: "String".to_string(),
message: None,
};
assert_eq!(conv.to_string(), "error converting Lua nil to String");
}