2020-03-10 16:06:20 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, INT};
|
2017-11-03 17:58:51 +01:00
|
|
|
|
|
|
|
#[test]
|
2020-03-02 15:11:56 +01:00
|
|
|
fn test_if() -> Result<(), EvalAltResult> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2017-11-03 17:58:51 +01:00
|
|
|
|
2020-03-10 16:06:20 +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!(
|
2020-03-10 16:06:20 +01:00
|
|
|
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!(
|
2020-03-10 16:06:20 +01:00
|
|
|
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 }
|
|
|
|
"
|
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<(), EvalAltResult> {
|
2020-04-07 07:23:06 +02:00
|
|
|
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
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
3
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|