]> code.delx.au - gnu-emacs-elpa/blobdiff - company-tests.el
Don't use end-of-buffer-workaround
[gnu-emacs-elpa] / company-tests.el
index abd970423e110c6ca898e554d22edd7f1fa942fe..6060aed1767236aa2425b330a90d618abcbe3111 100644 (file)
@@ -30,6 +30,9 @@
 (require 'company-keywords)
 (require 'company-clang)
 
+(defun company--column (&optional pos)
+  (car (company--col-row pos)))
+
 ;;; Core
 
 (ert-deftest company-sorted-keywords ()
                    (candidates '("c" "d")))))))
     (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
 
+(ert-deftest company-multi-backend-filters-backends-by-prefix ()
+  (let ((company-backend
+         (list (lambda (command &optional arg &rest ignore)
+                 (cl-case command
+                   (prefix (cons "z" t))
+                   (candidates '("a" "b"))))
+               (lambda (command &optional arg &rest ignore)
+                 (cl-case command
+                   (prefix "t")
+                   (candidates '("c" "d"))))
+               (lambda (command &optional arg &rest ignore)
+                 (cl-case command
+                   (prefix "z")
+                   (candidates '("e" "f")))))))
+    (should (equal (company-call-backend 'candidates "z") '("a" "b" "e" "f")))))
+
 (ert-deftest company-multi-backend-remembers-candidate-backend ()
   (let ((company-backend
          (list (lambda (command &optional arg)
     (insert "ab")
     (company-mode)
     (let (company-frontends
+          (company-minimum-prefix-length 2)
           (company-require-match 'company-explicit-action-p)
           (company-backends
            (list (lambda (command &optional arg)
                      (candidates '("abc" "abd")))))))
       (company-idle-begin (current-buffer) (selected-window)
                           (buffer-chars-modified-tick) (point))
+      (should (eq 2 company-candidates-length))
       (let ((last-command-event ?e))
         (company-call 'self-insert-command 1))
       (should (eq nil company-candidates-length))
       (should (eq 4 (point))))))
 
+(ert-deftest company-dont-require-match-if-old-prefix-ended-and-was-a-match ()
+  (with-temp-buffer
+    (insert "ab")
+    (company-mode)
+    (let (company-frontends
+          (company-require-match 'company-explicit-action-p)
+          (company-backends
+           (list (lambda (command &optional arg)
+                   (cl-case command
+                     (prefix (company-grab-word))
+                     (candidates '("abc" "ab" "abd"))
+                     (sorted t))))))
+      (let (this-command)
+        (company-complete))
+      (let ((last-command-event ?e))
+        (company-call 'self-insert-command 1))
+      (should (eq 3 company-candidates-length))
+      (should (eq 3 (point)))
+      (let ((last-command-event ? ))
+        (company-call 'self-insert-command 1))
+      (should (null company-candidates-length))
+      (should (eq 4 (point))))))
+
 (ert-deftest company-should-complete-whitelist ()
   (with-temp-buffer
     (insert "ab")
     (let (company-frontends
           (company-auto-complete 'company-explicit-action-p)
           (company-auto-complete-chars '(? ))
+          (company-minimum-prefix-length 2)
           (company-backends
            (list (lambda (command &optional arg)
                    (cl-case command
         (let (this-command)
           (company-call 'complete))
         (company-call 'open-line 1)
-        (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
+        (should (eq 1 (overlay-start company-pseudo-tooltip-overlay)))))))
 
 (ert-deftest company-pseudo-tooltip-show ()
   :tags '(interactive)
         ;; FIXME: Make it 2?
         (should (eq (overlay-get ov 'company-height) company-tooltip-limit))
         (should (eq (overlay-get ov 'company-column) col))
-        (should (string= (overlay-get ov 'company-after)
-                         "  123 \nc 45  c\nddd\n")))))))
+        (should (string= (overlay-get ov 'company-display)
+                         "\n  123 \nc 45  c\nddd\n")))))))
+
+(ert-deftest company-pseudo-tooltip-edit-updates-width ()
+  :tags '(interactive)
+  (with-temp-buffer
+    (set-window-buffer nil (current-buffer))
+    (let ((company-candidates-length 5)
+          (company-candidates '("123" "45" "67" "89" "1011"))
+          (company-backend 'ignore)
+          (company-tooltip-limit 4)
+          (company-tooltip-offset-display 'scrollbar))
+      (company-pseudo-tooltip-show (company--row)
+                                   (company--column)
+                                   0)
+      (should (eq (overlay-get company-pseudo-tooltip-overlay 'company-width)
+                  6))
+      (company-pseudo-tooltip-edit 4)
+      (should (eq (overlay-get company-pseudo-tooltip-overlay 'company-width)
+                  7)))))
 
 (ert-deftest company-preview-show-with-annotations ()
   :tags '(interactive)
       (let ((company-candidates-length 1)
             (company-candidates '("123")))
         (company-preview-show-at-point (point))
-        (let ((ov company-preview-overlay))
-          (should (string= (overlay-get ov 'display) "123\n")))))))
+        (let* ((ov company-preview-overlay)
+               (str (overlay-get ov 'after-string)))
+          (should (string= str "123"))
+          (should (eq (get-text-property 0 'cursor str) t)))))))
 
 (ert-deftest company-pseudo-tooltip-show-with-annotations ()
   :tags '(interactive)
                                  (cdr (assoc arg '(("123" . "(4)")))))))
             (company-candidates '("123" "45"))
             company-tooltip-align-annotations)
-        (company-pseudo-tooltip-show-at-point (point))
+        (company-pseudo-tooltip-show-at-point (point) 0)
         (let ((ov company-pseudo-tooltip-overlay))
           ;; With margins.
           (should (eq (overlay-get ov 'company-width) 8))
-          (should (string= (overlay-get ov 'company-after)
-                           " 123(4) \n 45     \n")))))))
+          (should (string= (overlay-get ov 'company-display)
+                           "\n 123(4) \n 45     \n")))))))
 
 (ert-deftest company-pseudo-tooltip-show-with-annotations-right-aligned ()
   :tags '(interactive)
                                                    ("67" . "(891011)")))))))
             (company-candidates '("123" "45" "67"))
             (company-tooltip-align-annotations t))
-        (company-pseudo-tooltip-show-at-point (point))
+        (company-pseudo-tooltip-show-at-point (point) 0)
         (let ((ov company-pseudo-tooltip-overlay))
           ;; With margins.
           (should (eq (overlay-get ov 'company-width) 13))
-          (should (string= (overlay-get ov 'company-after)
-                           " 123     (4) \n 45          \n 67 (891011) \n")))))))
+          (should (string= (overlay-get ov 'company-display)
+                           "\n 123     (4) \n 45          \n 67 (891011) \n")))))))
 
 (ert-deftest company-create-lines-shows-numbers ()
   (let ((company-show-numbers t)
                            (format " %s " (make-string (- ww 2) ?4)))
                      (company--create-lines 0 999))))))
 
+(ert-deftest company-create-lines-truncates-common-part ()
+  (let* ((ww (company--window-width))
+         (company-candidates-length 2)
+         (company-tooltip-margin 1)
+         (company-backend #'ignore))
+    (let* ((company-common (make-string (- ww 3) ?1))
+           (company-candidates `(,(concat company-common "2")
+                                 ,(concat company-common "3"))))
+      (should (equal (list (format " %s2 " (make-string (- ww 3) ?1))
+                           (format " %s3 " (make-string (- ww 3) ?1)))
+                     (company--create-lines 0 999))))
+    (let* ((company-common (make-string (- ww 2) ?1))
+           (company-candidates `(,(concat company-common "2")
+                                 ,(concat company-common "3"))))
+      (should (equal (list (format " %s " company-common)
+                           (format " %s " company-common))
+                     (company--create-lines 0 999))))
+    (let* ((company-common (make-string ww ?1))
+           (company-candidates `(,(concat company-common "2")
+                                 ,(concat company-common "3")))
+           (res (company--create-lines 0 999)))
+      (should (equal (list (format " %s " (make-string (- ww 2) ?1))
+                           (format " %s " (make-string (- ww 2) ?1)))
+                     res))
+      (should (eq 'company-tooltip-common-selection
+                    (get-text-property (- ww 2) 'face
+                                       (car res))))
+      (should (eq 'company-tooltip-selection
+                  (get-text-property (1- ww) 'face
+                                     (car res))))
+
+)))
+
 (ert-deftest company-column-with-composition ()
+  :tags '(interactive)
   (with-temp-buffer
-    (insert "lambda ()")
-    (compose-region 1 (1+ (length "lambda")) "\\")
-    (should (= (company--column) 4))))
+    (save-window-excursion
+      (set-window-buffer nil (current-buffer))
+      (insert "lambda ()")
+      (compose-region 1 (1+ (length "lambda")) "\\")
+      (should (= (company--column) 4)))))
 
 (ert-deftest company-column-with-line-prefix ()
+  :tags '(interactive)
+  (with-temp-buffer
+    (save-window-excursion
+      (set-window-buffer nil (current-buffer))
+      (insert "foo")
+      (put-text-property (point-min) (point) 'line-prefix "  ")
+      (should (= (company--column) 5)))))
+
+(ert-deftest company-column-with-line-prefix-on-empty-line ()
+  :tags '(interactive)
   (with-temp-buffer
-    (insert "foo")
-    (put-text-property (point-min) (point) 'line-prefix "  ")
-    (should (= (company--column) 5))))
+    (save-window-excursion
+      (set-window-buffer nil (current-buffer))
+      (insert "\n")
+      (forward-char -1)
+      (put-text-property (point-min) (point-max) 'line-prefix "  ")
+      (should (= (company--column) 2)))))
 
-(ert-deftest company-column-wth-line-prefix-on-empty-line ()
+(ert-deftest company-column-with-tabs ()
+  :tags '(interactive)
   (with-temp-buffer
-    (insert "\n")
-    (forward-char -1)
-    (put-text-property (point-min) (point-max) 'line-prefix "  ")
-    (should (= (company--column) 2))))
+    (save-window-excursion
+      (set-window-buffer nil (current-buffer))
+      (insert "|\t|\t|\t(")
+      (let ((tab-width 8))
+        (should (= (company--column) 25))))))
+
+(ert-deftest company-row-with-header-line-format ()
+  :tags '(interactive)
+  (with-temp-buffer
+    (save-window-excursion
+      (set-window-buffer nil (current-buffer))
+      (should (= (company--row) 0))
+      (setq header-line-format "aaaaaaa")
+      (should (= (company--row) 0)))))
 
 (ert-deftest company-plainify ()
   (let ((tab-width 8))
            (company-plainify (propertize "foobar" 'line-prefix "-*-"))
            "-*-foobar")))
 
+(ert-deftest company-buffer-lines-with-lines-folded ()
+  :tags '(interactive)
+  (with-temp-buffer
+    (insert (propertize "aaa\nbbb\nccc\nddd\n" 'display "aaa+\n"))
+    (insert "eee\nfff\nggg")
+    (should (equal (company-buffer-lines (point-min) (point-max))
+                   '("aaa" "eee" "fff" "ggg")))))
+
+(ert-deftest company-buffer-lines-with-multiline-display ()
+  :tags '(interactive)
+  (with-temp-buffer
+    (insert (propertize "a" 'display "bbb\nccc\ndddd\n"))
+    (insert "eee\nfff\nggg")
+    (should (equal (company-buffer-lines (point-min) (point-max))
+                   '("" "" "" "eee" "fff" "ggg")))))
+
 (ert-deftest company-modify-line ()
   (let ((str "-*-foobar"))
     (should (equal-including-properties
   (with-temp-buffer
     (company-mode)
     (let (company-frontends
+          company-transformers
           (company-backends (list 'company-async-backend)))
       (company-manual-begin)
       (should (equal "foo" company-prefix))
   (with-temp-buffer
     (company-mode)
     (let (company-frontends
+          company-transformers
           (company-backends (list 'company-async-backend)))
       (company-idle-begin (current-buffer) (selected-window)
                           (buffer-chars-modified-tick) (point))
       (should (null company-candidates))
       (insert "a")
       (sleep-for 0.1)
-      (should (null company-prefix))
       (should (null company-candidates)))))
 
 (ert-deftest company-idle-begin-async-allows-immediate-callbacks ()
       (let ((company-backend (list immediate)))
         (should (equal '("f") (company-call-backend 'candidates "foo")))))))
 
+;;; Transformers
+
+(ert-deftest company-occurrence-prefer-closest-above ()
+  (with-temp-buffer
+    (save-window-excursion
+      (set-window-buffer nil (current-buffer))
+      (insert "foo0
+foo1
+")
+      (save-excursion
+        (insert "
+foo3
+foo2"))
+      (let ((company-backend 'company-dabbrev)
+            (company-occurrence-weight-function
+             'company-occurrence-prefer-closest-above))
+        (should (equal '("foo1" "foo0" "foo3" "foo2" "foo4")
+                       (company-sort-by-occurrence
+                        '("foo0" "foo1" "foo2" "foo3" "foo4"))))))))
+
+(ert-deftest company-occurrence-prefer-any-closest ()
+  (with-temp-buffer
+    (save-window-excursion
+      (set-window-buffer nil (current-buffer))
+      (insert "foo0
+foo1
+")
+      (save-excursion
+        (insert "
+foo3
+foo2"))
+      (let ((company-backend 'company-dabbrev)
+            (company-occurrence-weight-function
+             'company-occurrence-prefer-any-closest))
+        (should (equal '("foo1" "foo3" "foo0" "foo2" "foo4")
+                       (company-sort-by-occurrence
+                        '("foo0" "foo1" "foo2" "foo3" "foo4"))))))))
+
 ;;; Template
 
 (ert-deftest company-template-removed-after-the-last-jump ()