summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Orlenko <zxteam@protonmail.com>2023-03-17 01:31:07 +0000
committerAlex Orlenko <zxteam@protonmail.com>2023-03-17 01:31:07 +0000
commit4bc6501d2effb826213d6a56eee4e20f5d60b49b (patch)
treeb613299d0db0ea271023714096e3a0b362d9ccac
parent8d8032173891bda1e1514e370cc4c60d9c157f30 (diff)
downloadmlua-4bc6501d2effb826213d6a56eee4e20f5d60b49b.zip
Tests for the `ErrorContext` trait
-rw-r--r--tests/error.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/error.rs b/tests/error.rs
new file mode 100644
index 0000000..2e88736
--- /dev/null
+++ b/tests/error.rs
@@ -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(())
+}