rhai/tests/method_call.rs
2020-03-30 16:10:40 +08:00

41 lines
856 B
Rust

#![cfg(not(feature = "no_object"))]
use rhai::{Engine, EvalAltResult, RegisterFn, INT};
#[test]
fn test_method_call() -> Result<(), EvalAltResult> {
#[derive(Debug, Clone, Eq, PartialEq)]
struct TestStruct {
x: INT,
}
impl TestStruct {
fn update(&mut self) {
self.x += 1000;
}
fn new() -> Self {
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);
assert_eq!(
engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?,
TestStruct { x: 1001 }
);
assert_eq!(
engine.eval::<TestStruct>("let x = new_ts(); update(x); x")?,
TestStruct { x: 1 }
);
Ok(())
}