Add strict variables mode.

This commit is contained in:
Stephen Chung
2021-12-04 17:57:28 +08:00
parent ff4827064b
commit b8c4054c20
5 changed files with 203 additions and 55 deletions

View File

@@ -1,7 +1,7 @@
use rhai::{Engine, EvalAltResult};
#[test]
fn test_options() -> Result<(), Box<EvalAltResult>> {
fn test_options_allow() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.compile("let x = if y { z } else { w };")?;
@@ -33,3 +33,59 @@ fn test_options() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_options_strict_var() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.compile("let x = if y { z } else { w };")?;
#[cfg(not(feature = "no_function"))]
engine.compile("fn foo(x) { x + y }")?;
#[cfg(not(feature = "no_module"))]
engine.compile("print(h::y::z);")?;
#[cfg(not(feature = "no_module"))]
engine.compile("let x = h::y::foo();")?;
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_module"))]
engine.compile("fn foo() { h::y::foo() }")?;
#[cfg(not(feature = "no_function"))]
engine.compile("let f = |y| x * y;")?;
engine.set_strict_variables(true);
assert!(engine.compile("let x = if y { z } else { w };").is_err());
engine.compile("let y = 42; let x = y;")?;
#[cfg(not(feature = "no_function"))]
assert!(engine.compile("fn foo(x) { x + y }").is_err());
#[cfg(not(feature = "no_module"))]
{
assert!(engine.compile("print(h::y::z);").is_err());
engine.compile(r#"import "hello" as h; print(h::y::z);"#)?;
assert!(engine.compile("let x = h::y::foo();").is_err());
engine.compile(r#"import "hello" as h; let x = h::y::foo();"#)?;
}
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_module"))]
engine.compile("fn foo() { h::y::foo() }")?;
#[cfg(not(feature = "no_function"))]
{
assert!(engine.compile("let f = |y| x * y;").is_err());
#[cfg(not(feature = "no_closure"))]
{
engine.compile("let x = 42; let f = |y| x * y;")?;
engine.compile("let x = 42; let f = |y| { || x + y };")?;
assert!(engine.compile("fn foo() { |y| { || x + y } }").is_err());
}
}
Ok(())
}