diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/async.rs | 2 | ||||
-rw-r--r-- | tests/conversion.rs | 175 | ||||
-rw-r--r-- | tests/serde.rs | 2 | ||||
-rw-r--r-- | tests/tests.rs | 37 | ||||
-rw-r--r-- | tests/thread.rs | 2 | ||||
-rw-r--r-- | tests/userdata.rs | 6 |
6 files changed, 184 insertions, 40 deletions
diff --git a/tests/async.rs b/tests/async.rs index 7055914..9c4c865 100644 --- a/tests/async.rs +++ b/tests/async.rs @@ -443,7 +443,7 @@ async fn test_async_userdata() -> Result<()> { let globals = lua.globals(); let userdata = lua.create_userdata(MyUserData(11))?; - globals.set("userdata", userdata.clone())?; + globals.set("userdata", &userdata)?; lua.load( r#" diff --git a/tests/conversion.rs b/tests/conversion.rs index 6d17d0a..8dc0ad2 100644 --- a/tests/conversion.rs +++ b/tests/conversion.rs @@ -3,7 +3,180 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::ffi::{CStr, CString}; use maplit::{btreemap, btreeset, hashmap, hashset}; -use mlua::{Error, Lua, Result}; +use mlua::{AnyUserData, Error, Function, IntoLua, Lua, Result, Table, Thread, UserDataRef, Value}; + +#[test] +fn test_string_into_lua() -> Result<()> { + let lua = Lua::new(); + + // Direct conversion + let s = lua.create_string("hello, world!")?; + let s2 = (&s).into_lua(&lua)?; + assert_eq!(s, s2.as_string().unwrap()); + + // Push into stack + let table = lua.create_table()?; + table.set("s", &s)?; + assert_eq!(s, table.get::<_, String>("s")?); + + Ok(()) +} + +#[test] +fn test_table_into_lua() -> Result<()> { + let lua = Lua::new(); + + // Direct conversion + let t = lua.create_table()?; + let t2 = (&t).into_lua(&lua)?; + assert_eq!(&t, t2.as_table().unwrap()); + + // Push into stack + let f = lua.create_function(|_, (t, s): (Table, String)| t.set("s", s))?; + f.call((&t, "hello"))?; + assert_eq!("hello", t.get::<_, String>("s")?); + + Ok(()) +} + +#[cfg(all(feature = "unstable", not(feature = "send")))] +#[test] +fn test_owned_table_into_lua() -> Result<()> { + let lua = Lua::new(); + + // Direct conversion + let t = lua.create_table()?.into_owned(); + let t2 = (&t).into_lua(&lua)?; + assert_eq!(t.to_ref(), *t2.as_table().unwrap()); + + // Push into stack + let f = lua.create_function(|_, (t, s): (Table, String)| t.set("s", s))?; + f.call((&t, "hello"))?; + assert_eq!("hello", t.to_ref().get::<_, String>("s")?); + + Ok(()) +} + +#[test] +fn test_function_into_lua() -> Result<()> { + let lua = Lua::new(); + + // Direct conversion + let f = lua.create_function(|_, ()| Ok::<_, Error>(()))?; + let f2 = (&f).into_lua(&lua)?; + assert_eq!(&f, f2.as_function().unwrap()); + + // Push into stack + let table = lua.create_table()?; + table.set("f", &f)?; + assert_eq!(f, table.get::<_, Function>("f")?); + + Ok(()) +} + +#[cfg(all(feature = "unstable", not(feature = "send")))] +#[test] +fn test_owned_function_into_lua() -> Result<()> { + let lua = Lua::new(); + + // Direct conversion + let f = lua + .create_function(|_, ()| Ok::<_, Error>(()))? + .into_owned(); + let f2 = (&f).into_lua(&lua)?; + assert_eq!(f.to_ref(), *f2.as_function().unwrap()); + + // Push into stack + let table = lua.create_table()?; + table.set("f", &f)?; + assert_eq!(f.to_ref(), table.get::<_, Function>("f")?); + + Ok(()) +} + +#[test] +fn test_thread_into_lua() -> Result<()> { + let lua = Lua::new(); + + // Direct conversion + let f = lua.create_function(|_, ()| Ok::<_, Error>(()))?; + let th = lua.create_thread(f)?; + let th2 = (&th).into_lua(&lua)?; + assert_eq!(&th, th2.as_thread().unwrap()); + + // Push into stack + let table = lua.create_table()?; + table.set("th", &th)?; + assert_eq!(th, table.get::<_, Thread>("th")?); + + Ok(()) +} + +#[test] +fn test_anyuserdata_into_lua() -> Result<()> { + let lua = Lua::new(); + + // Direct conversion + let ud = lua.create_any_userdata(String::from("hello"))?; + let ud2 = (&ud).into_lua(&lua)?; + assert_eq!(&ud, ud2.as_userdata().unwrap()); + + // Push into stack + let table = lua.create_table()?; + table.set("ud", &ud)?; + assert_eq!(ud, table.get::<_, AnyUserData>("ud")?); + assert_eq!("hello", *table.get::<_, UserDataRef<String>>("ud")?); + + Ok(()) +} + +#[cfg(all(feature = "unstable", not(feature = "send")))] +#[test] +fn test_owned_anyuserdata_into_lua() -> Result<()> { + let lua = Lua::new(); + + // Direct conversion + let ud = lua.create_any_userdata(String::from("hello"))?.into_owned(); + let ud2 = (&ud).into_lua(&lua)?; + assert_eq!(ud.to_ref(), *ud2.as_userdata().unwrap()); + + // Push into stack + let table = lua.create_table()?; + table.set("ud", &ud)?; + assert_eq!(ud.to_ref(), table.get::<_, AnyUserData>("ud")?); + assert_eq!("hello", *table.get::<_, UserDataRef<String>>("ud")?); + + Ok(()) +} + +#[test] +fn test_registry_value_into_lua() -> Result<()> { + let lua = Lua::new(); + + let t = lua.create_table()?; + let r = lua.create_registry_value(t)?; + let f = lua.create_function(|_, t: Table| t.raw_set("hello", "world"))?; + + f.call(&r)?; + let v = r.into_lua(&lua)?; + let t = v.as_table().unwrap(); + assert_eq!(t.get::<_, String>("hello")?, "world"); + + // Try to set nil registry key + let r_nil = lua.create_registry_value(Value::Nil)?; + t.set("hello", &r_nil)?; + assert_eq!(t.get::<_, Value>("hello")?, Value::Nil); + + // Check non-owned registry key + let lua2 = Lua::new(); + let r2 = lua2.create_registry_value("abc")?; + assert!(matches!( + f.call::<_, ()>(&r2), + Err(Error::MismatchedRegistryKey) + )); + + Ok(()) +} #[test] fn test_conv_vec() -> Result<()> { diff --git a/tests/serde.rs b/tests/serde.rs index 1fdd709..168407f 100644 --- a/tests/serde.rs +++ b/tests/serde.rs @@ -598,7 +598,7 @@ fn test_from_value_with_options() -> Result<(), Box<dyn StdError>> { // Check recursion when using `Serialize` impl let t = lua.create_table()?; - t.set("t", t.clone())?; + t.set("t", &t)?; assert!(serde_json::to_string(&t).is_err()); // Serialize Lua globals table diff --git a/tests/tests.rs b/tests/tests.rs index 55f283d..0310a82 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -8,8 +8,8 @@ use std::sync::Arc; use std::{error, f32, f64, fmt}; use mlua::{ - ChunkMode, Error, ExternalError, Function, IntoLua, Lua, LuaOptions, Nil, Result, StdLib, - String, Table, UserData, Value, Variadic, + ChunkMode, Error, ExternalError, Function, Lua, LuaOptions, Nil, Result, StdLib, String, Table, + UserData, Value, Variadic, }; #[cfg(not(feature = "luau"))] @@ -780,35 +780,6 @@ fn test_registry_value() -> Result<()> { } #[test] -fn test_registry_value_into_lua() -> Result<()> { - let lua = Lua::new(); - - let t = lua.create_table()?; - let r = lua.create_registry_value(t)?; - let f = lua.create_function(|_, t: Table| t.raw_set("hello", "world"))?; - - f.call(&r)?; - let v = r.into_lua(&lua)?; - let t = v.as_table().unwrap(); - assert_eq!(t.get::<_, String>("hello")?, "world"); - - // Try to set nil registry key - let r_nil = lua.create_registry_value(Value::Nil)?; - t.set("hello", &r_nil)?; - assert_eq!(t.get::<_, Value>("hello")?, Value::Nil); - - // Check non-owned registry key - let lua2 = Lua::new(); - let r2 = lua2.create_registry_value("abc")?; - assert!(matches!( - f.call::<_, ()>(&r2), - Err(Error::MismatchedRegistryKey) - )); - - Ok(()) -} - -#[test] fn test_drop_registry_value() -> Result<()> { struct MyUserdata(Arc<()>); @@ -994,7 +965,7 @@ fn test_recursion() -> Result<()> { Ok(()) })?; - lua.globals().set("f", f.clone())?; + lua.globals().set("f", &f)?; f.call::<_, ()>(1)?; Ok(()) @@ -1032,7 +1003,7 @@ fn test_too_many_recursions() -> Result<()> { let f = lua .create_function(move |lua, ()| lua.globals().get::<_, Function>("f")?.call::<_, ()>(()))?; - lua.globals().set("f", f.clone())?; + lua.globals().set("f", &f)?; assert!(f.call::<_, ()>(()).is_err()); Ok(()) diff --git a/tests/thread.rs b/tests/thread.rs index 9a84a2c..0473e88 100644 --- a/tests/thread.rs +++ b/tests/thread.rs @@ -182,7 +182,7 @@ fn test_coroutine_panic() { let thrd_main = lua.create_function(|_, ()| -> Result<()> { panic!("test_panic"); })?; - lua.globals().set("main", thrd_main.clone())?; + lua.globals().set("main", &thrd_main)?; let thrd: Thread = lua.create_thread(thrd_main)?; thrd.resume(()) }) { diff --git a/tests/userdata.rs b/tests/userdata.rs index da6c40d..141e329 100644 --- a/tests/userdata.rs +++ b/tests/userdata.rs @@ -58,7 +58,7 @@ fn test_methods() -> Result<()> { fn check_methods(lua: &Lua, userdata: AnyUserData) -> Result<()> { let globals = lua.globals(); - globals.set("userdata", userdata.clone())?; + globals.set("userdata", &userdata)?; lua.load( r#" function get_it() @@ -342,7 +342,7 @@ fn test_userdata_take() -> Result<()> { } fn check_userdata_take(lua: &Lua, userdata: AnyUserData, rc: Arc<i64>) -> Result<()> { - lua.globals().set("userdata", userdata.clone())?; + lua.globals().set("userdata", &userdata)?; assert_eq!(Arc::strong_count(&rc), 3); { let _value = userdata.borrow::<MyUserdata>()?; @@ -474,7 +474,7 @@ fn test_functions() -> Result<()> { let lua = Lua::new(); let globals = lua.globals(); let userdata = lua.create_userdata(MyUserData(42))?; - globals.set("userdata", userdata.clone())?; + globals.set("userdata", &userdata)?; lua.load( r#" function get_it() |