rhai/tests/bool_op.rs

83 lines
1.7 KiB
Rust
Raw Normal View History

2020-03-02 05:08:03 +01:00
use rhai::{Engine, EvalAltResult};
2017-11-03 17:58:51 +01:00
#[test]
fn test_bool_op1() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
2017-11-03 17:58:51 +01:00
2023-02-13 01:59:58 +01:00
assert!(engine.eval::<bool>("true && (false || true)")?);
assert!(engine.eval::<bool>("true & (false | true)")?);
2020-03-02 15:11:56 +01:00
Ok(())
2017-11-03 17:58:51 +01:00
}
#[test]
fn test_bool_op2() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
2017-11-03 17:58:51 +01:00
2023-02-13 01:59:58 +01:00
assert!(!engine.eval::<bool>("false && (false || true)")?);
assert!(!engine.eval::<bool>("false & (false | true)")?);
2020-03-02 15:11:56 +01:00
Ok(())
2020-03-02 05:08:03 +01:00
}
#[test]
fn test_bool_op3() -> Result<(), Box<EvalAltResult>> {
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());
2023-02-13 01:59:58 +01:00
assert!(engine.eval::<bool>("true && (true || { throw })")?);
2020-03-02 15:11:56 +01:00
assert!(engine.eval::<bool>("123 && (false || true)").is_err());
2023-02-13 01:59:58 +01:00
assert!(!engine.eval::<bool>("false && (true || { throw })")?);
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_short_circuit() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
2020-03-02 05:08:03 +01:00
2023-02-13 01:59:58 +01:00
assert!(engine.eval::<bool>(
"
2020-06-26 04:39:18 +02:00
let x = true;
x || { throw; };
2020-11-12 05:37:42 +01:00
"
2023-02-13 01:59:58 +01:00
)?);
2020-03-02 05:08:03 +01:00
2023-02-13 01:59:58 +01:00
assert!(!engine.eval::<bool>(
"
2020-06-26 04:39:18 +02:00
let x = false;
x && { throw; };
2020-11-12 05:37:42 +01:00
"
2023-02-13 01:59:58 +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() {
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() {
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());
}