-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathlooping.rs
91 lines (75 loc) · 2 KB
/
looping.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
use rhai::{Engine, ParseErrorType, INT};
#[test]
fn test_loop() {
let engine = Engine::new();
assert_eq!(
engine
.eval::<INT>(
"
let x = 0;
let i = 0;
loop {
if i < 10 {
i += 1;
if x > 20 { continue; }
x += i;
} else {
break;
}
}
x
"
)
.unwrap(),
21
);
assert_eq!(
engine
.eval::<INT>(
"
for n in 0..10 {
let x = if n <= 5 { n };
x ?? break 42;
}
"
)
.unwrap(),
42
);
assert_eq!(*engine.compile("let x = 0; break;").unwrap_err().err_type(), ParseErrorType::LoopBreak);
#[cfg(not(feature = "no_function"))]
assert_eq!(*engine.compile("loop { let f = || { break; } }").unwrap_err().err_type(), ParseErrorType::LoopBreak);
assert_eq!(*engine.compile("let x = 0; if x > 0 { continue; }").unwrap_err().err_type(), ParseErrorType::LoopBreak);
}
#[test]
fn test_loop_expression() {
let mut engine = Engine::new();
assert_eq!(
engine
.eval::<INT>(
"
let x = 0;
let value = while x < 10 {
if x % 5 == 0 { break 42; }
x += 1;
};
value
"
)
.unwrap(),
42
);
engine.set_allow_loop_expressions(false);
assert!(engine
.eval::<INT>(
"
let x = 0;
let value = while x < 10 {
if x % 5 == 0 { break 42; }
x += 1;
};
value
"
)
.is_err());
}