Fix xpcall return values, make the tests a lot better.
This commit is contained in:
parent
773bf3e9ba
commit
deab88a851
28
src/tests.rs
28
src/tests.rs
|
@ -528,39 +528,51 @@ fn coroutine_panic() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_pcall_xpcall() {
|
fn test_pcall_xpcall() {
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
|
let globals = lua.globals();
|
||||||
|
|
||||||
// make sure that we handle not enough arguments
|
// make sure that we handle not enough arguments
|
||||||
assert!(lua.exec::<()>("pcall()", None).is_err());
|
assert!(lua.exec::<()>("pcall()", None).is_err());
|
||||||
assert!(lua.exec::<()>("xpcall()", None).is_err());
|
assert!(lua.exec::<()>("xpcall()", None).is_err());
|
||||||
assert!(lua.exec::<()>("xpcall(function() end)", None).is_err());
|
assert!(lua.exec::<()>("xpcall(function() end)", None).is_err());
|
||||||
|
|
||||||
|
// Make sure that the return values from are correct on success
|
||||||
|
assert_eq!(lua.eval::<(bool, String)>("pcall(function(p) return p end, 'foo')", None).unwrap(), (true, "foo".to_owned()));
|
||||||
|
assert_eq!(lua.eval::<(bool, String)>("xpcall(function(p) return p end, print, 'foo')", None).unwrap(), (true, "foo".to_owned()));
|
||||||
|
|
||||||
|
// Make sure that the return values are correct on errors, and that error handling works
|
||||||
|
|
||||||
lua.exec::<()>(
|
lua.exec::<()>(
|
||||||
r#"
|
r#"
|
||||||
pcall_error = nil
|
pcall_error = nil
|
||||||
_, pcall_error = pcall(error, "testerror")
|
pcall_status, pcall_error = pcall(error, "testerror")
|
||||||
|
|
||||||
xpcall_error = nil
|
xpcall_error = nil
|
||||||
xpcall(error, function(err) xpcall_error = err end, "testerror")
|
xpcall_status, _ = xpcall(error, function(err) xpcall_error = err end, "testerror")
|
||||||
|
|
||||||
function xpcall_recursion()
|
|
||||||
xpcall(error, function(err) error(err) end, "testerror")
|
|
||||||
end
|
|
||||||
"#,
|
"#,
|
||||||
None,
|
None,
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
let globals = lua.globals();
|
assert_eq!(globals.get::<_, bool>("pcall_status").unwrap(), false);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
globals.get::<_, String>("pcall_error").unwrap(),
|
globals.get::<_, String>("pcall_error").unwrap(),
|
||||||
"testerror"
|
"testerror"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assert_eq!(globals.get::<_, bool>("xpcall_statusr").unwrap(), false);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
globals.get::<_, String>("xpcall_error").unwrap(),
|
globals.get::<_, String>("xpcall_error").unwrap(),
|
||||||
"testerror"
|
"testerror"
|
||||||
);
|
);
|
||||||
|
|
||||||
// Make sure that weird xpcall error recursion at least doesn't cause unsafety or panics.
|
// Make sure that weird xpcall error recursion at least doesn't cause unsafety or panics.
|
||||||
|
lua.exec::<()>(
|
||||||
|
r#"
|
||||||
|
function xpcall_recursion()
|
||||||
|
xpcall(error, function(err) error(err) end, "testerror")
|
||||||
|
end
|
||||||
|
"#,
|
||||||
|
None,
|
||||||
|
).unwrap();
|
||||||
let _ = globals
|
let _ = globals
|
||||||
.get::<_, Function>("xpcall_recursion")
|
.get::<_, Function>("xpcall_recursion")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
|
@ -497,8 +497,8 @@ pub unsafe extern "C" fn safe_xpcall(state: *mut ffi::lua_State) -> c_int {
|
||||||
2
|
2
|
||||||
} else {
|
} else {
|
||||||
ffi::lua_pushboolean(state, 1);
|
ffi::lua_pushboolean(state, 1);
|
||||||
ffi::lua_insert(state, 1);
|
ffi::lua_insert(state, 2);
|
||||||
ffi::lua_gettop(state)
|
ffi::lua_gettop(state) - 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue