Skip to content

Commit

Permalink
Separate the :to-throw failure types to enable better messages
Browse files Browse the repository at this point in the history
  • Loading branch information
snogge committed Oct 18, 2023
1 parent ab830f5 commit 2b0ed7d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
38 changes: 24 additions & 14 deletions buttercup.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
2 changes: 1 addition & 1 deletion tests/test-buttercup.el
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit 2b0ed7d

Please sign in to comment.