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