Fix tests.

This commit is contained in:
Stephen Chung 2022-08-18 21:36:00 +08:00
parent a9b6e8b98c
commit f9d74fe313
2 changed files with 14 additions and 20 deletions

View File

@ -26,6 +26,7 @@ where
x.checked_add(&y)
}
#[inline(always)]
#[allow(dead_code)]
fn regular_add<T>(x: T, y: T) -> Option<T>
where
T: Debug + Copy + PartialOrd + std::ops::Add<Output = T>,

View File

@ -1,19 +1,20 @@
use rhai::packages::{Package, StandardPackage as SSS};
use rhai::{def_package, Engine, EvalAltResult, Module, Scope, INT};
def_package! {
/// My custom package.
MyPackage(m) : SSS {
m.set_native_fn("hello", |x: INT| Ok(x + 1));
m.set_native_fn("@", |x: INT, y: INT| Ok(x * x + y * y));
} |> |engine| {
#[cfg(not(feature = "no_custom_syntax"))]
engine.register_custom_operator("@", 160).unwrap();
}
}
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_custom_syntax"))]
#[test]
fn test_packages() -> Result<(), Box<EvalAltResult>> {
def_package! {
/// My custom package.
MyPackage(m) : SSS {
m.set_native_fn("hello", |x: INT| Ok(x + 1));
m.set_native_fn("@", |x: INT, y: INT| Ok(x * x + y * y));
} |> |engine| {
engine.register_custom_operator("@", 160).unwrap();
}
}
let pkg = MyPackage::new();
let make_call = |x: INT| -> Result<INT, Box<EvalAltResult>> {
@ -32,19 +33,11 @@ fn test_packages() -> Result<(), Box<EvalAltResult>> {
// Evaluate script.
#[cfg(not(feature = "no_custom_syntax"))]
return engine.eval_with_scope::<INT>(&mut scope, "hello(x) @ foo::hello(x)");
#[cfg(feature = "no_custom_syntax")]
return engine.eval_with_scope::<INT>(&mut scope, "hello(x) + foo::hello(x)");
engine.eval_with_scope::<INT>(&mut scope, "hello(x) @ foo::hello(x)")
};
#[cfg(not(feature = "no_custom_syntax"))]
assert_eq!(make_call(42)?, 3698);
#[cfg(feature = "no_custom_syntax")]
assert_eq!(make_call(42)?, 86);
Ok(())
}