Add test case for overloading rename

This commit is contained in:
J Henry Waugh 2020-08-23 17:44:29 -05:00
parent 3fd3da6bfc
commit bb6a044182

View File

@ -750,6 +750,109 @@ mod generate_tests {
assert_streams_eq(item_mod.generate(), expected_tokens); assert_streams_eq(item_mod.generate(), expected_tokens);
} }
#[test]
fn two_fn_overload_module() {
let input_tokens: TokenStream = quote! {
pub mod two_fns {
#[rhai_fn(name = "add_n")]
pub fn add_one_to(x: INT) -> INT {
x + 1
}
#[rhai_fn(name = "add_n")]
pub fn add_n_to(x: INT, y: INT) -> INT {
x + y
}
}
};
let expected_tokens = quote! {
pub mod two_fns {
pub fn add_one_to(x: INT) -> INT {
x + 1
}
pub fn add_n_to(x: INT, y: INT) -> INT {
x + y
}
#[allow(unused_imports)]
use super::*;
#[allow(unused_mut)]
pub fn rhai_module_generate() -> Module {
let mut m = Module::new();
m.set_fn("add_n", FnAccess::Public, &[core::any::TypeId::of::<INT>()],
CallableFunction::from_plugin(add_one_to_token()));
m.set_fn("add_n", FnAccess::Public, &[core::any::TypeId::of::<INT>(),
core::any::TypeId::of::<INT>()],
CallableFunction::from_plugin(add_n_to_token()));
m
}
#[allow(non_camel_case_types)]
struct add_one_to_token();
impl PluginFunction for add_one_to_token {
fn call(&self,
args: &mut [&mut Dynamic], pos: Position
) -> Result<Dynamic, Box<EvalAltResult>> {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
let arg0 = mem::take(args[0usize]).clone().cast::<INT>();
Ok(Dynamic::from(add_one_to(arg0)))
}
fn is_method_call(&self) -> bool { false }
fn is_varadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> {
Box::new(add_one_to_token())
}
fn input_types(&self) -> Box<[TypeId]> {
new_vec![TypeId::of::<INT>()].into_boxed_slice()
}
}
pub fn add_one_to_token_callable() -> CallableFunction {
CallableFunction::from_plugin(add_one_to_token())
}
pub fn add_one_to_token_input_types() -> Box<[TypeId]> {
add_one_to_token().input_types()
}
#[allow(non_camel_case_types)]
struct add_n_to_token();
impl PluginFunction for add_n_to_token {
fn call(&self,
args: &mut [&mut Dynamic], pos: Position
) -> Result<Dynamic, Box<EvalAltResult>> {
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
let arg0 = mem::take(args[0usize]).clone().cast::<INT>();
let arg1 = mem::take(args[1usize]).clone().cast::<INT>();
Ok(Dynamic::from(add_n_to(arg0, arg1)))
}
fn is_method_call(&self) -> bool { false }
fn is_varadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> {
Box::new(add_n_to_token())
}
fn input_types(&self) -> Box<[TypeId]> {
new_vec![TypeId::of::<INT>(),
TypeId::of::<INT>()].into_boxed_slice()
}
}
pub fn add_n_to_token_callable() -> CallableFunction {
CallableFunction::from_plugin(add_n_to_token())
}
pub fn add_n_to_token_input_types() -> Box<[TypeId]> {
add_n_to_token().input_types()
}
}
};
let item_mod = syn::parse2::<Module>(input_tokens).unwrap();
assert_streams_eq(item_mod.generate(), expected_tokens);
}
#[test] #[test]
fn one_double_arg_fn_module() { fn one_double_arg_fn_module() {
let input_tokens: TokenStream = quote! { let input_tokens: TokenStream = quote! {