From 93a141bfa42de7eda9b79b464ab85ec8589cf24f Mon Sep 17 00:00:00 2001 From: KtorZ Date: Thu, 1 Aug 2024 16:55:37 +0200 Subject: [PATCH] Test and fix 'unwrap_void_or' --- crates/uplc/src/builder.rs | 41 +++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/crates/uplc/src/builder.rs b/crates/uplc/src/builder.rs index 5b354c0e..3db4a1c2 100644 --- a/crates/uplc/src/builder.rs +++ b/crates/uplc/src/builder.rs @@ -620,6 +620,7 @@ impl Term { where F: FnOnce(Term) -> Term, { + assert!(matches!(self, Term::Var(..))); Term::equals_integer() .apply(Term::integer(0.into())) .apply(Term::fst_pair().apply(Term::unconstr_data().apply(self.clone()))) @@ -631,7 +632,6 @@ impl Term { .delay(), otherwise.clone(), ) - .force() } /// 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)); } + + #[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)); + } }