]> code.delx.au - gnu-emacs-elpa/blob - ivy-test.el
counsel-grep now works with ivy-occur-revert-buffer
[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 (require 'ivy)
24 (require 'counsel)
25
26 (defvar ivy-expr nil
27 "Holds a test expression to evaluate with `ivy-eval'.")
28
29 (defvar ivy-result nil
30 "Holds the eval result of `ivy-expr' by `ivy-eval'.")
31
32 (defun ivy-eval ()
33 "Evaluate `ivy-expr'."
34 (interactive)
35 (setq ivy-result (eval ivy-expr)))
36
37 (global-set-key (kbd "C-c e") 'ivy-eval)
38
39 (defun ivy-with (expr keys)
40 "Evaluate EXPR followed by KEYS."
41 (let ((ivy-expr expr))
42 (execute-kbd-macro
43 (vconcat (kbd "C-c e")
44 (kbd keys)))
45 ivy-result))
46
47 (ert-deftest ivy-read ()
48 (should (equal
49 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
50 "C-m")
51 "blue"))
52 (should (equal
53 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
54 "y C-m")
55 "yellow"))
56 (should (equal
57 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
58 "y DEL b C-m")
59 "blue"))
60 (should (equal
61 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
62 "z C-m")
63 "z"))
64 (should (equal
65 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
66 "y <backspace> C-m")
67 "blue"))
68 (should (equal
69 (ivy-with '(let ((ivy-re-builders-alist '((t . ivy--regex-fuzzy))))
70 (ivy-read "pattern: " '("package-list-packages" "something-else")))
71 "plp C-m")
72 "package-list-packages"))
73 (should (equal
74 (ivy-with '(ivy-read "test" '("aaab" "aaac"))
75 "a C-n <tab> C-m")
76 "aaac"))
77 (should (equal
78 (ivy-with '(ivy-read "pattern: " '("can do" "can" "can't do"))
79 "can C-m")
80 "can")))
81
82 (ert-deftest swiper--re-builder ()
83 (setq swiper--width 4)
84 (should (string= (swiper--re-builder "^")
85 "."))
86 (should (string= (swiper--re-builder "^a")
87 "^ ?\\(a\\)"))
88 (should (string= (swiper--re-builder "^a b")
89 "^ \\(a\\).*?\\(b\\)")))
90
91 (ert-deftest ivy--split ()
92 (should (equal (ivy--split "King of the who?")
93 '("King" "of" "the" "who?")))
94 (should (equal (ivy--split "The Brittons.")
95 '("The Brittons.")))
96 (should (equal (ivy--split "Who are the Brittons?")
97 '("Who are" "the Brittons?")))
98 (should (equal (ivy--split "We're all Britons and I am your king.")
99 '("We're all Britons"
100 "and I am"
101 "your king."))))
102
103 (ert-deftest ivy--regex ()
104 (should (equal (ivy--regex
105 "\\(?:interactive\\|swiper\\) \\(?:list\\|symbol\\)")
106 "\\(\\(?:interactive\\|swiper\\)\\).*?\\(\\(?:list\\|symbol\\)\\)")))
107
108 (ert-deftest ivy--regex-fuzzy ()
109 (should (string= (ivy--regex-fuzzy "tmux")
110 "\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)"))
111 (should (string= (ivy--regex-fuzzy "^tmux")
112 "^\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)"))
113 (should (string= (ivy--regex-fuzzy "^tmux$")
114 "^\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)$"))
115 (should (string= (ivy--regex-fuzzy "")
116 ""))
117 (should (string= (ivy--regex-fuzzy "^")
118 "^"))
119 (should (string= (ivy--regex-fuzzy "$")
120 "$")))
121
122 (ert-deftest ivy--regex-ignore-order ()
123 (should (equal (ivy--regex-ignore-order "tmux")
124 '(("tmux" . t))))
125 (should (equal (ivy--regex-ignore-order "^tmux")
126 '(("^tmux" . t))))
127 (should (equal (ivy--regex-ignore-order "^tmux$")
128 '(("^tmux$" . t))))
129 (should (equal (ivy--regex-ignore-order "")
130 ""))
131 (should (equal (ivy--regex-ignore-order "^")
132 '(("^" . t))))
133 (should (equal (ivy--regex-ignore-order "$")
134 '(("$" . t))))
135 (should (equal (ivy--regex-ignore-order "one two")
136 '(("one" . t) ("two" . t))))
137 (should (equal (ivy--regex-ignore-order "one two !three")
138 '(("one" . t) ("two" . t) ("three"))))
139 (should (equal (ivy--regex-ignore-order "one two !three four")
140 '(("one" . t) ("two" . t) ("three") ("four"))))
141 (should (equal (ivy--regex-ignore-order "!three four")
142 '(("" . t) (("three") ("four"))))))
143
144 (ert-deftest ivy--format ()
145 (should (string= (let ((ivy--index 10)
146 (ivy-format-function (lambda (x) (mapconcat #'identity x "\n")))
147 (cands '("NAME"
148 "SYNOPSIS"
149 "DESCRIPTION"
150 "FUNCTION LETTERS"
151 "SWITCHES"
152 "DIAGNOSTICS"
153 "EXAMPLE 1"
154 "EXAMPLE 2"
155 "EXAMPLE 3"
156 "SEE ALSO"
157 "AUTHOR")))
158 (ivy--format cands))
159 #("\nDESCRIPTION\nFUNCTION LETTERS\nSWITCHES\nDIAGNOSTICS\nEXAMPLE 1\nEXAMPLE 2\nEXAMPLE 3\nSEE ALSO\nAUTHOR"
160 0 90 (read-only nil)
161 90 96 (face ivy-current-match read-only nil)))))
162
163 (ert-deftest ivy--filter ()
164 (setq ivy-last (make-ivy-state))
165 (should (equal (ivy--filter "the" '("foo" "the" "The"))
166 '("the" "The")))
167 (should (equal (ivy--filter "The" '("foo" "the" "The"))
168 '("The"))))
169
170 (ert-deftest counsel-unquote-regex-parens ()
171 (should (equal (counsel-unquote-regex-parens
172 (ivy--regex "foo bar"))
173 "(foo).*?(bar)"))
174 (should (equal (counsel-unquote-regex-parens
175 (ivy--regex "(foo bar"))
176 "(\\(foo).*?(bar)")))