Implement private modifier for functions.

This commit is contained in:
Stephen Chung
2020-05-09 11:29:30 +08:00
parent d1de84fdd2
commit 0d20137d6c
7 changed files with 183 additions and 49 deletions

View File

@@ -18,7 +18,12 @@ fn test_module_sub_module() -> Result<(), Box<EvalAltResult>> {
let mut sub_module2 = Module::new();
sub_module2.set_var("answer", 41 as INT);
let hash = sub_module2.set_fn_1("inc", |x: INT| Ok(x + 1));
let hash_inc = sub_module2.set_fn_1("inc", |x: INT| Ok(x + 1), false);
let hash_hidden = sub_module2.set_fn_0(
"hidden",
|| Err("shouldn't see me!".into()) as Result<(), Box<EvalAltResult>>,
true,
);
sub_module.set_sub_module("universe", sub_module2);
module.set_sub_module("life", sub_module);
@@ -30,11 +35,12 @@ fn test_module_sub_module() -> Result<(), Box<EvalAltResult>> {
let m2 = m.get_sub_module("universe").unwrap();
assert!(m2.contains_var("answer"));
assert!(m2.contains_fn(hash));
assert!(m2.contains_fn(hash_inc));
assert!(m2.contains_fn(hash_hidden));
assert_eq!(m2.get_var_value::<INT>("answer").unwrap(), 41);
let mut engine = Engine::new();
let engine = Engine::new();
let mut scope = Scope::new();
scope.push_module("question", module);
@@ -53,6 +59,11 @@ fn test_module_sub_module() -> Result<(), Box<EvalAltResult>> {
)?,
42
);
assert!(matches!(
*engine.eval_expression_with_scope::<()>(&mut scope, "question::life::universe::hidden()")
.expect_err("should error"),
EvalAltResult::ErrorFunctionNotFound(fn_name, _) if fn_name == "hidden"
));
Ok(())
}
@@ -102,6 +113,9 @@ fn test_module_from_ast() -> Result<(), Box<EvalAltResult>> {
fn add_len(x, y) {
x + y.len()
}
private fn hidden() {
throw "you shouldn't see me!";
}
// Imported modules become sub-modules
import "another module" as extra;
@@ -152,6 +166,12 @@ fn test_module_from_ast() -> Result<(), Box<EvalAltResult>> {
)?,
59
);
assert!(matches!(
*engine
.eval_expression_with_scope::<()>(&mut scope, "testing::hidden()")
.expect_err("should error"),
EvalAltResult::ErrorFunctionNotFound(fn_name, _) if fn_name == "hidden"
));
Ok(())
}