2020-03-10 16:06:20 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, INT};
|
2020-02-23 15:48:46 +01:00
|
|
|
|
2020-04-04 06:20:24 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-02-23 15:48:46 +01:00
|
|
|
#[test]
|
2020-04-04 06:20:24 +02:00
|
|
|
fn test_for_array() -> Result<(), EvalAltResult> {
|
2020-02-23 15:48:46 +01:00
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
let script = r"
|
|
|
|
let sum1 = 0;
|
|
|
|
let sum2 = 0;
|
|
|
|
let inputs = [1, 2, 3, 4, 5];
|
|
|
|
|
|
|
|
for x in inputs {
|
|
|
|
sum1 += x;
|
|
|
|
}
|
|
|
|
|
|
|
|
for x in range(1, 6) {
|
|
|
|
sum2 += x;
|
|
|
|
}
|
|
|
|
|
2020-04-04 06:20:24 +02:00
|
|
|
for x in range(1, 6, 3) {
|
|
|
|
sum2 += x;
|
|
|
|
}
|
|
|
|
|
2020-02-23 15:48:46 +01:00
|
|
|
sum1 + sum2
|
|
|
|
";
|
|
|
|
|
2020-04-04 06:20:24 +02:00
|
|
|
assert_eq!(engine.eval::<INT>(script)?, 35);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
#[test]
|
|
|
|
fn test_for_object() -> Result<(), EvalAltResult> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
let script = r#"
|
|
|
|
let sum = 0;
|
|
|
|
let keys = "";
|
|
|
|
let map = #{a: 1, b: 2, c: 3};
|
|
|
|
|
|
|
|
for key in keys(map) {
|
|
|
|
keys += key;
|
|
|
|
}
|
|
|
|
for value in values(map) {
|
|
|
|
sum += value;
|
|
|
|
}
|
|
|
|
|
|
|
|
keys.len() + sum
|
|
|
|
"#;
|
|
|
|
|
|
|
|
assert_eq!(engine.eval::<INT>(script)?, 9);
|
2020-03-02 15:11:56 +01:00
|
|
|
|
|
|
|
Ok(())
|
2020-02-23 15:48:46 +01:00
|
|
|
}
|