-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy paththrow.rs
97 lines (84 loc) · 2.39 KB
/
throw.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use rhai::{Engine, EvalAltResult, INT};
#[test]
fn test_throw() {
let engine = Engine::new();
assert!(matches!(
*engine.run("if true { throw 42 }").expect_err("expects error"),
EvalAltResult::ErrorRuntime(s, ..) if s.as_int().unwrap() == 42
));
assert!(matches!(
*engine.run(r#"throw"#).expect_err("expects error"),
EvalAltResult::ErrorRuntime(s, ..) if s.is_unit()
));
}
#[test]
fn test_try_catch() {
let engine = Engine::new();
assert_eq!(engine.eval::<INT>("try { throw 42; } catch (x) { return x; }").unwrap(), 42);
assert_eq!(engine.eval::<INT>("try { throw 42; } catch { return 123; }").unwrap(), 123);
#[cfg(not(feature = "unchecked"))]
assert_eq!(engine.eval::<INT>("let x = 42; try { let y = 123; print(x/0); } catch { x = 0 } x").unwrap(), 0);
#[cfg(not(feature = "no_function"))]
assert_eq!(
engine
.eval::<INT>(
"
fn foo(x) { try { throw 42; } catch (x) { return x; } }
foo(0)
"
)
.unwrap(),
42
);
assert_eq!(
engine
.eval::<INT>(
"
let err = 123;
let x = 0;
try { throw 42; } catch(err) { return err; }
"
)
.unwrap(),
42
);
assert_eq!(
engine
.eval::<INT>(
"
let err = 123;
let x = 0;
try { throw 42; } catch(err) { print(err); }
err
"
)
.unwrap(),
123
);
assert_eq!(
engine
.eval::<INT>(
"
let foo = 123;
let x = 0;
try { throw 42; } catch(err) { return foo; }
"
)
.unwrap(),
123
);
assert_eq!(
engine
.eval::<INT>(
"
let foo = 123;
let x = 0;
try { throw 42; } catch(err) { return err; }
"
)
.unwrap(),
42
);
#[cfg(not(feature = "unchecked"))]
assert!(matches!(*engine.run("try { 42/0; } catch { throw; }").expect_err("expects error"), EvalAltResult::ErrorArithmetic(..)));
}