1 ;;; ivy-test.el --- tests for ivy
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
7 ;; This file is part of GNU Emacs.
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)
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.
19 ;; For a full copy of the GNU General Public License
20 ;; see <http://www.gnu.org/licenses/>.
24 ;; useful for #'ivy-read-remap. It must arrive before (require 'ivy)
25 (define-key global-map (kbd "<S-right>") #'end-of-buffer)
31 "Holds a test expression to evaluate with `ivy-eval'.")
33 (defvar ivy-result nil
34 "Holds the eval result of `ivy-expr' by `ivy-eval'.")
37 "Evaluate `ivy-expr'."
39 (setq ivy-result (eval ivy-expr)))
41 (global-set-key (kbd "C-c e") 'ivy-eval)
43 (defun ivy-with (expr keys)
44 "Evaluate EXPR followed by KEYS."
45 (let ((ivy-expr expr))
47 (vconcat (kbd "C-c e")
51 (ert-deftest ivy-read ()
53 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
57 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
61 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
65 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
69 (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
73 (ivy-with '(let ((ivy-re-builders-alist '((t . ivy--regex-fuzzy))))
74 (ivy-read "pattern: " '("package-list-packages" "something-else")))
76 "package-list-packages"))
78 (ivy-with '(ivy-read "test" '("aaab" "aaac"))
82 (ivy-with '(ivy-read "test" '(("foo" . "bar")))
87 '(with-output-to-string
88 (ivy-read "test" '(("foo" . "bar"))
89 :action (lambda (x) (prin1 x))))
94 '(with-output-to-string
95 (ivy-read "test" '(("foo" . "bar"))
96 :action (lambda (x) (prin1 x))))
100 (ivy-with '(ivy-read "pattern: " '("can do" "can" "can't do"))
104 (ert-deftest ivy-read-remap ()
106 (ivy-with '(ivy-read "pattern: " '("blue" "yellow" "red"))
110 (ert-deftest swiper--re-builder ()
111 (setq swiper--width 4)
112 (should (string= (swiper--re-builder "^")
114 (should (string= (swiper--re-builder "^a")
116 (should (string= (swiper--re-builder "^a b")
117 "^ \\(a\\).*?\\(b\\)")))
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.")
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"
130 (should (equal (ivy--split "^[^ ]") '("^[^ ]")))
131 (should (equal (ivy--split "^[^ ] bar") '("^[^ ]" "bar"))))
133 (ert-deftest ivy--regex ()
134 (should (equal (ivy--regex
135 "\\(?:interactive\\|swiper\\) \\(?:list\\|symbol\\)")
136 "\\(\\(?:interactive\\|swiper\\)\\).*?\\(\\(?:list\\|symbol\\)\\)")))
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 "")
147 (should (string= (ivy--regex-fuzzy "^")
149 (should (string= (ivy--regex-fuzzy "$")
152 (ert-deftest ivy--regex-ignore-order ()
153 (should (equal (ivy--regex-ignore-order "tmux")
155 (should (equal (ivy--regex-ignore-order "^tmux")
157 (should (equal (ivy--regex-ignore-order "^tmux$")
159 (should (equal (ivy--regex-ignore-order "")
161 (should (equal (ivy--regex-ignore-order "^")
163 (should (equal (ivy--regex-ignore-order "$")
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"))))))
174 (ert-deftest ivy--format ()
175 (should (string= (let ((ivy--index 10)
176 (ivy-format-function (lambda (x) (mapconcat #'identity x "\n")))
189 #("\nDESCRIPTION\nFUNCTION LETTERS\nSWITCHES\nDIAGNOSTICS\nEXAMPLE 1\nEXAMPLE 2\nEXAMPLE 3\nSEE ALSO\nAUTHOR"
191 90 96 (face ivy-current-match read-only nil)))))
193 (ert-deftest ivy--filter ()
194 (setq ivy-last (make-ivy-state))
195 (should (equal (ivy--filter "the" '("foo" "the" "The"))
197 (should (equal (ivy--filter "The" '("foo" "the" "The"))
200 (ert-deftest counsel-unquote-regex-parens ()
201 (should (equal (counsel-unquote-regex-parens
202 (ivy--regex "foo bar"))
204 (should (equal (counsel-unquote-regex-parens
205 (ivy--regex "(foo bar"))
206 "(\\(foo).*?(bar)")))