2020-11-20 16:52:28 +08:00
|
|
|
use rhai::{Engine, EvalAltResult, Position, Scope, INT};
|
2017-11-03 09:58:51 -07:00
|
|
|
|
|
|
|
#[test]
|
2020-04-21 23:25:12 +08:00
|
|
|
fn test_var_scope() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 13:23:06 +08:00
|
|
|
let engine = Engine::new();
|
2019-09-18 11:21:07 +01:00
|
|
|
let mut scope = Scope::new();
|
2017-11-03 09:58:51 -07:00
|
|
|
|
2020-03-12 14:54:14 +08:00
|
|
|
engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5")?;
|
|
|
|
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x")?, 9);
|
2020-07-06 16:20:03 +08:00
|
|
|
engine.eval_with_scope::<()>(&mut scope, "x += 1; x += 2;")?;
|
2020-03-12 14:54:14 +08:00
|
|
|
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x")?, 12);
|
2020-04-05 19:17:48 +08:00
|
|
|
|
|
|
|
scope.set_value("x", 42 as INT);
|
|
|
|
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x")?, 42);
|
|
|
|
|
2020-03-24 16:57:35 +08:00
|
|
|
engine.eval_with_scope::<()>(&mut scope, "{let x = 3}")?;
|
2020-04-05 19:17:48 +08:00
|
|
|
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x")?, 42);
|
2017-11-03 09:58:51 -07:00
|
|
|
|
2020-03-02 22:11:56 +08:00
|
|
|
Ok(())
|
2017-11-03 09:58:51 -07:00
|
|
|
}
|
2020-03-03 15:20:20 +08:00
|
|
|
|
2021-03-01 22:44:56 +08:00
|
|
|
#[test]
|
|
|
|
fn test_var_is_def() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let engine = Engine::new();
|
|
|
|
|
|
|
|
assert!(engine.eval::<bool>(
|
|
|
|
r#"
|
|
|
|
let x = 42;
|
|
|
|
is_def_var("x")
|
|
|
|
"#
|
|
|
|
)?);
|
|
|
|
assert!(!engine.eval::<bool>(
|
|
|
|
r#"
|
|
|
|
let x = 42;
|
|
|
|
is_def_var("y")
|
|
|
|
"#
|
|
|
|
)?);
|
|
|
|
assert!(engine.eval::<bool>(
|
|
|
|
r#"
|
|
|
|
const x = 42;
|
|
|
|
is_def_var("x")
|
|
|
|
"#
|
|
|
|
)?);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-03 15:20:20 +08:00
|
|
|
#[test]
|
2020-04-21 23:25:12 +08:00
|
|
|
fn test_scope_eval() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 13:23:06 +08:00
|
|
|
let engine = Engine::new();
|
2020-03-03 15:20:20 +08:00
|
|
|
|
|
|
|
// First create the state
|
|
|
|
let mut scope = Scope::new();
|
|
|
|
|
|
|
|
// Then push some initialized variables into the state
|
2020-03-10 23:06:20 +08:00
|
|
|
// NOTE: Remember the default numbers used by Rhai are INT and f64.
|
2020-03-03 15:20:20 +08:00
|
|
|
// Better stick to them or it gets hard to work with other variables in the script.
|
2020-03-10 23:06:20 +08:00
|
|
|
scope.push("y", 42 as INT);
|
|
|
|
scope.push("z", 999 as INT);
|
2020-03-03 15:20:20 +08:00
|
|
|
|
|
|
|
// First invocation
|
|
|
|
engine
|
2020-03-12 14:54:14 +08:00
|
|
|
.eval_with_scope::<()>(&mut scope, " let x = 4 + 5 - y + z; y = 1;")
|
2020-03-03 15:20:20 +08:00
|
|
|
.expect("y and z not found?");
|
|
|
|
|
|
|
|
// Second invocation using the same state
|
2020-03-12 14:54:14 +08:00
|
|
|
let result = engine.eval_with_scope::<INT>(&mut scope, "x")?;
|
2020-03-09 21:09:53 +08:00
|
|
|
|
|
|
|
println!("result: {}", result); // should print 966
|
2020-03-03 15:20:20 +08:00
|
|
|
|
|
|
|
// Variable y is changed in the script
|
2020-03-12 12:35:30 +08:00
|
|
|
assert_eq!(
|
|
|
|
scope
|
|
|
|
.get_value::<INT>("y")
|
2020-03-19 20:55:53 +08:00
|
|
|
.expect("variable y should exist"),
|
2020-03-12 12:35:30 +08:00
|
|
|
1
|
|
|
|
);
|
2020-03-03 15:20:20 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-10-11 21:58:11 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_var_resolver() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
let mut scope = Scope::new();
|
|
|
|
scope.push("innocent", 1 as INT);
|
|
|
|
scope.push("chameleon", 123 as INT);
|
|
|
|
scope.push("DO_NOT_USE", 999 as INT);
|
|
|
|
|
2020-10-19 19:11:55 +08:00
|
|
|
engine.on_var(|name, _, context| {
|
2020-10-11 21:58:11 +08:00
|
|
|
match name {
|
|
|
|
"MYSTIC_NUMBER" => Ok(Some((42 as INT).into())),
|
|
|
|
// Override a variable - make it not found even if it exists!
|
|
|
|
"DO_NOT_USE" => {
|
2020-11-20 16:52:28 +08:00
|
|
|
Err(EvalAltResult::ErrorVariableNotFound(name.to_string(), Position::NONE).into())
|
2020-10-11 21:58:11 +08:00
|
|
|
}
|
|
|
|
// Silently maps 'chameleon' into 'innocent'.
|
2020-10-19 19:11:55 +08:00
|
|
|
"chameleon" => context
|
2020-12-14 23:05:13 +08:00
|
|
|
.scope()
|
2020-10-19 19:11:55 +08:00
|
|
|
.get_value("innocent")
|
|
|
|
.map(Some)
|
|
|
|
.ok_or_else(|| {
|
2020-11-20 16:52:28 +08:00
|
|
|
EvalAltResult::ErrorVariableNotFound(name.to_string(), Position::NONE).into()
|
2020-10-19 19:11:55 +08:00
|
|
|
}),
|
2020-10-11 21:58:11 +08:00
|
|
|
// Return Ok(None) to continue with the normal variable resolution process.
|
|
|
|
_ => Ok(None),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval_with_scope::<INT>(&mut scope, "MYSTIC_NUMBER")?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "chameleon")?, 1);
|
|
|
|
assert!(
|
|
|
|
matches!(*engine.eval_with_scope::<INT>(&mut scope, "DO_NOT_USE").expect_err("should error"),
|
|
|
|
EvalAltResult::ErrorVariableNotFound(n, _) if n == "DO_NOT_USE")
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|