]> code.delx.au - gnu-emacs-elpa/blob - yasnippet-tests.el
Should really exit all snippets
[gnu-emacs-elpa] / yasnippet-tests.el
1 ;;; yasnippet-tests.el --- some yasnippet tests
2
3 ;; Copyright (C) 2012 João Távora
4
5 ;; Author: João Távora <joaot@siscog.pt>
6 ;; Keywords: emulations, convenience
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;; Attempt to test basic snippet mechanics and the loading system
24
25 ;;; Code:
26
27 (require 'yasnippet)
28 (require 'ert)
29 (require 'ert-x)
30
31
32 ;;; Snippet mechanics
33
34 (ert-deftest field-navigation ()
35 (with-temp-buffer
36 (yas/minor-mode 1)
37 (yas/expand-snippet "${1:brother} from another ${2:mother}")
38 (should (string= (buffer-substring-no-properties (point-min) (point-max))
39 "brother from another mother"))
40
41 (should (looking-at "brother"))
42 (ert-simulate-command '(yas/next-field-or-maybe-expand))
43 (should (looking-at "mother"))
44 (ert-simulate-command '(yas/prev-field))
45 (should (looking-at "brother"))))
46
47 (ert-deftest simple-mirror ()
48 (with-temp-buffer
49 (yas/minor-mode 1)
50 (yas/expand-snippet "${1:brother} from another $1")
51 (should (string= (buffer-substring-no-properties (point-min) (point-max))
52 "brother from another brother"))
53 (ert-simulate-command `(yas/mock-insert "bla"))
54 (should (string= (buffer-substring-no-properties (point-min) (point-max))
55 "bla from another bla"))))
56
57 (ert-deftest mirror-with-transformation ()
58 (with-temp-buffer
59 (yas/minor-mode 1)
60 (yas/expand-snippet "${1:brother} from another ${1:$(upcase yas/text)}")
61 (should (string= (buffer-substring-no-properties (point-min) (point-max))
62 "brother from another BROTHER"))
63 (ert-simulate-command `(yas/mock-insert "bla"))
64 (should (string= (buffer-substring-no-properties (point-min) (point-max))
65 "bla from another BLA"))))
66
67 (ert-deftest nested-placeholders-kill-superfield ()
68 (with-temp-buffer
69 (yas/minor-mode 1)
70 (yas/expand-snippet "brother from ${2:another ${3:mother}}!")
71 (should (string= (buffer-substring-no-properties (point-min) (point-max))
72 "brother from another mother!"))
73 (ert-simulate-command `(yas/mock-insert "bla"))
74 (should (string= (buffer-substring-no-properties (point-min) (point-max))
75 "brother from bla!"))))
76
77 (ert-deftest nested-placeholders-use-subfield ()
78 (with-temp-buffer
79 (yas/minor-mode 1)
80 (yas/expand-snippet "brother from ${2:another ${3:mother}}!")
81 (ert-simulate-command '(yas/next-field-or-maybe-expand))
82 (ert-simulate-command `(yas/mock-insert "bla"))
83 (should (string= (buffer-substring-no-properties (point-min) (point-max))
84 "brother from another bla!"))))
85
86 ;; (ert-deftest in-snippet-undo ()
87 ;; (with-temp-buffer
88 ;; (yas/minor-mode 1)
89 ;; (yas/expand-snippet "brother from ${2:another ${3:mother}}!")
90 ;; (ert-simulate-command '(yas/next-field-or-maybe-expand))
91 ;; (ert-simulate-command `(yas/mock-insert "bla"))
92 ;; (ert-simulate-command '(undo))
93 ;; (should (string= (buffer-substring-no-properties (point-min) (point-max))
94 ;; "brother from another mother!"))))
95
96
97 ;;; Misc tests
98 ;;;
99
100 (ert-deftest protection-overlay-no-cheating ()
101 "Protection overlays at the very end of the buffer, are dealt by cheatingly inserting a newline!
102
103 TODO: correct this bug!"
104 :expected-result :failed
105 (with-temp-buffer
106 (yas/minor-mode 1)
107 (yas/expand-snippet "${2:brother} from another ${1:mother}")
108 (should (string= (buffer-substring-no-properties (point-min) (point-max))
109 "brother from another mother") ;; no newline should be here!
110 )))
111
112 ;;; Loading
113 ;;;
114 (ert-deftest basic-loading ()
115 "Test basic loading and expansion of snippets"
116 (yas/saving-variables
117 (with-snippet-dirs
118 '((".emacs.d/snippets"
119 ("c-mode"
120 (".yas-parents" . "cc-mode")
121 ("printf" . "printf($1);"))
122 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
123 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
124 ("library/snippets"
125 ("c-mode" (".yas-parents" . "c++-mode"))
126 ("cc-mode" ("def" . "# define"))
127 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
128 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
129 (yas/reload-all)
130 (with-temp-buffer
131 (lisp-interaction-mode)
132 (yas/minor-mode 1)
133 (insert "sc")
134 (ert-simulate-command '(yas/expand))
135 (should (string= (buffer-substring-no-properties (point-min) (point-max))
136 "brother from another mother"))))))
137
138
139
140 ;;; Helpers
141 ;;;
142
143 (defun yas/mock-insert (string)
144 (interactive)
145 (do ((i 0 (1+ i)))
146 ((= i (length string)))
147 (insert (aref string i))))
148
149 (defun yas/make-file-or-dirs (ass)
150 (let ((file-or-dir-name (car ass))
151 (content (cdr ass)))
152 (cond ((listp content)
153 (make-directory file-or-dir-name 'parents)
154 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
155 (mapc #'yas/make-file-or-dirs content)))
156 ((stringp content)
157 (with-current-buffer (find-file file-or-dir-name)
158 (insert content)
159 (save-buffer)
160 (kill-buffer)))
161 (t
162 (message "[yas] oops don't know this content")))))
163
164
165 (defun yas/variables ()
166 (let ((syms))
167 (mapatoms #'(lambda (sym)
168 (if (and (string-match "^yas/[^/]" (symbol-name sym))
169 (boundp sym))
170 (push sym syms))))
171 syms))
172
173
174 (defmacro yas/saving-variables (&rest body)
175 `(let ,(mapcar #'(lambda (sym)
176 `(,sym ,sym))
177 (yas/variables))
178 ,@body))
179
180 (defmacro with-snippet-dirs (dirs &rest body)
181 `(let ((default-directory (make-temp-file "yasnippet-fixture" t)))
182 (setq yas/snippet-dirs ',(mapcar #'car (cadr dirs)))
183 (mapc #'yas/make-file-or-dirs ,dirs)
184 ,@body))
185
186
187 (provide 'yasnippet-tests)
188 ;;; yasnippet-tests.el ends here