]> code.delx.au - gnu-emacs-elpa/blob - packages/ivy/ivy-test.el
Merge commit '0cda39255827f283e7578cd469ae42daad9556a2' from js2-mode
[gnu-emacs-elpa] / packages / ivy / ivy-test.el
1 ;;; ivy-test.el --- tests for ivy
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Oleh Krehel
6
7 ;; This file is part of GNU Emacs.
8
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
12 ;; any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; For a full copy of the GNU General Public License
20 ;; see <http://www.gnu.org/licenses/>.
21
22 (require 'ert)
23
24 ;; useful for #'ivy-read-remap. It must arrive before (require 'ivy)
25 (define-key global-map (kbd "<S-right>") #'end-of-buffer)
26
27 (require 'ivy)
28 (require 'counsel)
29
30 (defvar ivy-expr nil
31 "Holds a test expression to evaluate with `ivy-eval'.")
32
33 (defvar ivy-result nil
34 "Holds the eval result of `ivy-expr' by `ivy-eval'.")
35
36 (defun ivy-eval ()
37 "Evaluate `ivy-expr'."
38 (interactive)
39 (setq ivy-result (eval ivy-expr)))
40
41 (global-set-key (kbd "C-c e") 'ivy-eval)
42
43 (defun ivy-with (expr keys)
44 "Evaluate EXPR followed by KEYS."
45 (let ((ivy-expr expr))
46 (execute-kbd-macro
47 (vconcat (kbd "C-c e")
48 (kbd keys)))
49 ivy-result))
50
51 (ert-deftest ivy-read ()
52 (should (equal
53 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
54 "C-m")
55 "blue"))
56 (should (equal
57 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
58 "y C-m")
59 "yellow"))
60 (should (equal
61 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
62 "y DEL b C-m")
63 "blue"))
64 (should (equal
65 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
66 "z C-m")
67 "z"))
68 (should (equal
69 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
70 "y <backspace> C-m")
71 "blue"))
72 (should (equal
73 (ivy-with '(let ((ivy-re-builders-alist '((t . ivy--regex-fuzzy))))
74 (ivy-read "pattern: " '("package-list-packages" "something-else")))
75 "plp C-m")
76 "package-list-packages"))
77 (should (equal
78 (ivy-with '(ivy-read "test" '("aaab" "aaac"))
79 "a C-n <tab> C-m")
80 "aaac"))
81 (should (equal
82 (ivy-with '(ivy-read "test" '(("foo" . "bar")))
83 "asdf C-m")
84 "asdf"))
85 (should (equal
86 (ivy-with
87 '(with-output-to-string
88 (ivy-read "test" '(("foo" . "bar"))
89 :action (lambda (x) (prin1 x))))
90 "f C-m")
91 "\"bar\""))
92 (should (equal
93 (ivy-with
94 '(with-output-to-string
95 (ivy-read "test" '(("foo" . "bar"))
96 :action (lambda (x) (prin1 x))))
97 "asdf C-m")
98 "\"asdf\""))
99 (should (equal
100 (ivy-with '(ivy-read "pattern: " '("can do" "can" "can't do"))
101 "can C-m")
102 "can")))
103
104 (ert-deftest ivy-read-remap ()
105 (should (equal
106 (ivy-with '(ivy-read "pattern: " '("blue" "yellow" "red"))
107 "<S-right> C-m")
108 "red")))
109
110 (ert-deftest swiper--re-builder ()
111 (setq swiper--width 4)
112 (should (string= (swiper--re-builder "^")
113 "."))
114 (should (string= (swiper--re-builder "^a")
115 "^ ?\\(a\\)"))
116 (should (string= (swiper--re-builder "^a b")
117 "^ \\(a\\).*?\\(b\\)")))
118
119 (ert-deftest ivy--split ()
120 (should (equal (ivy--split "King of the who?")
121 '("King" "of" "the" "who?")))
122 (should (equal (ivy--split "The Brittons.")
123 '("The Brittons.")))
124 (should (equal (ivy--split "Who are the Brittons?")
125 '("Who are" "the Brittons?")))
126 (should (equal (ivy--split "We're all Britons and I am your king.")
127 '("We're all Britons"
128 "and I am"
129 "your king.")))
130 (should (equal (ivy--split "^[^ ]") '("^[^ ]")))
131 (should (equal (ivy--split "^[^ ] bar") '("^[^ ]" "bar"))))
132
133 (ert-deftest ivy--regex ()
134 (should (equal (ivy--regex
135 "\\(?:interactive\\|swiper\\) \\(?:list\\|symbol\\)")
136 "\\(\\(?:interactive\\|swiper\\)\\).*?\\(\\(?:list\\|symbol\\)\\)")))
137
138 (ert-deftest ivy--regex-fuzzy ()
139 (should (string= (ivy--regex-fuzzy "tmux")
140 "\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)"))
141 (should (string= (ivy--regex-fuzzy "^tmux")
142 "^\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)"))
143 (should (string= (ivy--regex-fuzzy "^tmux$")
144 "^\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)$"))
145 (should (string= (ivy--regex-fuzzy "")
146 ""))
147 (should (string= (ivy--regex-fuzzy "^")
148 "^"))
149 (should (string= (ivy--regex-fuzzy "$")
150 "$")))
151
152 (ert-deftest ivy--regex-ignore-order ()
153 (should (equal (ivy--regex-ignore-order "tmux")
154 '(("tmux" . t))))
155 (should (equal (ivy--regex-ignore-order "^tmux")
156 '(("^tmux" . t))))
157 (should (equal (ivy--regex-ignore-order "^tmux$")
158 '(("^tmux$" . t))))
159 (should (equal (ivy--regex-ignore-order "")
160 ""))
161 (should (equal (ivy--regex-ignore-order "^")
162 '(("^" . t))))
163 (should (equal (ivy--regex-ignore-order "$")
164 '(("$" . t))))
165 (should (equal (ivy--regex-ignore-order "one two")
166 '(("one" . t) ("two" . t))))
167 (should (equal (ivy--regex-ignore-order "one two !three")
168 '(("one" . t) ("two" . t) ("three"))))
169 (should (equal (ivy--regex-ignore-order "one two !three four")
170 '(("one" . t) ("two" . t) ("three") ("four"))))
171 (should (equal (ivy--regex-ignore-order "!three four")
172 '(("" . t) (("three") ("four"))))))
173
174 (ert-deftest ivy--format ()
175 (should (string= (let ((ivy--index 10)
176 (ivy-format-function (lambda (x) (mapconcat #'identity x "\n")))
177 (cands '("NAME"
178 "SYNOPSIS"
179 "DESCRIPTION"
180 "FUNCTION LETTERS"
181 "SWITCHES"
182 "DIAGNOSTICS"
183 "EXAMPLE 1"
184 "EXAMPLE 2"
185 "EXAMPLE 3"
186 "SEE ALSO"
187 "AUTHOR")))
188 (ivy--format cands))
189 #("\nDESCRIPTION\nFUNCTION LETTERS\nSWITCHES\nDIAGNOSTICS\nEXAMPLE 1\nEXAMPLE 2\nEXAMPLE 3\nSEE ALSO\nAUTHOR"
190 0 90 (read-only nil)
191 90 96 (face ivy-current-match read-only nil)))))
192
193 (ert-deftest ivy--filter ()
194 (setq ivy-last (make-ivy-state))
195 (should (equal (ivy--filter "the" '("foo" "the" "The"))
196 '("the" "The")))
197 (should (equal (ivy--filter "The" '("foo" "the" "The"))
198 '("The"))))
199
200 (ert-deftest counsel-unquote-regex-parens ()
201 (should (equal (counsel-unquote-regex-parens
202 (ivy--regex "foo bar"))
203 "(foo).*?(bar)"))
204 (should (equal (counsel-unquote-regex-parens
205 (ivy--regex "(foo bar"))
206 "(\\(foo).*?(bar)")))