2020-05-05 09:00:10 +02:00
|
|
|
#![cfg(not(feature = "no_module"))]
|
2020-05-05 17:57:25 +02:00
|
|
|
use rhai::{module_resolvers, Engine, EvalAltResult, Module, Scope, INT};
|
2020-05-05 09:00:10 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_module() {
|
|
|
|
let mut module = Module::new();
|
2020-05-05 11:51:40 +02:00
|
|
|
module.set_var("answer", 42 as INT);
|
2020-05-05 09:00:10 +02:00
|
|
|
|
2020-05-05 11:51:40 +02:00
|
|
|
assert!(module.contains_var("answer"));
|
|
|
|
assert_eq!(module.get_var_value::<INT>("answer").unwrap(), 42);
|
2020-05-05 09:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-05-05 17:57:25 +02:00
|
|
|
fn test_module_sub_module() -> Result<(), Box<EvalAltResult>> {
|
2020-05-05 09:00:10 +02:00
|
|
|
let mut module = Module::new();
|
|
|
|
|
|
|
|
let mut sub_module = Module::new();
|
|
|
|
|
|
|
|
let mut sub_module2 = Module::new();
|
2020-05-05 11:51:40 +02:00
|
|
|
sub_module2.set_var("answer", 41 as INT);
|
2020-05-09 18:19:13 +02:00
|
|
|
let hash_inc = sub_module2.set_fn_1("inc", |x: INT| Ok(x + 1));
|
2020-05-05 09:00:10 +02:00
|
|
|
|
2020-05-05 11:51:40 +02:00
|
|
|
sub_module.set_sub_module("universe", sub_module2);
|
|
|
|
module.set_sub_module("life", sub_module);
|
2020-05-05 09:00:10 +02:00
|
|
|
|
2020-05-05 11:51:40 +02:00
|
|
|
assert!(module.contains_sub_module("life"));
|
|
|
|
let m = module.get_sub_module("life").unwrap();
|
2020-05-05 09:00:10 +02:00
|
|
|
|
2020-05-05 11:51:40 +02:00
|
|
|
assert!(m.contains_sub_module("universe"));
|
|
|
|
let m2 = m.get_sub_module("universe").unwrap();
|
2020-05-05 09:00:10 +02:00
|
|
|
|
2020-05-05 11:51:40 +02:00
|
|
|
assert!(m2.contains_var("answer"));
|
2020-05-09 05:29:30 +02:00
|
|
|
assert!(m2.contains_fn(hash_inc));
|
2020-05-05 11:51:40 +02:00
|
|
|
|
|
|
|
assert_eq!(m2.get_var_value::<INT>("answer").unwrap(), 41);
|
|
|
|
|
2020-05-09 05:29:30 +02:00
|
|
|
let engine = Engine::new();
|
2020-05-05 11:51:40 +02:00
|
|
|
let mut scope = Scope::new();
|
|
|
|
|
|
|
|
scope.push_module("question", module);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval_expression_with_scope::<INT>(
|
|
|
|
&mut scope,
|
|
|
|
"question::life::universe::answer + 1"
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval_expression_with_scope::<INT>(
|
|
|
|
&mut scope,
|
|
|
|
"question::life::universe::inc(question::life::universe::answer)"
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
2020-05-05 09:00:10 +02:00
|
|
|
}
|
2020-05-05 17:57:25 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_module_resolver() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut resolver = module_resolvers::StaticModuleResolver::new();
|
|
|
|
|
|
|
|
let mut module = Module::new();
|
|
|
|
module.set_var("answer", 42 as INT);
|
|
|
|
|
2020-05-06 10:09:44 +02:00
|
|
|
resolver.insert("hello".to_string(), module);
|
2020-05-05 17:57:25 +02:00
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
2020-05-06 10:09:44 +02:00
|
|
|
engine.set_module_resolver(Some(resolver));
|
2020-05-05 17:57:25 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
import "hello" as h;
|
|
|
|
h::answer
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-05-06 17:00:26 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_module_from_ast() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
let mut resolver = rhai::module_resolvers::StaticModuleResolver::new();
|
|
|
|
let mut sub_module = Module::new();
|
|
|
|
sub_module.set_var("foo", true);
|
|
|
|
resolver.insert("another module".to_string(), sub_module);
|
|
|
|
|
|
|
|
engine.set_module_resolver(Some(resolver));
|
|
|
|
|
|
|
|
let ast = engine.compile(
|
|
|
|
r#"
|
|
|
|
// Functions become module functions
|
|
|
|
fn calc(x) {
|
|
|
|
x + 1
|
|
|
|
}
|
|
|
|
fn add_len(x, y) {
|
|
|
|
x + y.len()
|
|
|
|
}
|
2020-05-09 05:29:30 +02:00
|
|
|
private fn hidden() {
|
|
|
|
throw "you shouldn't see me!";
|
|
|
|
}
|
2020-05-06 17:00:26 +02:00
|
|
|
|
|
|
|
// Imported modules become sub-modules
|
|
|
|
import "another module" as extra;
|
|
|
|
|
|
|
|
// Variables defined at global level become module variables
|
|
|
|
const x = 123;
|
|
|
|
let foo = 41;
|
|
|
|
let hello;
|
|
|
|
|
|
|
|
// Final variable values become constant module variable values
|
|
|
|
foo = calc(foo);
|
|
|
|
hello = "hello, " + foo + " worlds!";
|
2020-05-08 16:38:56 +02:00
|
|
|
|
2020-05-09 04:00:59 +02:00
|
|
|
export
|
|
|
|
x as abc,
|
|
|
|
foo,
|
|
|
|
hello,
|
|
|
|
extra as foobar;
|
2020-05-06 17:00:26 +02:00
|
|
|
"#,
|
|
|
|
)?;
|
|
|
|
|
2020-05-08 16:38:56 +02:00
|
|
|
let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
|
2020-05-06 17:00:26 +02:00
|
|
|
|
|
|
|
let mut scope = Scope::new();
|
|
|
|
scope.push_module("testing", module);
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-05-08 16:38:56 +02:00
|
|
|
engine.eval_expression_with_scope::<INT>(&mut scope, "testing::abc")?,
|
2020-05-06 17:00:26 +02:00
|
|
|
123
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval_expression_with_scope::<INT>(&mut scope, "testing::foo")?,
|
|
|
|
42
|
|
|
|
);
|
2020-05-08 16:38:56 +02:00
|
|
|
assert!(engine.eval_expression_with_scope::<bool>(&mut scope, "testing::foobar::foo")?);
|
2020-05-06 17:00:26 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval_expression_with_scope::<String>(&mut scope, "testing::hello")?,
|
|
|
|
"hello, 42 worlds!"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval_expression_with_scope::<INT>(&mut scope, "testing::calc(999)")?,
|
|
|
|
1000
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval_expression_with_scope::<INT>(
|
|
|
|
&mut scope,
|
|
|
|
"testing::add_len(testing::foo, testing::hello)"
|
|
|
|
)?,
|
|
|
|
59
|
|
|
|
);
|
2020-05-09 05:29:30 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
|
|
|
.eval_expression_with_scope::<()>(&mut scope, "testing::hidden()")
|
|
|
|
.expect_err("should error"),
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(fn_name, _) if fn_name == "hidden"
|
|
|
|
));
|
2020-05-06 17:00:26 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|