New macro 'ert-with-message-capture'

* lisp/emacs-lisp/ert-x.el (ert-with-message-capture): New macro.
(Bug#25158)

* test/lisp/autorevert-tests.el (auto-revert--wait-for-revert)
(auto-revert-test00-auto-revert-mode)
(auto-revert-test01-auto-revert-several-files)
(auto-revert-test02-auto-revert-deleted-file)
(auto-revert-test03-auto-revert-tail-mode)
(auto-revert-test04-auto-revert-mode-dired):
* test/lisp/filenotify-tests.el (file-notify-test03-autorevert): Use
ert-with-message-capture.
This commit is contained in:
Gemini Lasswell 2017-02-04 13:36:43 +02:00 committed by Eli Zaretskii
parent 8ba27b7ce2
commit ef3d8d6f72
3 changed files with 123 additions and 119 deletions

View file

@ -285,6 +285,30 @@ BUFFER defaults to current buffer. Does not modify BUFFER."
(kill-buffer clone)))))))
(defmacro ert-with-message-capture (var &rest body)
"Execute BODY while collecting anything written with `message' in VAR.
Capture all messages produced by `message' when it is called from
Lisp, and concatenate them separated by newlines into one string.
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-advice (cl-gensym)))
`(let* ((,var "")
(,g-advice (lambda (func &rest args)
(if (or (null args) (equal (car args) ""))
(apply func args)
(let ((msg (apply #'format-message args)))
(setq ,var (concat ,var msg "\n"))
(funcall func "%s" msg))))))
(advice-add 'message :around ,g-advice)
(unwind-protect
(progn ,@body)
(advice-remove 'message ,g-advice)))))
(provide 'ert-x)
;;; ert-x.el ends here