forked from abo-abo/swiper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivy-test.el
162 lines (143 loc) · 5.86 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
;;; ivy-test.el --- tests for ivy
;; Copyright (C) 2015 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/>.
(require 'ert)
(require 'ivy)
(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-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 "pattern: " '("can do" "can" "can't do"))
"can C-m")
"can")))
(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."))))
(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"))))))
(ert-deftest ivy--format ()
(should (string= (let ((ivy--index 10)
(ivy-format-function (lambda (x) (mapconcat (lambda (y) (car y)) 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"))))