2019-10-14 17:21:30 -04:00
|
|
|
use mlua::{Lua, Nil, Result, Table, Value};
|
2018-02-09 23:52:05 -05:00
|
|
|
|
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_set_get() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
2018-02-09 23:52:05 -05:00
|
|
|
let globals = lua.globals();
|
2019-09-28 10:23:17 -04:00
|
|
|
globals.set("foo", "bar")?;
|
|
|
|
globals.set("baz", "baf")?;
|
|
|
|
assert_eq!(globals.get::<_, String>("foo")?, "bar");
|
|
|
|
assert_eq!(globals.get::<_, String>("baz")?, "baf");
|
|
|
|
|
|
|
|
Ok(())
|
2018-02-09 23:52:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_table() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
2018-02-09 23:52:05 -05:00
|
|
|
let globals = lua.globals();
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
globals.set("table", lua.create_table()?)?;
|
|
|
|
let table1: Table = globals.get("table")?;
|
|
|
|
let table2: Table = globals.get("table")?;
|
2018-02-09 23:52:05 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
table1.set("foo", "bar")?;
|
|
|
|
table2.set("baz", "baf")?;
|
2018-02-09 23:52:05 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(table2.get::<_, String>("foo")?, "bar");
|
|
|
|
assert_eq!(table1.get::<_, String>("baz")?, "baf");
|
2018-02-09 23:52:05 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.load(
|
2018-02-09 23:52:05 -05:00
|
|
|
r#"
|
2019-09-28 10:23:17 -04:00
|
|
|
table1 = {1, 2, 3, 4, 5}
|
|
|
|
table2 = {}
|
|
|
|
table3 = {1, 2, nil, 4, 5}
|
|
|
|
"#,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
2019-09-28 10:23:17 -04:00
|
|
|
.exec()?;
|
2018-02-09 23:52:05 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
let table1 = globals.get::<_, Table>("table1")?;
|
|
|
|
let table2 = globals.get::<_, Table>("table2")?;
|
|
|
|
let table3 = globals.get::<_, Table>("table3")?;
|
2018-02-09 23:52:05 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(table1.len()?, 5);
|
2018-02-09 23:52:05 -05:00
|
|
|
assert_eq!(
|
|
|
|
table1
|
|
|
|
.clone()
|
|
|
|
.pairs()
|
2019-09-28 10:23:17 -04:00
|
|
|
.collect::<Result<Vec<(i64, i64)>>>()?,
|
2018-02-09 23:52:05 -05:00
|
|
|
vec![(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
table1
|
|
|
|
.clone()
|
|
|
|
.sequence_values()
|
2019-09-28 10:23:17 -04:00
|
|
|
.collect::<Result<Vec<i64>>>()?,
|
2018-02-09 23:52:05 -05:00
|
|
|
vec![1, 2, 3, 4, 5]
|
|
|
|
);
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(table2.len()?, 0);
|
2018-02-09 23:52:05 -05:00
|
|
|
assert_eq!(
|
|
|
|
table2
|
|
|
|
.clone()
|
|
|
|
.pairs()
|
2019-09-28 10:23:17 -04:00
|
|
|
.collect::<Result<Vec<(i64, i64)>>>()?,
|
2018-02-09 23:52:05 -05:00
|
|
|
vec![]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-09-28 10:23:17 -04:00
|
|
|
table2.sequence_values().collect::<Result<Vec<i64>>>()?,
|
2018-02-09 23:52:05 -05:00
|
|
|
vec![]
|
|
|
|
);
|
|
|
|
|
|
|
|
// sequence_values should only iterate until the first border
|
|
|
|
assert_eq!(
|
2019-09-28 10:23:17 -04:00
|
|
|
table3.sequence_values().collect::<Result<Vec<i64>>>()?,
|
2018-02-09 23:52:05 -05:00
|
|
|
vec![1, 2]
|
|
|
|
);
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
globals.set("table4", lua.create_sequence_from(vec![1, 2, 3, 4, 5])?)?;
|
|
|
|
let table4 = globals.get::<_, Table>("table4")?;
|
2018-02-09 23:52:05 -05:00
|
|
|
assert_eq!(
|
2020-01-07 15:53:47 -05:00
|
|
|
table4
|
|
|
|
.clone()
|
|
|
|
.pairs()
|
|
|
|
.collect::<Result<Vec<(i64, i64)>>>()?,
|
2018-02-09 23:52:05 -05:00
|
|
|
vec![(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
|
|
|
|
);
|
2019-09-28 10:23:17 -04:00
|
|
|
|
2020-01-07 15:53:47 -05:00
|
|
|
table4.raw_insert(4, 35)?;
|
|
|
|
table4.raw_insert(7, 7)?;
|
|
|
|
assert_eq!(
|
|
|
|
table4
|
|
|
|
.clone()
|
|
|
|
.pairs()
|
|
|
|
.collect::<Result<Vec<(i64, i64)>>>()?,
|
|
|
|
vec![(1, 1), (2, 2), (3, 3), (4, 35), (5, 4), (6, 5), (7, 7)]
|
|
|
|
);
|
|
|
|
|
|
|
|
table4.raw_remove(1)?;
|
|
|
|
assert_eq!(
|
|
|
|
table4
|
|
|
|
.clone()
|
|
|
|
.pairs()
|
|
|
|
.collect::<Result<Vec<(i64, i64)>>>()?,
|
|
|
|
vec![(1, 2), (2, 3), (3, 35), (4, 4), (5, 5), (6, 7)]
|
|
|
|
);
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
Ok(())
|
2018-02-09 23:52:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_table_scope() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
2018-02-09 23:52:05 -05:00
|
|
|
let globals = lua.globals();
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.load(
|
2018-02-09 23:52:05 -05:00
|
|
|
r#"
|
2019-09-28 10:23:17 -04:00
|
|
|
touter = {
|
|
|
|
tin = {1, 2, 3}
|
|
|
|
}
|
|
|
|
"#,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
2019-09-28 10:23:17 -04:00
|
|
|
.exec()?;
|
2018-02-09 23:52:05 -05:00
|
|
|
|
|
|
|
// Make sure that table gets do not borrow the table, but instead just borrow lua.
|
|
|
|
let tin;
|
|
|
|
{
|
2019-09-28 10:23:17 -04:00
|
|
|
let touter = globals.get::<_, Table>("touter")?;
|
|
|
|
tin = touter.get::<_, Table>("tin")?;
|
2018-02-09 23:52:05 -05:00
|
|
|
}
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(tin.get::<_, i64>(1)?, 1);
|
|
|
|
assert_eq!(tin.get::<_, i64>(2)?, 2);
|
|
|
|
assert_eq!(tin.get::<_, i64>(3)?, 3);
|
|
|
|
|
|
|
|
Ok(())
|
2018-02-09 23:52:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_metatable() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
let table = lua.create_table()?;
|
|
|
|
let metatable = lua.create_table()?;
|
|
|
|
metatable.set("__index", lua.create_function(|_, ()| Ok("index_value"))?)?;
|
2018-02-09 23:52:05 -05:00
|
|
|
table.set_metatable(Some(metatable));
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(table.get::<_, String>("any_key")?, "index_value");
|
|
|
|
match table.raw_get::<_, Value>("any_key")? {
|
2018-02-09 23:52:05 -05:00
|
|
|
Nil => {}
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
table.set_metatable(None);
|
2019-09-28 10:23:17 -04:00
|
|
|
match table.get::<_, Value>("any_key")? {
|
2018-02-09 23:52:05 -05:00
|
|
|
Nil => {}
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
2018-02-09 23:52:05 -05:00
|
|
|
}
|
|
|
|
|
2020-01-06 18:59:50 -05:00
|
|
|
#[test]
|
|
|
|
fn test_table_eq() -> Result<()> {
|
|
|
|
let lua = Lua::new();
|
|
|
|
let globals = lua.globals();
|
|
|
|
|
|
|
|
lua.load(
|
|
|
|
r#"
|
|
|
|
table1 = {1}
|
|
|
|
table2 = {1}
|
|
|
|
table3 = table1
|
|
|
|
table4 = {1}
|
|
|
|
|
|
|
|
setmetatable(table4, {
|
|
|
|
__eq = function(a, b) return a[1] == b[1] end
|
|
|
|
})
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.exec()?;
|
|
|
|
|
|
|
|
let table1 = globals.get::<_, Table>("table1")?;
|
|
|
|
let table2 = globals.get::<_, Table>("table2")?;
|
|
|
|
let table3 = globals.get::<_, Table>("table3")?;
|
|
|
|
let table4 = globals.get::<_, Table>("table4")?;
|
|
|
|
|
|
|
|
assert!(table1 != table2);
|
|
|
|
assert!(!table1.equals(&table2)?);
|
|
|
|
assert!(table1 == table3);
|
|
|
|
assert!(table1.equals(&table3)?);
|
|
|
|
assert!(table1 != table4);
|
|
|
|
assert!(table1.equals(&table4)?);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-02-09 23:52:05 -05:00
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_table_error() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
2018-02-09 23:52:05 -05:00
|
|
|
let globals = lua.globals();
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.load(
|
2018-02-09 23:52:05 -05:00
|
|
|
r#"
|
2019-09-28 10:23:17 -04:00
|
|
|
table = {}
|
|
|
|
setmetatable(table, {
|
|
|
|
__index = function()
|
|
|
|
error("lua error")
|
|
|
|
end,
|
|
|
|
__newindex = function()
|
|
|
|
error("lua error")
|
|
|
|
end,
|
|
|
|
__len = function()
|
|
|
|
error("lua error")
|
|
|
|
end
|
|
|
|
})
|
|
|
|
"#,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
2019-09-28 10:23:17 -04:00
|
|
|
.exec()?;
|
2018-02-09 23:52:05 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
let bad_table: Table = globals.get("table")?;
|
2018-02-09 23:52:05 -05:00
|
|
|
assert!(bad_table.set(1, 1).is_err());
|
|
|
|
assert!(bad_table.get::<_, i32>(1).is_err());
|
|
|
|
assert!(bad_table.len().is_err());
|
|
|
|
assert!(bad_table.raw_set(1, 1).is_ok());
|
|
|
|
assert!(bad_table.raw_get::<_, i32>(1).is_ok());
|
|
|
|
assert_eq!(bad_table.raw_len(), 1);
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
2018-02-09 23:52:05 -05:00
|
|
|
}
|