]> code.delx.au - gnu-emacs-elpa/blob - test/template-tests.el
New transformer: company-sort-prefer-same-case-prefix
[gnu-emacs-elpa] / test / template-tests.el
1 ;;; template-tests.el --- company-mode tests -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Dmitry Gutov
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs 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 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs 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 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 (require 'company-tests)
23 (require 'company-template)
24
25 (defun company-template-field-assert-text (str &optional pos)
26 (let ((field (company-template-field-at pos)))
27 (should (equal (buffer-substring-no-properties
28 (overlay-start field)
29 (overlay-end field))
30 str))))
31
32 (ert-deftest company-template-removed-after-the-last-jump ()
33 (with-temp-buffer
34 (insert "{ }")
35 (goto-char 2)
36 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
37 (save-excursion
38 (dotimes (_ 2)
39 (insert " foo")
40 (company-template-add-field tpl (- (point) 3) (point))))
41 (company-call 'template-forward-field)
42 (should (= 3 (point)))
43 (company-call 'template-forward-field)
44 (should (= 7 (point)))
45 (company-call 'template-forward-field)
46 (should (= 11 (point)))
47 (should (zerop (length (overlay-get tpl 'company-template-fields))))
48 (should (null (overlay-buffer tpl))))))
49
50 (ert-deftest company-template-removed-after-input-and-jump ()
51 (with-temp-buffer
52 (insert "{ }")
53 (goto-char 2)
54 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
55 (save-excursion
56 (insert " bar")
57 (company-template-add-field tpl (- (point) 3) (point)))
58 (company-call 'template-move-to-first tpl)
59 (should (= 3 (point)))
60 (dolist (c (string-to-list "tee"))
61 (let ((last-command-event c))
62 (company-call 'self-insert-command 1)))
63 (should (string= "{ tee }" (buffer-string)))
64 (should (overlay-buffer tpl))
65 (company-call 'template-forward-field)
66 (should (= 7 (point)))
67 (should (null (overlay-buffer tpl))))))
68
69 (ert-deftest company-template-c-like-templatify ()
70 (with-temp-buffer
71 (let ((text "foo(int a, short b)"))
72 (insert text)
73 (company-template-c-like-templatify text)
74 (should (equal "foo(int a, short b)" (buffer-string)))
75 (company-template-field-assert-text "int a"))))
76
77 (ert-deftest company-template-c-like-templatify-trims-after-closing-paren ()
78 (with-temp-buffer
79 (let ((text "foo(int a, short b)!@ #1334 a"))
80 (insert text)
81 (company-template-c-like-templatify text)
82 (should (equal "foo(int a, short b)" (buffer-string)))
83 (company-template-field-assert-text "int a"))))
84
85 (ert-deftest company-template-c-like-templatify-generics ()
86 (with-temp-buffer
87 (let ((text "foo<TKey, TValue>(int i, Dict<TKey, TValue>, long l)"))
88 (insert text)
89 (company-template-c-like-templatify text)
90 (should (equal (buffer-string) text))
91 (company-template-field-assert-text "TKey")
92 (search-forward "Dict")
93 (forward-char -1)
94 (company-template-field-assert-text "Dict<TKey, TValue>"))))
95
96 (ert-deftest company-template-c-like-func-ptr ()
97 (with-temp-buffer
98 (let ((text "foo(*)(int)"))
99 (insert text)
100 (company-template-c-like-templatify text)
101 (should (equal (buffer-string) "foo(int)"))
102 (company-template-field-assert-text "int"))))
103
104 (ert-deftest company-clang-objc-templatify-empty-args ()
105 (with-temp-buffer
106 (let ((text "createBookWithTitle:andAuthor:"))
107 (insert text)
108 (company-template-objc-templatify text)
109 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
110 (should (looking-at "arg0"))
111 (should (null (overlay-get (company-template-field-at) 'display))))))
112
113 (ert-deftest company-template-objc-templatify ()
114 (with-temp-buffer
115 (let ((text "createBookWithTitle:(NSString) andAuthor:(id)"))
116 (insert text)
117 (company-template-objc-templatify text)
118 (should (equal (buffer-string) text))
119 (company-template-field-assert-text "(NSString)"))))