summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/memory.rs18
-rw-r--r--tests/table.rs34
-rw-r--r--tests/tests.rs12
-rw-r--r--tests/thread.rs20
-rw-r--r--tests/userdata.rs37
-rw-r--r--tests/value.rs57
6 files changed, 140 insertions, 38 deletions
diff --git a/tests/memory.rs b/tests/memory.rs
index 7c141cd..0bcdeb6 100644
--- a/tests/memory.rs
+++ b/tests/memory.rs
@@ -51,15 +51,15 @@ fn test_gc_error() {
match lua
.load(
r#"
- val = nil
- table = {}
- setmetatable(table, {
- __gc = function()
- error("gcwascalled")
- end
- })
- table = nil
- collectgarbage("collect")
+ val = nil
+ table = {}
+ setmetatable(table, {
+ __gc = function()
+ error("gcwascalled")
+ end
+ })
+ table = nil
+ collectgarbage("collect")
"#,
)
.exec()
diff --git a/tests/table.rs b/tests/table.rs
index 14ccb87..1d788e2 100644
--- a/tests/table.rs
+++ b/tests/table.rs
@@ -149,6 +149,40 @@ fn test_metatable() -> Result<()> {
}
#[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(())
+}
+
+#[test]
fn test_table_error() -> Result<()> {
let lua = Lua::new();
diff --git a/tests/tests.rs b/tests/tests.rs
index 2e71dc1..8ef9d48 100644
--- a/tests/tests.rs
+++ b/tests/tests.rs
@@ -92,13 +92,13 @@ fn test_lua_multi() -> Result<()> {
lua.load(
r#"
- function concat(arg1, arg2)
- return arg1 .. arg2
- end
+ function concat(arg1, arg2)
+ return arg1 .. arg2
+ end
- function mreturn()
- return 1, 2, 3, 4, 5, 6
- end
+ function mreturn()
+ return 1, 2, 3, 4, 5, 6
+ end
"#,
)
.exec()?;
diff --git a/tests/thread.rs b/tests/thread.rs
index 3344b7c..d3266ba 100644
--- a/tests/thread.rs
+++ b/tests/thread.rs
@@ -20,13 +20,13 @@ fn test_thread() -> Result<()> {
let thread = lua.create_thread(
lua.load(
r#"
- function (s)
- local sum = s
- for i = 1,4 do
- sum = sum + coroutine.yield(sum)
- end
- return sum
+ function (s)
+ local sum = s
+ for i = 1,4 do
+ sum = sum + coroutine.yield(sum)
end
+ return sum
+ end
"#,
)
.eval()?,
@@ -47,11 +47,11 @@ fn test_thread() -> Result<()> {
let accumulate = lua.create_thread(
lua.load(
r#"
- function (sum)
- while true do
- sum = sum + coroutine.yield(sum)
- end
+ function (sum)
+ while true do
+ sum = sum + coroutine.yield(sum)
end
+ end
"#,
)
.eval::<Function>()?,
diff --git a/tests/userdata.rs b/tests/userdata.rs
index 4f01669..34aa8b4 100644
--- a/tests/userdata.rs
+++ b/tests/userdata.rs
@@ -13,7 +13,7 @@ use std::sync::Arc;
use mlua::{
AnyUserData, ExternalError, Function, Lua, MetaMethod, Result, String, UserData,
- UserDataMethods,
+ UserDataMethods, Value,
};
#[test]
@@ -96,6 +96,9 @@ fn test_metamethods() -> Result<()> {
MetaMethod::Sub,
|_, (lhs, rhs): (MyUserData, MyUserData)| Ok(MyUserData(lhs.0 - rhs.0)),
);
+ methods.add_meta_function(MetaMethod::Eq, |_, (lhs, rhs): (MyUserData, MyUserData)| {
+ Ok(lhs.0 == rhs.0)
+ });
methods.add_meta_method(MetaMethod::Index, |_, data, index: String| {
if index.to_str()? == "inner" {
Ok(data.0)
@@ -122,6 +125,7 @@ fn test_metamethods() -> Result<()> {
let globals = lua.globals();
globals.set("userdata1", MyUserData(7))?;
globals.set("userdata2", MyUserData(3))?;
+ globals.set("userdata3", MyUserData(3))?;
assert_eq!(
lua.load("userdata1 + userdata2").eval::<MyUserData>()?.0,
10
@@ -151,6 +155,13 @@ fn test_metamethods() -> Result<()> {
assert_eq!(ipairs_it.call::<_, i64>(())?, 28);
assert!(lua.load("userdata2.nonexist_field").eval::<()>().is_err());
+ let userdata2: Value = globals.get("userdata2")?;
+ let userdata3: Value = globals.get("userdata3")?;
+
+ assert!(lua.load("userdata2 == userdata3").eval::<bool>()?);
+ assert!(userdata2 != userdata3); // because references are differ
+ assert!(userdata2.equals(userdata3)?);
+
Ok(())
}
@@ -175,18 +186,18 @@ fn test_gc_userdata() -> Result<()> {
assert!(lua
.load(
r#"
- local tbl = setmetatable({
- userdata = userdata
- }, { __gc = function(self)
- -- resurrect userdata
- hatch = self.userdata
- end })
-
- tbl = nil
- userdata = nil -- make table and userdata collectable
- collectgarbage("collect")
- hatch:access()
- "#
+ local tbl = setmetatable({
+ userdata = userdata
+ }, { __gc = function(self)
+ -- resurrect userdata
+ hatch = self.userdata
+ end })
+
+ tbl = nil
+ userdata = nil -- make table and userdata collectable
+ collectgarbage("collect")
+ hatch:access()
+ "#
)
.exec()
.is_err());
diff --git a/tests/value.rs b/tests/value.rs
new file mode 100644
index 0000000..9beb68c
--- /dev/null
+++ b/tests/value.rs
@@ -0,0 +1,57 @@
+use mlua::{Lua, Result, Value};
+
+#[test]
+fn test_value_eq() -> Result<()> {
+ let lua = Lua::new();
+ let globals = lua.globals();
+
+ lua.load(
+ r#"
+ table1 = {1}
+ table2 = {1}
+ string1 = "hello"
+ string2 = "hello"
+ num1 = 1
+ num2 = 1.0
+ num3 = "1"
+ func1 = function() end
+ func2 = func1
+ func3 = function() end
+ thread1 = coroutine.create(function() end)
+ thread2 = thread1
+
+ setmetatable(table1, {
+ __eq = function(a, b) return a[1] == b[1] end
+ })
+ "#,
+ )
+ .exec()?;
+
+ let table1: Value = globals.get("table1")?;
+ let table2: Value = globals.get("table2")?;
+ let string1: Value = globals.get("string1")?;
+ let string2: Value = globals.get("string2")?;
+ let num1: Value = globals.get("num1")?;
+ let num2: Value = globals.get("num2")?;
+ let num3: Value = globals.get("num3")?;
+ let func1: Value = globals.get("func1")?;
+ let func2: Value = globals.get("func2")?;
+ let func3: Value = globals.get("func3")?;
+ let thread1: Value = globals.get("thread1")?;
+ let thread2: Value = globals.get("thread2")?;
+
+ assert!(table1 != table2);
+ assert!(table1.equals(table2)?);
+ assert!(string1 == string2);
+ assert!(string1.equals(string2)?);
+ assert!(num1 == num2);
+ assert!(num1.equals(num2)?);
+ assert!(num1 != num3);
+ assert!(func1 == func2);
+ assert!(func1 != func3);
+ assert!(!func1.equals(func3)?);
+ assert!(thread1 == thread2);
+ assert!(thread1.equals(thread2)?);
+
+ Ok(())
+}