]> code.delx.au - gnu-emacs-elpa/blob - yasnippet-tests.el
Closes #271: 2nd try, remove unescaping of backquote replacements
[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 ;; Test basic snippet mechanics and the loading system
24
25 ;;; Code:
26
27 (require 'yasnippet)
28 (require 'ert)
29 (require 'ert-x)
30
31 \f
32 ;;; Snippet mechanics
33
34 (defun yas--buffer-contents ()
35 (buffer-substring-no-properties (point-min) (point-max)))
36
37 (ert-deftest field-navigation ()
38 (with-temp-buffer
39 (yas-minor-mode 1)
40 (yas-expand-snippet "${1:brother} from another ${2:mother}")
41 (should (string= (yas--buffer-contents)
42 "brother from another mother"))
43
44 (should (looking-at "brother"))
45 (ert-simulate-command '(yas-next-field-or-maybe-expand))
46 (should (looking-at "mother"))
47 (ert-simulate-command '(yas-prev-field))
48 (should (looking-at "brother"))))
49
50 (ert-deftest simple-mirror ()
51 (with-temp-buffer
52 (yas-minor-mode 1)
53 (yas-expand-snippet "${1:brother} from another $1")
54 (should (string= (yas--buffer-contents)
55 "brother from another brother"))
56 (ert-simulate-command `(yas-mock-insert "bla"))
57 (should (string= (yas--buffer-contents)
58 "bla from another bla"))))
59
60 (ert-deftest mirror-with-transformation ()
61 (with-temp-buffer
62 (yas-minor-mode 1)
63 (yas-expand-snippet "${1:brother} from another ${1:$(upcase yas-text)}")
64 (should (string= (yas--buffer-contents)
65 "brother from another BROTHER"))
66 (ert-simulate-command `(yas-mock-insert "bla"))
67 (should (string= (yas--buffer-contents)
68 "bla from another BLA"))))
69
70 (ert-deftest nested-placeholders-kill-superfield ()
71 (with-temp-buffer
72 (yas-minor-mode 1)
73 (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
74 (should (string= (yas--buffer-contents)
75 "brother from another mother!"))
76 (ert-simulate-command `(yas-mock-insert "bla"))
77 (should (string= (yas--buffer-contents)
78 "brother from bla!"))))
79
80 (ert-deftest nested-placeholders-use-subfield ()
81 (with-temp-buffer
82 (yas-minor-mode 1)
83 (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
84 (ert-simulate-command '(yas-next-field-or-maybe-expand))
85 (ert-simulate-command `(yas-mock-insert "bla"))
86 (should (string= (yas--buffer-contents)
87 "brother from another bla!"))))
88
89 ;; (ert-deftest in-snippet-undo ()
90 ;; (with-temp-buffer
91 ;; (yas-minor-mode 1)
92 ;; (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
93 ;; (ert-simulate-command '(yas-next-field-or-maybe-expand))
94 ;; (ert-simulate-command `(yas-mock-insert "bla"))
95 ;; (ert-simulate-command '(undo))
96 ;; (should (string= (yas--buffer-contents)
97 ;; "brother from another mother!"))))
98
99 \f
100 ;;; Snippet expansion
101 ;;;
102 (ert-deftest escape-dollar ()
103 (with-temp-buffer
104 (yas-minor-mode 1)
105 (yas-expand-snippet "bla\\${1:bla}ble")
106 (should (string= (yas--buffer-contents) "bla${1:bla}ble"))))
107
108 (ert-deftest escape-closing-brace ()
109 (with-temp-buffer
110 (yas-minor-mode 1)
111 (yas-expand-snippet "bla${1:bla\\}}ble")
112 (should (string= (yas--buffer-contents) "blabla}ble"))
113 (should (string= (yas-field-value 1) "bla}"))))
114
115 (ert-deftest escape-backslashes ()
116 (with-temp-buffer
117 (yas-minor-mode 1)
118 (yas-expand-snippet "bla\\ble")
119 (should (string= (yas--buffer-contents) "bla\\ble"))))
120
121 (ert-deftest escape-some-elisp-with-strings ()
122 "elisp with strings and unbalance parens inside it"
123 (with-temp-buffer
124 (yas-minor-mode 1)
125 ;; The rules here is: to output a literal `"' you need to escape
126 ;; it with one backslash. You don't need to escape them in
127 ;; embedded elisp.
128 (yas-expand-snippet "soon \\\"`(concat (upcase \"(my arms\")\"\\\" were all around her\")`")
129 (should (string= (yas--buffer-contents) "soon \"(MY ARMS\" were all around her"))))
130
131 (ert-deftest escape-some-elisp-with-backslashes ()
132 (with-temp-buffer
133 (yas-minor-mode 1)
134 ;; And the rule here is: to output a literal `\' inside a string
135 ;; inside embedded elisp you need a total of six `\'
136 (yas-expand-snippet "bla`(upcase \"hey\\\\\\yo\")`ble")
137 (should (string= (yas--buffer-contents) "blaHEY\\YOble"))))
138
139 (ert-deftest be-careful-when-escaping-in-yas-selected-text ()
140 (with-temp-buffer
141 (yas-minor-mode 1)
142 (let ((yas/selected-text "He\\\\o world!"))
143 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
144 (should (string= (yas--buffer-contents) "Look ma! He\\\\o world!")))
145 (yas-exit-all-snippets)
146 (erase-buffer)
147 (let ((yas/selected-text "He\"o world!"))
148 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
149 (should (string= (yas--buffer-contents) "Look ma! He\"o world!")))
150 (yas-exit-all-snippets)
151 (erase-buffer)
152 (let ((yas/selected-text "He\"\)\\o world!"))
153 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
154 (should (string= (yas--buffer-contents) "Look ma! He\"\)\\o world!")))
155 (yas-exit-all-snippets)
156 (erase-buffer)))
157
158 (ert-deftest be-careful-when-escaping-in-yas-selected-text-2 ()
159 (with-temp-buffer
160 (let ((yas/selected-text "He)}o world!"))
161 (yas-expand-snippet "Look ma! ${1:`(yas/selected-text)`} OK?")
162 (should (string= (yas--buffer-contents) "Look ma! He)}o world! OK?")))))
163
164 (ert-deftest mirror-transformation ()
165 (with-temp-buffer
166 (yas-minor-mode 1)
167 (let ((snippet "${1:`(concat \"foo\" \"bar\")`} ${1:$(concat (upcase yas/text) \"baz\")}"))
168 (yas-expand-snippet snippet)
169 (should (string= (yas--buffer-contents) "foobar FOOBARbaz"))
170 (yas-exit-all-snippets)
171 (erase-buffer)
172 (yas-expand-snippet snippet)
173 (ert-simulate-command `(yas-mock-insert "bla"))
174 (should (string= (yas--buffer-contents) "bla BLAbaz")))))
175
176 (ert-deftest primary-field-transformation ()
177 (with-temp-buffer
178 (yas-minor-mode 1)
179 (let ((snippet "${1:$$(upcase yas/text)}${1:$(concat \"bar\" yas/text)}"))
180 (yas-expand-snippet snippet)
181 (should (string= (yas--buffer-contents) "bar"))
182 (ert-simulate-command `(yas-mock-insert "foo"))
183 (should (string= (yas--buffer-contents) "FOObarFOO")))))
184
185 (ert-deftest example-for-issue-271 ()
186 (with-temp-buffer
187 (yas-minor-mode 1)
188 (let ((yas-selected-text "aaa")
189 (snippet "if ${1:condition}\n`yas/selected-text`\nelse\n$3\nend"))
190 (yas-expand-snippet snippet)
191 (yas-next-field)
192 (ert-simulate-command `(yas-mock-insert "bbb"))
193 (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
194
195 (ert-deftest another-example-for-issue-271 ()
196 (with-temp-buffer
197 (yas-minor-mode 1)
198 (let ((snippet "\\${${1:1}:`yas/selected-text`}"))
199 (insert "aaabbbccc")
200 (set-mark 4)
201 (goto-char 7)
202 (yas-expand-snippet snippet)
203 (ert-simulate-command `(yas-mock-insert "bbb"))
204 (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
205
206 \f
207 ;;; Misc tests
208 ;;;
209
210 (ert-deftest protection-overlay-no-cheating ()
211 "Protection overlays at the very end of the buffer, are dealt by cheatingly inserting a newline!
212
213 TODO: correct this bug!"
214 :expected-result :failed
215 (with-temp-buffer
216 (yas-minor-mode 1)
217 (yas-expand-snippet "${2:brother} from another ${1:mother}")
218 (should (string= (yas--buffer-contents)
219 "brother from another mother") ;; no newline should be here!
220 )))
221 \f
222 ;;; Loading
223 ;;;
224 (defmacro yas-with-some-interesting-snippet-dirs (&rest body)
225 `(yas-saving-variables
226 (yas-with-snippet-dirs
227 '((".emacs.d/snippets"
228 ("c-mode"
229 (".yas-parents" . "cc-mode")
230 ("printf" . "printf($1);"))
231 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
232 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
233 ("library/snippets"
234 ("c-mode" (".yas-parents" . "c++-mode"))
235 ("cc-mode" ("def" . "# define"))
236 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
237 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
238 ,@body)))
239
240 (ert-deftest basic-jit-loading ()
241 "Test basic loading and expansion of snippets"
242 (yas-with-some-interesting-snippet-dirs
243 (yas-reload-all)
244 (yas--basic-jit-loading-1)))
245
246 (ert-deftest basic-jit-loading-with-compiled-snippets ()
247 "Test basic loading and expansion of snippets"
248 (yas-with-some-interesting-snippet-dirs
249 (yas-reload-all)
250 (yas-recompile-all)
251 (flet ((yas--load-directory-2
252 (&rest dummies)
253 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
254 (yas-reload-all)
255 (yas--basic-jit-loading-1))))
256
257 (defun yas--basic-jit-loading-1 (&optional compile)
258 (with-temp-buffer
259 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
260 (should (= 0 (hash-table-count yas--tables)))
261 (lisp-interaction-mode)
262 (yas-minor-mode 1)
263 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
264 (should (= 2 (hash-table-count yas--tables)))
265 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
266 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
267 (yas-should-expand '(("sc" . "brother from another mother")
268 ("dolist" . "(dolist)")
269 ("ert-deftest" . "(ert-deftest name () )")))
270 (c-mode)
271 (yas-minor-mode 1)
272 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
273 (should (= 4 (hash-table-count yas--tables)))
274 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
275 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
276 (yas-should-expand '(("printf" . "printf();")
277 ("def" . "# define")))
278 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
279
280 \f
281 ;;; Menu
282 ;;;
283 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
284 `(yas-saving-variables
285 (yas-with-snippet-dirs
286 `((".emacs.d/snippets"
287 ("c-mode"
288 (".yas-make-groups" . "")
289 ("printf" . "printf($1);")
290 ("foo-group-a"
291 ("fnprintf" . "fprintf($1);")
292 ("snprintf" . "snprintf($1);"))
293 ("foo-group-b"
294 ("strcmp" . "strecmp($1);")
295 ("strcasecmp" . "strcasecmp($1);")))
296 ("lisp-interaction-mode"
297 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
298 ("fancy-mode"
299 ("a-guy" . "# uuid: 999\n# --\nyo!")
300 ("a-sir" . "# uuid: 12345\n# --\nindeed!")
301 ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
302 ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
303 ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
304 (".yas-setup.el" . , (pp-to-string
305 '(yas-define-menu 'fancy-mode
306 '((yas-ignore-item "0101")
307 (yas-item "999")
308 (yas-submenu "sirs"
309 ((yas-item "12345")))
310 (yas-submenu "ladies"
311 ((yas-item "54321"))))
312 '("666")))))))
313 ,@body)))
314
315 (ert-deftest test-yas-define-menu ()
316 (let ((yas-use-menu t))
317 (yas-with-even-more-interesting-snippet-dirs
318 (yas-reload-all 'no-jit)
319 (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
320 (should (eql 4 (length menu)))
321 (dolist (item '("a-guy" "a-beggar"))
322 (should (find item menu :key #'third :test #'string=)))
323 (should-not (find "an-outcast" menu :key #'third :test #'string=))
324 (dolist (submenu '("sirs" "ladies"))
325 (should (keymapp
326 (fourth
327 (find submenu menu :key #'third :test #'string=)))))
328 ))))
329
330 (ert-deftest test-group-menus ()
331 "Test group-based menus using .yas-make-groups and the group directive"
332 (let ((yas-use-menu t))
333 (yas-with-even-more-interesting-snippet-dirs
334 (yas-reload-all 'no-jit)
335 ;; first the subdir-based groups
336 ;;
337 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
338 (should (eql 3 (length menu)))
339 (dolist (item '("printf" "foo-group-a" "foo-group-b"))
340 (should (find item menu :key #'third :test #'string=)))
341 (dolist (submenu '("foo-group-a" "foo-group-b"))
342 (should (keymapp
343 (fourth
344 (find submenu menu :key #'third :test #'string=))))))
345 ;; now group directives
346 ;;
347 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
348 (should (eql 1 (length menu)))
349 (should (find "barbar" menu :key #'third :test #'string=))
350 (should (keymapp
351 (fourth
352 (find "barbar" menu :key #'third :test #'string=))))))))
353
354 (ert-deftest test-group-menus-twisted ()
355 "Same as similarly named test, but be mean.
356
357 TODO: be meaner"
358 (let ((yas-use-menu t))
359 (yas-with-even-more-interesting-snippet-dirs
360 ;; add a group directive conflicting with the subdir and watch
361 ;; behaviour
362 (with-temp-buffer
363 (insert "# group: foo-group-c\n# --\nstrecmp($1)")
364 (write-region nil nil (concat (first (yas-snippet-dirs))
365 "/c-mode/foo-group-b/strcmp")))
366 (yas-reload-all 'no-jit)
367 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
368 (should (eql 4 (length menu)))
369 (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
370 (should (find item menu :key #'third :test #'string=)))
371 (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
372 (should (keymapp
373 (fourth
374 (find submenu menu :key #'third :test #'string=))))))
375 ;; delete the .yas-make-groups file and watch behaviour
376 ;;
377 (delete-file (concat (first (yas-snippet-dirs))
378 "/c-mode/.yas-make-groups"))
379 (yas-reload-all 'no-jit)
380 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
381 (should (eql 5 (length menu))))
382 ;; Change a group directive and reload
383 ;;
384 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
385 (should (find "barbar" menu :key #'third :test #'string=)))
386
387 (with-temp-buffer
388 (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
389 (write-region nil nil (concat (first (yas-snippet-dirs))
390 "/lisp-interaction-mode/ert-deftest")))
391 (yas-reload-all 'no-jit)
392 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
393 (should (eql 1 (length menu)))
394 (should (find "foofoo" menu :key #'third :test #'string=))
395 (should (keymapp
396 (fourth
397 (find "foofoo" menu :key #'third :test #'string=))))))))
398
399 \f
400 ;;; Helpers
401 ;;;
402
403 (defun yas-should-expand (keys-and-expansions)
404 (dolist (key-and-expansion keys-and-expansions)
405 (yas-exit-all-snippets)
406 (erase-buffer)
407 (insert (car key-and-expansion))
408 (let ((yas-fallback-behavior nil))
409 (ert-simulate-command '(yas-expand)))
410 (should (string= (yas--buffer-contents) (cdr key-and-expansion))))
411 (yas-exit-all-snippets))
412
413 (defun yas-should-not-expand (keys)
414 (dolist (key keys)
415 (yas-exit-all-snippets)
416 (erase-buffer)
417 (insert key)
418 (let ((yas-fallback-behavior nil))
419 (ert-simulate-command '(yas-expand)))
420 (should (string= (yas--buffer-contents) key))))
421
422 (defun yas-mock-insert (string)
423 (interactive)
424 (do ((i 0 (1+ i)))
425 ((= i (length string)))
426 (insert (aref string i))))
427
428 (defun yas-make-file-or-dirs (ass)
429 (let ((file-or-dir-name (car ass))
430 (content (cdr ass)))
431 (cond ((listp content)
432 (make-directory file-or-dir-name 'parents)
433 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
434 (mapc #'yas-make-file-or-dirs content)))
435 ((stringp content)
436 (with-temp-buffer
437 (insert content)
438 (write-region nil nil file-or-dir-name nil 'nomessage)))
439 (t
440 (message "[yas] oops don't know this content")))))
441
442
443 (defun yas-variables ()
444 (let ((syms))
445 (mapatoms #'(lambda (sym)
446 (if (and (string-match "^yas-[^/]" (symbol-name sym))
447 (boundp sym))
448 (push sym syms))))
449 syms))
450
451 (defun yas-call-with-saving-variables (fn)
452 (let* ((vars (yas-variables))
453 (saved-values (mapcar #'symbol-value vars)))
454 (unwind-protect
455 (funcall fn)
456 (loop for var in vars
457 for saved in saved-values
458 do (set var saved)))))
459
460 (defmacro yas-saving-variables (&rest body)
461 `(yas-call-with-saving-variables #'(lambda () ,@body)))
462
463
464 (defun yas-call-with-snippet-dirs (dirs fn)
465 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
466 (yas-snippet-dirs (mapcar #'car dirs)))
467 (with-temp-message ""
468 (unwind-protect
469 (progn
470 (mapc #'yas-make-file-or-dirs dirs)
471 (funcall fn))
472 (when (>= emacs-major-version 23)
473 (delete-directory default-directory 'recursive))))))
474
475 (defmacro yas-with-snippet-dirs (dirs &rest body)
476 `(yas-call-with-snippet-dirs ,dirs
477 #'(lambda ()
478 ,@body)))
479
480 ;;; Older emacsen
481 ;;;
482 (unless (fboundp 'special-mode)
483 (define-minor-mode special-mode "Just a placeholder for something isn't in emacs 22"))
484
485 ;;; btw to test this in emacs22 mac osx:
486 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
487 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
488 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
489
490
491 (provide 'yasnippet-tests)
492 ;;; yasnippet-tests.el ends here
493 ;; Local Variables:
494 ;; lexical-binding: t
495 ;; End: