2020-03-15 22:39:58 +08:00
|
|
|
use rhai::{Engine, EvalAltResult, INT};
|
2020-03-13 18:12:41 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_constant() -> Result<(), EvalAltResult> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2020-03-15 22:39:58 +08:00
|
|
|
assert_eq!(engine.eval::<INT>("const x = 123; x")?, 123);
|
2020-03-13 18:12:41 +08:00
|
|
|
|
2020-03-14 23:40:30 +08:00
|
|
|
assert!(
|
2020-03-15 22:39:58 +08:00
|
|
|
matches!(engine.eval::<INT>("const x = 123; x = 42;").expect_err("expects error"),
|
2020-03-14 23:40:30 +08:00
|
|
|
EvalAltResult::ErrorAssignmentToConstant(var, _) if var == "x")
|
|
|
|
);
|
2020-03-14 19:46:44 +08:00
|
|
|
|
2020-03-14 20:06:40 +08:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-03-14 23:40:30 +08:00
|
|
|
assert!(
|
2020-03-15 22:39:58 +08:00
|
|
|
matches!(engine.eval::<INT>("const x = [1, 2, 3, 4, 5]; x[2] = 42;").expect_err("expects error"),
|
2020-03-14 23:40:30 +08:00
|
|
|
EvalAltResult::ErrorAssignmentToConstant(var, _) if var == "x")
|
|
|
|
);
|
2020-03-13 18:12:41 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|