Add plugins test.

This commit is contained in:
Stephen Chung
2020-08-02 18:53:25 +08:00
parent 675c4eb606
commit 5eed5fe6a3
5 changed files with 35 additions and 23 deletions

View File

@@ -16,7 +16,7 @@ fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
);
let mut engine = Engine::new();
engine.load_package(module.into());
engine.load_package(module);
#[cfg(not(feature = "no_object"))]
assert_eq!(

View File

@@ -1,8 +1,12 @@
use rhai::{export_module, exported_module};
use rhai::{
export_fn, export_module, exported_module,
plugin::{CallableFunction, PluginFunction},
register_exported_fn,
};
use rhai::{Engine, EvalAltResult, INT};
#[export_module]
pub mod array_package {
mod special_array_package {
use rhai::{Array, INT};
pub fn len(array: &mut Array, mul: INT) -> INT {
@@ -10,15 +14,25 @@ pub mod array_package {
}
}
#[export_fn]
fn make_greeting(n: INT) -> String {
format!("{} {}", n, if n > 1 { "kitties" } else { "kitty" }).into()
}
#[test]
fn test_plugins() -> Result<(), Box<EvalAltResult>> {
fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let m = exported_module!(array_package);
let mut m = exported_module!(special_array_package);
register_exported_fn!(m, "greet", make_greeting);
engine.load_package(m.into());
engine.load_package(m);
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; a.len(2)")?, 6);
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; len(a, 2)")?, 6);
assert_eq!(
engine.eval::<String>("let a = [1, 2, 3]; greet(len(a, 2))")?,
"6 kitties"
);
Ok(())
}