Reduce usage of Default::default() to make it easier to refactor.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#![cfg(not(feature = "no_function"))]
|
||||
use rhai::{Engine, EvalAltResult, FnNamespace, Module, Shared, INT};
|
||||
use rhai::{Engine, EvalAltResult, FnNamespace, Module, NativeCallContext, Shared, INT};
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
#[test]
|
||||
@@ -43,7 +43,35 @@ fn test_functions_trait_object() -> Result<(), Box<EvalAltResult>> {
|
||||
fn test_functions_namespaces() -> Result<(), Box<EvalAltResult>> {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
{
|
||||
let mut m = Module::new();
|
||||
let hash = m.set_native_fn("test", || Ok(999 as INT));
|
||||
m.update_fn_namespace(hash, FnNamespace::Global);
|
||||
|
||||
engine.register_static_module("hello", m.into());
|
||||
|
||||
let mut m = Module::new();
|
||||
m.set_var("ANSWER", 123 as INT);
|
||||
|
||||
assert_eq!(engine.eval::<INT>("test()")?, 999);
|
||||
|
||||
assert_eq!(engine.eval::<INT>("fn test() { 123 } test()")?, 123);
|
||||
}
|
||||
|
||||
engine.register_fn("test", || 42 as INT);
|
||||
|
||||
assert_eq!(engine.eval::<INT>("fn test() { 123 } test()")?, 123);
|
||||
|
||||
assert_eq!(engine.eval::<INT>("test()")?, 42);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_functions_global_module() -> Result<(), Box<EvalAltResult>> {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
{
|
||||
assert_eq!(
|
||||
@@ -56,43 +84,28 @@ fn test_functions_namespaces() -> Result<(), Box<EvalAltResult>> {
|
||||
)?,
|
||||
42
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
{
|
||||
let mut m = Module::new();
|
||||
let hash = m.set_native_fn("test", || Ok(999 as INT));
|
||||
m.update_fn_namespace(hash, FnNamespace::Global);
|
||||
assert!(matches!(*engine.run("
|
||||
fn foo() { global::ANSWER }
|
||||
|
||||
engine.register_static_module("hello", m.into());
|
||||
{
|
||||
const ANSWER = 42;
|
||||
foo()
|
||||
}
|
||||
").expect_err("should error"),
|
||||
EvalAltResult::ErrorInFunctionCall(_, _, err, _)
|
||||
if matches!(&*err, EvalAltResult::ErrorVariableNotFound(v, _) if v == "global::ANSWER")
|
||||
));
|
||||
|
||||
let mut m = Module::new();
|
||||
m.set_var("ANSWER", 123 as INT);
|
||||
|
||||
engine.register_static_module("global", m.into());
|
||||
|
||||
assert_eq!(engine.eval::<INT>("test()")?, 999);
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
assert_eq!(engine.eval::<INT>("fn test() { 123 } test()")?, 123);
|
||||
}
|
||||
|
||||
engine.register_fn("test", || 42 as INT);
|
||||
|
||||
assert_eq!(engine.eval::<INT>("test()")?, 42);
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
{
|
||||
assert_eq!(engine.eval::<INT>("fn test() { 123 } test()")?, 123);
|
||||
let mut module = Module::new();
|
||||
module.set_var("ANSWER", 123 as INT);
|
||||
engine.register_static_module("global", module.into());
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<INT>(
|
||||
"
|
||||
const ANSWER = 42;
|
||||
|
||||
fn foo() { global::ANSWER }
|
||||
|
||||
foo()
|
||||
"
|
||||
)?,
|
||||
@@ -100,5 +113,23 @@ fn test_functions_namespaces() -> Result<(), Box<EvalAltResult>> {
|
||||
);
|
||||
}
|
||||
|
||||
engine.register_result_fn(
|
||||
"do_stuff",
|
||||
|context: NativeCallContext, callback: rhai::FnPtr| {
|
||||
callback.call_dynamic(&context, None, [])
|
||||
},
|
||||
);
|
||||
|
||||
#[cfg(not(feature = "no_closure"))]
|
||||
assert!(matches!(*engine.run("
|
||||
do_stuff(|| {
|
||||
const LOCAL_VALUE = 42;
|
||||
global::LOCAL_VALUE
|
||||
});
|
||||
").expect_err("should error"),
|
||||
EvalAltResult::ErrorInFunctionCall(_, _, err, _)
|
||||
if matches!(&*err, EvalAltResult::ErrorVariableNotFound(v, _) if v == "global::LOCAL_VALUE")
|
||||
));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user