diff options
| author | Gemini Lasswell | 2017-09-21 13:35:45 -0700 |
|---|---|---|
| committer | Gemini Lasswell | 2017-09-21 13:35:45 -0700 |
| commit | 68baca3ee142b42de0bbe4eba84945780fd157d6 (patch) | |
| tree | fb1f5cc7ac7abeda14947c49a91b95319709a1e7 | |
| parent | 28e0c410c972ad8db9bf8a5d32f64921108104d7 (diff) | |
| download | emacs-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.el | 57 |
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 | ||
| 291 | Capture all messages produced by `message' when it is called from | 291 | Capture messages issued by Lisp code and concatenate them |
| 292 | Lisp, and concatenate them separated by newlines into one string. | 292 | separated by newlines into one string. This includes messages |
| 293 | written by `message' as well as objects printed by `print', | ||
| 294 | `prin1' and `princ' to the echo area. Messages issued from C | ||
| 295 | code using the above mentioned functions will not be captured. | ||
| 293 | 296 | ||
| 294 | This is useful for separating the issuance of messages by the | 297 | This is useful for separating the issuance of messages by the |
| 295 | code under test from the behavior of the *Messages* buffer." | 298 | code 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'. | ||
| 321 | COLLECTOR will be called with the message before it is passed | ||
| 322 | to 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'. | ||
| 332 | The created advice function will just call the original function | ||
| 333 | unless the output is going to the echo area (when PRINTCHARFUN is | ||
| 334 | t or PRINTCHARFUN is nil and `standard-output' is t). If the | ||
| 335 | output is destined for the echo area, the advice function will | ||
| 336 | convert 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) |