diff --git a/tests/call_fn.rs b/tests/call_fn.rs index f3d2a689..e3c61765 100644 --- a/tests/call_fn.rs +++ b/tests/call_fn.rs @@ -41,13 +41,13 @@ fn test_call_fn() -> Result<(), Box> { ", )?; - let r: i64 = engine.call_fn(&mut scope, &ast, "hello", (42 as INT, 123 as INT))?; + let r: INT = engine.call_fn(&mut scope, &ast, "hello", (42 as INT, 123 as INT))?; assert_eq!(r, 165); - let r: i64 = engine.call_fn(&mut scope, &ast, "hello", (123 as INT,))?; + let r: INT = engine.call_fn(&mut scope, &ast, "hello", (123 as INT,))?; assert_eq!(r, 5166); - let r: i64 = engine.call_fn(&mut scope, &ast, "hello", ())?; + let r: INT = engine.call_fn(&mut scope, &ast, "hello", ())?; assert_eq!(r, 42); assert_eq!( @@ -60,6 +60,27 @@ fn test_call_fn() -> Result<(), Box> { Ok(()) } +#[test] +fn test_call_fn_private() -> Result<(), Box> { + let engine = Engine::new(); + let mut scope = Scope::new(); + + let ast = engine.compile("fn add(x, n) { x + n }")?; + + let r: INT = engine.call_fn(&mut scope, &ast, "add", (40 as INT, 2 as INT))?; + assert_eq!(r, 42); + + let ast = engine.compile("private fn add(x, n) { x + n }")?; + + assert!(matches!( + *engine.call_fn::<_, INT>(&mut scope, &ast, "add", (40 as INT, 2 as INT)) + .expect_err("should error"), + EvalAltResult::ErrorFunctionNotFound(fn_name, _) if fn_name == "add" + )); + + Ok(()) +} + #[test] fn test_anonymous_fn() -> Result<(), Box> { let calc_func = Func::<(INT, INT, INT), INT>::create_from_script( @@ -70,5 +91,16 @@ fn test_anonymous_fn() -> Result<(), Box> { assert_eq!(calc_func(42, 123, 9)?, 1485); + let calc_func = Func::<(INT, INT, INT), INT>::create_from_script( + Engine::new(), + "private fn calc(x, y, z) { (x + y) * z }", + "calc", + )?; + + assert!(matches!( + *calc_func(42, 123, 9).expect_err("should error"), + EvalAltResult::ErrorFunctionNotFound(fn_name, _) if fn_name == "calc" + )); + Ok(()) } diff --git a/tests/functions.rs b/tests/functions.rs index 8200962b..422394c1 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -5,19 +5,19 @@ use rhai::{Engine, EvalAltResult, Func, ParseErrorType, Scope, INT}; fn test_functions() -> Result<(), Box> { let engine = Engine::new(); - assert_eq!(engine.eval::("fn add(x, n) { x + n } add(40, 2)")?, 42); + assert_eq!(engine.eval::("fn add(x, n) { x + n } add(40, 2)")?, 42); #[cfg(not(feature = "no_object"))] assert_eq!( - engine.eval::("fn add(x, n) { x + n } let x = 40; x.add(2)")?, + engine.eval::("fn add(x, n) { x + n } let x = 40; x.add(2)")?, 42 ); - assert_eq!(engine.eval::("fn mul2(x) { x * 2 } mul2(21)")?, 42); + assert_eq!(engine.eval::("fn mul2(x) { x * 2 } mul2(21)")?, 42); #[cfg(not(feature = "no_object"))] assert_eq!( - engine.eval::("fn mul2(x) { x * 2 } let x = 21; x.mul2()")?, + engine.eval::("fn mul2(x) { x * 2 } let x = 21; x.mul2()")?, 42 );