Add tests.

This commit is contained in:
Stephen Chung 2021-12-30 12:23:35 +08:00
parent 64bf2eef5c
commit 80ccd52319
2 changed files with 118 additions and 0 deletions

View File

@ -6,6 +6,20 @@ fn test_eval() -> Result<(), Box<EvalAltResult>> {
assert_eq!(engine.eval::<INT>(r#"eval("40 + 2")"#)?, 42);
assert_eq!(
engine.eval::<INT>(
r#"
let foo = 42;
eval("let foo = 123");
eval("let xyz = 10");
foo + xyz
"#
)?,
133
);
Ok(())
}
@ -33,6 +47,72 @@ fn test_eval_blocks() -> Result<(), Box<EvalAltResult>> {
41
);
assert_eq!(
engine.eval::<INT>(
r#"
let foo = 42;
eval("{ let foo = 123; }");
foo
"#
)?,
42
);
assert_eq!(
engine.eval::<INT>(
r#"
let foo = 42;
{ { {
eval("let foo = 123");
} } }
foo
"#
)?,
42
);
Ok(())
}
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_module"))]
#[test]
fn test_eval_globals() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(
engine.eval::<INT>(
r#"
const XYZ = 123;
fn foo() { global::XYZ }
{
eval("const XYZ = 42;");
}
foo()
"#
)?,
123
);
assert_eq!(
engine.eval::<INT>(
r#"
const XYZ = 123;
fn foo() { global::XYZ }
eval("const XYZ = 42;");
foo()
"#
)?,
42
);
Ok(())
}

View File

@ -181,5 +181,43 @@ fn test_functions_bang() -> Result<(), Box<EvalAltResult>> {
123
);
assert_eq!(
engine.eval::<INT>(
"
fn foo() {
let hello = bar + 42;
}
let bar = 999;
let hello = 123;
foo!();
hello
",
)?,
123
);
assert_eq!(
engine.eval::<INT>(
r#"
fn foo(x) {
let hello = bar + 42 + x;
}
let bar = 999;
let hello = 123;
let f = Fn("foo");
call!(f, 1);
hello
"#,
)?,
123
);
Ok(())
}