rhai/tests/if_block.rs

48 lines
1.1 KiB
Rust
Raw Normal View History

use rhai::{Engine, EvalAltResult, INT};
2017-11-03 17:58:51 +01:00
#[test]
fn test_if() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
2017-11-03 17:58:51 +01:00
assert_eq!(engine.eval::<INT>("if true { 55 }")?, 55);
assert_eq!(engine.eval::<INT>("if false { 55 } else { 44 }")?, 44);
assert_eq!(engine.eval::<INT>("if true { 55 } else { 44 }")?, 55);
2020-03-02 10:04:56 +01:00
assert_eq!(
engine.eval::<INT>("if false { 55 } else if true { 33 } else { 44 }")?,
2020-03-02 15:11:56 +01:00
33
2020-03-02 10:04:56 +01:00
);
assert_eq!(
engine.eval::<INT>(
2020-03-02 10:04:56 +01:00
r"
if false { 55 }
else if false { 33 }
else if false { 66 }
else if false { 77 }
else if false { 88 }
else { 44 }
2021-03-27 11:08:34 +01:00
"
2020-03-02 15:11:56 +01:00
)?,
44
2020-03-02 10:04:56 +01:00
);
2020-03-02 15:11:56 +01:00
Ok(())
2017-11-03 17:58:51 +01:00
}
2020-03-27 16:47:23 +01:00
#[test]
fn test_if_expr() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
2020-03-27 16:47:23 +01:00
assert_eq!(
engine.eval::<INT>(
r"
let x = 42;
let y = 1 + if x > 40 { 100 } else { 0 } / x;
y
2021-03-27 11:08:34 +01:00
"
2020-03-27 16:47:23 +01:00
)?,
3
);
Ok(())
}