rhai/tests/for.rs

57 lines
1.0 KiB
Rust
Raw Normal View History

use rhai::{Engine, EvalAltResult, INT};
#[cfg(not(feature = "no_index"))]
#[test]
fn test_for_array() -> Result<(), EvalAltResult> {
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;
}
for x in range(1, 6, 3) {
sum2 += x;
}
sum1 + sum2
";
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(())
}