2020-03-22 14:03:58 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, Scope, INT};
|
|
|
|
|
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_expressions() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-03-22 14:03:58 +01:00
|
|
|
let mut scope = Scope::new();
|
|
|
|
|
|
|
|
scope.push("x", 10 as INT);
|
|
|
|
|
|
|
|
assert_eq!(engine.eval_expression::<INT>("2 + (10 + 10) * 2")?, 42);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval_expression_with_scope::<INT>(&mut scope, "2 + (x + 10) * 2")?,
|
|
|
|
42
|
|
|
|
);
|
2022-08-26 17:10:58 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval_expression_with_scope::<INT>(&mut scope, "if x > 0 { 42 } else { 123 }")?,
|
|
|
|
42
|
|
|
|
);
|
2022-12-06 14:41:38 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
{
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval_expression_with_scope::<INT>(
|
|
|
|
&mut scope,
|
|
|
|
"[1, 2, 3, 4].map(|x| x * x).reduce(|a, v| a + v, 0)"
|
|
|
|
)?,
|
|
|
|
30
|
|
|
|
);
|
|
|
|
assert!(engine
|
|
|
|
.eval_expression_with_scope::<INT>(
|
|
|
|
&mut scope,
|
|
|
|
"[1, 2, 3, 4].map(|x| { let r = 2; x * r }).reduce(|a, v| a + v, 0)"
|
|
|
|
)
|
|
|
|
.is_err());
|
|
|
|
}
|
2022-08-26 17:10:58 +02:00
|
|
|
assert!(engine
|
|
|
|
.eval_expression_with_scope::<INT>(&mut scope, "if x > 0 { let y = 42; y } else { 123 }")
|
|
|
|
.is_err());
|
|
|
|
assert!(engine
|
|
|
|
.eval_expression_with_scope::<INT>(&mut scope, "if x > 0 { 42 } else { let y = 123; y }")
|
|
|
|
.is_err());
|
|
|
|
assert!(engine
|
|
|
|
.eval_expression_with_scope::<INT>(&mut scope, "if x > 0 { 42 } else {}")
|
|
|
|
.is_err());
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval_expression_with_scope::<INT>(
|
|
|
|
&mut scope,
|
|
|
|
"
|
|
|
|
switch x {
|
|
|
|
0 => 1,
|
|
|
|
10 => 42,
|
2023-02-12 16:20:14 +01:00
|
|
|
1..10 => 123,
|
2022-08-26 17:10:58 +02:00
|
|
|
}
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
2020-06-01 07:26:20 +02:00
|
|
|
assert!(engine
|
2022-08-26 17:10:58 +02:00
|
|
|
.eval_expression_with_scope::<INT>(
|
|
|
|
&mut scope,
|
|
|
|
"
|
|
|
|
switch x {
|
|
|
|
0 => 1,
|
2023-02-12 16:20:14 +01:00
|
|
|
10 => 42,
|
2022-08-26 17:10:58 +02:00
|
|
|
1..10 => {
|
|
|
|
let y = 123;
|
|
|
|
y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"
|
|
|
|
)
|
2020-06-01 07:26:20 +02:00
|
|
|
.is_err());
|
2020-03-22 14:03:58 +01:00
|
|
|
|
2022-10-29 06:09:18 +02:00
|
|
|
assert!(engine.compile_expression("40 + 2;").is_err());
|
|
|
|
assert!(engine.compile_expression("40 + { 2 }").is_err());
|
|
|
|
assert!(engine.compile_expression("x = 42").is_err());
|
2020-03-22 14:03:58 +01:00
|
|
|
assert!(engine.compile_expression("let x = 42").is_err());
|
2022-10-29 06:09:18 +02:00
|
|
|
assert!(engine
|
|
|
|
.compile_expression("do { break 42; } while true")
|
|
|
|
.is_err());
|
2020-03-22 14:03:58 +01:00
|
|
|
|
2020-06-12 12:04:16 +02:00
|
|
|
engine.compile("40 + { let x = 2; x }")?;
|
|
|
|
|
2020-03-22 14:03:58 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-25 04:50:58 +01:00
|
|
|
|
2021-01-07 10:30:06 +01:00
|
|
|
/// This example taken from https://github.com/rhaiscript/rhai/issues/115
|
2020-03-25 04:50:58 +01:00
|
|
|
#[test]
|
2020-03-29 11:15:12 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_expressions_eval() -> Result<(), Box<EvalAltResult>> {
|
2022-12-30 18:07:39 +01:00
|
|
|
#[allow(clippy::upper_case_acronyms)]
|
2020-03-25 04:50:58 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct AGENT {
|
|
|
|
pub gender: String,
|
|
|
|
pub age: INT,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AGENT {
|
|
|
|
pub fn get_gender(&mut self) -> String {
|
|
|
|
self.gender.clone()
|
|
|
|
}
|
|
|
|
pub fn get_age(&mut self) -> INT {
|
|
|
|
self.age
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is your agent
|
|
|
|
let my_agent = AGENT {
|
|
|
|
gender: "male".into(),
|
|
|
|
age: 42,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create the engine
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
// Register your AGENT type
|
|
|
|
engine.register_type_with_name::<AGENT>("AGENT");
|
|
|
|
engine.register_get("gender", AGENT::get_gender);
|
|
|
|
engine.register_get("age", AGENT::get_age);
|
|
|
|
|
2021-11-07 11:12:37 +01:00
|
|
|
// Create your scope, add the agent as a constant
|
2020-03-25 04:50:58 +01:00
|
|
|
let mut scope = Scope::new();
|
|
|
|
scope.push_constant("agent", my_agent);
|
|
|
|
|
|
|
|
// Evaluate the expression
|
2022-12-30 18:07:39 +01:00
|
|
|
assert!(engine.eval_expression_with_scope(
|
2020-03-25 04:50:58 +01:00
|
|
|
&mut scope,
|
|
|
|
r#"
|
2021-03-27 11:08:34 +01:00
|
|
|
agent.age > 10 && agent.gender == "male"
|
2020-03-25 04:50:58 +01:00
|
|
|
"#,
|
2022-12-30 18:07:39 +01:00
|
|
|
)?);
|
2020-03-25 04:50:58 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|