2020-03-02 05:08:03 +01:00
|
|
|
use rhai::{Engine, EvalAltResult};
|
2017-11-03 17:58:51 +01:00
|
|
|
|
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_bool_op1() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2017-11-03 17:58:51 +01:00
|
|
|
|
2022-12-30 18:07:39 +01:00
|
|
|
assert!(engine.eval::<bool>("true && (false || true)").unwrap());
|
|
|
|
assert!(engine.eval::<bool>("true & (false | true)").unwrap());
|
2020-03-02 15:11:56 +01:00
|
|
|
|
|
|
|
Ok(())
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_bool_op2() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2017-11-03 17:58:51 +01:00
|
|
|
|
2022-12-30 18:07:39 +01:00
|
|
|
assert!(!engine.eval::<bool>("false && (false || true)").unwrap());
|
|
|
|
assert!(!engine.eval::<bool>("false & (false | true)").unwrap());
|
2020-03-02 15:11:56 +01:00
|
|
|
|
|
|
|
Ok(())
|
2020-03-02 05:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_bool_op3() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-03-02 05:08:03 +01:00
|
|
|
|
2020-03-02 15:11:56 +01:00
|
|
|
assert!(engine.eval::<bool>("true && (false || 123)").is_err());
|
2022-12-30 18:07:39 +01:00
|
|
|
assert!(engine.eval::<bool>("true && (true || { throw })").unwrap());
|
2020-03-02 15:11:56 +01:00
|
|
|
assert!(engine.eval::<bool>("123 && (false || true)").is_err());
|
2022-12-30 18:07:39 +01:00
|
|
|
assert!(!engine.eval::<bool>("false && (true || { throw })").unwrap());
|
2020-03-02 05:08:03 +01:00
|
|
|
|
2020-03-02 15:11:56 +01:00
|
|
|
Ok(())
|
2020-03-02 05:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_bool_op_short_circuit() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-03-02 05:08:03 +01:00
|
|
|
|
2022-12-30 18:07:39 +01:00
|
|
|
assert!(engine
|
|
|
|
.eval::<bool>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-06-26 04:39:18 +02:00
|
|
|
let x = true;
|
|
|
|
x || { throw; };
|
2020-11-12 05:37:42 +01:00
|
|
|
"
|
2022-12-30 18:07:39 +01:00
|
|
|
)
|
|
|
|
.unwrap());
|
2020-03-02 05:08:03 +01:00
|
|
|
|
2022-12-30 18:07:39 +01:00
|
|
|
assert!(!engine
|
|
|
|
.eval::<bool>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-06-26 04:39:18 +02:00
|
|
|
let x = false;
|
|
|
|
x && { throw; };
|
2020-11-12 05:37:42 +01:00
|
|
|
"
|
2022-12-30 18:07:39 +01:00
|
|
|
)
|
|
|
|
.unwrap());
|
2020-03-02 15:11:56 +01:00
|
|
|
|
|
|
|
Ok(())
|
2020-03-02 05:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bool_op_no_short_circuit1() {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-03-02 05:08:03 +01:00
|
|
|
|
2022-12-30 18:07:39 +01:00
|
|
|
let _ = engine
|
2020-03-12 05:35:30 +01:00
|
|
|
.eval::<bool>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-11-12 05:37:42 +01:00
|
|
|
let x = true;
|
|
|
|
x | { throw; }
|
2022-12-30 18:07:39 +01:00
|
|
|
",
|
2020-03-12 05:35:30 +01:00
|
|
|
)
|
2022-12-30 18:07:39 +01:00
|
|
|
.unwrap_err();
|
2020-03-02 05:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bool_op_no_short_circuit2() {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-03-02 05:08:03 +01:00
|
|
|
|
2020-03-12 05:35:30 +01:00
|
|
|
assert!(engine
|
|
|
|
.eval::<bool>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-11-12 05:37:42 +01:00
|
|
|
let x = false;
|
|
|
|
x & { throw; }
|
|
|
|
"
|
2020-03-12 05:35:30 +01:00
|
|
|
)
|
|
|
|
.is_err());
|
2018-06-13 10:56:29 +02:00
|
|
|
}
|