2021-03-15 04:36:30 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, ParseErrorType, Scope, 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"),
|
2022-02-08 02:02:15 +01:00
|
|
|
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!(
|
2021-08-06 08:46:27 +02:00
|
|
|
*engine.run("const x = [1, 2, 3, 4, 5]; x[2] = 42;").expect_err("expects error"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorAssignmentToConstant(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
|
|
|
|
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!(
|
2021-08-06 08:46:27 +02:00
|
|
|
*engine.run_with_scope(&mut scope, "x = 1").expect_err("expects error"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorAssignmentToConstant(x, ..) if x == "x"
|
2020-11-03 06:08:19 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-11-21 07:46:05 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
#[test]
|
|
|
|
fn test_constant_mut() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct TestStruct(INT); // custom type
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2021-05-15 05:41:42 +02:00
|
|
|
fn set_value(obj: &mut TestStruct, value: INT) {
|
|
|
|
obj.0 = value;
|
|
|
|
}
|
|
|
|
|
2020-11-21 07:46:05 +01:00
|
|
|
engine
|
|
|
|
.register_type_with_name::<TestStruct>("TestStruct")
|
2021-05-15 05:41:42 +02:00
|
|
|
.register_fn("new_ts", || TestStruct(123))
|
2020-11-21 07:46:05 +01:00
|
|
|
.register_get("value", |obj: &mut TestStruct| obj.0)
|
2021-05-15 05:41:42 +02:00
|
|
|
.register_set("value", set_value)
|
|
|
|
.register_fn("update_value", set_value);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
const MY_NUMBER = new_ts();
|
|
|
|
MY_NUMBER.update_value(42);
|
|
|
|
MY_NUMBER.value
|
|
|
|
",
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
const MY_NUMBER = new_ts();
|
|
|
|
update_value(MY_NUMBER, 42);
|
|
|
|
MY_NUMBER.value
|
|
|
|
",
|
|
|
|
)?,
|
|
|
|
123
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2021-08-06 08:46:27 +02:00
|
|
|
.run(
|
2021-05-15 05:41:42 +02:00
|
|
|
"
|
|
|
|
const MY_NUMBER = new_ts();
|
|
|
|
MY_NUMBER.value = 42;
|
|
|
|
"
|
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-10-27 16:08:47 +02:00
|
|
|
EvalAltResult::ErrorNonPureMethodCallOnConstant(..)
|
2021-05-15 05:41:42 +02:00
|
|
|
));
|
2020-11-21 07:46:05 +01:00
|
|
|
|
|
|
|
let mut scope = Scope::new();
|
|
|
|
|
|
|
|
scope.push_constant("MY_NUMBER", TestStruct(123));
|
|
|
|
|
2021-05-15 05:41:42 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval_with_scope::<INT>(
|
|
|
|
&mut scope,
|
|
|
|
"
|
|
|
|
update_value(MY_NUMBER, 42);
|
|
|
|
MY_NUMBER.value
|
|
|
|
",
|
|
|
|
)?,
|
|
|
|
123
|
|
|
|
);
|
|
|
|
|
2020-11-21 07:46:05 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval_with_scope::<INT>(
|
|
|
|
&mut scope,
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-11-21 07:46:05 +01:00
|
|
|
MY_NUMBER.update_value(42);
|
|
|
|
MY_NUMBER.value
|
|
|
|
",
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
2021-05-15 05:41:42 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2021-08-06 08:46:27 +02:00
|
|
|
.run_with_scope(&mut scope, "MY_NUMBER.value = 42;")
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-10-27 16:08:47 +02:00
|
|
|
EvalAltResult::ErrorNonPureMethodCallOnConstant(..)
|
2021-05-15 05:41:42 +02:00
|
|
|
));
|
|
|
|
|
2020-11-21 07:46:05 +01:00
|
|
|
Ok(())
|
|
|
|
}
|