]> git.notmuchmail.org Git - notmuch/blob - test/test-lib.el
test: New test for incremental search output parsing
[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 (defun notmuch-test-wait ()
39   "Wait for process completion."
40   (while (get-buffer-process (current-buffer))
41     (sleep-for 0.1)))
42
43 (defun test-output (&optional filename)
44   "Save current buffer to file FILENAME.  Default FILENAME is OUTPUT."
45   (write-region (point-min) (point-max) (or filename "OUTPUT")))
46
47 (defun test-visible-output (&optional filename)
48   "Save visible text in current buffer to file FILENAME.  Default
49 FILENAME is OUTPUT."
50   (let ((text (visible-buffer-string)))
51     (with-temp-file (or filename "OUTPUT") (insert text))))
52
53 (defun visible-buffer-string ()
54   "Same as `buffer-string', but excludes invisible text and
55 removes any text properties."
56   (visible-buffer-substring (point-min) (point-max)))
57
58 (defun visible-buffer-substring (start end)
59   "Same as `buffer-substring-no-properties', but excludes
60 invisible text."
61   (let (str)
62     (while (< start end)
63       (let ((next-pos (next-char-property-change start end)))
64         (when (not (invisible-p start))
65           (setq str (concat str (buffer-substring-no-properties
66                                  start next-pos))))
67         (setq start next-pos)))
68     str))
69
70 (defun orphan-watchdog (pid)
71   "Periodically check that the process with id PID is still
72 running, quit if it terminated."
73   (if (not (process-attributes pid))
74       (kill-emacs)
75     (run-at-time "1 min" nil 'orphan-watchdog pid)))
76
77 (defun hook-counter (hook)
78   "Count how many times a hook is called.  Increments
79 `hook'-counter variable value if it is bound, otherwise does
80 nothing."
81   (let ((counter (intern (concat (symbol-name hook) "-counter"))))
82     (if (boundp counter)
83         (set counter (1+ (symbol-value counter))))))
84
85 (defun add-hook-counter (hook)
86   "Add hook to count how many times `hook' is called."
87   (add-hook hook (apply-partially 'hook-counter hook)))
88
89 (add-hook-counter 'notmuch-hello-mode-hook)
90 (add-hook-counter 'notmuch-hello-refresh-hook)
91
92 (defadvice notmuch-search-process-filter (around pessimal activate disable)
93   "Feed notmuch-search-process-filter one character at a time."
94   (let ((string (ad-get-arg 1)))
95     (loop for char across string
96           do (progn
97                (ad-set-arg 1 (char-to-string char))
98                ad-do-it))))
99
100 (defmacro notmuch-test-run (&rest body)
101   "Evaluate a BODY of test expressions and output the result."
102   `(with-temp-buffer
103      (let ((buffer (current-buffer))
104            (result (progn ,@body)))
105        (switch-to-buffer buffer)
106        (insert (if (stringp result)
107                    result
108                  (prin1-to-string result)))
109        (test-output))))
110
111 (defun notmuch-test-report-unexpected (output expected)
112   "Report that the OUTPUT does not match the EXPECTED result."
113   (concat "Expect:\t" (prin1-to-string expected) "\n"
114           "Output:\t" (prin1-to-string output) "\n"))
115
116 (defun notmuch-test-expect-equal (output expected)
117   "Compare OUTPUT with EXPECTED. Report any discrepencies."
118   (if (equal output expected)
119       t
120     (cond
121      ((and (listp output)
122            (listp expected))
123       ;; Reporting the difference between two lists is done by
124       ;; reporting differing elements of OUTPUT and EXPECTED
125       ;; pairwise. This is expected to make analysis of failures
126       ;; simpler.
127       (apply #'concat (loop for o in output
128                             for e in expected
129                             if (not (equal o e))
130                             collect (notmuch-test-report-unexpected o e))))
131
132      (t
133       (notmuch-test-report-unexpected output expected)))))