Add test for call_fn_raw.

This commit is contained in:
Stephen Chung 2021-12-19 23:22:14 +08:00
parent 123012404b
commit 5729f0cdd4
2 changed files with 30 additions and 1 deletions

View File

@ -159,7 +159,6 @@ impl Engine {
let orig_scope_len = scope.len();
if eval_ast && !statements.is_empty() {
// Make sure new variables introduced at global level do not _spill_ into the function call
self.eval_global_statements(scope, mods, state, statements, &[ast.as_ref()], 0)?;
if rewind_scope {

View File

@ -69,6 +69,36 @@ fn test_call_fn() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_call_fn_scope() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
let mut scope = Scope::new();
let ast = engine.compile(
"
fn foo(x) {
let hello = 42;
bar + hello + x
}
let bar = 123;
",
)?;
for _ in 0..50 {
assert_eq!(
engine
.call_fn_raw(&mut scope, &ast, true, false, "foo", None, [Dynamic::THREE])?
.as_int()?,
168
);
}
assert_eq!(scope.len(), 100);
Ok(())
}
struct Options {
pub foo: bool,
pub bar: String,