rhai/tests/mismatched_op.rs

55 lines
1.5 KiB
Rust
Raw Normal View History

use rhai::{Engine, EvalAltResult, INT};
2017-11-03 17:58:51 +01:00
#[test]
fn test_mismatched_op() {
let engine = Engine::new();
2017-11-03 17:58:51 +01: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"
));
2019-09-30 19:57:21 +02:00
}
#[test]
#[cfg(not(feature = "no_object"))]
fn test_mismatched_op_custom_type() -> Result<(), Box<EvalAltResult>> {
2020-07-03 04:45:01 +02:00
#[derive(Debug, Clone)]
2019-09-30 19:57:21 +02:00
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 {
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
2021-04-20 06:01:35 +02:00
assert!(matches!(*engine.eval::<bool>("
let x = new_ts();
let y = new_ts();
x == y
").expect_err("should error"),
EvalAltResult::ErrorFunctionNotFound(f, _) if f == "== (TestStruct, TestStruct)"));
assert!(!engine.eval::<bool>("new_ts() == 42")?);
assert!(matches!(
2020-07-03 04:45:01 +02:00
*engine.eval::<INT>("60 + new_ts()").expect_err("should error"),
EvalAltResult::ErrorFunctionNotFound(f, _) if f == format!("+ ({}, TestStruct)", std::any::type_name::<INT>())
));
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>()
));
Ok(())
2017-11-03 17:58:51 +01:00
}