Skip to content

Commit

Permalink
New test for BuildType with operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Jun 6, 2024
1 parent 2bf4b72 commit f7728f7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/api/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ impl Engine {
const X: bool,
R: Variant + Clone,
const F: bool,
FUNC: RhaiNativeFunc<A, N, X, R, F> + SendSync + 'static,
>(
&mut self,
name: impl AsRef<str> + Into<Identifier>,
func: FUNC,
func: impl RhaiNativeFunc<A, N, X, R, F> + SendSync + 'static,
) -> &mut Self {
FuncRegistration::new(name.into()).register_into_engine(self, func);

Expand Down
29 changes: 29 additions & 0 deletions tests/build_type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg(not(feature = "no_object"))]
use rhai::{CustomType, Engine, EvalAltResult, Position, TypeBuilder, INT};
use std::cmp::Ordering;

#[test]
fn test_build_type() {
Expand Down Expand Up @@ -219,3 +220,31 @@ fn test_build_type_macro() {
}
);
}

#[test]
fn test_build_type_operators() {
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct XYZ(INT);

impl CustomType for XYZ {
fn build(mut tb: TypeBuilder<Self>) {
tb.with_fn("new_xyz", XYZ)
.with_fn("<", |a: XYZ, b: XYZ| Self::cmp(&a, &b) == Ordering::Less)
.with_fn(">", |a: XYZ, b: XYZ| Self::cmp(&a, &b) == Ordering::Greater)
.with_fn("<=", |a: XYZ, b: XYZ| Self::cmp(&a, &b) != Ordering::Greater)
.with_fn(">=", |a: XYZ, b: XYZ| Self::cmp(&a, &b) != Ordering::Less)
.with_fn("!=", |a: XYZ, b: XYZ| Self::cmp(&a, &b) != Ordering::Equal)
.with_fn("==", |a: XYZ, b: XYZ| Self::cmp(&a, &b) == Ordering::Equal);
}
}

let mut engine = Engine::new();
engine.build_type::<XYZ>();

assert!(!engine.eval::<bool>("let a = new_xyz(1); let b = new_xyz(2); a == b").unwrap());
assert!(engine.eval::<bool>("let a = new_xyz(1); let b = new_xyz(2); a != b").unwrap());
assert!(engine.eval::<bool>("let a = new_xyz(1); let b = new_xyz(2); a < b").unwrap());
assert!(engine.eval::<bool>("let a = new_xyz(1); let b = new_xyz(2); a <= b").unwrap());
assert!(!engine.eval::<bool>("let a = new_xyz(1); let b = new_xyz(2); a > b").unwrap());
assert!(!engine.eval::<bool>("let a = new_xyz(1); let b = new_xyz(2); a >= b").unwrap());
}

0 comments on commit f7728f7

Please sign in to comment.