]> code.delx.au - gnu-emacs-elpa/blobdiff - ivy-test.el
ivy.el (ivy--format-minibuffer-line): Update
[gnu-emacs-elpa] / ivy-test.el
index 957f82406d654eb169f14ba6848c3404033c4db6..40dae48286bb4254a290fc04585f31222ea8bd0e 100644 (file)
 ;; see <http://www.gnu.org/licenses/>.
 
 (require 'ert)
+
+;; useful for #'ivy-read-remap. It must arrive before (require 'ivy)
+(define-key global-map (kbd "<S-right>") #'end-of-buffer)
+
 (require 'ivy)
+(require 'counsel)
 
 (defvar ivy-expr nil
   "Holds a test expression to evaluate with `ivy-eval'.")
     ivy-result))
 
 (ert-deftest ivy-read ()
-  (should (equal
-           (ivy-read "pattern: " nil)
-           nil))
-  (should (equal
-           (ivy-read "pattern: " '("42"))
-           "42"))
   (should (equal
            (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
                      "C-m")
   (should (equal
            (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
                      "z C-m")
-           nil)))
+           "z"))
+  (should (equal
+           (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
+                     "y <backspace> C-m")
+           "blue"))
+  (should (equal
+           (ivy-with '(let ((ivy-re-builders-alist '((t . ivy--regex-fuzzy))))
+                       (ivy-read "pattern: " '("package-list-packages" "something-else")))
+                     "plp C-m")
+           "package-list-packages"))
+  (should (equal
+           (ivy-with '(ivy-read "test" '("aaab" "aaac"))
+                     "a C-n <tab> C-m")
+           "aaac"))
+  (should (equal
+           (ivy-with '(ivy-read "test" '(("foo" . "bar")))
+                     "asdf C-m")
+           "asdf"))
+  (should (equal
+           (ivy-with
+            '(with-output-to-string
+              (ivy-read "test" '(("foo" . "bar"))
+               :action (lambda (x) (prin1 x))))
+            "f C-m")
+           "\"bar\""))
+  (should (equal
+           (ivy-with
+            '(with-output-to-string
+              (ivy-read "test" '(("foo" . "bar"))
+               :action (lambda (x) (prin1 x))))
+            "asdf C-m")
+           "\"asdf\""))
+  (should (equal
+           (ivy-with '(ivy-read "pattern: " '("can do" "can" "can't do"))
+                     "can C-m")
+           "can")))
+
+(ert-deftest ivy-read-remap ()
+  (should (equal
+           (ivy-with '(ivy-read "pattern: " '("blue" "yellow" "red"))
+                  "<S-right> C-m")
+           "red")))
+
+(ert-deftest swiper--re-builder ()
+  (setq swiper--width 4)
+  (should (string= (swiper--re-builder "^")
+                   "."))
+  (should (string= (swiper--re-builder "^a")
+                   "^ ?\\(a\\)"))
+  (should (string= (swiper--re-builder "^a b")
+                   "^ \\(a\\).*?\\(b\\)")))
+
+(ert-deftest ivy--split ()
+  (should (equal (ivy--split "King of the who?")
+                 '("King" "of" "the" "who?")))
+  (should (equal (ivy--split "The  Brittons.")
+                 '("The Brittons.")))
+  (should (equal (ivy--split "Who  are the  Brittons?")
+                 '("Who are" "the Brittons?")))
+  (should (equal (ivy--split "We're  all  Britons and   I   am your   king.")
+                 '("We're all Britons"
+                   "and  I  am"
+                   "your  king.")))
+  (should (equal (ivy--split "^[^ ]") '("^[^ ]")))
+  (should (equal (ivy--split "^[^ ] bar") '("^[^ ]" "bar"))))
+
+(ert-deftest ivy--regex ()
+  (should (equal (ivy--regex
+                  "\\(?:interactive\\|swiper\\) \\(?:list\\|symbol\\)")
+                 "\\(\\(?:interactive\\|swiper\\)\\).*?\\(\\(?:list\\|symbol\\)\\)")))
+
+(ert-deftest ivy--regex-fuzzy ()
+  (should (string= (ivy--regex-fuzzy "tmux")
+                   "\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)"))
+  (should (string= (ivy--regex-fuzzy "^tmux")
+                   "^\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)"))
+  (should (string= (ivy--regex-fuzzy "^tmux$")
+                   "^\\(t\\).*\\(m\\).*\\(u\\).*\\(x\\)$"))
+  (should (string= (ivy--regex-fuzzy "")
+                   ""))
+  (should (string= (ivy--regex-fuzzy "^")
+                   "^"))
+  (should (string= (ivy--regex-fuzzy "$")
+                   "$")))
+
+(ert-deftest ivy--regex-ignore-order ()
+  (should (equal (ivy--regex-ignore-order "tmux")
+                 '(("tmux" . t))))
+  (should (equal (ivy--regex-ignore-order "^tmux")
+                 '(("^tmux" . t))))
+  (should (equal (ivy--regex-ignore-order "^tmux$")
+                 '(("^tmux$" . t))))
+  (should (equal (ivy--regex-ignore-order "")
+                 ""))
+  (should (equal (ivy--regex-ignore-order "^")
+                 '(("^" . t))))
+  (should (equal (ivy--regex-ignore-order "$")
+                 '(("$" . t))))
+  (should (equal (ivy--regex-ignore-order "one two")
+                 '(("one" . t) ("two" . t))))
+  (should (equal (ivy--regex-ignore-order "one two !three")
+                 '(("one" . t) ("two" . t) ("three"))))
+  (should (equal (ivy--regex-ignore-order "one two !three four")
+                 '(("one" . t) ("two" . t) ("three") ("four"))))
+  (should (equal (ivy--regex-ignore-order "!three four")
+                 '(("" . t) (("three") ("four"))))))
+
+(ert-deftest ivy--format ()
+  (should (string= (let ((ivy--index 10)
+                         (ivy-format-function (lambda (x) (mapconcat #'identity x "\n")))
+                         (cands '("NAME"
+                                  "SYNOPSIS"
+                                  "DESCRIPTION"
+                                  "FUNCTION LETTERS"
+                                  "SWITCHES"
+                                  "DIAGNOSTICS"
+                                  "EXAMPLE 1"
+                                  "EXAMPLE 2"
+                                  "EXAMPLE 3"
+                                  "SEE ALSO"
+                                  "AUTHOR")))
+                     (ivy--format cands))
+                   #("\nDESCRIPTION\nFUNCTION LETTERS\nSWITCHES\nDIAGNOSTICS\nEXAMPLE 1\nEXAMPLE 2\nEXAMPLE 3\nSEE ALSO\nAUTHOR"
+                     0 90 (read-only nil)
+                     90 96 (face ivy-current-match read-only nil)))))
+
+(ert-deftest ivy--filter ()
+  (setq ivy-last (make-ivy-state))
+  (should (equal (ivy--filter "the" '("foo" "the" "The"))
+                 '("the" "The")))
+  (should (equal (ivy--filter "The" '("foo" "the" "The"))
+                 '("The"))))
+
+(ert-deftest counsel-unquote-regex-parens ()
+  (should (equal (counsel-unquote-regex-parens
+                  (ivy--regex "foo bar"))
+                 "(foo).*?(bar)"))
+  (should (equal (counsel-unquote-regex-parens
+                  (ivy--regex "(foo bar"))
+                 "(\\(foo).*?(bar)")))