2020-05-15 05:43:32 +02:00
|
|
|
#![cfg(not(feature = "unchecked"))]
|
2020-11-02 05:50:27 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, INT};
|
2020-05-15 05:43:32 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_max_operations() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_max_operations(500);
|
|
|
|
|
2020-12-12 03:10:27 +01:00
|
|
|
engine.on_progress(|count| {
|
2020-05-15 05:43:32 +02:00
|
|
|
if count % 100 == 0 {
|
|
|
|
println!("{}", count);
|
|
|
|
}
|
2020-11-02 05:50:27 +01:00
|
|
|
None
|
2020-05-15 05:43:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
engine.eval::<()>("let x = 0; while x < 20 { x += 1; }")?;
|
|
|
|
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
|
|
|
.eval::<()>("for x in range(0, 500) {}")
|
|
|
|
.expect_err("should error"),
|
|
|
|
EvalAltResult::ErrorTooManyOperations(_)
|
|
|
|
));
|
|
|
|
|
|
|
|
engine.set_max_operations(0);
|
|
|
|
|
|
|
|
engine.eval::<()>("for x in range(0, 10000) {}")?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_max_operations_functions() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_max_operations(500);
|
|
|
|
|
2020-12-12 03:10:27 +01:00
|
|
|
engine.on_progress(|count| {
|
2020-05-15 05:43:32 +02:00
|
|
|
if count % 100 == 0 {
|
|
|
|
println!("{}", count);
|
|
|
|
}
|
2020-11-02 05:50:27 +01:00
|
|
|
None
|
2020-05-15 05:43:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
engine.eval::<()>(
|
|
|
|
r#"
|
2021-03-27 11:08:34 +01:00
|
|
|
print("Test1");
|
|
|
|
let x = 0;
|
|
|
|
|
|
|
|
while x < 28 {
|
|
|
|
print(x);
|
|
|
|
x += 1;
|
|
|
|
}
|
|
|
|
"#,
|
2020-05-17 16:19:49 +02:00
|
|
|
)?;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
engine.eval::<()>(
|
|
|
|
r#"
|
2021-03-27 11:08:34 +01:00
|
|
|
print("Test2");
|
|
|
|
fn inc(x) { x + 1 }
|
|
|
|
let x = 0;
|
|
|
|
while x < 20 { x = inc(x); }
|
|
|
|
"#,
|
2020-05-15 05:43:32 +02:00
|
|
|
)?;
|
|
|
|
|
2020-05-17 16:19:49 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-05-15 05:43:32 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
|
|
|
.eval::<()>(
|
|
|
|
r#"
|
2020-05-17 16:19:49 +02:00
|
|
|
print("Test3");
|
2020-05-15 05:43:32 +02:00
|
|
|
fn inc(x) { x + 1 }
|
|
|
|
let x = 0;
|
2020-05-17 16:19:49 +02:00
|
|
|
|
2021-04-21 12:16:24 +02:00
|
|
|
while x < 36 {
|
2020-05-17 16:19:49 +02:00
|
|
|
print(x);
|
|
|
|
x = inc(x);
|
|
|
|
}
|
2021-03-27 11:08:34 +01:00
|
|
|
"#,
|
2020-05-15 05:43:32 +02:00
|
|
|
)
|
|
|
|
.expect_err("should error"),
|
|
|
|
EvalAltResult::ErrorTooManyOperations(_)
|
|
|
|
));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_max_operations_eval() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_max_operations(500);
|
|
|
|
|
2020-12-12 03:10:27 +01:00
|
|
|
engine.on_progress(|count| {
|
2020-05-15 05:43:32 +02:00
|
|
|
if count % 100 == 0 {
|
|
|
|
println!("{}", count);
|
|
|
|
}
|
2020-11-02 05:50:27 +01:00
|
|
|
None
|
2020-05-15 05:43:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
|
|
|
.eval::<()>(
|
|
|
|
r#"
|
|
|
|
let script = "for x in range(0, 500) {}";
|
|
|
|
eval(script);
|
2021-03-27 11:08:34 +01:00
|
|
|
"#
|
2020-05-15 05:43:32 +02:00
|
|
|
)
|
|
|
|
.expect_err("should error"),
|
2021-02-18 07:33:12 +01:00
|
|
|
EvalAltResult::ErrorInFunctionCall(_, _, err, _) if matches!(*err, EvalAltResult::ErrorTooManyOperations(_))
|
2020-05-15 05:43:32 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-06-14 08:25:47 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_max_operations_progress() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_max_operations(500);
|
|
|
|
|
2020-12-12 03:10:27 +01:00
|
|
|
engine.on_progress(|count| {
|
2020-11-02 05:50:27 +01:00
|
|
|
if count < 100 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some((42 as INT).into())
|
|
|
|
}
|
|
|
|
});
|
2020-06-14 08:25:47 +02:00
|
|
|
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
|
|
|
.eval::<()>("for x in range(0, 500) {}")
|
|
|
|
.expect_err("should error"),
|
2020-11-02 05:50:27 +01:00
|
|
|
EvalAltResult::ErrorTerminated(x, _) if x.as_int()? == 42
|
2020-06-14 08:25:47 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|