]> git.notmuchmail.org Git - notmuch/blob - test/test-lib.el
test: Add `test_emacs_expect_t'.
[notmuch] / test / test-lib.el
1 ;; test-lib.el --- auxiliary stuff for Notmuch Emacs tests.
2 ;;
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
5 ;;
6 ;; This file is part of Notmuch test suit.
7 ;;
8 ;; Notmuch is free software: you can redistribute it and/or modify it
9 ;; under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12 ;;
13 ;; Notmuch is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ;; General Public License for more details.
17 ;;
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.
20 ;;
21 ;; Authors: Dmitry Kurochkin <dmitry.kurochkin@gmail.com>
22
23 ;; `read-file-name' by default uses `completing-read' function to read
24 ;; user input.  It does not respect `standard-input' variable which we
25 ;; use in tests to provide user input.  So replace it with a plain
26 ;; `read' call.
27 (setq read-file-name-function (lambda (&rest _) (read)))
28
29 ;; Work around a bug in emacs 23.1 and emacs 23.2 which prevents
30 ;; noninteractive (kill-emacs) from emacsclient.
31 (if (and (= emacs-major-version 23) (< emacs-minor-version 3))
32   (defadvice kill-emacs (before disable-yes-or-no-p activate)
33     "Disable yes-or-no-p before executing kill-emacs"
34     (defun yes-or-no-p (prompt) t)))
35
36 (defun notmuch-test-wait ()
37   "Wait for process completion."
38   (while (get-buffer-process (current-buffer))
39     (sleep-for 0.1)))
40
41 (defun test-output (&optional filename)
42   "Save current buffer to file FILENAME.  Default FILENAME is OUTPUT."
43   (write-region (point-min) (point-max) (or filename "OUTPUT")))
44
45 (defun test-visible-output (&optional filename)
46   "Save visible text in current buffer to file FILENAME.  Default
47 FILENAME is OUTPUT."
48   (let ((text (visible-buffer-string)))
49     (with-temp-file (or filename "OUTPUT") (insert text))))
50
51 (defun visible-buffer-string ()
52   "Same as `buffer-string', but excludes invisible text."
53   (visible-buffer-substring (point-min) (point-max)))
54
55 (defun visible-buffer-substring (start end)
56   "Same as `buffer-substring', but excludes invisible text."
57   (let (str)
58     (while (< start end)
59       (let ((next-pos (next-char-property-change start end)))
60         (when (not (invisible-p start))
61           (setq str (concat str (buffer-substring start next-pos))))
62         (setq start next-pos)))
63     str))
64
65 (defun orphan-watchdog (pid)
66   "Periodically check that the process with id PID is still
67 running, quit if it terminated."
68   (if (not (process-attributes pid))
69       (kill-emacs)
70     (run-at-time "1 min" nil 'orphan-watchdog pid)))
71
72 (defun hook-counter (hook)
73   "Count how many times a hook is called.  Increments
74 `hook'-counter variable value if it is bound, otherwise does
75 nothing."
76   (let ((counter (intern (concat (symbol-name hook) "-counter"))))
77     (if (boundp counter)
78         (set counter (1+ (symbol-value counter))))))
79
80 (defun add-hook-counter (hook)
81   "Add hook to count how many times `hook' is called."
82   (add-hook hook (apply-partially 'hook-counter hook)))
83
84 (add-hook-counter 'notmuch-hello-mode-hook)
85 (add-hook-counter 'notmuch-hello-refresh-hook)
86
87 (defmacro notmuch-test-run (&rest body)
88   "Evaluate a BODY of test expressions and output the result."
89   `(with-temp-buffer
90      (let ((result (progn ,@body)))
91        (insert (if (stringp result)
92                    result
93                  (prin1-to-string result)))
94        (test-output))))