rhai/tests/constants.rs

65 lines
1.5 KiB
Rust
Raw Normal View History

2020-11-03 06:08:19 +01:00
use rhai::{Engine, EvalAltResult, ParseErrorType, Scope, 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!(
2020-11-03 06:08:19 +01:00
*engine.consume("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(())
}
2020-10-03 15:59:19 +02:00
2020-11-03 06:08:19 +01:00
#[test]
fn test_constant_scope() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
let mut scope = Scope::new();
scope.push_constant("x", 42 as INT);
assert!(matches!(
*engine.consume_with_scope(&mut scope, "x = 1").expect_err("expects error"),
EvalAltResult::ErrorAssignmentToConstant(x, _) if x == "x"
));
Ok(())
}
2020-10-03 15:59:19 +02: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(())
}