rhai/tests/mismatched_op.rs

45 lines
1.1 KiB
Rust
Raw Normal View History

use rhai::{Engine, EvalAltResult, RegisterFn, INT};
2017-11-03 17:58:51 +01:00
#[test]
#[cfg(not(feature = "no_stdlib"))]
2017-11-03 17:58:51 +01:00
fn test_mismatched_op() {
let mut engine = Engine::new();
2020-03-14 16:40:30 +01:00
assert!(
matches!(engine.eval::<INT>(r#"60 + "hello""#).expect_err("expects error"),
EvalAltResult::ErrorMismatchOutputType(err, _) if err == "string")
);
2019-09-30 19:57:21 +02:00
}
#[test]
fn test_mismatched_op_custom_type() {
#[derive(Clone)]
struct TestStruct {
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();
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
#[cfg(feature = "only_i32")]
assert!(
matches!(r, EvalAltResult::ErrorFunctionNotFound(err, _) if err == "+ (i32, TestStruct)")
);
#[cfg(not(feature = "only_i32"))]
assert!(
matches!(r, EvalAltResult::ErrorFunctionNotFound(err, _) if err == "+ (i64, TestStruct)")
);
2017-11-03 17:58:51 +01:00
}