Add tests for multiple renames.

This commit is contained in:
Stephen Chung 2020-09-05 10:46:24 +08:00 committed by J Henry Waugh
parent dbfd3df810
commit 397acb4fce
5 changed files with 156 additions and 0 deletions

View File

@ -222,6 +222,73 @@ fn duplicate_fn_rename_test() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
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.load_package(m);
let output_array = engine.eval::<Array>(
r#"
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]
"#,
)?;
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(())
}
mod export_by_prefix {
use rhai::plugin::*;
#[export_module(export_prefix = "foo_")]

View File

@ -0,0 +1,30 @@
use rhai::plugin::*;
#[derive(Clone)]
pub struct Point {
x: f32,
y: f32,
}
#[export_module]
pub mod test_module {
pub use super::Point;
#[rhai_fn(name = "foo", get = "bar")]
pub fn test_fn(input: Point) -> bool {
input.x > input.y
}
#[rhai_fn(get = "bar")]
pub fn foo(input: Point) -> bool {
input.x < input.y
}
}
fn main() {
let n = Point { x: 0.0, y: 10.0 };
if test_module::test_fn(n) {
println!("yes");
} else {
println!("no");
}
}

View File

@ -0,0 +1,17 @@
error: duplicate Rhai signature for 'get$bar'
--> $DIR/rhai_fn_rename_collision_oneattr_multiple.rs:17:15
|
17 | #[rhai_fn(get = "bar")]
| ^^^
error: duplicated function renamed 'get$bar'
--> $DIR/rhai_fn_rename_collision_oneattr_multiple.rs:12:15
|
12 | #[rhai_fn(name = "foo", get = "bar")]
| ^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/rhai_fn_rename_collision_oneattr_multiple.rs:25:8
|
25 | if test_module::test_fn(n) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`

View File

@ -0,0 +1,25 @@
use rhai::plugin::*;
#[derive(Clone)]
pub struct Point {
x: f32,
y: f32,
}
#[export_module]
pub mod test_module {
pub use super::Point;
#[rhai_fn(name = "foo", name = "bar", name = "foo")]
pub fn test_fn(input: Point) -> bool {
input.x > input.y
}
}
fn main() {
let n = Point { x: 0.0, y: 10.0 };
if test_module::test_fn(n) {
println!("yes");
} else {
println!("no");
}
}

View File

@ -0,0 +1,17 @@
error: duplicate Rhai signature for 'foo'
--> $DIR/rhai_fn_rename_collision_with_itself.rs:12:15
|
12 | #[rhai_fn(name = "foo", name = "bar", name = "foo")]
| ^^^^
error: duplicated function renamed 'foo'
--> $DIR/rhai_fn_rename_collision_with_itself.rs:12:15
|
12 | #[rhai_fn(name = "foo", name = "bar", name = "foo")]
| ^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/rhai_fn_rename_collision_with_itself.rs:20:8
|
20 | if test_module::test_fn(n) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`