mirror of
https://github.com/mlua-rs/mlua
synced 2026-06-08 16:05:43 +00:00
Tests for the ErrorContext trait
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
use mlua::{Error, ErrorContext, Lua, Result};
|
||||
|
||||
#[test]
|
||||
fn test_error_context() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let func = lua.create_function(|_, ()| {
|
||||
Err::<(), _>(Error::RuntimeError("runtime error".into())).context("some context")
|
||||
})?;
|
||||
lua.globals().set("func", func)?;
|
||||
|
||||
let msg = lua
|
||||
.load("local _, err = pcall(func); return tostring(err)")
|
||||
.eval::<String>()?;
|
||||
assert!(msg.contains("some context"));
|
||||
assert!(msg.contains("runtime error"));
|
||||
|
||||
let func2 = lua.create_function(|lua, ()| {
|
||||
lua.globals()
|
||||
.get::<_, String>("nonextant")
|
||||
.with_context(|_| "failed to find global")
|
||||
})?;
|
||||
lua.globals().set("func2", func2)?;
|
||||
|
||||
let msg2 = lua
|
||||
.load("local _, err = pcall(func2); return tostring(err)")
|
||||
.eval::<String>()?;
|
||||
assert!(msg2.contains("failed to find global"));
|
||||
println!("{msg2}");
|
||||
assert!(msg2.contains("error converting Lua nil to String"));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user