Test and fix 'unwrap_void_or'

This commit is contained in:
KtorZ 2024-08-01 16:55:37 +02:00 committed by Kasey
parent 49ddcccd12
commit 93a141bfa4
1 changed files with 40 additions and 1 deletions

View File

@ -620,6 +620,7 @@ impl Term<Name> {
where where
F: FnOnce(Term<Name>) -> Term<Name>, F: FnOnce(Term<Name>) -> Term<Name>,
{ {
assert!(matches!(self, Term::Var(..)));
Term::equals_integer() Term::equals_integer()
.apply(Term::integer(0.into())) .apply(Term::integer(0.into()))
.apply(Term::fst_pair().apply(Term::unconstr_data().apply(self.clone()))) .apply(Term::fst_pair().apply(Term::unconstr_data().apply(self.clone())))
@ -631,7 +632,6 @@ impl Term<Name> {
.delay(), .delay(),
otherwise.clone(), otherwise.clone(),
) )
.force()
} }
/// Convert an arbitrary 'term' into a pair and pass it into a 'callback'. /// Convert an arbitrary 'term' into a pair and pass it into a 'callback'.
@ -845,4 +845,43 @@ mod tests {
assert_eq!(result, Err(Error::EvaluationFailure)); assert_eq!(result, Err(Error::EvaluationFailure));
} }
#[test]
fn unwrap_void_or_happy() {
let result = quick_eval(
Term::data(Data::constr(0, vec![])).as_var("__unit", |unit| {
Term::Var(unit)
.unwrap_void_or(|u| u.delay(), &Term::Error.delay())
.force()
}),
);
assert_eq!(result, Ok(Term::unit()));
}
#[test]
fn unwrap_void_or_wrong_constr() {
let result = quick_eval(
Term::data(Data::constr(14, vec![])).as_var("__unit", |unit| {
Term::Var(unit)
.unwrap_void_or(|u| u.delay(), &Term::Error.delay())
.force()
}),
);
assert_eq!(result, Err(Error::EvaluationFailure));
}
#[test]
fn unwrap_void_or_too_many_args() {
let result = quick_eval(
Term::data(Data::constr(0, vec![Data::integer(0.into())])).as_var("__unit", |unit| {
Term::Var(unit)
.unwrap_void_or(|u| u.delay(), &Term::Error.delay())
.force()
}),
);
assert_eq!(result, Err(Error::EvaluationFailure));
}
} }