Add test with &str parameter.

This commit is contained in:
Stephen Chung 2021-03-17 22:32:22 +08:00
parent 80c7e9310e
commit 1200ffcd2b
2 changed files with 18 additions and 10 deletions

View File

@ -32,15 +32,15 @@ pub trait Func<ARGS, RET> {
/// // 2) the return type of the script function /// // 2) the return type of the script function
/// ///
/// // 'func' will have type Box<dyn Fn(i64, String) -> Result<bool, Box<EvalAltResult>>> and is callable! /// // 'func' will have type Box<dyn Fn(i64, String) -> Result<bool, Box<EvalAltResult>>> and is callable!
/// let func = Func::<(i64, String), bool>::create_from_ast( /// let func = Func::<(i64, &str), bool>::create_from_ast(
/// // ^^^^^^^^^^^^^ function parameter types in tuple /// // ^^^^^^^^^^^ function parameter types in tuple
/// ///
/// engine, // the 'Engine' is consumed into the closure /// engine, // the 'Engine' is consumed into the closure
/// ast, // the 'AST' /// ast, // the 'AST'
/// "calc" // the entry-point function name /// "calc" // the entry-point function name
/// ); /// );
/// ///
/// func(123, "hello".to_string())? == false; // call the anonymous function /// func(123, "hello")? == false; // call the anonymous function
/// # Ok(()) /// # Ok(())
/// # } /// # }
fn create_from_ast(self, ast: AST, entry_point: &str) -> Self::Output; fn create_from_ast(self, ast: AST, entry_point: &str) -> Self::Output;
@ -64,15 +64,15 @@ pub trait Func<ARGS, RET> {
/// // 2) the return type of the script function /// // 2) the return type of the script function
/// ///
/// // 'func' will have type Box<dyn Fn(i64, String) -> Result<bool, Box<EvalAltResult>>> and is callable! /// // 'func' will have type Box<dyn Fn(i64, String) -> Result<bool, Box<EvalAltResult>>> and is callable!
/// let func = Func::<(i64, String), bool>::create_from_script( /// let func = Func::<(i64, &str), bool>::create_from_script(
/// // ^^^^^^^^^^^^^ function parameter types in tuple /// // ^^^^^^^^^^^ function parameter types in tuple
/// ///
/// engine, // the 'Engine' is consumed into the closure /// engine, // the 'Engine' is consumed into the closure
/// script, // the script, notice number of parameters must match /// script, // the script, notice number of parameters must match
/// "calc" // the entry-point function name /// "calc" // the entry-point function name
/// )?; /// )?;
/// ///
/// func(123, "hello".to_string())? == false; // call the anonymous function /// func(123, "hello")? == false; // call the anonymous function
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```

View File

@ -215,5 +215,13 @@ fn test_anonymous_fn() -> Result<(), Box<EvalAltResult>> {
assert_eq!(calc_func(42, "hello".to_string(), 9)?, 423); assert_eq!(calc_func(42, "hello".to_string(), 9)?, 423);
let calc_func = Func::<(INT, &str, INT), INT>::create_from_script(
Engine::new(),
"fn calc(x, y, z) { (x + len(y)) * z }",
"calc",
)?;
assert_eq!(calc_func(42, "hello", 9)?, 423);
Ok(()) Ok(())
} }