diff --git a/tests/eval.rs b/tests/eval.rs index 494a36f4..5187acf8 100644 --- a/tests/eval.rs +++ b/tests/eval.rs @@ -6,6 +6,20 @@ fn test_eval() -> Result<(), Box> { assert_eq!(engine.eval::(r#"eval("40 + 2")"#)?, 42); + assert_eq!( + engine.eval::( + 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> { 41 ); + assert_eq!( + engine.eval::( + r#" + let foo = 42; + + eval("{ let foo = 123; }"); + + foo + "# + )?, + 42 + ); + + assert_eq!( + engine.eval::( + 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> { + let engine = Engine::new(); + + assert_eq!( + engine.eval::( + r#" + const XYZ = 123; + + fn foo() { global::XYZ } + { + eval("const XYZ = 42;"); + } + + foo() + "# + )?, + 123 + ); + + assert_eq!( + engine.eval::( + r#" + const XYZ = 123; + + fn foo() { global::XYZ } + + eval("const XYZ = 42;"); + + foo() + "# + )?, + 42 + ); + Ok(()) } diff --git a/tests/functions.rs b/tests/functions.rs index 491f9559..a81c56a3 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -181,5 +181,43 @@ fn test_functions_bang() -> Result<(), Box> { 123 ); + assert_eq!( + engine.eval::( + " + fn foo() { + let hello = bar + 42; + } + + let bar = 999; + let hello = 123; + + foo!(); + + hello + ", + )?, + 123 + ); + + assert_eq!( + engine.eval::( + 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(()) }