rhai/codegen/tests/test_modules.rs

183 lines
4.6 KiB
Rust
Raw Normal View History

2020-08-01 18:52:26 +02:00
use rhai::module_resolvers::*;
2020-08-03 02:27:19 +02:00
use rhai::{Engine, EvalAltResult, RegisterFn, FLOAT, INT};
2020-08-01 18:52:26 +02:00
pub mod empty_module {
2020-08-03 02:27:19 +02:00
use rhai::plugin::*;
2020-08-01 18:52:26 +02:00
#[export_module]
2020-08-02 09:39:08 +02:00
pub mod EmptyModule {}
2020-08-01 18:52:26 +02:00
}
#[test]
fn empty_module_test() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let m = rhai::exported_module!(crate::empty_module::EmptyModule);
let mut r = StaticModuleResolver::new();
r.insert("Module::Empty".to_string(), m);
engine.set_module_resolver(Some(r));
2020-08-02 09:39:08 +02:00
assert_eq!(
engine.eval::<INT>(r#"import "Module::Empty" as m; 42"#)?,
42
);
2020-08-01 18:52:26 +02:00
Ok(())
}
pub mod one_fn_module {
2020-08-03 02:27:19 +02:00
use rhai::plugin::*;
2020-08-01 18:52:26 +02:00
#[export_module]
pub mod advanced_math {
use rhai::FLOAT;
pub fn get_mystic_number() -> FLOAT {
42.0 as FLOAT
}
}
}
#[test]
fn one_fn_module_test() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let m = rhai::exported_module!(crate::one_fn_module::advanced_math);
let mut r = StaticModuleResolver::new();
r.insert("Math::Advanced".to_string(), m);
engine.set_module_resolver(Some(r));
2020-08-02 09:39:08 +02:00
assert_eq!(
engine.eval::<FLOAT>(
r#"import "Math::Advanced" as math;
2020-08-01 18:52:26 +02:00
let m = math::get_mystic_number();
2020-08-02 09:39:08 +02:00
m"#
)?,
42.0
);
2020-08-01 18:52:26 +02:00
Ok(())
}
pub mod one_fn_and_const_module {
2020-08-03 02:27:19 +02:00
use rhai::plugin::*;
2020-08-01 18:52:26 +02:00
#[export_module]
pub mod advanced_math {
use rhai::FLOAT;
pub const MYSTIC_NUMBER: FLOAT = 42.0 as FLOAT;
pub fn euclidean_distance(x1: FLOAT, y1: FLOAT, x2: FLOAT, y2: FLOAT) -> FLOAT {
2020-08-02 09:39:08 +02:00
((y2 - y1).abs().powf(2.0) + (x2 - x1).abs().powf(2.0)).sqrt()
2020-08-01 18:52:26 +02:00
}
}
}
#[test]
fn one_fn_and_const_module_test() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let m = rhai::exported_module!(crate::one_fn_and_const_module::advanced_math);
let mut r = StaticModuleResolver::new();
r.insert("Math::Advanced".to_string(), m);
engine.set_module_resolver(Some(r));
2020-08-02 09:39:08 +02:00
assert_eq!(
engine.eval::<FLOAT>(
r#"import "Math::Advanced" as math;
2020-08-01 18:52:26 +02:00
let m = math::MYSTIC_NUMBER;
let x = math::euclidean_distance(0.0, 1.0, 0.0, m);
2020-08-02 09:39:08 +02:00
x"#
)?,
41.0
);
2020-08-01 18:52:26 +02:00
Ok(())
}
pub mod raw_fn_str_module {
2020-08-03 02:27:19 +02:00
use rhai::plugin::*;
2020-08-01 18:52:26 +02:00
#[export_module]
pub mod host_io {
pub fn write_out_str(message: &str) -> bool {
eprintln!("{}", message);
true
}
}
}
#[test]
fn raw_fn_str_module_test() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let m = rhai::exported_module!(crate::raw_fn_str_module::host_io);
let mut r = StaticModuleResolver::new();
r.insert("Host::IO".to_string(), m);
engine.set_module_resolver(Some(r));
2020-08-02 09:39:08 +02:00
assert_eq!(
engine.eval::<bool>(
r#"import "Host::IO" as io;
2020-08-01 18:52:26 +02:00
let x = io::write_out_str("hello world!");
2020-08-02 09:39:08 +02:00
x"#
)?,
true
);
2020-08-01 18:52:26 +02:00
Ok(())
}
pub mod mut_opaque_ref_module {
2020-08-03 02:27:19 +02:00
use rhai::plugin::*;
2020-08-02 09:39:08 +02:00
use rhai::INT;
2020-08-01 18:52:26 +02:00
#[derive(Clone)]
pub struct StatusMessage {
os_code: Option<INT>,
message: String,
2020-08-02 09:39:08 +02:00
is_ok: bool,
2020-08-01 18:52:26 +02:00
}
#[export_module]
pub mod host_msg {
2020-08-02 09:39:08 +02:00
use super::{StatusMessage, INT};
2020-08-01 18:52:26 +02:00
pub fn new_message(is_ok: bool, message: &str) -> StatusMessage {
StatusMessage {
is_ok,
os_code: None,
message: message.to_string(),
}
}
pub fn new_os_message(is_ok: bool, os_code: INT) -> StatusMessage {
StatusMessage {
is_ok,
os_code: Some(os_code),
message: format!("OS Code {}", os_code),
}
}
pub fn write_out_message(message: &mut StatusMessage) -> bool {
eprintln!("{}", message.message);
true
}
}
}
#[test]
fn mut_opaque_ref_test() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let m = rhai::exported_module!(crate::mut_opaque_ref_module::host_msg);
let mut r = StaticModuleResolver::new();
r.insert("Host::Msg".to_string(), m);
engine.set_module_resolver(Some(r));
2020-08-02 09:39:08 +02:00
assert_eq!(
engine.eval::<bool>(
r#"import "Host::Msg" as msg;
2020-08-01 18:52:26 +02:00
let success = "it worked";
let message1 = msg::new_message(true, success);
let ok1 = msg::write_out_message(message1);
let message2 = msg::new_os_message(true, 0);
let ok2 = msg::write_out_message(message2);
2020-08-02 09:39:08 +02:00
ok1 && ok2"#
)?,
true
);
2020-08-01 18:52:26 +02:00
Ok(())
}