aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGemini Lasswell2017-09-21 13:35:45 -0700
committerGemini Lasswell2017-09-21 13:35:45 -0700
commit68baca3ee142b42de0bbe4eba84945780fd157d6 (patch)
treefb1f5cc7ac7abeda14947c49a91b95319709a1e7
parent28e0c410c972ad8db9bf8a5d32f64921108104d7 (diff)
downloademacs-68baca3ee142b42de0bbe4eba84945780fd157d6.tar.gz
emacs-68baca3ee142b42de0bbe4eba84945780fd157d6.zip
Catch more messages in ert-with-message-capture
* lisp/emacs-lisp/ert-x.el (ert-with-message-capture): Capture messages from prin1, princ and print. (ert--make-message-advice): New function. (ert--make-print-advice): New function.
-rw-r--r--lisp/emacs-lisp/ert-x.el57
1 files changed, 45 insertions, 12 deletions
diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el
index 6d9a7d9211a..5af5262e5da 100644
--- a/lisp/emacs-lisp/ert-x.el
+++ b/lisp/emacs-lisp/ert-x.el
@@ -286,27 +286,60 @@ BUFFER defaults to current buffer. Does not modify BUFFER."
286 286
287 287
288(defmacro ert-with-message-capture (var &rest body) 288(defmacro ert-with-message-capture (var &rest body)
289 "Execute BODY while collecting anything written with `message' in VAR. 289 "Execute BODY while collecting messages in VAR.
290 290
291Capture all messages produced by `message' when it is called from 291Capture messages issued by Lisp code and concatenate them
292Lisp, and concatenate them separated by newlines into one string. 292separated by newlines into one string. This includes messages
293written by `message' as well as objects printed by `print',
294`prin1' and `princ' to the echo area. Messages issued from C
295code using the above mentioned functions will not be captured.
293 296
294This is useful for separating the issuance of messages by the 297This is useful for separating the issuance of messages by the
295code under test from the behavior of the *Messages* buffer." 298code under test from the behavior of the *Messages* buffer."
296 (declare (debug (symbolp body)) 299 (declare (debug (symbolp body))
297 (indent 1)) 300 (indent 1))
298 (let ((g-advice (gensym))) 301 (let ((g-message-advice (gensym))
302 (g-print-advice (gensym))
303 (g-collector (gensym)))
299 `(let* ((,var "") 304 `(let* ((,var "")
300 (,g-advice (lambda (func &rest args) 305 (,g-collector (lambda (msg) (setq ,var (concat ,var msg))))
301 (if (or (null args) (equal (car args) "")) 306 (,g-message-advice (ert--make-message-advice ,g-collector))
302 (apply func args) 307 (,g-print-advice (ert--make-print-advice ,g-collector)))
303 (let ((msg (apply #'format-message args))) 308 (advice-add 'message :around ,g-message-advice)
304 (setq ,var (concat ,var msg "\n")) 309 (advice-add 'prin1 :around ,g-print-advice)
305 (funcall func "%s" msg)))))) 310 (advice-add 'princ :around ,g-print-advice)
306 (advice-add 'message :around ,g-advice) 311 (advice-add 'print :around ,g-print-advice)
307 (unwind-protect 312 (unwind-protect
308 (progn ,@body) 313 (progn ,@body)
309 (advice-remove 'message ,g-advice))))) 314 (advice-remove 'print ,g-print-advice)
315 (advice-remove 'princ ,g-print-advice)
316 (advice-remove 'prin1 ,g-print-advice)
317 (advice-remove 'message ,g-message-advice)))))
318
319(defun ert--make-message-advice (collector)
320 "Create around advice for `message' for `ert-collect-messages'.
321COLLECTOR will be called with the message before it is passed
322to the real `message'."
323 (lambda (func &rest args)
324 (if (or (null args) (equal (car args) ""))
325 (apply func args)
326 (let ((msg (apply #'format-message args)))
327 (funcall collector (concat msg "\n"))
328 (funcall func "%s" msg)))))
329
330(defun ert--make-print-advice (collector)
331 "Create around advice for print functions for `ert-collect-messsges'.
332The created advice function will just call the original function
333unless the output is going to the echo area (when PRINTCHARFUN is
334t or PRINTCHARFUN is nil and `standard-output' is t). If the
335output is destined for the echo area, the advice function will
336convert it to a string and pass it to COLLECTOR first."
337 (lambda (func object &optional printcharfun)
338 (if (not (eq t (or printcharfun standard-output)))
339 (funcall func object printcharfun)
340 (funcall collector (with-output-to-string
341 (funcall func object)))
342 (funcall func object printcharfun))))
310 343
311 344
312(provide 'ert-x) 345(provide 'ert-x)