From 36b7124dd57253c0a8ac2a63d1f47b514a969288 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 19 Mar 2020 20:55:53 +0800 Subject: [PATCH] Add eval test. --- tests/eval.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++ tests/var_scope.rs | 5 +--- 2 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 tests/eval.rs diff --git a/tests/eval.rs b/tests/eval.rs new file mode 100644 index 00000000..24559d83 --- /dev/null +++ b/tests/eval.rs @@ -0,0 +1,61 @@ +use rhai::{Engine, EvalAltResult, Scope, INT}; + +#[test] +fn test_eval() -> Result<(), EvalAltResult> { + let mut engine = Engine::new(); + let mut scope = Scope::new(); + + assert_eq!( + engine.eval_with_scope::( + &mut scope, + r#" + let x = 10; + + fn foo(x) { x += 12; x } + + let script = "let y = x;"; // build a script + script += "y += foo(y);"; + script += "x + y"; + + eval(script) + "# + )?, + 42 + ); + + assert_eq!( + scope + .get_value::("x") + .expect("variable x should exist"), + 10 + ); + + assert_eq!( + scope + .get_value::("y") + .expect("variable y should exist"), + 32 + ); + + assert!(!scope.contains("z")); + + Ok(()) +} + +#[test] +fn test_eval_override() -> Result<(), EvalAltResult> { + let mut engine = Engine::new(); + + assert_eq!( + engine.eval::( + r#" + fn eval(x) { x } // reflect the script back + + eval("40 + 2") + "# + )?, + "40 + 2" + ); + + Ok(()) +} diff --git a/tests/var_scope.rs b/tests/var_scope.rs index bdb4674e..7b573fd8 100644 --- a/tests/var_scope.rs +++ b/tests/var_scope.rs @@ -42,10 +42,7 @@ fn test_scope_eval() -> Result<(), EvalAltResult> { assert_eq!( scope .get_value::("y") - .ok_or(EvalAltResult::ErrorRuntime( - "variable y not found".into(), - Default::default() - ))?, + .expect("variable y should exist"), 1 );