rhai/tests/var_scope.rs

219 lines
6.6 KiB
Rust
Raw Normal View History

2022-02-13 11:46:25 +01:00
use rhai::{Engine, EvalAltResult, ParseErrorType, Position, Scope, INT};
2017-11-03 17:58:51 +01:00
#[test]
fn test_var_scope() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
2019-09-18 12:21:07 +02:00
let mut scope = Scope::new();
2017-11-03 17:58:51 +01:00
2022-02-18 08:04:46 +01:00
engine.run_with_scope(&mut scope, "let x = 4 + 5")?;
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x")?, 9);
2022-02-18 08:04:46 +01:00
engine.run_with_scope(&mut scope, "x += 1; x += 2;")?;
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x")?, 12);
2020-04-05 13:17:48 +02:00
scope.set_value("x", 42 as INT);
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x")?, 42);
2022-02-18 08:04:46 +01:00
engine.run_with_scope(&mut scope, "{ let x = 3 }")?;
2020-04-05 13:17:48 +02:00
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x")?, 42);
2017-11-03 17:58:51 +01:00
2022-02-18 08:04:46 +01:00
#[cfg(not(feature = "no_optimize"))]
if engine.optimization_level() != rhai::OptimizationLevel::None {
scope.clear();
engine.run_with_scope(&mut scope, "let x = 3; let x = 42; let x = 123;")?;
assert_eq!(scope.len(), 1);
assert_eq!(scope.get_value::<INT>("x").unwrap(), 123);
scope.clear();
engine.run_with_scope(
&mut scope,
"let x = 3; let y = 0; let x = 42; let y = 999; let x = 123;",
)?;
assert_eq!(scope.len(), 2);
assert_eq!(scope.get_value::<INT>("x").unwrap(), 123);
assert_eq!(scope.get_value::<INT>("y").unwrap(), 999);
scope.clear();
engine.run_with_scope(
&mut scope,
"const x = 3; let y = 0; let x = 42; let y = 999;",
)?;
assert_eq!(scope.len(), 2);
assert_eq!(scope.get_value::<INT>("x").unwrap(), 42);
assert_eq!(scope.get_value::<INT>("y").unwrap(), 999);
assert!(!scope.is_constant("x").unwrap());
assert!(!scope.is_constant("y").unwrap());
2022-02-18 08:04:46 +01:00
scope.clear();
engine.run_with_scope(
&mut scope,
"const x = 3; let y = 0; let x = 42; let y = 999; const x = 123;",
)?;
assert_eq!(scope.len(), 2);
2022-02-18 08:04:46 +01:00
assert_eq!(scope.get_value::<INT>("x").unwrap(), 123);
assert_eq!(scope.get_value::<INT>("y").unwrap(), 999);
assert!(scope.is_constant("x").unwrap());
assert!(!scope.is_constant("y").unwrap());
2022-02-18 08:04:46 +01:00
scope.clear();
engine.run_with_scope(
&mut scope,
"let x = 3; let y = 0; { let x = 42; let y = 999; } let x = 123;",
)?;
assert_eq!(
engine.eval::<INT>(
"
let sum = 0;
for x in 0..10 {
let x = 42;
sum += x;
}
sum
",
)?,
420
);
}
assert_eq!(scope.len(), 2);
assert_eq!(scope.get_value::<INT>("x").unwrap(), 123);
assert_eq!(scope.get_value::<INT>("y").unwrap(), 0);
2020-03-02 15:11:56 +01:00
Ok(())
2017-11-03 17:58:51 +01:00
}
2020-03-03 08:20:20 +01:00
2021-03-01 15:44:56 +01: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")
2021-03-27 11:08:34 +01:00
"#
2021-03-01 15:44:56 +01:00
)?);
assert!(!engine.eval::<bool>(
r#"
let x = 42;
is_def_var("y")
2021-03-27 11:08:34 +01:00
"#
2021-03-01 15:44:56 +01:00
)?);
assert!(engine.eval::<bool>(
r#"
const x = 42;
is_def_var("x")
2021-03-27 11:08:34 +01:00
"#
2021-03-01 15:44:56 +01:00
)?);
Ok(())
}
2020-03-03 08:20:20 +01:00
#[test]
fn test_scope_eval() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
2020-03-03 08:20:20 +01:00
// First create the state
let mut scope = Scope::new();
// Then push some initialized variables into the state
// NOTE: Remember the default numbers used by Rhai are INT and f64.
2020-03-03 08:20:20 +01:00
// Better stick to them or it gets hard to work with other variables in the script.
scope.push("y", 42 as INT);
scope.push("z", 999 as INT);
2020-03-03 08:20:20 +01:00
// First invocation
engine
2022-02-18 08:04:46 +01:00
.run_with_scope(&mut scope, " let x = 4 + 5 - y + z; y = 1;")
2021-05-22 13:14:24 +02:00
.expect("variables y and z should exist");
2020-03-03 08:20:20 +01:00
// Second invocation using the same state
let result = engine.eval_with_scope::<INT>(&mut scope, "x")?;
2020-03-09 14:09:53 +01:00
println!("result: {}", result); // should print 966
2020-03-03 08:20:20 +01:00
// Variable y is changed in the script
2020-03-12 05:35:30 +01:00
assert_eq!(
scope
.get_value::<INT>("y")
2020-03-19 13:55:53 +01:00
.expect("variable y should exist"),
2020-03-12 05:35:30 +01:00
1
);
2020-03-03 08:20:20 +01:00
Ok(())
}
2020-10-11 15:58:11 +02: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);
engine.on_var(|name, _, context| {
2020-10-11 15:58:11 +02: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 09:52:28 +01:00
Err(EvalAltResult::ErrorVariableNotFound(name.to_string(), Position::NONE).into())
2020-10-11 15:58:11 +02:00
}
// Silently maps 'chameleon' into 'innocent'.
"chameleon" => context
2020-12-14 16:05:13 +01:00
.scope()
.get_value("innocent")
.map(Some)
.ok_or_else(|| {
2020-11-20 09:52:28 +01:00
EvalAltResult::ErrorVariableNotFound(name.to_string(), Position::NONE).into()
}),
2020-10-11 15:58:11 +02: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"),
2022-02-08 02:02:15 +01:00
EvalAltResult::ErrorVariableNotFound(n, ..) if n == "DO_NOT_USE")
2020-10-11 15:58:11 +02:00
);
Ok(())
}
2022-02-04 15:59:41 +01:00
#[test]
fn test_var_def_filter() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
2022-02-13 11:46:25 +01:00
let ast = engine.compile("let x = 42;")?;
engine.run_ast(&ast)?;
2022-02-15 03:56:05 +01:00
engine.on_def_var(|_, info, _| match (info.name, info.nesting_level) {
2022-02-04 15:59:41 +01:00
("x", 0 | 1) => Ok(false),
_ => Ok(true),
});
assert_eq!(
engine.eval::<INT>("let y = 42; let y = 123; let z = y + 1; z")?,
124
);
2022-02-13 11:46:25 +01:00
assert!(matches!(
*engine.compile("let x = 42;").expect_err("should error").0,
ParseErrorType::ForbiddenVariable(s) if s == "x"
));
assert!(matches!(
*engine.run_ast(&ast).expect_err("should err"),
EvalAltResult::ErrorForbiddenVariable(s, _) if s == "x"
));
2022-02-04 15:59:41 +01:00
assert!(engine.run("const x = 42;").is_err());
assert!(engine.run("let y = 42; { let x = y + 1; }").is_err());
assert!(engine.run("let y = 42; { let x = y + 1; }").is_err());
engine.run("let y = 42; { let z = y + 1; { let x = z + 1; } }")?;
Ok(())
}