More tests

This commit is contained in:
Alex Orlenko 2021-04-11 13:19:12 +01:00
parent 41a1a0d15a
commit c7541ef7d3
4 changed files with 106 additions and 2 deletions

View File

@ -66,6 +66,7 @@ reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1.0", features = ["full"] }
futures-timer = "3.0"
serde_json = "1.0"
maplit = "1.0"
[[bench]]
name = "benchmark"

View File

@ -708,7 +708,10 @@ impl<'lua> AnyUserData<'lua> {
lua.pop_value()
};
#[cfg(any(feature = "lua52", feature = "lua51", feature = "luajit"))]
return Table::from_lua(res, lua)?.get(1);
return match <Option<Table>>::from_lua(res, lua)? {
Some(t) => t.get(1),
None => V::from_lua(Value::Nil, lua),
};
#[cfg(any(feature = "lua54", feature = "lua53"))]
V::from_lua(res, lua)
}

90
tests/conversion.rs Normal file
View File

@ -0,0 +1,90 @@
use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::ffi::CString;
use maplit::{btreemap, btreeset, hashmap, hashset};
use mlua::{Lua, Result};
#[test]
fn test_conv_vec() -> Result<()> {
let lua = Lua::new();
let v = vec![1, 2, 3];
lua.globals().set("v", v.clone())?;
let v2: Vec<i32> = lua.globals().get("v")?;
assert!(v == v2);
Ok(())
}
#[test]
fn test_conv_hashmap() -> Result<()> {
let lua = Lua::new();
let map = hashmap! {"hello".to_string() => "world".to_string()};
lua.globals().set("map", map.clone())?;
let map2: HashMap<String, String> = lua.globals().get("map")?;
assert!(map == map2);
Ok(())
}
#[test]
fn test_conv_hashset() -> Result<()> {
let lua = Lua::new();
let set = hashset! {"hello".to_string(), "world".to_string()};
lua.globals().set("set", set.clone())?;
let set2: HashSet<String> = lua.globals().get("set")?;
assert!(set == set2);
Ok(())
}
#[test]
fn test_conv_btreemap() -> Result<()> {
let lua = Lua::new();
let map = btreemap! {"hello".to_string() => "world".to_string()};
lua.globals().set("map", map.clone())?;
let map2: BTreeMap<String, String> = lua.globals().get("map")?;
assert!(map == map2);
Ok(())
}
#[test]
fn test_conv_btreeset() -> Result<()> {
let lua = Lua::new();
let set = btreeset! {"hello".to_string(), "world".to_string()};
lua.globals().set("set", set.clone())?;
let set2: BTreeSet<String> = lua.globals().get("set")?;
assert!(set == set2);
Ok(())
}
#[test]
fn test_conv_cstring() -> Result<()> {
let lua = Lua::new();
let s = CString::new(b"hello".to_vec()).unwrap();
lua.globals().set("s", s.clone())?;
let s2: CString = lua.globals().get("s")?;
assert!(s == s2);
Ok(())
}
#[test]
fn test_conv_cow() -> Result<()> {
let lua = Lua::new();
let s = Cow::from("hello");
lua.globals().set("s", s.clone())?;
let s2: String = lua.globals().get("s")?;
assert!(s == s2);
Ok(())
}

View File

@ -295,7 +295,7 @@ fn test_functions() -> Result<()> {
methods.add_function("get_value", |_, ud: AnyUserData| {
Ok(ud.borrow::<MyUserData>()?.0)
});
methods.add_function("set_value", |_, (ud, value): (AnyUserData, i64)| {
methods.add_function_mut("set_value", |_, (ud, value): (AnyUserData, i64)| {
ud.borrow_mut::<MyUserData>()?.0 = value;
Ok(())
});
@ -349,6 +349,11 @@ fn test_fields() -> Result<()> {
Ok(())
});
// Use userdata "uservalue" storage
fields.add_field_function_get("uval", |_, ud| ud.get_user_value::<Option<String>>());
fields
.add_field_function_set("uval", |_, ud, s| ud.set_user_value::<Option<String>>(s));
fields.add_meta_field_with(MetaMethod::Index, |lua| {
let index = lua.create_table()?;
index.set("f", 321)?;
@ -365,6 +370,11 @@ fn test_fields() -> Result<()> {
assert(ud.val == 7)
ud.val = 10
assert(ud.val == 10)
assert(ud.uval == nil)
ud.uval = "hello"
assert(ud.uval == "hello")
assert(ud.f == 321)
"#,
)