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();
let r = engine.eval::<INT>("60 + \"hello\"");
2020-03-02 15:11:56 +01:00
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: INT,
2019-09-30 19:57:21 +02:00
}
impl TestStruct {
fn new() -> TestStruct {
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);
let r = engine.eval::<INT>("60 + new_ts()");
2020-03-02 15:11:56 +01:00
match r {
#[cfg(feature = "only_i32")]
Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err == "+ (i32, TestStruct)" => (),
#[cfg(not(feature = "only_i32"))]
Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err == "+ (i64, TestStruct)" => (),
2020-03-02 15:11:56 +01:00
_ => panic!(),
}
2017-11-03 17:58:51 +01:00
}