use std::sync::{Arc, Mutex};
use luaur_rt::{DebugWhat, Lua, Result};
#[test]
fn test_debug_format_deferred() -> Result<()> {
let lua = Lua::new();
let globals = lua.globals();
let dump = format!("{globals:#?}");
assert!(!dump.is_empty());
Ok(())
}
#[test]
fn test_inspect_stack() -> Result<()> {
let lua = Lua::new();
let captured = Arc::new(Mutex::new(None));
let cap2 = captured.clone();
let probe = lua.create_function(move |lua, ()| {
let caller = lua.inspect_stack(1);
*cap2.lock().unwrap() = caller.map(|d| (d.what(), d.current_line()));
Ok(())
})?;
lua.globals().set("probe", probe)?;
lua.load(
r#"
local x = 1
probe()
"#,
)
.exec()?;
let (what, line) = captured
.lock()
.unwrap()
.clone()
.expect("inspect_stack found the caller frame");
assert!(
matches!(what, DebugWhat::Main | DebugWhat::Lua),
"caller frame is a Lua/main chunk, got {what:?}"
);
assert!(line.is_some(), "Lua frame exposes a current line");
Ok(())
}
#[test]
fn test_inspect_stack_self() -> Result<()> {
let lua = Lua::new();
let what = Arc::new(Mutex::new(None));
let what2 = what.clone();
let probe = lua.create_function(move |lua, ()| {
*what2.lock().unwrap() = lua.inspect_stack(0).map(|d| d.what());
Ok(())
})?;
probe.call::<()>(())?;
let got = *what.lock().unwrap();
assert_eq!(got, Some(DebugWhat::C));
Ok(())
}