branch: elpa/buttercup commit c100fad6384507ffe6329d95c0a3d787db2f0731 Author: Ryan C. Thompson <r...@thompsonclan.org> Commit: Ryan C. Thompson <r...@thompsonclan.org>
Implement buttercup-suppress-warning-capture Fixes #124. --- buttercup.el | 13 +++++++++++++ docs/writing-tests.md | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/buttercup.el b/buttercup.el index 5000735..c12a34c 100644 --- a/buttercup.el +++ b/buttercup.el @@ -1657,6 +1657,7 @@ function is disabled to suppress display of all warning messages. The contents of this buffer are then displayed after the test finishes." (when (and (null buffer-name) + buttercup-warning-buffer-name (get-buffer buttercup-warning-buffer-name)) (setq buffer-name buttercup-warning-buffer-name)) (if (equal buffer-name buttercup-warning-buffer-name) @@ -1677,6 +1678,18 @@ finishes." (white . 37)) "List of text colors.") +(defmacro buttercup-suppress-warning-capture (&rest body) + "Suppress Buttercup's warning capturing within BODY. + +Buttercup normally captures all warnings while a test is running +so it can defer displaying them until after the test is complete. +However, if you want to catch any warnings yourself as part of +the test, you need to wrap your code in this macro to suppress +the capturing behavior." + (declare (indent 0)) + `(let ((buttercup-warning-buffer-name nil)) + ,@body)) + (defun buttercup-colorize (string color) "Format STRING with COLOR." (let ((color-code (cdr (assoc color buttercup-colors)))) diff --git a/docs/writing-tests.md b/docs/writing-tests.md index 3383467..7be3782 100644 --- a/docs/writing-tests.md +++ b/docs/writing-tests.md @@ -636,9 +636,22 @@ Finally, `spy-calls-reset` clears all tracking for a spy. ## Warnings in tests +By default, Buttercup captures any warning emitted during a test and +displays them all after the test completes in order to keep the output +readable. If you need to suppress this (for example if your test deals +with the warnings itself), you can use the macro +`buttercup-suppress-warning-capture`. + ```Emacs-Lisp (describe "A test" (it "can issue warnings while running" (display-warning 'buttercup "This warning should be visible after the test report.") - (expect (+ 2 2) :to-equal 4))) + (expect (+ 2 2) :to-equal 4)) + + (it "can capture its own warnings as part of the test" + (buttercup-suppress-warning-capture + (display-warning 'buttercup "This warning should be captured in `collected-output'.") + (expect (with-current-buffer "*Warnings*" + (buffer-string)) + :to-match "This warning should be captured")))) ```