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