2020-05-07 06:25:09 +02:00
|
|
|
use rhai::{Engine, EvalAltResult, ParseErrorType, INT};
|
2020-03-13 11:12:41 +01:00
|
|
|
|
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_constant() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-03-13 11:12:41 +01:00
|
|
|
|
2020-03-15 15:39:58 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("const x = 123; x")?, 123);
|
2020-03-13 11:12:41 +01:00
|
|
|
|
2020-04-21 17:25:12 +02:00
|
|
|
assert!(matches!(
|
2020-06-12 12:04:16 +02:00
|
|
|
*engine
|
|
|
|
.eval::<INT>("const x = 123; x = 42;")
|
|
|
|
.expect_err("expects error"),
|
|
|
|
EvalAltResult::ErrorParsing(ParseErrorType::AssignmentToConstant(x), _) if x == "x"
|
2020-04-21 17:25:12 +02:00
|
|
|
));
|
2020-03-14 12:46:44 +01:00
|
|
|
|
2020-03-14 13:06:40 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-04-21 17:25:12 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine.eval::<INT>("const x = [1, 2, 3, 4, 5]; x[2] = 42;").expect_err("expects error"),
|
2020-06-12 12:04:16 +02:00
|
|
|
EvalAltResult::ErrorParsing(ParseErrorType::AssignmentToConstant(x), _) if x == "x"
|
2020-04-21 17:25:12 +02:00
|
|
|
));
|
2020-03-13 11:12:41 +01:00
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|