Prefer Engine::disable_symbol to disable eval.

This commit is contained in:
Stephen Chung
2020-11-21 15:08:18 +08:00
parent 6c07d5fd73
commit 0046fe7e73
10 changed files with 96 additions and 84 deletions

View File

@@ -1,4 +1,4 @@
use rhai::{Engine, EvalAltResult, Scope, INT};
use rhai::{Engine, EvalAltResult, LexError, ParseErrorType, RegisterFn, Scope, INT};
#[test]
fn test_eval() -> Result<(), Box<EvalAltResult>> {
@@ -98,10 +98,34 @@ fn test_eval_override() -> Result<(), Box<EvalAltResult>> {
fn eval(x) { x } // reflect the script back
eval("40 + 2")
"#
"#
)?,
"40 + 2"
);
let mut engine = Engine::new();
// Reflect the script back
engine.register_fn("eval", |script: &str| script.to_string());
assert_eq!(engine.eval::<String>(r#"eval("40 + 2")"#)?, "40 + 2");
Ok(())
}
#[test]
fn test_eval_disabled() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.disable_symbol("eval");
assert!(matches!(
*engine
.compile(r#"eval("40 + 2")"#)
.expect_err("should error")
.0,
ParseErrorType::BadInput(LexError::ImproperSymbol(err, _)) if err == "eval"
));
Ok(())
}