rhai/tests/method_call.rs

151 lines
3.4 KiB
Rust
Raw Normal View History

#![cfg(not(feature = "no_object"))]
use rhai::{Engine, EvalAltResult, INT};
2017-11-03 17:58:51 +01:00
#[derive(Debug, Clone, Eq, PartialEq)]
struct TestStruct {
x: INT,
}
2017-11-03 17:58:51 +01:00
impl TestStruct {
fn update(&mut self, n: INT) {
self.x += n;
}
2017-11-03 17:58:51 +01:00
fn new() -> Self {
Self { x: 1 }
2017-11-03 17:58:51 +01:00
}
}
2017-11-03 17:58:51 +01:00
#[test]
fn test_method_call() -> Result<(), Box<EvalAltResult>> {
2017-11-03 17:58:51 +01:00
let mut engine = Engine::new();
2020-07-12 05:46:53 +02:00
engine
.register_type::<TestStruct>()
.register_fn("update", TestStruct::update)
.register_fn("new_ts", TestStruct::new);
2017-11-03 17:58:51 +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(); 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")?,
TestStruct { x: 1001 }
2020-03-30 10:10:40 +02:00
);
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
}
#[test]
fn test_method_call_style() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(engine.eval::<INT>("let x = -123; x.abs(); x")?, -123);
Ok(())
}
#[cfg(not(feature = "no_optimize"))]
#[test]
fn test_method_call_with_full_optimization() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.set_optimization_level(rhai::OptimizationLevel::Full);
engine
2022-12-30 18:07:39 +01:00
.register_fn("new_ts", TestStruct::new)
.register_fn("ymd", |_: INT, _: INT, _: INT| 42 as INT)
.register_fn("range", |_: &mut TestStruct, _: INT, _: INT| {
TestStruct::new()
});
assert_eq!(
engine.eval::<TestStruct>(
"
let xs = new_ts();
let ys = xs.range(ymd(2022, 2, 1), ymd(2022, 2, 2));
ys
"
)?,
TestStruct::new()
);
Ok(())
}
2023-03-22 09:05:25 +01:00
#[cfg(not(feature = "no_function"))]
#[test]
fn test_method_call_typed() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine
.register_type_with_name::<TestStruct>("Test-Struct#ABC")
.register_fn("update", TestStruct::update)
.register_fn("new_ts", TestStruct::new);
assert_eq!(
engine.eval::<TestStruct>(
r#"
fn "Test-Struct#ABC".foo(x) {
this.update(x);
}
fn foo(x) {
this += x;
}
let z = 1000;
z.foo(1);
let x = new_ts();
x.foo(z);
x
"#
)?,
TestStruct { x: 1002 }
);
2023-03-30 10:43:15 +02:00
assert!(engine.eval::<bool>(
r#"
fn "Test-Struct#ABC".foo(x) {
this.update(x);
}
is_def_fn("Test-Struct#ABC", "foo", 1)
"#
)?);
2023-03-22 09:05:25 +01:00
assert!(matches!(
*engine
.run(
r#"
fn "Test-Struct#ABC".foo(x) {
this.update(x);
}
foo(1000);
"#
)
2023-04-10 17:23:59 +02:00
.unwrap_err(),
2023-03-22 09:05:25 +01:00
EvalAltResult::ErrorFunctionNotFound(f, ..) if f.starts_with("foo")
));
assert!(matches!(
*engine
.run(
r#"
fn "Test-Struct#ABC".foo(x) {
this.update(x);
}
let x = 42;
x.foo(1000);
"#
)
2023-04-10 17:23:59 +02:00
.unwrap_err(),
2023-03-22 09:05:25 +01:00
EvalAltResult::ErrorFunctionNotFound(f, ..) if f.starts_with("foo")
));
Ok(())
}