rhai/tests/throw.rs

42 lines
999 B
Rust
Raw Normal View History

2020-10-20 17:16:03 +02:00
use rhai::{Engine, EvalAltResult, INT};
2020-03-03 11:15:20 +01:00
#[test]
fn test_throw() {
let engine = Engine::new();
2020-03-03 11:15:20 +01:00
2020-03-14 16:40:30 +01:00
assert!(matches!(
*engine.eval::<()>("if true { throw 42 }").expect_err("expects error"),
EvalAltResult::ErrorRuntime(s, _) if s.as_int().unwrap() == 42
));
2020-03-03 11:15:20 +01:00
2020-03-14 16:40:30 +01:00
assert!(matches!(
*engine.eval::<()>(r#"throw"#).expect_err("expects error"),
EvalAltResult::ErrorRuntime(s, _) if s.is::<()>()
));
2020-03-03 11:15:20 +01:00
}
2020-10-20 17:16:03 +02:00
#[test]
fn test_try_catch() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(
engine.eval::<INT>("try { throw 42; } catch (x) { return x; }")?,
42
);
assert_eq!(
engine.eval::<INT>("try { throw 42; } catch { return 123; }")?,
123
);
2020-10-20 17:30:01 +02:00
#[cfg(not(feature = "unchecked"))]
2020-10-20 17:16:03 +02:00
assert!(matches!(
*engine
.eval::<()>("try { 42/0; } catch { throw; }")
.expect_err("expects error"),
EvalAltResult::ErrorArithmetic(_, _)
));
Ok(())
}