From 2b0ed7dc0ca7aa23d651f656853959fb63c99c32 Mon Sep 17 00:00:00 2001 From: Ola Nilsson Date: Tue, 7 Mar 2023 22:33:35 +0100 Subject: [PATCH] Separate the :to-throw failure types to enable better messages --- buttercup.el | 38 ++++++++++++++++++++++++-------------- tests/test-buttercup.el | 2 +- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/buttercup.el b/buttercup.el index 0ea4314..d6bf927 100644 --- a/buttercup.el +++ b/buttercup.el @@ -692,20 +692,30 @@ UNEVALUATED-EXPR if it did not raise any signal." "a signal") ?a (if expected-signal-args (format " with args `%S'" expected-signal-args) - "")))) - (if (null thrown-signal) - ;; Match is not possible unless a signal was raied - (cons nil (buttercup--simple-format spec "Expected `%E' to throw %s%a, but instead it returned `%e'")) - ;; non-matching signal - (buttercup--test-expectation matched - :expect-match-phrase - (buttercup--simple-format - spec - "Expected `%E' to throw %s%a, but instead it threw `%S'%A") - :expect-mismatch-phrase - (buttercup--simple-format - spec - "Expected `%E' not to throw %s%a, but it threw `%S'%A")))))) + "") + ?q (lambda () (format "%S" explained-signal-args)) + ))) + (cond + (matched ;; should be the most likely result + `(t . ,(buttercup--simple-format + spec "Expected `%E' not to throw %s%a, but it threw `%S'%A"))) + ((null thrown-signal) ; no signal raised + `(nil . ,(buttercup--simple-format + spec "Expected `%E' to throw %s%a, but instead it returned `%e'"))) + ((and explained-signal-args (not matching-signal-symbol)) ; neither symbol nor args matched + `(nil . ,(buttercup--simple-format + spec + "Expected `%E' to throw %s%a, but instead it threw `%S'%A"))) + (explained-signal-args ; symbol matched + `(nil . ,(buttercup--simple-format + spec + "Expected `%E' to signal %s%a, but instead signalled%A which does not match because %q."))) + ((not matching-signal-symbol) ; args matched + `(nil . ,(buttercup--simple-format + spec + "Expected `%E' to throw %s%a, but instead it threw `%S'%A"))) + (t (error "`buttercup--handle-to-throw' could not handle args %S %S" + thrown-signal expected-signal)))))) (buttercup-define-matcher :to-have-been-called (spy) (cl-assert (symbolp (funcall spy))) diff --git a/tests/test-buttercup.el b/tests/test-buttercup.el index cdd74d2..c1930ad 100644 --- a/tests/test-buttercup.el +++ b/tests/test-buttercup.el @@ -633,7 +633,7 @@ text properties using `ansi-color-apply'." (list 'overflow-error (concat "Foo" "bar" "baz")) '(myfunc) nil) :to-equal - '(nil . "Expected `(myfunc)' to throw a child signal of `overflow-error' with args `(\"Foobarbaz\")', but instead it threw `overflow-error' with args `(\"Foobar\")'"))) + '(nil . "Expected `(myfunc)' to signal a child signal of `overflow-error' with args `(\"Foobarbaz\")', but instead signalled with args `(\"Foobar\")' which does not match because (list-elt 0 (arrays-of-different-length 6 9 \"Foobar\" \"Foobarbaz\" first-mismatch-at 6))."))) (it "should not match an unrelated symbol" (expect (buttercup--handle-to-throw '(void-variable "Foobar") (list 'overflow-error (concat "Foo" "bar"))