2020-03-02 05:08:03 +01:00
|
|
|
use rhai::{Engine, EvalAltResult};
|
2017-11-03 17:58:51 +01:00
|
|
|
|
|
|
|
#[test]
|
2020-03-02 15:11:56 +01:00
|
|
|
fn test_bool_op1() -> Result<(), EvalAltResult> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2017-11-03 17:58:51 +01:00
|
|
|
|
2020-03-02 15:11:56 +01:00
|
|
|
assert_eq!(engine.eval::<bool>("true && (false || true)")?, true);
|
|
|
|
assert_eq!(engine.eval::<bool>("true & (false | true)")?, true);
|
|
|
|
|
|
|
|
Ok(())
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-02 15:11:56 +01:00
|
|
|
fn test_bool_op2() -> Result<(), EvalAltResult> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2017-11-03 17:58:51 +01:00
|
|
|
|
2020-03-02 15:11:56 +01:00
|
|
|
assert_eq!(engine.eval::<bool>("false && (false || true)")?, false);
|
|
|
|
assert_eq!(engine.eval::<bool>("false & (false | true)")?, false);
|
|
|
|
|
|
|
|
Ok(())
|
2020-03-02 05:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-02 15:11:56 +01:00
|
|
|
fn test_bool_op3() -> Result<(), 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());
|
|
|
|
assert_eq!(engine.eval::<bool>("true && (true || 123)")?, true);
|
|
|
|
assert!(engine.eval::<bool>("123 && (false || true)").is_err());
|
|
|
|
assert_eq!(engine.eval::<bool>("false && (true || 123)")?, false);
|
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-03-02 15:11:56 +01:00
|
|
|
fn test_bool_op_short_circuit() -> Result<(), EvalAltResult> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-03-02 05:08:03 +01:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<bool>(
|
|
|
|
r"
|
2020-03-11 06:28:12 +01:00
|
|
|
let this = true;
|
2020-03-02 05:08:03 +01:00
|
|
|
|
2020-03-11 06:28:12 +01:00
|
|
|
this || { throw; };
|
2020-03-02 05:08:03 +01:00
|
|
|
"
|
2020-03-02 15:11:56 +01:00
|
|
|
)?,
|
|
|
|
true
|
2020-03-02 05:08:03 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<bool>(
|
|
|
|
r"
|
2020-03-11 06:28:12 +01:00
|
|
|
let this = false;
|
2020-03-02 05:08:03 +01:00
|
|
|
|
2020-03-11 06:28:12 +01:00
|
|
|
this && { throw; };
|
2020-03-02 05:08:03 +01:00
|
|
|
"
|
2020-03-02 15:11:56 +01:00
|
|
|
)?,
|
|
|
|
false
|
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]
|
|
|
|
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
|
|
|
|
2020-03-12 05:35:30 +01:00
|
|
|
assert!(engine
|
|
|
|
.eval::<bool>(
|
|
|
|
r"
|
2020-03-11 06:28:12 +01:00
|
|
|
let this = true;
|
2020-03-02 15:11:56 +01:00
|
|
|
|
2020-03-11 06:28:12 +01:00
|
|
|
this | { throw; }
|
2020-03-02 15:11:56 +01:00
|
|
|
"
|
2020-03-12 05:35:30 +01:00
|
|
|
)
|
|
|
|
.is_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>(
|
|
|
|
r"
|
2020-03-11 06:28:12 +01:00
|
|
|
let this = false;
|
2020-03-02 15:11:56 +01:00
|
|
|
|
2020-03-11 06:28:12 +01:00
|
|
|
this & { throw; }
|
2020-03-02 15:11:56 +01:00
|
|
|
"
|
2020-03-12 05:35:30 +01:00
|
|
|
)
|
|
|
|
.is_err());
|
2018-06-13 10:56:29 +02:00
|
|
|
}
|