Skip to content

Commit

Permalink
add tests for various cases of function definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
divarvel committed Oct 24, 2024
1 parent 3175834 commit aa7e2bd
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions biscuit-auth/src/datalog/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,15 @@ mod tests {
Op::Value(Term::Integer(42)),
Op::Unary(Unary::Ffi("test_un".to_owned())),
Op::Binary(Binary::And),
Op::Value(Term::Integer(42)),
Op::Unary(Unary::Ffi("test_closure".to_owned())),
Op::Binary(Binary::And),
Op::Value(Term::Str(i)),
Op::Unary(Unary::Ffi("test_closure".to_owned())),
Op::Binary(Binary::And),
Op::Value(Term::Integer(42)),
Op::Unary(Unary::Ffi("test_fn".to_owned())),
Op::Binary(Binary::And),
];

let values = HashMap::new();
Expand Down Expand Up @@ -1172,7 +1181,29 @@ mod tests {
}
})),
);
let closed_over_int = 42;
let closed_over_string = "test".to_string();
extern_funcs.insert(
"test_closure".to_owned(),
ExternFunc::new(Rc::new(move |left, right| match (&left, &right) {
(builder::Term::Integer(left), None) => {
Ok(builder::boolean(*left == closed_over_int))
}
(builder::Term::Str(left), None) => {
Ok(builder::boolean(left == &closed_over_string))
}
_ => {
println!("{left:?}, {right:?}");
Err("expecting a single integer".to_string())
}
})),
);
extern_funcs.insert("test_fn".to_owned(), ExternFunc::new(Rc::new(toto)));
let res = e.evaluate(&values, &mut tmp_symbols, &extern_funcs);
assert_eq!(res, Ok(Term::Bool(true)));
}

fn toto(_left: builder::Term, _right: Option<builder::Term>) -> Result<builder::Term, String> {
Ok(builder::Term::Bool(true))
}
}

0 comments on commit aa7e2bd

Please sign in to comment.