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