Skip to content

Commit 7535a23

Browse files
committed
Update function tests
1 parent 3787ff9 commit 7535a23

File tree

3 files changed

+122
-167
lines changed

3 files changed

+122
-167
lines changed

tests/function.rs

Lines changed: 121 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1-
use mlua::{Error, Function, Lua, Result, String, Table};
1+
use mlua::{Error, Function, Lua, Result, String, Table, Variadic};
22

33
#[test]
4-
fn test_function() -> Result<()> {
4+
fn test_function_call() -> Result<()> {
55
let lua = Lua::new();
66

7-
let globals = lua.globals();
8-
lua.load(
9-
r#"
10-
function concat(arg1, arg2)
11-
return arg1 .. arg2
12-
end
13-
"#,
14-
)
15-
.exec()?;
16-
17-
let concat = globals.get::<Function>("concat")?;
7+
let concat = lua
8+
.load(r#"function(arg1, arg2) return arg1 .. arg2 end"#)
9+
.eval::<Function>()?;
1810
assert_eq!(concat.call::<String>(("foo", "bar"))?, "foobar");
1911

2012
Ok(())
2113
}
2214

2315
#[test]
24-
fn test_bind() -> Result<()> {
16+
fn test_function_call_error() -> Result<()> {
17+
let lua = Lua::new();
18+
19+
let concat_err = lua
20+
.load(r#"function(arg1, arg2) error("concat error") end"#)
21+
.eval::<Function>()?;
22+
match concat_err.call::<String>(("foo", "bar")) {
23+
Err(Error::RuntimeError(msg)) if msg.contains("concat error") => {}
24+
other => panic!("unexpected result: {other:?}"),
25+
}
26+
27+
Ok(())
28+
}
29+
30+
#[test]
31+
fn test_function_bind() -> Result<()> {
2532
let lua = Lua::new();
2633

2734
let globals = lua.globals();
@@ -54,74 +61,29 @@ fn test_bind() -> Result<()> {
5461
}
5562

5663
#[test]
57-
fn test_rust_function() -> Result<()> {
64+
#[cfg(not(target_arch = "wasm32"))]
65+
fn test_function_bind_error() -> Result<()> {
5866
let lua = Lua::new();
5967

60-
let globals = lua.globals();
61-
lua.load(
62-
r#"
63-
function lua_function()
64-
return rust_function()
65-
end
66-
67-
-- Test to make sure chunk return is ignored
68-
return 1
69-
"#,
70-
)
71-
.exec()?;
72-
73-
let lua_function = globals.get::<Function>("lua_function")?;
74-
let rust_function = lua.create_function(|_, ()| Ok("hello"))?;
75-
76-
globals.set("rust_function", rust_function)?;
77-
assert_eq!(lua_function.call::<String>(())?, "hello");
78-
79-
Ok(())
80-
}
81-
82-
#[test]
83-
fn test_c_function() -> Result<()> {
84-
let lua = Lua::new();
85-
86-
unsafe extern "C-unwind" fn c_function(state: *mut mlua::lua_State) -> std::os::raw::c_int {
87-
ffi::lua_pushboolean(state, 1);
88-
ffi::lua_setglobal(state, b"c_function\0" as *const _ as *const _);
89-
0
90-
}
91-
92-
let func = unsafe { lua.create_c_function(c_function)? };
93-
func.call::<()>(())?;
94-
assert_eq!(lua.globals().get::<bool>("c_function")?, true);
95-
96-
Ok(())
97-
}
98-
99-
#[cfg(not(feature = "luau"))]
100-
#[test]
101-
fn test_dump() -> Result<()> {
102-
let lua = unsafe { Lua::unsafe_new() };
103-
104-
let concat_lua = lua
105-
.load(r#"function(arg1, arg2) return arg1 .. arg2 end"#)
106-
.eval::<Function>()?;
107-
let concat = lua.load(&concat_lua.dump(false)).into_function()?;
108-
109-
assert_eq!(concat.call::<String>(("foo", "bar"))?, "foobar");
68+
let func = lua.load(r#"function(...) end"#).eval::<Function>()?;
69+
assert!(func.bind(Variadic::from_iter(1..1000000)).is_err());
70+
assert!(func.call::<()>(Variadic::from_iter(1..1000000)).is_err());
11071

11172
Ok(())
11273
}
11374

11475
#[test]
11576
fn test_function_environment() -> Result<()> {
11677
let lua = Lua::new();
78+
let globals = lua.globals();
11779

11880
// We must not get or set environment for C functions
11981
let rust_func = lua.create_function(|_, ()| Ok("hello"))?;
12082
assert_eq!(rust_func.environment(), None);
121-
assert_eq!(rust_func.set_environment(lua.globals()).ok(), Some(false));
83+
assert_eq!(rust_func.set_environment(globals.clone()).ok(), Some(false));
12284

12385
// Test getting Lua function environment
124-
lua.globals().set("hello", "global")?;
86+
globals.set("hello", "global")?;
12587
let lua_func = lua
12688
.load(
12789
r#"
@@ -135,7 +97,7 @@ fn test_function_environment() -> Result<()> {
13597
.eval::<Function>()?;
13698
let lua_func2 = lua.load("return hello").into_function()?;
13799
assert_eq!(lua_func.call::<String>(())?, "global");
138-
assert_eq!(lua_func.environment(), Some(lua.globals()));
100+
assert_eq!(lua_func.environment().as_ref(), Some(&globals));
139101

140102
// Test changing the environment
141103
let env = lua.create_table_from([("hello", "local")])?;
@@ -154,9 +116,9 @@ fn test_function_environment() -> Result<()> {
154116
"#,
155117
)
156118
.exec()?;
157-
let lucky = lua.globals().get::<Function>("lucky")?;
119+
let lucky = globals.get::<Function>("lucky")?;
158120
assert_eq!(lucky.call::<String>(())?, "number is 15");
159-
let new_env = lua.globals().get::<Table>("new_env")?;
121+
let new_env = globals.get::<Table>("new_env")?;
160122
lucky.set_environment(new_env)?;
161123
assert_eq!(lucky.call::<String>(())?, "15");
162124

@@ -235,6 +197,95 @@ fn test_function_info() -> Result<()> {
235197
Ok(())
236198
}
237199

200+
#[cfg(not(feature = "luau"))]
201+
#[test]
202+
fn test_function_dump() -> Result<()> {
203+
let lua = unsafe { Lua::unsafe_new() };
204+
205+
let concat_lua = lua
206+
.load(r#"function(arg1, arg2) return arg1 .. arg2 end"#)
207+
.eval::<Function>()?;
208+
let concat = lua.load(&concat_lua.dump(false)).into_function()?;
209+
210+
assert_eq!(concat.call::<String>(("foo", "bar"))?, "foobar");
211+
212+
Ok(())
213+
}
214+
215+
#[cfg(feature = "luau")]
216+
#[test]
217+
fn test_finction_coverage() -> Result<()> {
218+
let lua = Lua::new();
219+
220+
lua.set_compiler(mlua::Compiler::default().set_coverage_level(1));
221+
222+
let f = lua
223+
.load(
224+
r#"local s = "abc"
225+
assert(#s == 3)
226+
227+
function abc(i)
228+
if i < 5 then
229+
return 0
230+
else
231+
return 1
232+
end
233+
end
234+
235+
(function()
236+
(function() abc(10) end)()
237+
end)()
238+
"#,
239+
)
240+
.into_function()?;
241+
242+
f.call::<()>(())?;
243+
244+
let mut report = Vec::new();
245+
f.coverage(|cov| {
246+
report.push(cov);
247+
});
248+
249+
assert_eq!(
250+
report[0],
251+
mlua::CoverageInfo {
252+
function: None,
253+
line_defined: 1,
254+
depth: 0,
255+
hits: vec![-1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1],
256+
}
257+
);
258+
assert_eq!(
259+
report[1],
260+
mlua::CoverageInfo {
261+
function: Some("abc".into()),
262+
line_defined: 4,
263+
depth: 1,
264+
hits: vec![-1, -1, -1, -1, -1, 1, 0, -1, 1, -1, -1, -1, -1, -1, -1, -1],
265+
}
266+
);
267+
assert_eq!(
268+
report[2],
269+
mlua::CoverageInfo {
270+
function: None,
271+
line_defined: 12,
272+
depth: 1,
273+
hits: vec![-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1],
274+
}
275+
);
276+
assert_eq!(
277+
report[3],
278+
mlua::CoverageInfo {
279+
function: None,
280+
line_defined: 13,
281+
depth: 2,
282+
hits: vec![-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1],
283+
}
284+
);
285+
286+
Ok(())
287+
}
288+
238289
#[test]
239290
fn test_function_pointer() -> Result<()> {
240291
let lua = Lua::new();

tests/luau.rs

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use std::panic::{catch_unwind, AssertUnwindSafe};
66
use std::sync::atomic::{AtomicU64, Ordering};
77
use std::sync::Arc;
88

9-
use mlua::{
10-
Compiler, CoverageInfo, Error, Lua, LuaOptions, Result, StdLib, Table, ThreadStatus, Value, Vector,
11-
VmState,
12-
};
9+
use mlua::{Compiler, Error, Lua, LuaOptions, Result, StdLib, Table, ThreadStatus, Value, Vector, VmState};
1310

1411
#[test]
1512
fn test_version() -> Result<()> {
@@ -392,79 +389,6 @@ fn test_interrupts() -> Result<()> {
392389
Ok(())
393390
}
394391

395-
#[test]
396-
fn test_coverage() -> Result<()> {
397-
let lua = Lua::new();
398-
399-
lua.set_compiler(Compiler::default().set_coverage_level(1));
400-
401-
let f = lua
402-
.load(
403-
r#"local s = "abc"
404-
assert(#s == 3)
405-
406-
function abc(i)
407-
if i < 5 then
408-
return 0
409-
else
410-
return 1
411-
end
412-
end
413-
414-
(function()
415-
(function() abc(10) end)()
416-
end)()
417-
"#,
418-
)
419-
.into_function()?;
420-
421-
f.call::<()>(())?;
422-
423-
let mut report = Vec::new();
424-
f.coverage(|cov| {
425-
report.push(cov);
426-
});
427-
428-
assert_eq!(
429-
report[0],
430-
CoverageInfo {
431-
function: None,
432-
line_defined: 1,
433-
depth: 0,
434-
hits: vec![-1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1],
435-
}
436-
);
437-
assert_eq!(
438-
report[1],
439-
CoverageInfo {
440-
function: Some("abc".into()),
441-
line_defined: 4,
442-
depth: 1,
443-
hits: vec![-1, -1, -1, -1, -1, 1, 0, -1, 1, -1, -1, -1, -1, -1, -1, -1],
444-
}
445-
);
446-
assert_eq!(
447-
report[2],
448-
CoverageInfo {
449-
function: None,
450-
line_defined: 12,
451-
depth: 1,
452-
hits: vec![-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1],
453-
}
454-
);
455-
assert_eq!(
456-
report[3],
457-
CoverageInfo {
458-
function: None,
459-
line_defined: 13,
460-
depth: 2,
461-
hits: vec![-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1],
462-
}
463-
);
464-
465-
Ok(())
466-
}
467-
468392
#[test]
469393
fn test_fflags() {
470394
// We cannot really on any particular feature flag to be present

tests/tests.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -925,26 +925,6 @@ fn test_too_many_recursions() -> Result<()> {
925925
Ok(())
926926
}
927927

928-
#[test]
929-
#[cfg(not(target_arch = "wasm32"))]
930-
fn test_too_many_binds() -> Result<()> {
931-
let lua = Lua::new();
932-
let globals = lua.globals();
933-
lua.load(
934-
r#"
935-
function f(...)
936-
end
937-
"#,
938-
)
939-
.exec()?;
940-
941-
let concat = globals.get::<Function>("f")?;
942-
assert!(concat.bind(Variadic::from_iter(1..1000000)).is_err());
943-
assert!(concat.call::<()>(Variadic::from_iter(1..1000000)).is_err());
944-
945-
Ok(())
946-
}
947-
948928
#[test]
949929
#[cfg(not(target_arch = "wasm32"))]
950930
fn test_ref_stack_exhaustion() {

0 commit comments

Comments
 (0)