Reduce usage of Default::default() to make it easier to refactor.

This commit is contained in:
Stephen Chung
2021-11-07 18:12:37 +08:00
parent 61cc3d0bf2
commit 68c0ee08c0
26 changed files with 224 additions and 189 deletions

View File

@@ -338,7 +338,7 @@ fn test_closures_external() -> Result<(), Box<EvalAltResult>> {
// Create native call context
let fn_name = fn_ptr.fn_name().to_string();
let context = NativeCallContext::new(&engine, &fn_name, &lib, rhai::Position::NONE);
let context = NativeCallContext::new(&engine, &fn_name, &lib);
// Closure 'f' captures: the engine, the AST, and the curried function pointer
let f = move |x: INT| fn_ptr.call_dynamic(&context, None, [x.into()]);

View File

@@ -196,7 +196,7 @@ fn test_custom_syntax_raw() -> Result<(), Box<EvalAltResult>> {
2 => match stream[1].as_str() {
"world" => Ok(Some("$$hello".into())),
"kitty" => Ok(None),
s => Err(LexError::ImproperSymbol(s.to_string(), Default::default())
s => Err(LexError::ImproperSymbol(s.to_string(), String::new())
.into_err(Position::NONE)
.into()),
},

View File

@@ -59,7 +59,7 @@ fn test_expressions_eval() -> Result<(), Box<EvalAltResult>> {
engine.register_get("gender", AGENT::get_gender);
engine.register_get("age", AGENT::get_age);
// Create your context, add the agent as a constant
// Create your scope, add the agent as a constant
let mut scope = Scope::new();
scope.push_constant("agent", my_agent);

View File

@@ -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(())
}

View File

@@ -444,7 +444,7 @@ fn test_module_ast_namespace() -> Result<(), Box<EvalAltResult>> {
let ast = engine.compile(script)?;
let module = Module::eval_ast_as_new(Default::default(), &ast, &engine)?;
let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
let mut resolver = StaticModuleResolver::new();
resolver.insert("testing", module);
@@ -512,6 +512,6 @@ fn test_module_file() -> Result<(), Box<EvalAltResult>> {
print("top");
"#,
)?;
Module::eval_ast_as_new(Default::default(), &ast, &engine)?;
Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
Ok(())
}