rhai/tests/constants.rs

24 lines
720 B
Rust
Raw Normal View History

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