From 8408579758d79829170583b30e46aee1de9deb2b Mon Sep 17 00:00:00 2001 From: Ola Nilsson Date: Thu, 29 Aug 2024 22:42:14 +0200 Subject: [PATCH] Rewrite buttercup-expect with pcase Replace the special case of (null matcher) with using the :to-be-truthy matcher. buttercup-expect is probably never called without a matcher as the expect macro will pass the :to-be-truthy matcher if none is given. --- buttercup.el | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/buttercup.el b/buttercup.el index 702192a..4c9b74f 100644 --- a/buttercup.el +++ b/buttercup.el @@ -220,24 +220,14 @@ the arguments is delayed until the containing spec is executed." See the macro documentation for details and the definition of ARG, MATCHER and ARGS." (cl-assert (cl-every #'buttercup--wrapper-fun-p (cons arg args)) t) - (if (not matcher) - (progn - (cl-assert (not args) t) - (when (not (funcall arg)) - (buttercup-fail "Expected %S to be non-nil" - (buttercup--enclosed-expr arg)))) - (let ((result (buttercup--apply-matcher matcher (cons arg args)))) - (if (consp result) - (when (not (car result)) - (buttercup-fail "%s" (cdr result))) - (when (not result) - (buttercup-fail "Expected %S %S %s" - (buttercup--enclosed-expr arg) - matcher - (mapconcat (lambda (obj) - (format "%S" (funcall obj))) - args - " "))))))) + (pcase (buttercup--apply-matcher (or matcher :to-be-truthy) (cons arg args)) + (`(,result . ,message) (unless result (buttercup-fail message))) + (result (unless result (buttercup-fail "Expected %S %S %s" + (buttercup--enclosed-expr arg) + matcher + (mapconcat (lambda (obj) + (format "%S" (funcall obj))) + args " ")))))) (defun buttercup-fail (format &rest args) "Fail the current test with the given description.