2020-03-10 16:06:20 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, RegisterFn, INT};
|
2017-11-03 17:58:51 +01:00
|
|
|
|
|
|
|
#[test]
|
2020-03-02 15:11:56 +01:00
|
|
|
fn test_get_set() -> Result<(), EvalAltResult> {
|
2017-11-03 17:58:51 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct TestStruct {
|
2020-03-10 16:06:20 +01:00
|
|
|
x: INT,
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestStruct {
|
2020-03-10 16:06:20 +01:00
|
|
|
fn get_x(&mut self) -> INT {
|
2017-11-03 17:58:51 +01:00
|
|
|
self.x
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
fn set_x(&mut self, new_x: INT) {
|
2017-11-03 17:58:51 +01:00
|
|
|
self.x = new_x;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new() -> TestStruct {
|
|
|
|
TestStruct { x: 1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine.register_type::<TestStruct>();
|
|
|
|
|
|
|
|
engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x);
|
|
|
|
engine.register_fn("new_ts", TestStruct::new);
|
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("let a = new_ts(); a.x = 500; a.x")?, 500);
|
2020-03-02 15:11:56 +01:00
|
|
|
|
|
|
|
Ok(())
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-02 15:11:56 +01:00
|
|
|
fn test_big_get_set() -> Result<(), EvalAltResult> {
|
2017-11-03 17:58:51 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct TestChild {
|
2020-03-10 16:06:20 +01:00
|
|
|
x: INT,
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestChild {
|
2020-03-10 16:06:20 +01:00
|
|
|
fn get_x(&mut self) -> INT {
|
2017-11-03 17:58:51 +01:00
|
|
|
self.x
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
fn set_x(&mut self, new_x: INT) {
|
2017-11-03 17:58:51 +01:00
|
|
|
self.x = new_x;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new() -> TestChild {
|
|
|
|
TestChild { x: 1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct TestParent {
|
|
|
|
child: TestChild,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestParent {
|
|
|
|
fn get_child(&mut self) -> TestChild {
|
|
|
|
self.child.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_child(&mut self, new_child: TestChild) {
|
|
|
|
self.child = new_child;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new() -> TestParent {
|
2019-09-18 12:21:07 +02:00
|
|
|
TestParent {
|
|
|
|
child: TestChild::new(),
|
|
|
|
}
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine.register_type::<TestChild>();
|
2020-03-04 15:00:01 +01:00
|
|
|
engine.register_type_with_name::<TestParent>("TestParent");
|
2017-11-03 17:58:51 +01:00
|
|
|
|
|
|
|
engine.register_get_set("x", TestChild::get_x, TestChild::set_x);
|
|
|
|
engine.register_get_set("child", TestParent::get_child, TestParent::set_child);
|
|
|
|
|
|
|
|
engine.register_fn("new_tp", TestParent::new);
|
|
|
|
|
2019-09-18 12:21:07 +02:00
|
|
|
assert_eq!(
|
2020-03-10 16:06:20 +01:00
|
|
|
engine.eval::<INT>("let a = new_tp(); a.child.x = 500; a.child.x")?,
|
2020-03-02 15:11:56 +01:00
|
|
|
500
|
2019-09-18 12:21:07 +02:00
|
|
|
);
|
2020-03-02 15:11:56 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<String>("let a = new_tp(); a.type_of()")?,
|
|
|
|
"TestParent"
|
|
|
|
);
|
|
|
|
|
2020-03-02 15:11:56 +01:00
|
|
|
Ok(())
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|