rhai/tests/constants.rs

22 lines
628 B
Rust
Raw Normal View History

2020-03-13 11:12:41 +01:00
use rhai::{Engine, EvalAltResult};
#[test]
fn test_constant() -> Result<(), EvalAltResult> {
let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("const x = 123; x")?, 123);
2020-03-14 16:40:30 +01:00
assert!(
matches!(engine.eval::<i64>("const x = 123; x = 42;").expect_err("expects error"),
EvalAltResult::ErrorAssignmentToConstant(var, _) if var == "x")
);
#[cfg(not(feature = "no_index"))]
2020-03-14 16:40:30 +01:00
assert!(
matches!(engine.eval::<i64>("const x = [1, 2, 3, 4, 5]; x[2] = 42;").expect_err("expects error"),
EvalAltResult::ErrorAssignmentToConstant(var, _) if var == "x")
);
2020-03-13 11:12:41 +01:00
Ok(())
}