rhai/tests/looping.rs

41 lines
785 B
Rust
Raw Normal View History

use rhai::{Engine, EvalAltResult, ParseError, ParseErrorType, INT};
2017-11-03 09:58:51 -07:00
#[test]
fn test_loop() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
2017-11-03 09:58:51 -07:00
2020-03-17 17:33:37 +08:00
assert_eq!(
engine.eval::<INT>(
r"
let x = 0;
let i = 0;
2017-11-03 09:58:51 -07:00
2020-03-17 17:33:37 +08:00
loop {
if i < 10 {
2020-04-01 16:22:18 +08:00
i += 1;
if x > 20 { continue; }
2020-03-17 17:33:37 +08:00
x = x + i;
} else {
break;
}
2017-11-03 09:58:51 -07:00
}
2020-03-17 17:33:37 +08:00
return x;
2019-09-18 11:21:07 +01:00
"
2020-03-17 17:33:37 +08:00
)?,
2020-04-01 16:22:18 +08:00
21
2020-03-17 17:33:37 +08:00
);
2020-03-02 22:11:56 +08:00
assert!(matches!(
engine.compile("let x = 0; break;").expect_err("should error"),
ParseError(x, _) if *x == ParseErrorType::LoopBreak
));
assert!(matches!(
engine.compile("let x = 0; if x > 0 { continue; }").expect_err("should error"),
ParseError(x, _) if *x == ParseErrorType::LoopBreak
));
2020-03-02 22:11:56 +08:00
Ok(())
2017-11-03 09:58:51 -07:00
}