Skip to content

Commit

Permalink
Rewrite buttercup-expect with pcase
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
snogge committed Aug 29, 2024
1 parent ac60ad3 commit 8e69e3c
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions buttercup.el
Original file line number Diff line number Diff line change
Expand Up @@ -219,24 +219,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.
Expand Down

0 comments on commit 8e69e3c

Please sign in to comment.