1 ;;; yasnippet-tests.el --- some yasnippet tests -*- lexical-binding: t -*-
3 ;; Copyright (C) 2012, 2013, 2014 Free Software Foundation, Inc.
5 ;; Author: João Távora <joaot@siscog.pt>
6 ;; Keywords: emulations, convenience
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.
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.
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/>.
23 ;; Test basic snippet mechanics and the loading system
35 (defun yas--buffer-contents ()
36 (buffer-substring-no-properties (point-min) (point-max)))
38 (ert-deftest field-navigation ()
41 (yas-expand-snippet "${1:brother} from another ${2:mother}")
42 (should (string= (yas--buffer-contents)
43 "brother from another mother"))
45 (should (looking-at "brother"))
46 (ert-simulate-command '(yas-next-field-or-maybe-expand))
47 (should (looking-at "mother"))
48 (ert-simulate-command '(yas-prev-field))
49 (should (looking-at "brother"))))
51 (ert-deftest simple-mirror ()
54 (yas-expand-snippet "${1:brother} from another $1")
55 (should (string= (yas--buffer-contents)
56 "brother from another brother"))
57 (ert-simulate-command `(yas-mock-insert "bla"))
58 (should (string= (yas--buffer-contents)
59 "bla from another bla"))))
61 (ert-deftest mirror-with-transformation ()
64 (yas-expand-snippet "${1:brother} from another ${1:$(upcase yas-text)}")
65 (should (string= (yas--buffer-contents)
66 "brother from another BROTHER"))
67 (ert-simulate-command `(yas-mock-insert "bla"))
68 (should (string= (yas--buffer-contents)
69 "bla from another BLA"))))
71 (ert-deftest primary-field-transformation ()
74 (let ((snippet "${1:$$(upcase yas/text)}${1:$(concat \"bar\" yas/text)}"))
75 (yas-expand-snippet snippet)
76 (should (string= (yas--buffer-contents) "bar"))
77 (ert-simulate-command `(yas-mock-insert "foo"))
78 (should (string= (yas--buffer-contents) "FOObarFOO")))))
80 (ert-deftest nested-placeholders-kill-superfield ()
83 (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
84 (should (string= (yas--buffer-contents)
85 "brother from another mother!"))
86 (ert-simulate-command `(yas-mock-insert "bla"))
87 (should (string= (yas--buffer-contents)
88 "brother from bla!"))))
90 (ert-deftest nested-placeholders-use-subfield ()
93 (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
94 (ert-simulate-command '(yas-next-field-or-maybe-expand))
95 (ert-simulate-command `(yas-mock-insert "bla"))
96 (should (string= (yas--buffer-contents)
97 "brother from another bla!"))))
99 (ert-deftest mirrors-adjacent-to-fields-with-nested-mirrors ()
102 (yas-expand-snippet "<%= f.submit \"${1:Submit}\"${2:$(and (yas-text) \", :disable_with => '\")}${2:$1ing...}${2:$(and (yas-text) \"'\")} %>")
103 (should (string= (yas--buffer-contents)
104 "<%= f.submit \"Submit\", :disable_with => 'Submiting...' %>"))
105 (ert-simulate-command `(yas-mock-insert "Send"))
106 (should (string= (yas--buffer-contents)
107 "<%= f.submit \"Send\", :disable_with => 'Sending...' %>"))))
109 (ert-deftest deep-nested-mirroring-issue-351 ()
112 (yas-expand-snippet "${1:FOOOOOOO}${2:$1}${3:$2}${4:$3}")
113 (ert-simulate-command `(yas-mock-insert "abc"))
114 (should (string= (yas--buffer-contents) "abcabcabcabc"))))
116 ;; (ert-deftest in-snippet-undo ()
118 ;; (yas-minor-mode 1)
119 ;; (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
120 ;; (ert-simulate-command '(yas-next-field-or-maybe-expand))
121 ;; (ert-simulate-command `(yas-mock-insert "bla"))
122 ;; (ert-simulate-command '(undo))
123 ;; (should (string= (yas--buffer-contents)
124 ;; "brother from another mother!"))))
127 ;;; Snippet expansion and character escaping
128 ;;; Thanks to @zw963 (Billy) for the testing
130 (ert-deftest escape-dollar ()
133 (yas-expand-snippet "bla\\${1:bla}ble")
134 (should (string= (yas--buffer-contents) "bla${1:bla}ble"))))
136 (ert-deftest escape-closing-brace ()
139 (yas-expand-snippet "bla${1:bla\\}}ble")
140 (should (string= (yas--buffer-contents) "blabla}ble"))
141 (should (string= (yas-field-value 1) "bla}"))))
143 (ert-deftest escape-backslashes ()
146 (yas-expand-snippet "bla\\ble")
147 (should (string= (yas--buffer-contents) "bla\\ble"))))
149 (ert-deftest escape-backquotes ()
152 (yas-expand-snippet "bla`(upcase \"foo\\`bar\")`ble")
153 (should (string= (yas--buffer-contents) "blaFOO`BARble"))))
155 (ert-deftest escape-some-elisp-with-strings ()
156 "elisp with strings and unbalance parens inside it"
159 ;; The rules here is: to output a literal `"' you need to escape
160 ;; it with one backslash. You don't need to escape them in
162 (yas-expand-snippet "soon \\\"`(concat (upcase \"(my arms\")\"\\\" were all around her\")`")
163 (should (string= (yas--buffer-contents) "soon \"(MY ARMS\" were all around her"))))
165 (ert-deftest escape-some-elisp-with-backslashes ()
168 ;; And the rule here is: to output a literal `\' inside a string
169 ;; inside embedded elisp you need a total of six `\'
170 (yas-expand-snippet "bla`(upcase \"hey\\\\\\yo\")`ble")
171 (should (string= (yas--buffer-contents) "blaHEY\\YOble"))))
173 (ert-deftest be-careful-when-escaping-in-yas-selected-text ()
176 (let ((yas/selected-text "He\\\\o world!"))
177 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
178 (should (string= (yas--buffer-contents) "Look ma! He\\\\o world!")))
179 (yas-exit-all-snippets)
181 (let ((yas/selected-text "He\"o world!"))
182 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
183 (should (string= (yas--buffer-contents) "Look ma! He\"o world!")))
184 (yas-exit-all-snippets)
186 (let ((yas/selected-text "He\"\)\\o world!"))
187 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
188 (should (string= (yas--buffer-contents) "Look ma! He\"\)\\o world!")))
189 (yas-exit-all-snippets)
192 (ert-deftest be-careful-when-escaping-in-yas-selected-text-2 ()
194 (let ((yas/selected-text "He)}o world!"))
195 (yas-expand-snippet "Look ma! ${1:`(yas/selected-text)`} OK?")
196 (should (string= (yas--buffer-contents) "Look ma! He)}o world! OK?")))))
198 (ert-deftest example-for-issue-271 ()
201 (let ((yas-selected-text "aaa")
202 (snippet "if ${1:condition}\n`yas/selected-text`\nelse\n$3\nend"))
203 (yas-expand-snippet snippet)
205 (ert-simulate-command `(yas-mock-insert "bbb"))
206 (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
208 (ert-deftest another-example-for-issue-271 ()
209 ;; expect this to fail in batch mode since `region-active-p' doesn't
210 ;; used by `yas-expand-snippet' doesn't make sense in that context.
212 :expected-result (if noninteractive
217 (let ((snippet "\\${${1:1}:`yas/selected-text`}"))
221 (yas-expand-snippet snippet)
222 (should (string= (yas--buffer-contents) "aaa${1:bbb}ccc")))))
224 (ert-deftest string-match-with-subregexp-in-embedded-elisp ()
227 ;; the rule here is: To use regexps in embedded `(elisp)` expressions, write
228 ;; it like you would normal elisp, i.e. no need to escape the backslashes.
229 (let ((snippet "`(if (string-match \"foo\\\\(ba+r\\\\)foo\" \"foobaaaaaaaaaarfoo\")
232 (yas-expand-snippet snippet))
233 (should (string= (yas--buffer-contents) "ok"))))
235 (ert-deftest string-match-with-subregexp-in-mirror-transformations ()
238 ;; the rule here is: To use regexps in embedded `(elisp)` expressions,
239 ;; escape backslashes once, i.e. to use \\( \\) constructs, write \\\\( \\\\).
240 (let ((snippet "$1${1:$(if (string-match \"foo\\\\\\\\(ba+r\\\\\\\\)baz\" yas/text)
243 (yas-expand-snippet snippet)
244 (should (string= (yas--buffer-contents) "fail"))
245 (ert-simulate-command `(yas-mock-insert "foobaaar"))
246 (should (string= (yas--buffer-contents) "foobaaarfail"))
247 (ert-simulate-command `(yas-mock-insert "baz"))
248 (should (string= (yas--buffer-contents) "foobaaarbazok")))))
253 (ert-deftest protection-overlay-no-cheating ()
254 "Protection overlays at the very end of the buffer are dealt
255 with by cheatingly inserting a newline!
257 TODO: correct this bug!"
258 :expected-result :failed
261 (yas-expand-snippet "${2:brother} from another ${1:mother}")
262 (should (string= (yas--buffer-contents)
263 "brother from another mother") ;; no newline should be here!
268 (defun yas--call-with-temporary-redefinitions (function
269 &rest function-names-and-overriding-functions)
270 (let* ((overrides (remove-if-not #'(lambda (fdef)
271 (fboundp (first fdef)))
272 function-names-and-overriding-functions))
273 (definition-names (mapcar #'first overrides))
274 (overriding-functions (mapcar #'second overrides))
275 (saved-functions (mapcar #'symbol-function definition-names)))
276 ;; saving all definitions before overriding anything ensures FDEFINITION
277 ;; errors don't cause accidental permanent redefinitions.
279 (labels ((set-fdefinitions (names functions)
280 (loop for name in names
283 (set-fdefinitions definition-names overriding-functions)
284 (unwind-protect (funcall function)
285 (set-fdefinitions definition-names saved-functions)))))
287 (defmacro yas--with-temporary-redefinitions (fdefinitions &rest body)
288 ;; "Temporarily (but globally) redefine each function in FDEFINITIONS.
289 ;; E.g.: (yas--with-temporary-redefinitions ((foo (x) ...)
291 ;; ;; code that eventually calls foo, bar of (setf foo)
293 ;; FIXME: This is hideous! Better use defadvice (or at least letf).
294 `(yas--call-with-temporary-redefinitions
296 ,@(mapcar #'(lambda (thingy)
297 `(list ',(first thingy)
298 (lambda ,@(rest thingy))))
301 (put 'yas--with-temporary-redefinitions 'lisp-indent-function 1)
302 (put 'yas--with-temporary-redefinitions 'edebug-form-spec '((&rest (defun*)) cl-declarations body))
304 (defmacro yas-with-overriden-buffer-list (&rest body)
305 (let ((saved-sym (make-symbol "yas--buffer-list")))
306 `(let ((,saved-sym (symbol-function 'buffer-list)))
307 (yas--with-temporary-redefinitions
309 (remove-if #'(lambda (buf)
310 (with-current-buffer buf
311 (eq major-mode 'lisp-interaction-mode)))
312 (funcall ,saved-sym))))
315 (defmacro yas-with-some-interesting-snippet-dirs (&rest body)
316 `(yas-saving-variables
317 (yas-with-overriden-buffer-list
318 (yas-with-snippet-dirs
319 '((".emacs.d/snippets"
321 (".yas-parents" . "cc-mode")
322 ("printf" . "printf($1);")) ;; notice the overriding for issue #281
323 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
324 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
327 (".yas-parents" . "c++-mode")
328 ("printf" . "printf"))
329 ("cc-mode" ("def" . "# define"))
330 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
331 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
334 (ert-deftest basic-jit-loading ()
335 "Test basic loading and expansion of snippets"
336 (yas-with-some-interesting-snippet-dirs
338 (yas--basic-jit-loading-1)))
340 (ert-deftest basic-jit-loading-with-compiled-snippets ()
341 "Test basic loading and expansion of snippets"
342 (yas-with-some-interesting-snippet-dirs
345 (yas--with-temporary-redefinitions ((yas--load-directory-2
347 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
349 (yas--basic-jit-loading-1))))
351 (ert-deftest loading-with-cyclic-parenthood ()
352 "Test loading when cyclic parenthood is setup."
353 (yas-saving-variables
354 (yas-with-snippet-dirs '((".emacs.d/snippets"
356 (".yas-parents" . "cc-mode"))
358 (".yas-parents" . "yet-another-c-mode"))
359 ("yet-another-c-mode"
360 (".yas-parents" . "c-mode"))))
363 (yas--all-parents 'c-mode)
365 (ert-fail "cyclic parenthood test failed"))))))
367 (defun yas--basic-jit-loading-1 ()
369 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
370 (should (= 0 (hash-table-count yas--tables)))
371 (lisp-interaction-mode)
373 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
374 (should (= 2 (hash-table-count yas--tables)))
375 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
376 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
377 (yas-should-expand '(("sc" . "brother from another mother")
378 ("dolist" . "(dolist)")
379 ("ert-deftest" . "(ert-deftest name () )")))
382 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
383 (should (= 4 (hash-table-count yas--tables)))
384 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
385 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
386 (yas-should-expand '(("printf" . "printf();")
387 ("def" . "# define")))
388 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
393 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
394 `(yas-saving-variables
395 (yas-with-snippet-dirs
396 `((".emacs.d/snippets"
398 (".yas-make-groups" . "")
399 ("printf" . "printf($1);")
401 ("fnprintf" . "fprintf($1);")
402 ("snprintf" . "snprintf($1);"))
404 ("strcmp" . "strecmp($1);")
405 ("strcasecmp" . "strcasecmp($1);")))
406 ("lisp-interaction-mode"
407 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
409 ("a-guy" . "# uuid: 999\n# --\nyo!")
410 ("a-sir" . "# uuid: 12345\n# --\nindeed!")
411 ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
412 ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
413 ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
414 (".yas-setup.el" . , (pp-to-string
415 '(yas-define-menu 'fancy-mode
416 '((yas-ignore-item "0101")
419 ((yas-item "12345")))
420 (yas-submenu "ladies"
421 ((yas-item "54321"))))
425 (ert-deftest test-yas-define-menu ()
426 (let ((yas-use-menu t))
427 (yas-with-even-more-interesting-snippet-dirs
428 (yas-reload-all 'no-jit)
429 (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
430 (should (eql 4 (length menu)))
431 (dolist (item '("a-guy" "a-beggar"))
432 (should (find item menu :key #'third :test #'string=)))
433 (should-not (find "an-outcast" menu :key #'third :test #'string=))
434 (dolist (submenu '("sirs" "ladies"))
437 (find submenu menu :key #'third :test #'string=)))))
440 (ert-deftest test-group-menus ()
441 "Test group-based menus using .yas-make-groups and the group directive"
442 (let ((yas-use-menu t))
443 (yas-with-even-more-interesting-snippet-dirs
444 (yas-reload-all 'no-jit)
445 ;; first the subdir-based groups
447 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
448 (should (eql 3 (length menu)))
449 (dolist (item '("printf" "foo-group-a" "foo-group-b"))
450 (should (find item menu :key #'third :test #'string=)))
451 (dolist (submenu '("foo-group-a" "foo-group-b"))
454 (find submenu menu :key #'third :test #'string=))))))
455 ;; now group directives
457 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
458 (should (eql 1 (length menu)))
459 (should (find "barbar" menu :key #'third :test #'string=))
462 (find "barbar" menu :key #'third :test #'string=))))))))
464 (ert-deftest test-group-menus-twisted ()
465 "Same as similarly named test, but be mean.
468 (let ((yas-use-menu t))
469 (yas-with-even-more-interesting-snippet-dirs
470 ;; add a group directive conflicting with the subdir and watch
473 (insert "# group: foo-group-c\n# --\nstrecmp($1)")
474 (write-region nil nil (concat (first (yas-snippet-dirs))
475 "/c-mode/foo-group-b/strcmp")))
476 (yas-reload-all 'no-jit)
477 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
478 (should (eql 4 (length menu)))
479 (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
480 (should (find item menu :key #'third :test #'string=)))
481 (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
484 (find submenu menu :key #'third :test #'string=))))))
485 ;; delete the .yas-make-groups file and watch behaviour
487 (delete-file (concat (first (yas-snippet-dirs))
488 "/c-mode/.yas-make-groups"))
489 (yas-reload-all 'no-jit)
490 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
491 (should (eql 5 (length menu))))
492 ;; Change a group directive and reload
494 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
495 (should (find "barbar" menu :key #'third :test #'string=)))
498 (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
499 (write-region nil nil (concat (first (yas-snippet-dirs))
500 "/lisp-interaction-mode/ert-deftest")))
501 (yas-reload-all 'no-jit)
502 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
503 (should (eql 1 (length menu)))
504 (should (find "foofoo" menu :key #'third :test #'string=))
507 (find "foofoo" menu :key #'third :test #'string=))))))))
510 ;;; The infamous and problematic tab keybinding
512 (ert-deftest test-yas-tab-binding ()
515 (should (not (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand)))
517 (should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))
518 (yas-expand-snippet "$1 $2 $3")
519 (should (eq (key-binding [(tab)]) 'yas-next-field-or-maybe-expand))
520 (should (eq (key-binding (kbd "TAB")) 'yas-next-field-or-maybe-expand))
521 (should (eq (key-binding [(shift tab)]) 'yas-prev-field))
522 (should (eq (key-binding [backtab]) 'yas-prev-field))))
524 (ert-deftest test-rebindings ()
527 (define-key yas-minor-mode-map [tab] nil)
528 (define-key yas-minor-mode-map (kbd "TAB") nil)
529 (define-key yas-minor-mode-map (kbd "SPC") 'yas-expand)
532 (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
533 (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))
535 (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
536 (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))))
537 (setcdr yas-minor-mode-map (cdr (yas--init-minor-keymap)))))
539 (ert-deftest test-yas-in-org ()
543 (should (eq (key-binding [(tab)]) 'yas-expand))
544 (should (eq (key-binding (kbd "TAB")) 'yas-expand))))
552 (yas--with-temporary-redefinitions
553 ((message (&rest _args) nil))
554 (ert t (buffer-name (current-buffer)))
555 (princ (buffer-string)))))
558 (defun yas-should-expand (keys-and-expansions)
559 (dolist (key-and-expansion keys-and-expansions)
560 (yas-exit-all-snippets)
562 (insert (car key-and-expansion))
563 (let ((yas-fallback-behavior nil))
564 (ert-simulate-command '(yas-expand)))
565 (should (string= (yas--buffer-contents) (cdr key-and-expansion))))
566 (yas-exit-all-snippets))
568 (defun yas-should-not-expand (keys)
570 (yas-exit-all-snippets)
573 (let ((yas-fallback-behavior nil))
574 (ert-simulate-command '(yas-expand)))
575 (should (string= (yas--buffer-contents) key))))
577 (defun yas-mock-insert (string)
580 ((= i (length string)))
581 (insert (aref string i))))
583 (defun yas-make-file-or-dirs (ass)
584 (let ((file-or-dir-name (car ass))
586 (cond ((listp content)
587 (make-directory file-or-dir-name 'parents)
588 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
589 (mapc #'yas-make-file-or-dirs content)))
593 (write-region nil nil file-or-dir-name nil 'nomessage)))
595 (message "[yas] oops don't know this content")))))
598 (defun yas-variables ()
600 (mapatoms #'(lambda (sym)
601 (if (and (string-match "^yas-[^/]" (symbol-name sym))
606 (defun yas-call-with-saving-variables (fn)
607 (let* ((vars (yas-variables))
608 (saved-values (mapcar #'symbol-value vars)))
611 (loop for var in vars
612 for saved in saved-values
613 do (set var saved)))))
615 (defmacro yas-saving-variables (&rest body)
616 `(yas-call-with-saving-variables #'(lambda () ,@body)))
619 (defun yas-call-with-snippet-dirs (dirs fn)
620 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
621 (yas-snippet-dirs (mapcar #'car dirs)))
622 (with-temp-message ""
625 (mapc #'yas-make-file-or-dirs dirs)
627 (when (>= emacs-major-version 24)
628 (delete-directory default-directory 'recursive))))))
630 (defmacro yas-with-snippet-dirs (dirs &rest body)
631 (declare (indent defun))
632 `(yas-call-with-snippet-dirs ,dirs
638 (unless (fboundp 'special-mode)
639 ;; FIXME: Why provide this default definition here?!?
640 (defalias 'special-mode 'fundamental))
642 ;;; btw to test this in emacs22 mac osx:
643 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
644 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
645 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
648 (provide 'yasnippet-tests)
649 ;;; yasnippet-tests.el ends here