Function: ert-with-message-capture
ert-with-message-capture is a macro defined in ert-x.el.gz.
Signature
(ert-with-message-capture VAR &rest BODY)
Documentation
Execute BODY while collecting messages in VAR.
Capture messages issued by Lisp code and concatenate them
separated by newlines into one string. This includes messages
written by message as well as objects printed by print,
prin1 and princ to the echo area. Messages issued from C
code using the above mentioned functions will not be captured.
This is useful for separating the issuance of messages by the code under test from the behavior of the *Messages* buffer.
Source Code
;; Defined in /usr/src/emacs/lisp/emacs-lisp/ert-x.el.gz
(defmacro ert-with-message-capture (var &rest body)
"Execute BODY while collecting messages in VAR.
Capture messages issued by Lisp code and concatenate them
separated by newlines into one string. This includes messages
written by `message' as well as objects printed by `print',
`prin1' and `princ' to the echo area. Messages issued from C
code using the above mentioned functions will not be captured.
This is useful for separating the issuance of messages by the
code under test from the behavior of the *Messages* buffer."
(declare (debug (symbolp body))
(indent 1))
(let ((g-message-advice (gensym))
(g-print-advice (gensym))
(g-collector (gensym)))
`(let* ((,var "")
(,g-collector (lambda (msg) (setq ,var (concat ,var msg))))
(,g-message-advice (ert--make-message-advice ,g-collector))
(,g-print-advice (ert--make-print-advice ,g-collector)))
(advice-add 'message :around ,g-message-advice)
(advice-add 'prin1 :around ,g-print-advice)
(advice-add 'princ :around ,g-print-advice)
(advice-add 'print :around ,g-print-advice)
(unwind-protect
(progn ,@body)
(advice-remove 'print ,g-print-advice)
(advice-remove 'princ ,g-print-advice)
(advice-remove 'prin1 ,g-print-advice)
(advice-remove 'message ,g-message-advice)))))