1 ;;; yasnippet-tests.el --- some yasnippet tests
3 ;; Copyright (C) 2012, 2013 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
34 (defun yas--buffer-contents ()
35 (buffer-substring-no-properties (point-min) (point-max)))
37 (ert-deftest field-navigation ()
40 (yas-expand-snippet "${1:brother} from another ${2:mother}")
41 (should (string= (yas--buffer-contents)
42 "brother from another mother"))
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"))))
50 (ert-deftest simple-mirror ()
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"))))
60 (ert-deftest mirror-with-transformation ()
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"))))
70 (ert-deftest primary-field-transformation ()
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")))))
79 (ert-deftest nested-placeholders-kill-superfield ()
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!"))))
89 (ert-deftest nested-placeholders-use-subfield ()
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!"))))
98 (ert-deftest mirrors-adjacent-to-fields-with-nested-mirrors ()
101 (yas-expand-snippet "<%= f.submit \"${1:Submit}\"${2:$(and (yas-text) \", :disable_with => '\")}${2:$1ing...}${2:$(and (yas-text) \"'\")} %>")
102 (should (string= (yas--buffer-contents)
103 "<%= f.submit \"Submit\", :disable_with => 'Submiting...' %>"))
104 (ert-simulate-command `(yas-mock-insert "Send"))
105 (should (string= (yas--buffer-contents)
106 "<%= f.submit \"Send\", :disable_with => 'Sending...' %>"))))
108 (ert-deftest deep-nested-mirroring-issue-351 ()
111 (yas-expand-snippet "${1:FOOOOOOO}${2:$1}${3:$2}${4:$3}")
112 (ert-simulate-command `(yas-mock-insert "abc"))
113 (should (string= (yas--buffer-contents) "abcabcabcabc"))))
115 ;; (ert-deftest in-snippet-undo ()
117 ;; (yas-minor-mode 1)
118 ;; (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
119 ;; (ert-simulate-command '(yas-next-field-or-maybe-expand))
120 ;; (ert-simulate-command `(yas-mock-insert "bla"))
121 ;; (ert-simulate-command '(undo))
122 ;; (should (string= (yas--buffer-contents)
123 ;; "brother from another mother!"))))
126 ;;; Snippet expansion and character escaping
127 ;;; Thanks to @zw963 (Billy) for the testing
129 (ert-deftest escape-dollar ()
132 (yas-expand-snippet "bla\\${1:bla}ble")
133 (should (string= (yas--buffer-contents) "bla${1:bla}ble"))))
135 (ert-deftest escape-closing-brace ()
138 (yas-expand-snippet "bla${1:bla\\}}ble")
139 (should (string= (yas--buffer-contents) "blabla}ble"))
140 (should (string= (yas-field-value 1) "bla}"))))
142 (ert-deftest escape-backslashes ()
145 (yas-expand-snippet "bla\\ble")
146 (should (string= (yas--buffer-contents) "bla\\ble"))))
148 (ert-deftest escape-backquotes ()
151 (yas-expand-snippet "bla`(upcase \"foo\\`bar\")`ble")
152 (should (string= (yas--buffer-contents) "blaFOO`BARble"))))
154 (ert-deftest escape-some-elisp-with-strings ()
155 "elisp with strings and unbalance parens inside it"
158 ;; The rules here is: to output a literal `"' you need to escape
159 ;; it with one backslash. You don't need to escape them in
161 (yas-expand-snippet "soon \\\"`(concat (upcase \"(my arms\")\"\\\" were all around her\")`")
162 (should (string= (yas--buffer-contents) "soon \"(MY ARMS\" were all around her"))))
164 (ert-deftest escape-some-elisp-with-backslashes ()
167 ;; And the rule here is: to output a literal `\' inside a string
168 ;; inside embedded elisp you need a total of six `\'
169 (yas-expand-snippet "bla`(upcase \"hey\\\\\\yo\")`ble")
170 (should (string= (yas--buffer-contents) "blaHEY\\YOble"))))
172 (ert-deftest be-careful-when-escaping-in-yas-selected-text ()
175 (let ((yas/selected-text "He\\\\o world!"))
176 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
177 (should (string= (yas--buffer-contents) "Look ma! He\\\\o world!")))
178 (yas-exit-all-snippets)
180 (let ((yas/selected-text "He\"o world!"))
181 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
182 (should (string= (yas--buffer-contents) "Look ma! He\"o world!")))
183 (yas-exit-all-snippets)
185 (let ((yas/selected-text "He\"\)\\o world!"))
186 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
187 (should (string= (yas--buffer-contents) "Look ma! He\"\)\\o world!")))
188 (yas-exit-all-snippets)
191 (ert-deftest be-careful-when-escaping-in-yas-selected-text-2 ()
193 (let ((yas/selected-text "He)}o world!"))
194 (yas-expand-snippet "Look ma! ${1:`(yas/selected-text)`} OK?")
195 (should (string= (yas--buffer-contents) "Look ma! He)}o world! OK?")))))
197 (ert-deftest example-for-issue-271 ()
200 (let ((yas-selected-text "aaa")
201 (snippet "if ${1:condition}\n`yas/selected-text`\nelse\n$3\nend"))
202 (yas-expand-snippet snippet)
204 (ert-simulate-command `(yas-mock-insert "bbb"))
205 (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
207 (ert-deftest another-example-for-issue-271 ()
208 ;; expect this to fail in batch mode since `region-active-p' doesn't
209 ;; used by `yas-expand-snippet' doesn't make sense in that context.
211 :expected-result (if noninteractive
216 (let ((snippet "\\${${1:1}:`yas/selected-text`}"))
220 (yas-expand-snippet snippet)
221 (should (string= (yas--buffer-contents) "aaa${1:bbb}ccc")))))
223 (ert-deftest string-match-with-subregexp-in-embedded-elisp ()
226 ;; the rule here is: To use regexps in embedded `(elisp)` expressions, write
227 ;; it like you would normal elisp, i.e. no need to escape the backslashes.
228 (let ((snippet "`(if (string-match \"foo\\\\(ba+r\\\\)foo\" \"foobaaaaaaaaaarfoo\")
231 (yas-expand-snippet snippet))
232 (should (string= (yas--buffer-contents) "ok"))))
234 (ert-deftest string-match-with-subregexp-in-mirror-transformations ()
237 ;; the rule here is: To use regexps in embedded `(elisp)` expressions,
238 ;; escape backslashes once, i.e. to use \\( \\) constructs, write \\\\( \\\\).
239 (let ((snippet "$1${1:$(if (string-match \"foo\\\\\\\\(ba+r\\\\\\\\)baz\" yas/text)
242 (yas-expand-snippet snippet)
243 (should (string= (yas--buffer-contents) "fail"))
244 (ert-simulate-command `(yas-mock-insert "foobaaar"))
245 (should (string= (yas--buffer-contents) "foobaaarfail"))
246 (ert-simulate-command `(yas-mock-insert "baz"))
247 (should (string= (yas--buffer-contents) "foobaaarbazok")))))
252 (ert-deftest protection-overlay-no-cheating ()
253 "Protection overlays at the very end of the buffer are dealt
254 with by cheatingly inserting a newline!
256 TODO: correct this bug!"
257 :expected-result :failed
260 (yas-expand-snippet "${2:brother} from another ${1:mother}")
261 (should (string= (yas--buffer-contents)
262 "brother from another mother") ;; no newline should be here!
267 (defun yas--call-with-temporary-redefinitions (function
268 &rest function-names-and-overriding-functions)
269 (let* ((overrides (remove-if-not #'(lambda (fdef)
270 (fboundp (first fdef)))
271 function-names-and-overriding-functions))
272 (definition-names (mapcar #'first overrides))
273 (overriding-functions (mapcar #'second overrides))
274 (saved-functions (mapcar #'symbol-function definition-names)))
275 ;; saving all definitions before overriding anything ensures FDEFINITION
276 ;; errors don't cause accidental permanent redefinitions.
278 (labels ((set-fdefinitions (names functions)
279 (loop for name in names
282 (set-fdefinitions definition-names overriding-functions)
283 (unwind-protect (funcall function)
284 (set-fdefinitions definition-names saved-functions)))))
286 (defmacro yas--with-temporary-redefinitions (fdefinitions &rest body)
287 ;; "Temporarily (but globally) redefine each function in FDEFINITIONS.
288 ;; E.g.: (yas--with-temporary-redefinitions ((foo (x) ...)
290 ;; ;; code that eventually calls foo, bar of (setf foo)
292 ;; FIXME: This is hideous! Better use defadvice (or at least letf).
293 `(yas--call-with-temporary-redefinitions
295 ,@(mapcar #'(lambda (thingy)
296 `(list ',(first thingy)
297 (lambda ,@(rest thingy))))
300 (put 'yas--with-temporary-redefinitions 'lisp-indent-function 1)
301 (put 'yas--with-temporary-redefinitions 'edebug-form-spec '((&rest (defun*)) cl-declarations body))
303 (defmacro yas-with-overriden-buffer-list (&rest body)
304 (let ((saved-sym (make-symbol "yas--buffer-list")))
305 `(let ((,saved-sym (symbol-function 'buffer-list)))
306 (yas--with-temporary-redefinitions
308 (remove-if #'(lambda (buf)
309 (with-current-buffer buf
310 (eq major-mode 'lisp-interaction-mode)))
311 (funcall ,saved-sym))))
314 (defmacro yas-with-some-interesting-snippet-dirs (&rest body)
315 `(yas-saving-variables
316 (yas-with-overriden-buffer-list
317 (yas-with-snippet-dirs
318 '((".emacs.d/snippets"
320 (".yas-parents" . "cc-mode")
321 ("printf" . "printf($1);")) ;; notice the overriding for issue #281
322 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
323 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
326 (".yas-parents" . "c++-mode")
327 ("printf" . "printf"))
328 ("cc-mode" ("def" . "# define"))
329 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
330 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
333 (ert-deftest basic-jit-loading ()
334 "Test basic loading and expansion of snippets"
335 (yas-with-some-interesting-snippet-dirs
337 (yas--basic-jit-loading-1)))
339 (ert-deftest basic-jit-loading-with-compiled-snippets ()
340 "Test basic loading and expansion of snippets"
341 (yas-with-some-interesting-snippet-dirs
344 (yas--with-temporary-redefinitions ((yas--load-directory-2
346 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
348 (yas--basic-jit-loading-1))))
350 (ert-deftest loading-with-cyclic-parenthood ()
351 "Test loading when cyclic parenthood is setup."
352 (yas-saving-variables
353 (yas-with-snippet-dirs '((".emacs.d/snippets"
355 (".yas-parents" . "cc-mode"))
357 (".yas-parents" . "yet-another-c-mode"))
358 ("yet-another-c-mode"
359 (".yas-parents" . "c-mode"))))
362 (yas--all-parents 'c-mode)
364 (ert-fail "cyclic parenthood test failed"))))))
366 (defun yas--basic-jit-loading-1 ()
368 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
369 (should (= 0 (hash-table-count yas--tables)))
370 (lisp-interaction-mode)
372 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
373 (should (= 2 (hash-table-count yas--tables)))
374 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
375 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
376 (yas-should-expand '(("sc" . "brother from another mother")
377 ("dolist" . "(dolist)")
378 ("ert-deftest" . "(ert-deftest name () )")))
381 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
382 (should (= 4 (hash-table-count yas--tables)))
383 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
384 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
385 (yas-should-expand '(("printf" . "printf();")
386 ("def" . "# define")))
387 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
392 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
393 `(yas-saving-variables
394 (yas-with-snippet-dirs
395 `((".emacs.d/snippets"
397 (".yas-make-groups" . "")
398 ("printf" . "printf($1);")
400 ("fnprintf" . "fprintf($1);")
401 ("snprintf" . "snprintf($1);"))
403 ("strcmp" . "strecmp($1);")
404 ("strcasecmp" . "strcasecmp($1);")))
405 ("lisp-interaction-mode"
406 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
408 ("a-guy" . "# uuid: 999\n# --\nyo!")
409 ("a-sir" . "# uuid: 12345\n# --\nindeed!")
410 ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
411 ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
412 ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
413 (".yas-setup.el" . , (pp-to-string
414 '(yas-define-menu 'fancy-mode
415 '((yas-ignore-item "0101")
418 ((yas-item "12345")))
419 (yas-submenu "ladies"
420 ((yas-item "54321"))))
424 (ert-deftest test-yas-define-menu ()
425 (let ((yas-use-menu t))
426 (yas-with-even-more-interesting-snippet-dirs
427 (yas-reload-all 'no-jit)
428 (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
429 (should (eql 4 (length menu)))
430 (dolist (item '("a-guy" "a-beggar"))
431 (should (find item menu :key #'third :test #'string=)))
432 (should-not (find "an-outcast" menu :key #'third :test #'string=))
433 (dolist (submenu '("sirs" "ladies"))
436 (find submenu menu :key #'third :test #'string=)))))
439 (ert-deftest test-group-menus ()
440 "Test group-based menus using .yas-make-groups and the group directive"
441 (let ((yas-use-menu t))
442 (yas-with-even-more-interesting-snippet-dirs
443 (yas-reload-all 'no-jit)
444 ;; first the subdir-based groups
446 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
447 (should (eql 3 (length menu)))
448 (dolist (item '("printf" "foo-group-a" "foo-group-b"))
449 (should (find item menu :key #'third :test #'string=)))
450 (dolist (submenu '("foo-group-a" "foo-group-b"))
453 (find submenu menu :key #'third :test #'string=))))))
454 ;; now group directives
456 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
457 (should (eql 1 (length menu)))
458 (should (find "barbar" menu :key #'third :test #'string=))
461 (find "barbar" menu :key #'third :test #'string=))))))))
463 (ert-deftest test-group-menus-twisted ()
464 "Same as similarly named test, but be mean.
467 (let ((yas-use-menu t))
468 (yas-with-even-more-interesting-snippet-dirs
469 ;; add a group directive conflicting with the subdir and watch
472 (insert "# group: foo-group-c\n# --\nstrecmp($1)")
473 (write-region nil nil (concat (first (yas-snippet-dirs))
474 "/c-mode/foo-group-b/strcmp")))
475 (yas-reload-all 'no-jit)
476 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
477 (should (eql 4 (length menu)))
478 (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
479 (should (find item menu :key #'third :test #'string=)))
480 (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
483 (find submenu menu :key #'third :test #'string=))))))
484 ;; delete the .yas-make-groups file and watch behaviour
486 (delete-file (concat (first (yas-snippet-dirs))
487 "/c-mode/.yas-make-groups"))
488 (yas-reload-all 'no-jit)
489 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
490 (should (eql 5 (length menu))))
491 ;; Change a group directive and reload
493 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
494 (should (find "barbar" menu :key #'third :test #'string=)))
497 (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
498 (write-region nil nil (concat (first (yas-snippet-dirs))
499 "/lisp-interaction-mode/ert-deftest")))
500 (yas-reload-all 'no-jit)
501 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
502 (should (eql 1 (length menu)))
503 (should (find "foofoo" menu :key #'third :test #'string=))
506 (find "foofoo" menu :key #'third :test #'string=))))))))
509 ;;; The infamous and problematic tab keybinding
511 (ert-deftest test-yas-tab-binding ()
514 (should (not (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand)))
516 (should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))
517 (yas-expand-snippet "$1 $2 $3")
518 (should (eq (key-binding [(tab)]) 'yas-next-field-or-maybe-expand))
519 (should (eq (key-binding (kbd "TAB")) 'yas-next-field-or-maybe-expand))
520 (should (eq (key-binding [(shift tab)]) 'yas-prev-field))
521 (should (eq (key-binding [backtab]) 'yas-prev-field))))
523 (ert-deftest test-rebindings ()
526 (define-key yas-minor-mode-map [tab] nil)
527 (define-key yas-minor-mode-map (kbd "TAB") nil)
528 (define-key yas-minor-mode-map (kbd "SPC") 'yas-expand)
531 (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
532 (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))
534 (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
535 (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))))
536 (setcdr yas-minor-mode-map (cdr (yas--init-minor-keymap)))))
538 (ert-deftest test-yas-in-org ()
542 (should (eq (key-binding [(tab)]) 'yas-expand))
543 (should (eq (key-binding (kbd "TAB")) 'yas-expand))))
551 (yas--with-temporary-redefinitions
552 ((message (&rest _args) nil))
553 (ert t (buffer-name (current-buffer)))
554 (princ (buffer-string)))))
557 (defun yas-should-expand (keys-and-expansions)
558 (dolist (key-and-expansion keys-and-expansions)
559 (yas-exit-all-snippets)
561 (insert (car key-and-expansion))
562 (let ((yas-fallback-behavior nil))
563 (ert-simulate-command '(yas-expand)))
564 (should (string= (yas--buffer-contents) (cdr key-and-expansion))))
565 (yas-exit-all-snippets))
567 (defun yas-should-not-expand (keys)
569 (yas-exit-all-snippets)
572 (let ((yas-fallback-behavior nil))
573 (ert-simulate-command '(yas-expand)))
574 (should (string= (yas--buffer-contents) key))))
576 (defun yas-mock-insert (string)
579 ((= i (length string)))
580 (insert (aref string i))))
582 (defun yas-make-file-or-dirs (ass)
583 (let ((file-or-dir-name (car ass))
585 (cond ((listp content)
586 (make-directory file-or-dir-name 'parents)
587 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
588 (mapc #'yas-make-file-or-dirs content)))
592 (write-region nil nil file-or-dir-name nil 'nomessage)))
594 (message "[yas] oops don't know this content")))))
597 (defun yas-variables ()
599 (mapatoms #'(lambda (sym)
600 (if (and (string-match "^yas-[^/]" (symbol-name sym))
605 (defun yas-call-with-saving-variables (fn)
606 (let* ((vars (yas-variables))
607 (saved-values (mapcar #'symbol-value vars)))
610 (loop for var in vars
611 for saved in saved-values
612 do (set var saved)))))
614 (defmacro yas-saving-variables (&rest body)
615 `(yas-call-with-saving-variables #'(lambda () ,@body)))
618 (defun yas-call-with-snippet-dirs (dirs fn)
619 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
620 (yas-snippet-dirs (mapcar #'car dirs)))
621 (with-temp-message ""
624 (mapc #'yas-make-file-or-dirs dirs)
626 (when (>= emacs-major-version 24)
627 (delete-directory default-directory 'recursive))))))
629 (defmacro yas-with-snippet-dirs (dirs &rest body)
630 (declare (indent defun))
631 `(yas-call-with-snippet-dirs ,dirs
637 (unless (fboundp 'special-mode)
638 ;; FIXME: Why provide this default definition here?!?
639 (defalias 'special-mode 'fundamental))
641 ;;; btw to test this in emacs22 mac osx:
642 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
643 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
644 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
647 (provide 'yasnippet-tests)
648 ;;; yasnippet-tests.el ends here
650 ;; lexical-binding: t