2021-04-11 08:19:12 -04:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
2021-06-16 19:11:58 -04:00
|
|
|
use std::ffi::{CStr, CString};
|
2021-04-11 08:19:12 -04:00
|
|
|
|
|
|
|
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")?;
|
2021-06-24 20:49:09 -04:00
|
|
|
assert_eq!(v, v2);
|
2021-04-11 08:19:12 -04:00
|
|
|
|
|
|
|
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")?;
|
2021-06-24 20:49:09 -04:00
|
|
|
assert_eq!(map, map2);
|
2021-04-11 08:19:12 -04:00
|
|
|
|
|
|
|
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")?;
|
2021-06-24 20:49:09 -04:00
|
|
|
assert_eq!(set, set2);
|
|
|
|
|
|
|
|
let set3 = lua.load(r#"{"a", "b", "c"}"#).eval::<HashSet<String>>()?;
|
|
|
|
assert_eq!(set3, hashset! { "a".into(), "b".into(), "c".into() });
|
2021-04-11 08:19:12 -04:00
|
|
|
|
|
|
|
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")?;
|
2021-06-24 20:49:09 -04:00
|
|
|
assert_eq!(map, map2);
|
2021-04-11 08:19:12 -04:00
|
|
|
|
|
|
|
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")?;
|
2021-06-24 20:49:09 -04:00
|
|
|
assert_eq!(set, set2);
|
|
|
|
|
|
|
|
let set3 = lua.load(r#"{"a", "b", "c"}"#).eval::<BTreeSet<String>>()?;
|
|
|
|
assert_eq!(set3, btreeset! { "a".into(), "b".into(), "c".into() });
|
2021-04-11 08:19:12 -04:00
|
|
|
|
|
|
|
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")?;
|
2021-06-16 19:11:58 -04:00
|
|
|
assert_eq!(s, s2);
|
|
|
|
|
|
|
|
let cs = CStr::from_bytes_with_nul(b"hello\0").unwrap();
|
|
|
|
lua.globals().set("cs", cs)?;
|
|
|
|
let cs2: CString = lua.globals().get("cs")?;
|
|
|
|
assert_eq!(cs, cs2.as_c_str());
|
2021-04-11 08:19:12 -04:00
|
|
|
|
|
|
|
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")?;
|
2021-06-24 20:49:09 -04:00
|
|
|
assert_eq!(s, s2);
|
2021-04-11 08:19:12 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-05-02 20:15:36 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_conv_boxed_str() -> Result<()> {
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
let s = String::from("hello").into_boxed_str();
|
|
|
|
lua.globals().set("s", s.clone())?;
|
|
|
|
let s2: Box<str> = lua.globals().get("s")?;
|
2021-06-24 20:49:09 -04:00
|
|
|
assert_eq!(s, s2);
|
2021-05-02 20:15:36 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_conv_boxed_slice() -> Result<()> {
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
let v = vec![1, 2, 3].into_boxed_slice();
|
|
|
|
lua.globals().set("v", v.clone())?;
|
|
|
|
let v2: Box<[i32]> = lua.globals().get("v")?;
|
2021-06-24 20:49:09 -04:00
|
|
|
assert_eq!(v, v2);
|
2021-05-02 20:15:36 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|