Macro test for enums.

This commit is contained in:
Stephen Chung 2022-08-30 13:56:25 +08:00
parent cfdca74beb
commit 7dbc605d0b

View File

@ -75,12 +75,35 @@ fn make_greeting(n: impl std::fmt::Display) -> String {
gen_unary_functions!(greet = make_greeting(INT, bool, char) -> String); gen_unary_functions!(greet = make_greeting(INT, bool, char) -> String);
macro_rules! expand_enum {
($module:ident : $typ:ty => $($variant:ident),+) => {
#[export_module]
pub mod $module {
$(
pub const $variant: $typ = <$typ>::$variant;
)*
}
};
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum MyEnum {
Foo,
Bar,
Baz,
Hello,
World,
}
expand_enum! { my_enum_module: MyEnum => Foo, Bar, Baz, Hello, World }
#[test] #[test]
fn test_plugins_package() -> Result<(), Box<EvalAltResult>> { fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new(); let mut engine = Engine::new();
let mut m = Module::new(); let mut m = Module::new();
combine_with_exported_module!(&mut m, "test", test::special_array_package); combine_with_exported_module!(&mut m, "test", test::special_array_package);
combine_with_exported_module!(&mut m, "enum", my_enum_module);
engine.register_global_module(m.into()); engine.register_global_module(m.into());
reg_functions!(engine += greet::single(INT, bool, char)); reg_functions!(engine += greet::single(INT, bool, char));