Do not default compares of different types.

This commit is contained in:
Stephen Chung
2023-03-08 21:47:57 +08:00
parent fa4096e91e
commit 58fad030ee
8 changed files with 125 additions and 161 deletions

View File

@@ -39,7 +39,10 @@ fn test_mismatched_op_custom_type() -> Result<(), Box<EvalAltResult>> {
").expect_err("should error"),
EvalAltResult::ErrorFunctionNotFound(f, ..) if f == "== (TestStruct, TestStruct)"));
assert!(!engine.eval::<bool>("new_ts() == 42")?);
assert!(
matches!(*engine.eval::<bool>("new_ts() == 42").expect_err("should error"),
EvalAltResult::ErrorFunctionNotFound(f, ..) if f.starts_with("== (TestStruct, "))
);
assert!(matches!(
*engine.eval::<INT>("60 + new_ts()").expect_err("should error"),

View File

@@ -35,7 +35,11 @@ fn test_ops_other_number_types() -> Result<(), Box<EvalAltResult>> {
EvalAltResult::ErrorFunctionNotFound(f, ..) if f.starts_with("== (u16,")
));
assert!(!engine.eval_with_scope::<bool>(&mut scope, r#"x == "hello""#)?);
assert!(
matches!(*engine.eval_with_scope::<bool>(&mut scope, r#"x == "hello""#).expect_err("should error"),
EvalAltResult::ErrorFunctionNotFound(f, ..) if f.starts_with("== (u16,")
)
);
Ok(())
}
@@ -63,3 +67,24 @@ fn test_ops_precedence() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_ops_custom_types() -> Result<(), Box<EvalAltResult>> {
#[derive(Debug, Clone, PartialEq, Eq)]
struct Test1;
#[derive(Debug, Clone, PartialEq, Eq)]
struct Test2;
let mut engine = Engine::new();
engine
.register_type_with_name::<Test1>("Test1")
.register_type_with_name::<Test2>("Test2")
.register_fn("new_ts1", || Test1)
.register_fn("new_ts2", || Test2)
.register_fn("==", |x: Test1, y: Test2| true);
assert!(engine.eval::<bool>("let x = new_ts1(); let y = new_ts2(); x == y")?);
Ok(())
}