use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_mismatched_op() { let engine = Engine::new(); assert!(matches!( *engine.eval::(r#""hello, " + "world!""#).expect_err("expects error"), EvalAltResult::ErrorMismatchOutputType(need, actual, ..) if need == std::any::type_name::() && actual == "string" )); } #[test] #[cfg(not(feature = "no_object"))] fn test_mismatched_op_custom_type() -> Result<(), Box> { #[allow(dead_code)] // used inside `register_type_with_name` #[derive(Debug, Clone)] struct TestStruct { x: INT, } impl TestStruct { fn new() -> Self { Self { x: 1 } } } let mut engine = Engine::new(); engine .register_type_with_name::("TestStruct") .register_fn("new_ts", TestStruct::new); assert!(matches!(*engine.eval::( " let x = new_ts(); let y = new_ts(); x == y ").unwrap_err(), EvalAltResult::ErrorFunctionNotFound(f, ..) if f == "== (TestStruct, TestStruct)")); assert!( matches!(*engine.eval::("new_ts() == 42").unwrap_err(), EvalAltResult::ErrorFunctionNotFound(f, ..) if f.starts_with("== (TestStruct, ")) ); assert!(matches!( *engine.eval::("60 + new_ts()").unwrap_err(), EvalAltResult::ErrorFunctionNotFound(f, ..) if f == format!("+ ({}, TestStruct)", std::any::type_name::()) )); assert!(matches!( *engine.eval::("42").unwrap_err(), EvalAltResult::ErrorMismatchOutputType(need, actual, ..) if need == "TestStruct" && actual == std::any::type_name::() )); Ok(()) }