]> git.notmuchmail.org Git - notmuch/blob - test/test-lib.el
dece811e6136064b84afec343161c1e25f5c9dec
[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 (require 'cl)   ;; This code is generally used uncompiled.
24
25 ;; `read-file-name' by default uses `completing-read' function to read
26 ;; user input.  It does not respect `standard-input' variable which we
27 ;; use in tests to provide user input.  So replace it with a plain
28 ;; `read' call.
29 (setq read-file-name-function (lambda (&rest _) (read)))
30
31 ;; Work around a bug in emacs 23.1 and emacs 23.2 which prevents
32 ;; noninteractive (kill-emacs) from emacsclient.
33 (if (and (= emacs-major-version 23) (< emacs-minor-version 3))
34   (defadvice kill-emacs (before disable-yes-or-no-p activate)
35     "Disable yes-or-no-p before executing kill-emacs"
36     (defun yes-or-no-p (prompt) t)))
37
38 ;; Emacs bug #2930:
39 ;;      23.0.92; `accept-process-output' and `sleep-for' do not run sentinels
40 ;; seems to be present in Emacs 23.1.
41 ;; Running `list-processes' after `accept-process-output' seems to work
42 ;; around this problem.
43 (if (and (= emacs-major-version 23) (= emacs-minor-version 1))
44   (defadvice accept-process-output (after run-list-processes activate)
45     "run list-processes after executing accept-process-output"
46     (list-processes)))
47
48 (defun notmuch-test-wait ()
49   "Wait for process completion."
50   (while (get-buffer-process (current-buffer))
51     (accept-process-output nil 0.1)))
52
53 (defun test-output (&optional filename)
54   "Save current buffer to file FILENAME.  Default FILENAME is OUTPUT."
55   (write-region (point-min) (point-max) (or filename "OUTPUT")))
56
57 (defun test-visible-output (&optional filename)
58   "Save visible text in current buffer to file FILENAME.  Default
59 FILENAME is OUTPUT."
60   (let ((text (visible-buffer-string)))
61     (with-temp-file (or filename "OUTPUT") (insert text))))
62
63 (defun visible-buffer-string ()
64   "Same as `buffer-string', but excludes invisible text and
65 removes any text properties."
66   (visible-buffer-substring (point-min) (point-max)))
67
68 (defun visible-buffer-substring (start end)
69   "Same as `buffer-substring-no-properties', but excludes
70 invisible text."
71   (let (str)
72     (while (< start end)
73       (let ((next-pos (next-char-property-change start end)))
74         (when (not (invisible-p start))
75           (setq str (concat str (buffer-substring-no-properties
76                                  start next-pos))))
77         (setq start next-pos)))
78     str))
79
80 (defun orphan-watchdog (pid)
81   "Periodically check that the process with id PID is still
82 running, quit if it terminated."
83   (if (not (process-attributes pid))
84       (kill-emacs)
85     (run-at-time "1 min" nil 'orphan-watchdog pid)))
86
87 (defun hook-counter (hook)
88   "Count how many times a hook is called.  Increments
89 `hook'-counter variable value if it is bound, otherwise does
90 nothing."
91   (let ((counter (intern (concat (symbol-name hook) "-counter"))))
92     (if (boundp counter)
93         (set counter (1+ (symbol-value counter))))))
94
95 (defun add-hook-counter (hook)
96   "Add hook to count how many times `hook' is called."
97   (add-hook hook (apply-partially 'hook-counter hook)))
98
99 (add-hook-counter 'notmuch-hello-mode-hook)
100 (add-hook-counter 'notmuch-hello-refresh-hook)
101
102 (defadvice notmuch-search-process-filter (around pessimal activate disable)
103   "Feed notmuch-search-process-filter one character at a time."
104   (let ((string (ad-get-arg 1)))
105     (loop for char across string
106           do (progn
107                (ad-set-arg 1 (char-to-string char))
108                ad-do-it))))
109
110 (defun notmuch-test-mark-links ()
111   "Enclose links in the current buffer with << and >>."
112   ;; Links are often created by jit-lock functions
113   (jit-lock-fontify-now)
114   (save-excursion
115     (let ((inhibit-read-only t))
116       (goto-char (point-min))
117       (let ((button))
118         (while (setq button (next-button (point)))
119           (goto-char (button-start button))
120           (insert "<<")
121           (goto-char (button-end button))
122           (insert ">>"))))))
123
124 (defmacro notmuch-test-run (&rest body)
125   "Evaluate a BODY of test expressions and output the result."
126   `(with-temp-buffer
127      (let ((buffer (current-buffer))
128            (result (progn ,@body)))
129        (switch-to-buffer buffer)
130        (insert (if (stringp result)
131                    result
132                  (prin1-to-string result)))
133        (test-output))))
134
135 (defun notmuch-test-report-unexpected (output expected)
136   "Report that the OUTPUT does not match the EXPECTED result."
137   (concat "Expect:\t" (prin1-to-string expected) "\n"
138           "Output:\t" (prin1-to-string output) "\n"))
139
140 (defun notmuch-test-expect-equal (output expected)
141   "Compare OUTPUT with EXPECTED. Report any discrepencies."
142   (if (equal output expected)
143       t
144     (cond
145      ((and (listp output)
146            (listp expected))
147       ;; Reporting the difference between two lists is done by
148       ;; reporting differing elements of OUTPUT and EXPECTED
149       ;; pairwise. This is expected to make analysis of failures
150       ;; simpler.
151       (apply #'concat (loop for o in output
152                             for e in expected
153                             if (not (equal o e))
154                             collect (notmuch-test-report-unexpected o e))))
155
156      (t
157       (notmuch-test-report-unexpected output expected)))))