rhai/tests/bool_op.rs

102 lines
2.0 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() {
let mut engine = Engine::new();
assert_eq!(engine.eval::<bool>("true && (false || true)"), Ok(true));
2020-03-02 05:08:03 +01:00
assert_eq!(engine.eval::<bool>("true & (false | true)"), Ok(true));
2017-11-03 17:58:51 +01:00
}
#[test]
fn test_bool_op2() {
let mut engine = Engine::new();
assert_eq!(engine.eval::<bool>("false && (false || true)"), Ok(false));
2020-03-02 05:08:03 +01:00
assert_eq!(engine.eval::<bool>("false & (false | true)"), Ok(false));
}
#[test]
fn test_bool_op3() {
let mut engine = Engine::new();
assert_eq!(
engine.eval::<bool>("true && (false || 123)"),
Err(EvalAltResult::ErrorBooleanArgMismatch("OR".into()))
);
assert_eq!(engine.eval::<bool>("true && (true || 123)"), Ok(true));
assert_eq!(
engine.eval::<bool>("123 && (false || true)"),
Err(EvalAltResult::ErrorBooleanArgMismatch("AND".into()))
);
assert_eq!(engine.eval::<bool>("false && (true || 123)"), Ok(false));
}
#[test]
fn test_bool_op_short_circuit() {
let mut engine = Engine::new();
assert_eq!(
engine.eval::<bool>(
r"
fn this() { true }
fn that() { 9/0 }
this() || that();
"
),
Ok(true)
);
assert_eq!(
engine.eval::<bool>(
r"
fn this() { false }
fn that() { 9/0 }
this() && that();
"
),
Ok(false)
);
}
#[test]
#[should_panic]
fn test_bool_op_no_short_circuit1() {
let mut engine = Engine::new();
assert_eq!(
engine.eval::<bool>(
r"
fn this() { false }
fn that() { 9/0 }
this() | that();
"
),
Ok(false)
);
}
#[test]
#[should_panic]
fn test_bool_op_no_short_circuit2() {
let mut engine = Engine::new();
assert_eq!(
engine.eval::<bool>(
r"
fn this() { false }
fn that() { 9/0 }
this() & that();
"
),
Ok(false)
);
}