From ab317bec4e79ff7b391b05cacd7df5d2932de4d9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 24 Jan 2021 21:33:05 +0800 Subject: [PATCH] Add test for NativeCallContext. --- tests/native.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/native.rs diff --git a/tests/native.rs b/tests/native.rs new file mode 100644 index 00000000..d0e669fc --- /dev/null +++ b/tests/native.rs @@ -0,0 +1,34 @@ +use rhai::{Dynamic, Engine, EvalAltResult, NativeCallContext, RegisterFn, INT}; +use std::any::TypeId; + +#[test] +fn test_native_context() -> Result<(), Box> { + fn add_double( + context: NativeCallContext, + args: &mut [&mut Dynamic], + ) -> Result> { + let x = args[0].as_int().unwrap(); + let y = args[1].as_int().unwrap(); + Ok(format!("{}_{}", context.fn_name(), x + 2 * y).into()) + } + + let mut engine = Engine::new(); + + engine + .register_raw_fn( + "add_double", + &[TypeId::of::(), TypeId::of::()], + add_double, + ) + .register_raw_fn( + "adbl", + &[TypeId::of::(), TypeId::of::()], + add_double, + ); + + assert_eq!(engine.eval::("add_double(40, 1)")?, "add_double_42"); + + assert_eq!(engine.eval::("adbl(40, 1)")?, "adbl_42"); + + Ok(()) +}