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() {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-03-03 11:15:20 +01:00
|
|
|
|
2020-03-14 16:40:30 +01:00
|
|
|
assert!(matches!(
|
2022-01-10 06:26:33 +01:00
|
|
|
*engine.run("if true { throw 42 }").expect_err("expects error"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorRuntime(s, ..) if s.as_int().unwrap() == 42
|
2020-04-21 17:25:12 +02:00
|
|
|
));
|
2020-03-03 11:15:20 +01:00
|
|
|
|
2020-03-14 16:40:30 +01:00
|
|
|
assert!(matches!(
|
2022-01-10 06:26:33 +01:00
|
|
|
*engine.run(r#"throw"#).expect_err("expects error"),
|
2022-11-09 05:44:57 +01:00
|
|
|
EvalAltResult::ErrorRuntime(s, ..) if s.is_unit()
|
2020-04-21 17:25:12 +02:00
|
|
|
));
|
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
|
|
|
|
);
|
|
|
|
|
2022-01-24 01:49:41 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2022-01-24 01:34:21 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let x = 42; try { let y = 123; print(x/0); } catch { x = 0 } x")?,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
2021-09-02 16:17:45 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
fn foo(x) { try { throw 42; } catch (x) { return x; } }
|
|
|
|
foo(0)
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let err = 123;
|
|
|
|
let x = 0;
|
|
|
|
try { throw 42; } catch(err) { return err; }
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
2021-09-03 04:05:58 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let err = 123;
|
|
|
|
let x = 0;
|
|
|
|
try { throw 42; } catch(err) { print(err); }
|
|
|
|
err
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
123
|
|
|
|
);
|
|
|
|
|
2021-09-02 16:17:45 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let foo = 123;
|
|
|
|
let x = 0;
|
|
|
|
try { throw 42; } catch(err) { return foo; }
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
123
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let foo = 123;
|
|
|
|
let x = 0;
|
|
|
|
try { throw 42; } catch(err) { return err; }
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
2020-10-20 17:30:01 +02:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2020-10-20 17:16:03 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2022-01-10 06:26:33 +01:00
|
|
|
.run("try { 42/0; } catch { throw; }")
|
2020-10-20 17:16:03 +02:00
|
|
|
.expect_err("expects error"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorArithmetic(..)
|
2020-10-20 17:16:03 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|