Add try-catch.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use rhai::{Engine, EvalAltResult};
|
||||
use rhai::{Engine, EvalAltResult, INT};
|
||||
|
||||
#[test]
|
||||
fn test_throw() {
|
||||
@@ -14,3 +14,27 @@ fn test_throw() {
|
||||
EvalAltResult::ErrorRuntime(s, _) if s.is::<()>()
|
||||
));
|
||||
}
|
||||
|
||||
#[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
|
||||
);
|
||||
|
||||
assert!(matches!(
|
||||
*engine
|
||||
.eval::<()>("try { 42/0; } catch { throw; }")
|
||||
.expect_err("expects error"),
|
||||
EvalAltResult::ErrorArithmetic(_, _)
|
||||
));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user