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 "pattern: " '("can do" "can" "can't do"))
86 (ert-deftest ivy-read-remap ()
88 (ivy-with '(ivy-read "pattern: " '("blue" "yellow" "red"))
92 (ert-deftest swiper--re-builder ()
93 (setq swiper--width 4)
94 (should (string= (swiper--re-builder "^")
96 (should (string= (swiper--re-builder "^a")
98 (should (string= (swiper--re-builder "^a b")
99 "^ \\(a\\).*?\\(b\\)")))
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.")
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"
113 (ert-deftest ivy--regex ()
114 (should (equal (ivy--regex
115 "\\(?:interactive\\|swiper\\) \\(?:list\\|symbol\\)")
116 "\\(\\(?:interactive\\|swiper\\)\\).*?\\(\\(?:list\\|symbol\\)\\)")))
118 (ert-deftest ivy--regex-fuzzy ()
119 (should (string= (ivy--regex-fuzzy "tmux")
120 "\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)"))
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 "")
127 (should (string= (ivy--regex-fuzzy "^")
129 (should (string= (ivy--regex-fuzzy "$")
132 (ert-deftest ivy--regex-ignore-order ()
133 (should (equal (ivy--regex-ignore-order "tmux")
135 (should (equal (ivy--regex-ignore-order "^tmux")
137 (should (equal (ivy--regex-ignore-order "^tmux$")
139 (should (equal (ivy--regex-ignore-order "")
141 (should (equal (ivy--regex-ignore-order "^")
143 (should (equal (ivy--regex-ignore-order "$")
145 (should (equal (ivy--regex-ignore-order "one two")
146 '(("one" . t) ("two" . t))))
147 (should (equal (ivy--regex-ignore-order "one two !three")
148 '(("one" . t) ("two" . t) ("three"))))
149 (should (equal (ivy--regex-ignore-order "one two !three four")
150 '(("one" . t) ("two" . t) ("three") ("four"))))
151 (should (equal (ivy--regex-ignore-order "!three four")
152 '(("" . t) (("three") ("four"))))))
154 (ert-deftest ivy--format ()
155 (should (string= (let ((ivy--index 10)
156 (ivy-format-function (lambda (x) (mapconcat #'identity x "\n")))
169 #("\nDESCRIPTION\nFUNCTION LETTERS\nSWITCHES\nDIAGNOSTICS\nEXAMPLE 1\nEXAMPLE 2\nEXAMPLE 3\nSEE ALSO\nAUTHOR"
171 90 96 (face ivy-current-match read-only nil)))))
173 (ert-deftest ivy--filter ()
174 (setq ivy-last (make-ivy-state))
175 (should (equal (ivy--filter "the" '("foo" "the" "The"))
177 (should (equal (ivy--filter "The" '("foo" "the" "The"))
180 (ert-deftest counsel-unquote-regex-parens ()
181 (should (equal (counsel-unquote-regex-parens
182 (ivy--regex "foo bar"))
184 (should (equal (counsel-unquote-regex-parens
185 (ivy--regex "(foo bar"))
186 "(\\(foo).*?(bar)")))