rhai/tests/method_call.rs

35 lines
755 B
Rust
Raw Normal View History

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_method_call() -> Result<(), EvalAltResult> {
2017-11-03 17:58:51 +01:00
#[derive(Clone)]
struct TestStruct {
x: INT,
2017-11-03 17:58:51 +01:00
}
impl TestStruct {
fn update(&mut self) {
self.x += 1000;
}
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-02 15:11:56 +01:00
let ts = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?;
assert_eq!(ts.x, 1001);
2020-03-27 16:47:23 +01:00
let ts = engine.eval::<TestStruct>("let x = new_ts(); update(x); x")?;
assert_eq!(ts.x, 1);
2020-03-02 15:11:56 +01:00
Ok(())
2017-11-03 17:58:51 +01:00
}