2020-03-10 16:06:20 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, RegisterFn, INT};
|
2017-11-03 17:58:51 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mismatched_op() {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2017-11-03 17:58:51 +01:00
|
|
|
|
2020-03-14 16:40:30 +01:00
|
|
|
assert!(
|
2020-04-08 04:19:03 +02:00
|
|
|
matches!(engine.eval::<INT>(r#""hello, " + "world!""#).expect_err("expects error"),
|
2020-03-14 16:40:30 +01:00
|
|
|
EvalAltResult::ErrorMismatchOutputType(err, _) if err == "string")
|
|
|
|
);
|
2019-09-30 19:57:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-29 11:15:12 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2019-09-30 19:57:21 +02:00
|
|
|
fn test_mismatched_op_custom_type() {
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct TestStruct {
|
2020-03-10 16:06:20 +01:00
|
|
|
x: INT,
|
2019-09-30 19:57:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestStruct {
|
2020-03-19 06:52:10 +01:00
|
|
|
fn new() -> Self {
|
2019-09-30 19:57:21 +02:00
|
|
|
TestStruct { x: 1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
2020-03-10 12:48:47 +01:00
|
|
|
engine.register_type_with_name::<TestStruct>("TestStruct");
|
2019-09-30 19:57:21 +02:00
|
|
|
engine.register_fn("new_ts", TestStruct::new);
|
|
|
|
|
2020-03-14 16:40:30 +01:00
|
|
|
let r = engine
|
|
|
|
.eval::<INT>("60 + new_ts()")
|
|
|
|
.expect_err("expects error");
|
2020-03-02 15:11:56 +01:00
|
|
|
|
2020-03-15 15:39:58 +01:00
|
|
|
#[cfg(feature = "only_i32")]
|
|
|
|
assert!(
|
|
|
|
matches!(r, EvalAltResult::ErrorFunctionNotFound(err, _) if err == "+ (i32, TestStruct)")
|
|
|
|
);
|
2020-03-10 16:06:20 +01:00
|
|
|
|
2020-03-15 15:39:58 +01:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
assert!(
|
|
|
|
matches!(r, EvalAltResult::ErrorFunctionNotFound(err, _) if err == "+ (i64, TestStruct)")
|
|
|
|
);
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|