]> code.delx.au - gnu-emacs-elpa/blob - ivy-test.el
ivy.el (ivy--reset-state): file-directory-p needs a string
[gnu-emacs-elpa] / 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 "pattern: " '("can do" "can" "can't do"))
83 "can C-m")
84 "can")))
85
86 (ert-deftest ivy-read-remap ()
87 (should (equal
88 (ivy-with '(ivy-read "pattern: " '("blue" "yellow" "red"))
89 "<S-right> C-m")
90 "red")))
91
92 (ert-deftest swiper--re-builder ()
93 (setq swiper--width 4)
94 (should (string= (swiper--re-builder "^")
95 "."))
96 (should (string= (swiper--re-builder "^a")
97 "^ ?\\(a\\)"))
98 (should (string= (swiper--re-builder "^a b")
99 "^ \\(a\\).*?\\(b\\)")))
100
101 (ert-deftest ivy--split ()
102 (should (equal (ivy--split "King of the who?")
103 '("King" "of" "the" "who?")))
104 (should (equal (ivy--split "The Brittons.")
105 '("The Brittons.")))
106 (should (equal (ivy--split "Who are the Brittons?")
107 '("Who are" "the Brittons?")))
108 (should (equal (ivy--split "We're all Britons and I am your king.")
109 '("We're all Britons"
110 "and I am"
111 "your king.")))
112 (should (equal (ivy--split "^[^ ]") '("^[^ ]")))
113 (should (equal (ivy--split "^[^ ] bar") '("^[^ ]" "bar"))))
114
115 (ert-deftest ivy--regex ()
116 (should (equal (ivy--regex
117 "\\(?:interactive\\|swiper\\) \\(?:list\\|symbol\\)")
118 "\\(\\(?:interactive\\|swiper\\)\\).*?\\(\\(?:list\\|symbol\\)\\)")))
119
120 (ert-deftest ivy--regex-fuzzy ()
121 (should (string= (ivy--regex-fuzzy "tmux")
122 "\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)"))
123 (should (string= (ivy--regex-fuzzy "^tmux")
124 "^\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)"))
125 (should (string= (ivy--regex-fuzzy "^tmux$")
126 "^\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)$"))
127 (should (string= (ivy--regex-fuzzy "")
128 ""))
129 (should (string= (ivy--regex-fuzzy "^")
130 "^"))
131 (should (string= (ivy--regex-fuzzy "$")
132 "$")))
133
134 (ert-deftest ivy--regex-ignore-order ()
135 (should (equal (ivy--regex-ignore-order "tmux")
136 '(("tmux" . t))))
137 (should (equal (ivy--regex-ignore-order "^tmux")
138 '(("^tmux" . t))))
139 (should (equal (ivy--regex-ignore-order "^tmux$")
140 '(("^tmux$" . t))))
141 (should (equal (ivy--regex-ignore-order "")
142 ""))
143 (should (equal (ivy--regex-ignore-order "^")
144 '(("^" . t))))
145 (should (equal (ivy--regex-ignore-order "$")
146 '(("$" . t))))
147 (should (equal (ivy--regex-ignore-order "one two")
148 '(("one" . t) ("two" . t))))
149 (should (equal (ivy--regex-ignore-order "one two !three")
150 '(("one" . t) ("two" . t) ("three"))))
151 (should (equal (ivy--regex-ignore-order "one two !three four")
152 '(("one" . t) ("two" . t) ("three") ("four"))))
153 (should (equal (ivy--regex-ignore-order "!three four")
154 '(("" . t) (("three") ("four"))))))
155
156 (ert-deftest ivy--format ()
157 (should (string= (let ((ivy--index 10)
158 (ivy-format-function (lambda (x) (mapconcat #'identity x "\n")))
159 (cands '("NAME"
160 "SYNOPSIS"
161 "DESCRIPTION"
162 "FUNCTION LETTERS"
163 "SWITCHES"
164 "DIAGNOSTICS"
165 "EXAMPLE 1"
166 "EXAMPLE 2"
167 "EXAMPLE 3"
168 "SEE ALSO"
169 "AUTHOR")))
170 (ivy--format cands))
171 #("\nDESCRIPTION\nFUNCTION LETTERS\nSWITCHES\nDIAGNOSTICS\nEXAMPLE 1\nEXAMPLE 2\nEXAMPLE 3\nSEE ALSO\nAUTHOR"
172 0 90 (read-only nil)
173 90 96 (face ivy-current-match read-only nil)))))
174
175 (ert-deftest ivy--filter ()
176 (setq ivy-last (make-ivy-state))
177 (should (equal (ivy--filter "the" '("foo" "the" "The"))
178 '("the" "The")))
179 (should (equal (ivy--filter "The" '("foo" "the" "The"))
180 '("The"))))
181
182 (ert-deftest counsel-unquote-regex-parens ()
183 (should (equal (counsel-unquote-regex-parens
184 (ivy--regex "foo bar"))
185 "(foo).*?(bar)"))
186 (should (equal (counsel-unquote-regex-parens
187 (ivy--regex "(foo bar"))
188 "(\\(foo).*?(bar)")))