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-04-21 17:25:12 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine.eval::<INT>(r#""hello, " + "world!""#).expect_err("expects error"),
|
2020-07-03 04:45:01 +02:00
|
|
|
EvalAltResult::ErrorMismatchOutputType(need, actual, _) if need == std::any::type_name::<INT>() && actual == "string"
|
2020-04-21 17:25:12 +02:00
|
|
|
));
|
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() {
|
2020-07-03 04:45:01 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2019-09-30 19:57:21 +02:00
|
|
|
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 {
|
2020-10-19 08:26:15 +02:00
|
|
|
Self { x: 1 }
|
2019-09-30 19:57:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
2020-07-12 05:46:53 +02:00
|
|
|
|
|
|
|
engine
|
|
|
|
.register_type_with_name::<TestStruct>("TestStruct")
|
|
|
|
.register_fn("new_ts", TestStruct::new);
|
2019-09-30 19:57:21 +02:00
|
|
|
|
2020-04-21 17:25:12 +02:00
|
|
|
assert!(matches!(
|
2020-07-03 04:45:01 +02:00
|
|
|
*engine.eval::<INT>("60 + new_ts()").expect_err("should error"),
|
|
|
|
EvalAltResult::ErrorFunctionNotFound(err, _) if err == format!("+ ({}, TestStruct)", std::any::type_name::<INT>())
|
2020-04-21 17:25:12 +02:00
|
|
|
));
|
2020-03-10 16:06:20 +01:00
|
|
|
|
2020-04-21 17:25:12 +02:00
|
|
|
assert!(matches!(
|
2020-07-03 04:45:01 +02:00
|
|
|
*engine.eval::<TestStruct>("42").expect_err("should error"),
|
|
|
|
EvalAltResult::ErrorMismatchOutputType(need, actual, _)
|
|
|
|
if need == "TestStruct" && actual == std::any::type_name::<INT>()
|
2020-04-21 17:25:12 +02:00
|
|
|
));
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|