rhai/codegen/tests/test_modules.rs

421 lines
11 KiB
Rust
Raw Permalink Normal View History

use rhai::{Array, Engine, EvalAltResult, 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
2022-12-30 18:07:39 +01:00
#[allow(non_snake_case)]
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);
2020-12-26 06:05:57 +01:00
engine.register_static_module("Module::Empty", m.into());
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);
2020-12-26 06:05:57 +01:00
engine.register_static_module("Math::Advanced", m.into());
2020-08-01 18:52:26 +02:00
2020-08-02 09:39:08 +02:00
assert_eq!(
2020-12-26 06:05:57 +01:00
engine.eval::<FLOAT>(r#"let m = Math::Advanced::get_mystic_number();m"#)?,
2020-08-02 09:39:08 +02:00
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);
2020-12-26 06:05:57 +01:00
engine.register_static_module("Math::Advanced", m.into());
2020-08-01 18:52:26 +02:00
2020-08-02 09:39:08 +02:00
assert_eq!(
engine.eval::<FLOAT>(
2022-07-29 05:01:00 +02:00
"
let m = Math::Advanced::MYSTIC_NUMBER;
let x = Math::Advanced::euclidean_distance(0.0, 1.0, 0.0, m);
x
"
2020-08-02 09:39:08 +02:00
)?,
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 {
2023-02-05 17:59:02 +01:00
eprintln!("{message}");
2020-08-01 18:52:26 +02:00
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);
2020-12-26 06:05:57 +01:00
engine.register_static_module("Host::IO", m.into());
2020-08-01 18:52:26 +02:00
2022-12-30 18:07:39 +01:00
assert!(engine
.eval::<bool>(r#"let x = Host::IO::write_out_str("hello world!"); x"#)
.unwrap());
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
2022-12-30 18:07:39 +01:00
#[allow(dead_code)] // used inside `exported_module!`
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),
2022-08-11 13:01:23 +02:00
message: format!("OS Code {os_code}"),
2020-08-01 18:52:26 +02:00
}
}
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);
2020-12-26 06:05:57 +01:00
engine.register_static_module("Host::Msg", m.into());
2020-08-01 18:52:26 +02:00
2022-12-30 18:07:39 +01:00
assert!(engine
.eval::<bool>(
2020-12-26 06:05:57 +01:00
r#"
2022-07-29 05:01:00 +02:00
let success = "it worked";
let message1 = Host::Msg::new_message(true, success);
let ok1 = Host::Msg::write_out_message(message1);
let message2 = Host::Msg::new_os_message(true, 0);
let ok2 = Host::Msg::write_out_message(message2);
ok1 && ok2
"#
2022-12-30 18:07:39 +01:00
)
.unwrap());
2020-08-01 18:52:26 +02:00
Ok(())
}
mod duplicate_fn_rename {
use rhai::plugin::*;
#[export_module]
pub mod my_adds {
use rhai::{FLOAT, INT};
#[rhai_fn(name = "add")]
pub fn add_float(f1: FLOAT, f2: FLOAT) -> FLOAT {
f1 + f2
}
#[rhai_fn(name = "add")]
pub fn add_int(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 m = rhai::exported_module!(crate::duplicate_fn_rename::my_adds);
2020-12-26 06:05:57 +01:00
engine.register_static_module("Math::Advanced", m.into());
let output_array = engine.eval::<Array>(
2022-07-29 05:01:00 +02:00
"
let fx = get_mystic_number();
let fy = Math::Advanced::add(fx, 1.0);
let ix = 42;
let iy = Math::Advanced::add(ix, 1);
[fy, iy]
",
)?;
assert_eq!(&output_array[0].as_float().unwrap(), &43.0);
assert_eq!(&output_array[1].as_int().unwrap(), &43);
Ok(())
}
2020-09-05 04:46:24 +02:00
mod multiple_fn_rename {
use rhai::plugin::*;
#[export_module]
pub mod my_adds {
use rhai::{FLOAT, INT};
pub fn get_mystic_number() -> FLOAT {
42.0
}
#[rhai_fn(name = "add", name = "+", name = "add_together")]
pub fn add_float(f1: FLOAT, f2: FLOAT) -> FLOAT {
f1 + f2 * 2.0
}
#[rhai_fn(name = "add", name = "+", name = "add_together")]
pub fn add_int(i1: INT, i2: INT) -> INT {
i1 + i2 * 2
}
#[rhai_fn(name = "prop", get = "prop")]
pub fn get_prop(x: FLOAT) -> FLOAT {
x * 2.0
}
#[rhai_fn(name = "idx", index_get)]
pub fn index(x: FLOAT, i: INT) -> FLOAT {
x + (i as FLOAT)
}
}
}
#[test]
fn multiple_fn_rename_test() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let m = rhai::exported_module!(crate::multiple_fn_rename::my_adds);
engine.register_global_module(m.into());
2022-09-03 16:32:55 +02:00
engine.set_fast_operators(false);
2020-09-05 04:46:24 +02:00
let output_array = engine.eval::<Array>(
2022-07-29 05:01:00 +02:00
"
let fx = get_mystic_number();
let fy1 = add(fx, 1.0);
let fy2 = add_together(fx, 1.0);
let fy3 = fx + 1.0;
let p1 = fx.prop;
let p2 = prop(fx);
let idx1 = fx[1];
let idx2 = idx(fx, 1);
let ix = 42;
let iy1 = add(ix, 1);
let iy2 = add_together(ix, 1);
let iy3 = ix + 1;
[fy1, fy2, fy3, iy1, iy2, iy3, p1, p2, idx1, idx2]
",
2020-09-05 04:46:24 +02:00
)?;
assert_eq!(&output_array[0].as_float().unwrap(), &44.0);
assert_eq!(&output_array[1].as_float().unwrap(), &44.0);
assert_eq!(&output_array[2].as_float().unwrap(), &44.0);
assert_eq!(&output_array[3].as_int().unwrap(), &44);
assert_eq!(&output_array[4].as_int().unwrap(), &44);
assert_eq!(&output_array[5].as_int().unwrap(), &44);
assert_eq!(&output_array[6].as_float().unwrap(), &84.0);
assert_eq!(&output_array[7].as_float().unwrap(), &84.0);
assert_eq!(&output_array[8].as_float().unwrap(), &43.0);
assert_eq!(&output_array[9].as_float().unwrap(), &43.0);
Ok(())
}
2022-12-30 18:07:39 +01:00
#[allow(dead_code)] // used inside `export_module!`
mod export_by_prefix {
use rhai::plugin::*;
2020-12-24 14:28:40 +01:00
#[export_module(export_prefix = "foo_")]
pub mod my_adds {
use rhai::{FLOAT, INT};
#[rhai_fn(name = "foo_add_f")]
pub fn foo_add1(f1: FLOAT, f2: FLOAT) -> FLOAT {
f1 + f2
}
#[rhai_fn(name = "bar_add_i")]
fn foo_add_int(i1: INT, i2: INT) -> INT {
i1 + i2
}
#[rhai_fn(name = "foo_add_float2")]
pub fn add_float2(f1: FLOAT, f2: FLOAT) -> FLOAT {
f1 + f2
}
pub fn foo_m(f1: FLOAT, f2: FLOAT) -> FLOAT {
f1 + f2
}
fn foo_n(i1: INT, i2: INT) -> INT {
i1 + i2
}
pub fn bar_m(f1: FLOAT, f2: FLOAT) -> FLOAT {
f1 + f2
}
}
}
#[test]
fn export_by_prefix_test() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let m = rhai::exported_module!(crate::export_by_prefix::my_adds);
2020-12-26 06:05:57 +01:00
engine.register_static_module("Math::Advanced", m.into());
let output_array = engine.eval::<Array>(
2022-07-29 05:01:00 +02:00
"
let ex = 41.0;
let fx = Math::Advanced::foo_add_f(ex, 1.0);
let gx = Math::Advanced::foo_m(41.0, 1.0);
let ei = 41;
let fi = Math::Advanced::bar_add_i(ei, 1);
let gi = Math::Advanced::foo_n(41, 1);
[fx, gx, fi, gi]
",
)?;
assert_eq!(&output_array[0].as_float().unwrap(), &42.0);
assert_eq!(&output_array[1].as_float().unwrap(), &42.0);
assert_eq!(&output_array[2].as_int().unwrap(), &42);
assert_eq!(&output_array[3].as_int().unwrap(), &42);
assert!(matches!(*engine.eval::<FLOAT>(
"
let ex = 41.0;
let fx = Math::Advanced::foo_add_float2(ex, 1.0);
fx
").unwrap_err(),
2022-12-30 18:07:39 +01:00
EvalAltResult::ErrorFunctionNotFound(s, ..)
2022-07-29 05:01:00 +02:00
if s == "Math::Advanced::foo_add_float2 (f64, f64)"));
assert!(matches!(*engine.eval::<FLOAT>(
"
let ex = 41.0;
let fx = Math::Advanced::bar_m(ex, 1.0);
fx
").unwrap_err(),
2022-12-30 18:07:39 +01:00
EvalAltResult::ErrorFunctionNotFound(s, ..)
2022-07-29 05:01:00 +02:00
if s == "Math::Advanced::bar_m (f64, f64)"));
Ok(())
}
2022-12-30 18:07:39 +01:00
#[allow(dead_code)] // used inside `export_module!`
mod export_all {
use rhai::plugin::*;
2020-12-24 14:28:40 +01:00
#[export_module(export_all)]
pub mod my_adds {
use rhai::{FLOAT, INT};
#[rhai_fn(name = "foo_add_f")]
pub fn add_float(f1: FLOAT, f2: FLOAT) -> FLOAT {
f1 + f2
}
#[rhai_fn(name = "foo_add_i")]
fn add_int(i1: INT, i2: INT) -> INT {
i1 + i2
}
#[rhai_fn(skip)]
pub fn add_float2(f1: FLOAT, f2: FLOAT) -> FLOAT {
f1 + f2
}
pub fn foo_m(f1: FLOAT, f2: FLOAT) -> FLOAT {
f1 + f2
}
fn foo_n(i1: INT, i2: INT) -> INT {
i1 + i2
}
#[rhai_fn(skip)]
fn foo_p(i1: INT, i2: INT) -> INT {
i1 * i2
}
}
}
#[test]
fn export_all_test() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let m = rhai::exported_module!(crate::export_all::my_adds);
2020-12-26 06:05:57 +01:00
engine.register_static_module("Math::Advanced", m.into());
let output_array = engine.eval::<Array>(
2022-07-29 05:01:00 +02:00
"
let ex = 41.0;
let fx = Math::Advanced::foo_add_f(ex, 1.0);
let gx = Math::Advanced::foo_m(41.0, 1.0);
let ei = 41;
let fi = Math::Advanced::foo_add_i(ei, 1);
let gi = Math::Advanced::foo_n(41, 1);
[fx, gx, fi, gi]
",
)?;
assert_eq!(&output_array[0].as_float().unwrap(), &42.0);
assert_eq!(&output_array[1].as_float().unwrap(), &42.0);
assert_eq!(&output_array[2].as_int().unwrap(), &42);
assert_eq!(&output_array[3].as_int().unwrap(), &42);
assert!(matches!(*engine.eval::<INT>(
"
let ex = 41;
let fx = Math::Advanced::foo_p(ex, 1);
fx
").unwrap_err(),
2022-12-30 18:07:39 +01:00
EvalAltResult::ErrorFunctionNotFound(s, ..)
2022-07-29 05:01:00 +02:00
if s == "Math::Advanced::foo_p (i64, i64)"));
Ok(())
}