2020-03-29 11:15:12 +02:00
|
|
|
#![cfg(not(feature = "no_object"))]
|
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, RegisterFn, INT};
|
2017-11-03 17:58:51 +01:00
|
|
|
|
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_method_call() -> Result<(), Box<EvalAltResult>> {
|
2020-03-30 10:10:40 +02:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2017-11-03 17:58:51 +01:00
|
|
|
struct TestStruct {
|
2020-03-10 16:06:20 +01:00
|
|
|
x: INT,
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestStruct {
|
2020-05-12 04:20:29 +02:00
|
|
|
fn update(&mut self, n: INT) {
|
|
|
|
self.x += n;
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 06:52:10 +01:00
|
|
|
fn new() -> Self {
|
2017-11-03 17:58:51 +01:00
|
|
|
TestStruct { x: 1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine.register_type::<TestStruct>();
|
|
|
|
|
|
|
|
engine.register_fn("update", TestStruct::update);
|
|
|
|
engine.register_fn("new_ts", TestStruct::new);
|
|
|
|
|
2020-03-30 10:10:40 +02:00
|
|
|
assert_eq!(
|
2020-05-12 04:20:29 +02:00
|
|
|
engine.eval::<TestStruct>("let x = new_ts(); x.update(1000); x")?,
|
2020-03-30 10:10:40 +02:00
|
|
|
TestStruct { x: 1001 }
|
|
|
|
);
|
2020-03-02 15:11:56 +01:00
|
|
|
|
2020-03-30 10:10:40 +02:00
|
|
|
assert_eq!(
|
2020-05-12 04:20:29 +02:00
|
|
|
engine.eval::<TestStruct>("let x = new_ts(); update(x, 1000); x")?,
|
2020-03-30 10:10:40 +02:00
|
|
|
TestStruct { x: 1 }
|
|
|
|
);
|
2020-03-27 16:47:23 +01:00
|
|
|
|
2020-03-02 15:11:56 +01:00
|
|
|
Ok(())
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
2020-05-11 12:55:58 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_method_call_style() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
assert_eq!(engine.eval::<INT>("let x = -123; x.abs(); x")?, -123);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|