GNU bug report logs -
#24939
[PATCH] Add tests for lisp/kmacro.el
Previous Next
Reported by: Gemini Lasswell <gazally <at> runbox.com>
Date: Sun, 13 Nov 2016 21:25:01 UTC
Severity: wishlist
Tags: patch
Done: Eli Zaretskii <eliz <at> gnu.org>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
Eli Zaretskii <eliz <at> gnu.org> writes:
>> >> +(defmacro kmacro-tests-should-match-message (value &rest body)
>> >
>> > I don't like this implementation.
>>
>> This strategy is used in autorevert-tests.el and filenotify-tests.el,
>> which is where I copied it from. So those should be changed too.
>
> Most probably. But let's first see what better implementation we
> could come up with.
>
Here is an implementation of message capturing using advice:
(defmacro ert-with-message-capture (var &rest body)
"Execute BODY while collecting messages in VAR.
Capture all messages produced by `message' when it is called from
Lisp, and concatenate them separated by newlines into one string."
(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)))))
The reason I have set it up to bind a variable during the code body
execution is to make it usable by autorevert-tests.el and
filenotify-tests.el. For example, here's an excerpt from
filenotify-tests.el:
(with-current-buffer (get-buffer-create "*Messages*")
(narrow-to-region (point-max) (point-max)))
(sleep-for 1)
(write-region
"another text" nil file-notify--test-tmpfile nil 'no-message)
;; Check, that the buffer has been reverted.
(with-current-buffer (get-buffer-create "*Messages*")
(file-notify--wait-for-events
timeout
(string-match
(format-message "Reverting buffer `%s'." (buffer-name buf))
(buffer-string))))
file-notify--wait-for-events polls read-event in a loop with a
fractional timeout until the form that is its second argument becomes
non-nil, or the larger timeout expires. With the macro above, this
excerpt becomes:
(ert-with-message-capture captured-messages
(sleep-for 1)
(write-region
"another text" nil file-notify--test-tmpfile nil 'no-message)
;; Check, that the buffer has been reverted.
(file-notify--wait-for-events
timeout
(string-match
(format-message "Reverting buffer `%s'." (buffer-name buf))
captured-messages)))
Let me know what you think.
This bug report was last modified 8 years and 159 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.