2019-09-30 19:57:21 +02:00
|
|
|
use rhai::{Engine, EvalAltResult, RegisterFn};
|
2017-11-03 17:58:51 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mismatched_op() {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2020-03-02 15:11:56 +01:00
|
|
|
let r = engine.eval::<i64>("60 + \"hello\"");
|
|
|
|
|
|
|
|
match r {
|
2020-03-02 16:16:19 +01:00
|
|
|
Err(EvalAltResult::ErrorMismatchOutputType(err, _)) if err == "string" => (),
|
2020-03-02 15:11:56 +01:00
|
|
|
_ => panic!(),
|
|
|
|
}
|
2019-09-30 19:57:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mismatched_op_custom_type() {
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct TestStruct {
|
|
|
|
x: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestStruct {
|
|
|
|
fn new() -> TestStruct {
|
|
|
|
TestStruct { x: 1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.register_type::<TestStruct>();
|
|
|
|
engine.register_fn("new_ts", TestStruct::new);
|
|
|
|
|
2020-03-02 15:11:56 +01:00
|
|
|
let r = engine.eval::<i64>("60 + new_ts()");
|
|
|
|
|
|
|
|
match r {
|
|
|
|
Err(EvalAltResult::ErrorFunctionNotFound(err, _))
|
|
|
|
if err == "+ (i64, mismatched_op::test_mismatched_op_custom_type::TestStruct)" =>
|
|
|
|
{
|
|
|
|
()
|
|
|
|
}
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|