-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounsel-mairix-tests.el
294 lines (243 loc) · 10.9 KB
/
counsel-mairix-tests.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
;;; counsel-mairix.el --- Counsel interface for Mairix -*- lexical-binding: t -*-
;; Copyright (c) 2020 Antoine Kalmbach
;; Author: Antoine Kalmbach <[email protected]>
;; Created: 2020-10-10
;; Version: 0.1
;; Keywords: mail searching
;; Package-Requires: ((emacs "26.1") (ivy "0.13.1"))
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Tests for counsel-mairix. The ivy- bits are copyright (c) 2015-2019 Oleh
;; Krehel.
;;; Code:
(require 'ert)
(require 'ert-x)
(require 'seq)
(require 'mairix)
(require 'counsel-mairix)
;;; From https://github.com/abo-abo/swiper/blob/master/ivy-test.el
(defvar ivy-expr nil
"Holds a test expression to evaluate with `ivy-eval'.")
(defvar ivy-result nil
"Holds the eval result of `ivy-expr' by `ivy-eval'.")
(defvar ivy-eval-dir nil
"Hold the `default-directory' value to be used by `ivy-eval'.
Since `execute-kbd-macro' doesn't pick up a let-bound `default-directory'.")
(defun ivy-eval ()
"Evaluate `ivy-expr'."
(interactive)
(let ((default-directory (or ivy-eval-dir default-directory)))
(setq ivy-result (eval ivy-expr))))
(global-set-key (kbd "C-c e") 'ivy-eval)
(defvar ivy-test-inhibit-message t)
(cl-defun ivy-with (expr keys &key dir)
"Evaluate EXPR followed by KEYS."
(let ((ivy-expr expr)
(inhibit-message ivy-test-inhibit-message)
(buf (current-buffer)))
(save-window-excursion
(unwind-protect
(progn
(when dir
(setq dir (expand-file-name dir)))
(setq ivy-eval-dir dir)
(execute-kbd-macro
(vconcat (kbd "C-c e")
(kbd keys))))
(switch-to-buffer buf)))
ivy-result))
(defun ivy-input (expr keys)
"Return `ivy-text' after inputing KEYS."
(let ((ivy-expr expr)
(buf (current-buffer)))
(save-window-excursion
(unwind-protect
(execute-kbd-macro
(vconcat (kbd "C-c e")
(kbd keys)))
(switch-to-buffer buf)))
ivy-text))
;;; Some basic tests to ensure mairix is working correctly.
(ert-deftest test-mairix-presence ()
"Test whether mairix is installed on the system."
(should (not (null (executable-find "mairix")))))
(ert-deftest test-packages ()
"Test whether counsel and mairix are installed."
(should (and (featurep 'ivy)
(featurep 'mairix))))
(ert-deftest test-implementation-detection ()
"Test whether implementation detection works correctly."
(dolist (impl '(rmail gnus vm foo))
(let ((mairix-mail-program impl))
(should (eql impl (counsel-mairix-determine-frontend))))))
;;; Test fixtures and the like.
(defmacro with-test-mairix (&rest body)
(declare (indent 0) (debug t))
`(let ((saved mairix-saved-searches))
(unwind-protect
(progn
(let ((counsel-mairix-include-threads t)
(mairix-command (format "mairix -f %s" (expand-file-name "tests/mairixrc")))
(mairix-search-file "test.mbox"))
,@body))
(when-let (k (get-buffer "test.mbox"))
(kill-buffer k))
(when (file-exists-p "test.mbox")
(delete-file file))
(setq mairix-saved-searches saved))))
(ert-deftest test-implementation-override ()
"Test whether implementation override works correctly."
(let ((mairix-mail-program 'foobar))
(eql 'foobar (counsel-mairix-determine-frontend))))
(ert-deftest test-baseline-mairix-search ()
"Test whether our 'standard query' works as expected."
(ert-with-message-capture msgs
(with-test-mairix
;; you'd think the word 'lisp' would'be mentioned more than just 187 times in
;; one month on emacs-devel, huh?
(mairix-search "lisp" t))
(should (cl-search "Matched 187 messages" msgs))))
(ert-deftest test-counsel-mairix-threads-customization ()
"Test whether changing `counsel-mairix-include-threads' affects the behavior
of `counsel-mairix'."
(with-test-mairix
(let ((counsel-mairix-include-threads nil))
(let ((result (ivy-with '(call-interactively 'counsel-mairix) "melpa")))
(should (equal 74 (seq-length result)))
(should (cl-search "pogonyshev" result))))
(let ((counsel-mairix-include-threads t))
(let ((result (ivy-with '(call-interactively 'counsel-mairix) "elpa")))
(should (equal 81 (seq-length result)))
(should (cl-search "monnier" result))))))
(ert-deftest test-counsel-mairix-prompt-answer ()
"Test whether the answering y or n at the prompt actually does anything."
(with-test-mairix
(let* ((searchable (if noninteractive " C-m lisp" "lisp"))
(yes (concat "y" searchable))
(no (concat "n" searchable))
(counsel-mairix-include-threads 'prompt)
(ivy-test-inhibit-message t))
(should (equal 88 (seq-length (ivy-with '(call-interactively 'counsel-mairix) no))))
(should (equal 109 (seq-length (ivy-with '(call-interactively 'counsel-mairix) yes)))))))
(ert-deftest test-counsel-mairix-history ()
"Test calling `counsel-mairix-save-search' should prompt for
the recently used searches."
(with-test-mairix
(let ((counsel-mairix-include-threads nil)
(counsel-mairix-history nil))
(ivy-with '(call-interactively 'counsel-mairix) "elpa C-m")
(ivy-with '(call-interactively 'counsel-mairix) "f:rms C-m")
(should (equal (ivy-with '(counsel-mairix-save-search) "C-m foo C-m y")
"elpa"))
(should (equal (ivy-with '(counsel-mairix-save-search) "C-n C-m bar C-m y")
"f:rms")))))
(ert-deftest test-counsel-mairix-yanking ()
(with-test-mairix
(save-window-excursion
(rmail "./tests/feb-2020-emacs-devel.mbox")
(let ((from (mail-strip-quoted-names (rmail-get-header "from")))
(to (mail-strip-quoted-names (rmail-get-header "to")))
(mid (mail-strip-quoted-names (rmail-get-header "message-id")))
(rms "[email protected]")
(asdf "[email protected]")
(mmid "[email protected]"))
(should (string= from rms))
(should (string= to asdf))
(should (string= mid mmid))
(should (equal from (counsel-mairix--get-field "from")))
(should (equal to (counsel-mairix--get-field "to")))
(should (equal mid (counsel-mairix--get-field "message-id")))
(should (equal (ivy-input '(counsel-mairix) "C-c C-f f")
rms))
(should (equal (ivy-input '(counsel-mairix) "C-c C-f t")
asdf))
(should (equal (ivy-input '(counsel-mairix) "C-c C-f i")
mmid))
(should (equal (ivy-input '(counsel-mairix) "C-u C-c C-f f")
(concat "f:" rms)))
(should (equal (ivy-input '(counsel-mairix) "C-u C-c C-f t")
(concat "t:" asdf)))
(should (equal (ivy-input '(counsel-mairix) "C-u C-c C-f i")
(concat "m:" mmid)))))))
(ert-deftest test-counsel-mairix-insert-saved-searches ()
(with-test-mairix
(let ((mairix-saved-searches '(("a" . "s:elpa") ("b" . "f:[email protected]"))))
(should (equal (ivy-input '(call-interactively 'counsel-mairix)
"s:elpa")
"s:elpa"))
(should (equal (ivy-input '(call-interactively 'counsel-mairix)
"f:[email protected]")
"f:[email protected]")))))
(ert-deftest test-counsel-mairix-save-current-search ()
(with-test-mairix
(let ((mairix-saved-searches nil))
(ivy-with '(counsel-mairix) "s:elpa C-c C-s s foo C-m y")
(should (equal '(("foo" . "s:elpa")) mairix-saved-searches)))))
(ert-deftest test-counsel-mairix-search-from ()
(with-test-mairix
(rmail "./tests/feb-2020-emacs-devel.mbox")
(should (equal (ivy-input '(call-interactively 'counsel-mairix-search-from) "")
"f:[email protected]"))))
(ert-deftest test-counsel-mairix-search-thread ()
(with-test-mairix
(rmail "./tests/feb-2020-emacs-devel.mbox")
(should (equal (ivy-input '(call-interactively 'counsel-mairix-search-thread) "")
"m:[email protected]"))))
(when (featurep 'avy)
(ert-deftest test-counsel-mairix-avy-subject ()
(with-test-mairix
(rmail "./tests/feb-2020-emacs-devel.mbox")
(should (equal (ivy-input '(counsel-mairix) "C-c C-a s s")
"Support"))
(should (equal (ivy-input '(counsel-mairix) "C-u C-c C-a s s")
"s:Support"))
(should (equal (ivy-input '(counsel-mairix) "M-- C-c C-a s s")
"s:~Support"))
(should (equal (ivy-input '(counsel-mairix) "C-u C-c C-a s s C-c C-a s d")
"s:Support,for"))
(should (equal (ivy-input '(counsel-mairix) "C-u C-c C-a s s SPC C-u C-c C-a s d")
"s:Support s:for"))
(should (equal (ivy-input '(counsel-mairix) "C-u C-c C-a s s SPC M-- C-c C-a s d")
"s:Support s:~for"))))
(ert-deftest test-counsel-mairix-avy-cc ()
(with-test-mairix
(rmail "./tests/feb-2020-emacs-devel.mbox")
(should (equal (ivy-input '(counsel-mairix) "C-c C-a c d")
(should (equal (ivy-input '(counsel-mairix) "C-u C-c C-a c d")
"c:[email protected]"))
(should (equal (ivy-input '(counsel-mairix) "M-- C-c C-a c d")
"c:[email protected]"))
(should (equal (ivy-input '(counsel-mairix) "C-u C-c C-a c d C-c C-a c s")
(should (equal (ivy-input '(counsel-mairix) "C-u C-c C-a c d SPC C-u C-c C-a c s")
"c:[email protected] c:[email protected]"))
(should (equal (ivy-input '(counsel-mairix) "C-u C-c C-a c d SPC M-- C-c C-a c s")
"c:[email protected] c:[email protected]"))))
;; it's the same macro... bit pointless?
(ert-deftest test-counsel-mairix-avy-from ()
(with-test-mairix
(rmail "./tests/feb-2020-emacs-devel.mbox")
(should (equal (ivy-input '(counsel-mairix) "C-c C-a f")
"[email protected]"))))
(ert-deftest test-counsel-mairix-avy-word ()
(with-test-mairix
(rmail "./tests/feb-2020-emacs-devel.mbox")
(should (equal (ivy-input '(counsel-mairix) "C-c C-a b g f")
"Snowden's"))))
)
;; Local Variables:
;; checkdoc-force-docstrings-flag: nil
;; End:
(provide 'counsel-mairix-tests)
;;; counsel-mairix-tests.el ends here.