rhai/tests/functions.rs

169 lines
4.1 KiB
Rust
Raw Normal View History

2020-05-12 04:20:29 +02:00
#![cfg(not(feature = "no_function"))]
use rhai::{Engine, EvalAltResult, FnNamespace, Module, NativeCallContext, Shared, INT};
2020-05-12 04:20:29 +02:00
2021-04-03 06:00:22 +02:00
#[cfg(not(feature = "no_object"))]
2020-05-12 04:20:29 +02:00
#[test]
fn test_functions_trait_object() -> Result<(), Box<EvalAltResult>> {
trait TestTrait {
fn greet(&self) -> INT;
}
2020-06-26 04:39:18 +02:00
#[derive(Debug, Clone)]
struct ABC(INT);
2020-05-12 04:20:29 +02:00
impl TestTrait for ABC {
fn greet(&self) -> INT {
self.0
}
}
2021-04-03 06:10:08 +02:00
#[cfg(not(feature = "sync"))]
type MySharedTestTrait = Shared<dyn TestTrait>;
2020-06-25 12:07:57 +02:00
2021-04-03 06:10:08 +02:00
#[cfg(feature = "sync")]
type MySharedTestTrait = Shared<dyn TestTrait + Send + Sync>;
2021-03-15 14:30:45 +01:00
let mut engine = Engine::new();
engine
.register_type_with_name::<MySharedTestTrait>("MySharedTestTrait")
.register_fn("new_ts", || Shared::new(ABC(42)) as MySharedTestTrait)
.register_fn("greet", |x: MySharedTestTrait| x.greet());
2021-03-15 14:30:45 +01:00
2021-01-28 08:29:55 +01:00
assert_eq!(
engine.eval::<String>("type_of(new_ts())")?,
"MySharedTestTrait"
2021-01-28 08:29:55 +01:00
);
assert_eq!(engine.eval::<INT>("let x = new_ts(); greet(x)")?, 42);
2021-01-28 08:29:55 +01:00
Ok(())
}
2020-12-07 14:54:52 +01:00
#[test]
fn test_functions_namespaces() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
#[cfg(not(feature = "no_module"))]
{
let mut m = Module::new();
2021-03-15 05:39:06 +01:00
let hash = m.set_native_fn("test", || Ok(999 as INT));
2020-12-07 14:54:52 +01:00
m.update_fn_namespace(hash, FnNamespace::Global);
engine.register_static_module("hello", m.into());
2020-12-07 14:54:52 +01:00
2021-10-27 11:52:48 +02:00
let mut m = Module::new();
m.set_var("ANSWER", 123 as INT);
2020-12-07 14:54:52 +01:00
assert_eq!(engine.eval::<INT>("test()")?, 999);
2021-03-15 14:30:45 +01:00
2020-12-07 14:54:52 +01:00
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);
2020-12-07 14:54:52 +01:00
assert_eq!(engine.eval::<INT>("test()")?, 42);
2021-03-15 14:30:45 +01:00
Ok(())
}
2021-11-07 11:25:32 +01:00
#[cfg(not(feature = "no_module"))]
#[test]
fn test_functions_global_module() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
2021-11-07 11:25:32 +01:00
assert_eq!(
engine.eval::<INT>(
"
const ANSWER = 42;
fn foo() { global::ANSWER }
2021-11-07 11:25:32 +01:00
foo()
"
)?,
42
);
2021-11-07 11:25:32 +01:00
assert!(matches!(*engine.run("
fn foo() { global::ANSWER }
{
const ANSWER = 42;
foo()
}
").expect_err("should error"),
EvalAltResult::ErrorInFunctionCall(_, _, err, _)
if matches!(&*err, EvalAltResult::ErrorVariableNotFound(v, _) if v == "global::ANSWER")
));
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")
));
#[cfg(not(feature = "no_closure"))]
assert_eq!(
engine.eval::<INT>(
"
const GLOBAL_VALUE = 42;
do_stuff(|| global::GLOBAL_VALUE);
"
)?,
42
);
// Override global
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()
"
)?,
123
);
2020-12-07 14:54:52 +01:00
Ok(())
}
2021-11-10 15:10:03 +01:00
#[test]
fn test_functions_bang() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(
engine.eval::<INT>(
"
fn foo() {
hello + bar
}
let hello = 42;
let bar = 123;
foo!()
",
)?,
165
);
Ok(())
}