branch: elpa/buttercup commit a970d10c2f681b65e205d52189c36cd1b0122e43 Author: Ryan C. Thompson <r...@thompsonclan.org> Commit: Ryan C. Thompson <r...@thompsonclan.org>
Implement spy-calls-count-returned and spy-calls-count-errors A test is added for both functions. --- buttercup.el | 10 ++++++++++ docs/writing-tests.md | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/buttercup.el b/buttercup.el index 29d8a16..2bed332 100644 --- a/buttercup.el +++ b/buttercup.el @@ -1195,6 +1195,16 @@ responsibility to ensure ARG is a command." "Return the number of times SPY has been called so far." (length (spy-calls-all spy))) +(defun spy-calls-count-returned (spy) + "Return the number of times SPY has been called successfully so far." + (length (cl-remove-if-not 'spy-context-return-p + (spy-calls-all spy)))) + +(defun spy-calls-count-errors (spy) + "Return the number of times SPY has been called and thrown errors so far." + (length (cl-remove-if-not 'spy-context-thrown-p + (spy-calls-all spy)))) + (defun spy-calls-args-for (spy index) "Return the context of the INDEXth call to SPY." (let ((context (elt (spy-calls-all spy) diff --git a/docs/writing-tests.md b/docs/writing-tests.md index 9307f43..bd23376 100644 --- a/docs/writing-tests.md +++ b/docs/writing-tests.md @@ -657,6 +657,29 @@ Finally, `spy-calls-reset` clears all tracking for a spy. (spy-calls-most-recent 'set-foo)) :to-throw)) + (it "counts the number of successful and failed calls" + ;; Set up `set-foo' so that it can either return a value or throw + ;; an error + (spy-on 'set-foo :and-call-fake + (lambda (val &rest ignored) + (if (>= val 0) + val + (error "Value must not be negative")))) + (expect (set-foo 1) :to-be 1) + (expect (set-foo 2) :to-be 2) + (expect (set-foo 3) :to-be 3) + (expect (set-foo -1) :to-throw 'error) + (expect (set-foo -2) :to-throw 'error) + (expect (set-foo -3) :to-throw 'error) + (expect (set-foo -4) :to-throw 'error) + + (expect (spy-calls-count 'set-foo) + :to-be 7) + (expect (spy-calls-count-returned 'set-foo) + :to-be 3) + (expect (spy-calls-count-errors 'set-foo) + :to-be 4)) + (it "can be reset" (set-foo 123) (set-foo 456 "baz")