This commit is contained in:
Stephen Chung 2020-08-17 09:44:28 +08:00
parent 810514dd31
commit 371c131395

View File

@ -160,78 +160,6 @@ fn mut_opaque_ref_test() -> Result<(), Box<EvalAltResult>> {
Ok(()) Ok(())
} }
mod rename_fn {
use rhai::plugin::*;
use rhai::FLOAT;
#[export_fn(name = "add_float")]
pub fn add(f1: FLOAT, f2: FLOAT) -> FLOAT {
f1 + f2
}
}
#[test]
fn rename_fn_test() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.register_fn("get_mystic_number", || 42 as FLOAT);
let mut m = Module::new();
rhai::set_exported_fn!(m, "add_two_floats", rename_fn::add_float);
let mut r = StaticModuleResolver::new();
r.insert("Math::Advanced".to_string(), m);
engine.set_module_resolver(Some(r));
assert_eq!(
engine.eval::<FLOAT>(
r#"import "Math::Advanced" as math;
let x = get_mystic_number();
let y = math::add_two_floats(x, 1.0);
y"#
)?,
43.0
);
Ok(())
}
mod duplicate_fn_rename {
use rhai::plugin::*;
use rhai::{FLOAT, INT};
#[export_fn(name = "add_float")]
pub fn add(f1: FLOAT, f2: FLOAT) -> FLOAT {
f1 + f2
}
#[export_fn(name = "add_int")]
pub fn add(i1: INT, i2: INT) -> INT {
i1 + i2
}
}
#[test]
fn duplicate_fn_rename_test() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.register_fn("get_mystic_number", || 42 as FLOAT);
let mut m = Module::new();
rhai::set_exported_fn!(m, "add_two_floats", duplicate_fn_rename::add_float);
rhai::set_exported_fn!(m, "add_two_ints", duplicate_fn_rename::add_int);
let mut r = StaticModuleResolver::new();
r.insert("Math::Advanced".to_string(), m);
engine.set_module_resolver(Some(r));
let output_array = engine.eval::<Array>(
r#"import "Math::Advanced" as math;
let fx = get_mystic_number();
let fy = math::add_two_floats(fx, 1.0);
let ix = 42;
let iy = math::add_two_ints(ix, 1);
[fy, iy]
"#,
)?;
assert_eq!(&output_array[0].as_float().unwrap(), &43.0);
assert_eq!(&output_array[1].as_int().unwrap(), &43);
Ok(())
}
pub mod raw_returning_fn { pub mod raw_returning_fn {
use rhai::plugin::*; use rhai::plugin::*;
use rhai::FLOAT; use rhai::FLOAT;