forked from abo-abo/swiper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivy-test.el
523 lines (486 loc) · 16.9 KB
/
ivy-test.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
;;; ivy-test.el --- tests for ivy
;; Copyright (C) 2015-2017 Free Software Foundation, Inc.
;; Author: Oleh Krehel
;; This file is part of GNU Emacs.
;; This file 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, 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.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; This packages provides the tests for `ert'. They can be executed
;; from the command line as well by calling "make test".
;;; Code:
(require 'ert)
;; useful for #'ivy-read-remap. It must arrive before (require 'ivy)
(define-key global-map (kbd "<S-right>") #'end-of-buffer)
(require 'ivy)
(require 'counsel)
(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'.")
(defun ivy-eval ()
"Evaluate `ivy-expr'."
(interactive)
(setq ivy-result (eval ivy-expr)))
(global-set-key (kbd "C-c e") 'ivy-eval)
(defun ivy-with (expr keys)
"Evaluate EXPR followed by KEYS."
(let ((ivy-expr expr))
(execute-kbd-macro
(vconcat (kbd "C-c e")
(kbd keys)))
ivy-result))
(ert-deftest ivy-partial ()
(should (equal
(ivy-with '(ivy-read "test: " '("case" "Case"))
"ca TAB C-m")
"case"))
(should (equal
(ivy-with '(ivy-read "test: " '("case" "Case"))
"Ca TAB C-m")
"Case")))
(ert-deftest ivy-read ()
(should (equal
(ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
"C-m")
"blue"))
(should (equal
(ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
"y C-m")
"yellow"))
(should (equal
(ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
"y DEL b C-m")
"blue"))
(should (equal
(ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
"z C-m")
"z"))
(should (equal
(ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
"y <backspace> C-m")
"blue"))
(should (equal
(ivy-with '(let ((ivy-re-builders-alist '((t . ivy--regex-fuzzy))))
(ivy-read "pattern: " '("package-list-packages" "something-else")))
"plp C-m")
"package-list-packages"))
(should (equal
(ivy-with '(ivy-read "test" '("aaab" "aaac"))
"a C-n <tab> C-m")
"aaac"))
(should (equal
(ivy-with '(ivy-read "test" '(("foo" . "bar")))
"asdf C-m")
"asdf"))
(should (equal
(ivy-with
'(with-output-to-string
(ivy-read "test" '(("foo" . "bar"))
:action (lambda (x) (prin1 x))))
"f C-m")
"(#(\"foo\" 0 1 (idx 0)) . \"bar\")"))
(should (equal
(ivy-with
'(with-output-to-string
(ivy-read "test" '(("foo" . "bar"))
:action (lambda (x) (prin1 x))))
"asdf C-m")
"\"asdf\""))
(should (equal
(ivy-with '(ivy-read "pattern: " '("can do" "can" "can't do"))
"can C-m")
"can")))
(ert-deftest ivy-read-remap ()
(should (equal
(ivy-with '(ivy-read "pattern: " '("blue" "yellow" "red"))
"<S-right> C-m")
"red")))
(ert-deftest swiper--re-builder ()
(setq swiper--width 4)
(should (string= (swiper--re-builder "^")
"."))
(should (string= (swiper--re-builder "^a")
"^ ?\\(a\\)"))
(should (string= (swiper--re-builder "^a b")
"^ \\(a\\).*?\\(b\\)")))
(ert-deftest ivy--split ()
(should (equal (ivy--split "King of the who?")
'("King" "of" "the" "who?")))
(should (equal (ivy--split "The Brittons.")
'("The Brittons.")))
(should (equal (ivy--split "Who are the Brittons?")
'("Who are" "the Brittons?")))
(should (equal (ivy--split "We're all Britons and I am your king.")
'("We're all Britons"
"and I am"
"your king.")))
(should (equal (ivy--split "^[^ ]") '("^[^ ]")))
(should (equal (ivy--split "^[^ ] bar") '("^[^ ]" "bar"))))
(ert-deftest ivy--regex ()
(should (equal (ivy--regex
"\\(?:interactive\\|swiper\\) \\(?:list\\|symbol\\)")
"\\(\\(?:interactive\\|swiper\\)\\).*?\\(\\(?:list\\|symbol\\)\\)")))
(ert-deftest ivy--regex-fuzzy ()
(should (string= (ivy--regex-fuzzy "tmux")
"\\(t\\).*?\\(m\\).*?\\(u\\).*?\\(x\\)"))
(should (string= (ivy--regex-fuzzy "^tmux")
"^\\(t\\).*?\\(m\\).*?\\(u\\).*?\\(x\\)"))
(should (string= (ivy--regex-fuzzy "^tmux$")
"^\\(t\\).*?\\(m\\).*?\\(u\\).*?\\(x\\)$"))
(should (string= (ivy--regex-fuzzy "")
""))
(should (string= (ivy--regex-fuzzy "^")
"^"))
(should (string= (ivy--regex-fuzzy "$")
"$")))
(ert-deftest ivy--regex-ignore-order ()
(should (equal (ivy--regex-ignore-order "tmux")
'(("tmux" . t))))
(should (equal (ivy--regex-ignore-order "^tmux")
'(("^tmux" . t))))
(should (equal (ivy--regex-ignore-order "^tmux$")
'(("^tmux$" . t))))
(should (equal (ivy--regex-ignore-order "")
""))
(should (equal (ivy--regex-ignore-order "^")
'(("^" . t))))
(should (equal (ivy--regex-ignore-order "$")
'(("$" . t))))
(should (equal (ivy--regex-ignore-order "one two")
'(("one" . t) ("two" . t))))
(should (equal (ivy--regex-ignore-order "one two !three")
'(("one" . t) ("two" . t) ("three"))))
(should (equal (ivy--regex-ignore-order "one two !three four")
'(("one" . t) ("two" . t) ("three") ("four"))))
(should (equal (ivy--regex-ignore-order "!three four")
'(("" . t) (("three") ("four")))))
(should (equal (ivy--regex-ignore-order "foo[ bar[xy]")
'(("foo\\[" . t) ("bar[xy]" . t)))))
(ert-deftest ivy--format ()
(should (string= (let ((ivy--index 10)
(ivy-format-function (lambda (x) (mapconcat #'identity x "\n")))
(cands '("NAME"
"SYNOPSIS"
"DESCRIPTION"
"FUNCTION LETTERS"
"SWITCHES"
"DIAGNOSTICS"
"EXAMPLE 1"
"EXAMPLE 2"
"EXAMPLE 3"
"SEE ALSO"
"AUTHOR")))
(ivy--format cands))
#("\nDESCRIPTION\nFUNCTION LETTERS\nSWITCHES\nDIAGNOSTICS\nEXAMPLE 1\nEXAMPLE 2\nEXAMPLE 3\nSEE ALSO\nAUTHOR"
0 90 (read-only nil)
90 96 (face ivy-current-match read-only nil)))))
(ert-deftest ivy--filter ()
(setq ivy-last (make-ivy-state))
(should (equal (ivy--filter "the" '("foo" "the" "The"))
'("the" "The")))
(should (equal (ivy--filter "The" '("foo" "the" "The"))
'("The"))))
(ert-deftest counsel-unquote-regex-parens ()
(should (equal (counsel-unquote-regex-parens
(ivy--regex "foo bar"))
"(foo).*?(bar)"))
(should (equal (counsel-unquote-regex-parens
(ivy--regex "(foo bar"))
"(\\(foo).*?(bar)")))
(ert-deftest colir-color-parse ()
(should (equal (colir-color-parse "#ab1234")
;; (color-name-to-rgb "#ab1234")
'(0.6705882352941176
0.07058823529411765
0.20392156862745098))))
;;* prefix arg tests
;;** tests with no prefix
(ert-deftest ivy-no-prefix-arg ()
"Tests with no prefix arg."
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"C-m")
nil))
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"C-j")
nil))
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"C-M-j")
nil))
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"C-M-m")
nil))
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"C-M-n")
nil))
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"C-M-p")
nil))
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"M-o o")
nil))
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"TAB TAB")
nil)))
;;** tests with one prefix
(ert-deftest ivy-one-prefix-arg ()
"Tests with no prefix arg."
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"C-u C-m")
'(4)))
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"C-u C-j")
'(4)))
;; C-M-j does not pass a prefix on.
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"C-u C-M-j")
nil))
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"C-u C-M-m")
'(4)))
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"C-u C-M-n")
'(4)))
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"C-u C-M-p")
'(4)))
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"C-u M-o o")
'(4)))
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action
'(1 ("o" (lambda (x)
(setq res ivy-current-prefix-arg)))
("p" (lambda (x)
(setq res ivy-current-prefix-arg)))))
res)
"C-u M-o p")
'(4)))
;; TAB TAB does not pass prefix arg
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"TAB TAB")
nil)))
(ert-deftest ivy-numeric-prefix-arg ()
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"M-1 M-2 M-3 C-m")
123))
(should (equal
(ivy-with
'(let (res)
(ivy-read "pattern: " '("blue" "yellow")
:action (lambda (x)
(setq res ivy-current-prefix-arg)))
res)
"C-u 123 C-m")
123)))
(ert-deftest ivy-re-match ()
(should (ivy-re-match '(("counsel" . t)) "(defun counsel"))
(should (ivy-re-match '(("defun" . t) ("counsel" . t)) "(defun counsel"))
(should (ivy-re-match '(("counsel" . t) ("defun" . t)) "(defun counsel"))
(should (not (ivy-re-match '(("counsel" . nil) ("defun" . t)) "(defun counsel")))
(should (not (ivy-re-match '(("defun" . t) ("counsel" . nil)) "(defun counsel"))))
(ert-deftest ivy-read-preselect ()
(should (equal
(ivy-with
'(ivy-read "test: "
(list "abc" "default" "def")
:preselect 1)
"RET")
"default"))
(should (equal
(ivy-with
'(ivy-read "test: "
(list "abc" "default" "def")
:preselect "defa")
"RET")
"default")))
(ert-deftest ivy-read-prompt ()
(let ((prompt "pattern: ")
(collection '("blue" "yellow")))
(should (equal
(ivy-with
'(let ((ivy-use-selectable-prompt nil))
(ivy-read prompt collection))
"bl C-m")
"blue"))
(should (equal
(ivy-with
'(let ((ivy-use-selectable-prompt nil))
(ivy-read prompt collection))
"bl C-p C-m")
"blue"))
(should (equal
(ivy-with
'(let ((ivy-use-selectable-prompt nil))
(ivy-read prompt collection))
"bl C-j")
"blue"))
(should (equal
(ivy-with
'(let ((ivy-use-selectable-prompt nil))
(ivy-read prompt collection))
"bl C-p C-j")
"blue"))
(should (equal
(ivy-with
'(let ((ivy-use-selectable-prompt nil))
(ivy-read prompt collection))
"bl C-M-j")
"bl"))
(should (equal
(ivy-with
'(let ((ivy-use-selectable-prompt nil))
(ivy-read prompt collection))
"bl C-p C-M-j")
"bl"))
(should (equal
(ivy-with
'(let ((ivy-use-selectable-prompt t))
(ivy-read prompt collection))
"bl C-m")
"blue"))
(should (equal
(ivy-with
'(let ((ivy-use-selectable-prompt t))
(ivy-read prompt collection))
"bl C-p C-m")
"bl"))
(should (equal
(ivy-with
'(let ((ivy-use-selectable-prompt t))
(ivy-read prompt collection))
"bl C-j")
"blue"))
(should (equal
(ivy-with
'(let ((ivy-use-selectable-prompt t))
(ivy-read prompt collection))
"bl C-p C-j")
"bl"))
(should (equal
(ivy-with
'(let ((ivy-use-selectable-prompt t))
(ivy-read prompt collection))
"bl C-M-j")
"bl"))
(should (equal
(ivy-with
'(let ((ivy-use-selectable-prompt t))
(ivy-read prompt collection))
"bl C-p C-M-j")
"bl"))))
(provide 'ivy-test)
;;; ivy-test.el ends here