1 ;;; yasnippet-tests.el --- some yasnippet tests -*- lexical-binding: t -*-
3 ;; Copyright (C) 2012, 2013, 2014, 2015 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 (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 (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 (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 (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 (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 (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 (yas-mock-insert "abc")
114 (should (string= (yas--buffer-contents) "abcabcabcabc"))))
116 (ert-deftest delete-numberless-inner-snippet-issue-562 ()
119 (yas-expand-snippet "${3:${test}bla}$0${2:ble}")
120 (ert-simulate-command '(yas-next-field-or-maybe-expand))
121 (should (looking-at "testblable"))
122 (ert-simulate-command '(yas-next-field-or-maybe-expand))
123 (ert-simulate-command '(yas-skip-and-clear-or-delete-char))
124 (should (looking-at "ble"))
125 (should (null (yas--snippets-at-point)))))
127 (ert-deftest ignore-trailing-whitespace ()
130 (insert "# key: foo\n# --\nfoo")
131 (yas--parse-template))
133 (insert "# key: foo \n# --\nfoo")
134 (yas--parse-template)))))
136 ;; (ert-deftest in-snippet-undo ()
138 ;; (yas-minor-mode 1)
139 ;; (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
140 ;; (ert-simulate-command '(yas-next-field-or-maybe-expand))
141 ;; (yas-mock-insert "bla")
142 ;; (ert-simulate-command '(undo))
143 ;; (should (string= (yas--buffer-contents)
144 ;; "brother from another mother!"))))
146 (ert-deftest dont-clear-on-partial-deletion-issue-515 ()
147 "Ensure fields are not cleared when user doesn't really mean to."
150 (yas-expand-snippet "my ${1:kid brother} from another ${2:mother}")
152 (ert-simulate-command '(kill-word 1))
153 (ert-simulate-command '(delete-char 1))
155 (should (string= (yas--buffer-contents)
156 "my brother from another mother"))
157 (should (looking-at "brother"))
159 (ert-simulate-command '(yas-next-field))
160 (should (looking-at "mother"))
161 (ert-simulate-command '(yas-prev-field))
162 (should (looking-at "brother"))))
164 (ert-deftest do-clear-on-yank-issue-515 ()
165 "A yank should clear an unmodified field."
168 (yas-expand-snippet "my ${1:kid brother} from another ${2:mother}")
169 (yas-mock-yank "little sibling")
170 (should (string= (yas--buffer-contents)
171 "my little sibling from another mother"))
172 (ert-simulate-command '(yas-next-field))
173 (ert-simulate-command '(yas-prev-field))
174 (should (looking-at "little sibling"))))
177 ;;; Snippet expansion and character escaping
178 ;;; Thanks to @zw963 (Billy) for the testing
180 (ert-deftest escape-dollar ()
183 (yas-expand-snippet "bla\\${1:bla}ble")
184 (should (string= (yas--buffer-contents) "bla${1:bla}ble"))))
186 (ert-deftest escape-closing-brace ()
189 (yas-expand-snippet "bla${1:bla\\}}ble")
190 (should (string= (yas--buffer-contents) "blabla}ble"))
191 (should (string= (yas-field-value 1) "bla}"))))
193 (ert-deftest escape-backslashes ()
196 (yas-expand-snippet "bla\\ble")
197 (should (string= (yas--buffer-contents) "bla\\ble"))))
199 (ert-deftest escape-backquotes ()
202 (yas-expand-snippet "bla`(upcase \"foo\\`bar\")`ble")
203 (should (string= (yas--buffer-contents) "blaFOO`BARble"))))
205 (ert-deftest escape-some-elisp-with-strings ()
206 "elisp with strings and unbalance parens inside it"
209 ;; The rules here is: to output a literal `"' you need to escape
210 ;; it with one backslash. You don't need to escape them in
212 (yas-expand-snippet "soon \\\"`(concat (upcase \"(my arms\")\"\\\" were all around her\")`")
213 (should (string= (yas--buffer-contents) "soon \"(MY ARMS\" were all around her"))))
215 (ert-deftest escape-some-elisp-with-backslashes ()
218 ;; And the rule here is: to output a literal `\' inside a string
219 ;; inside embedded elisp you need a total of six `\'
220 (yas-expand-snippet "bla`(upcase \"hey\\\\\\yo\")`ble")
221 (should (string= (yas--buffer-contents) "blaHEY\\YOble"))))
223 (ert-deftest be-careful-when-escaping-in-yas-selected-text ()
226 (let ((yas-selected-text "He\\\\o world!"))
227 (yas-expand-snippet "Look ma! `(yas-selected-text)`")
228 (should (string= (yas--buffer-contents) "Look ma! He\\\\o world!")))
229 (yas-exit-all-snippets)
231 (let ((yas-selected-text "He\"o world!"))
232 (yas-expand-snippet "Look ma! `(yas-selected-text)`")
233 (should (string= (yas--buffer-contents) "Look ma! He\"o world!")))
234 (yas-exit-all-snippets)
236 (let ((yas-selected-text "He\"\)\\o world!"))
237 (yas-expand-snippet "Look ma! `(yas-selected-text)`")
238 (should (string= (yas--buffer-contents) "Look ma! He\"\)\\o world!")))
239 (yas-exit-all-snippets)
242 (ert-deftest be-careful-when-escaping-in-yas-selected-text-2 ()
245 (let ((yas-selected-text "He)}o world!"))
246 (yas-expand-snippet "Look ma! ${1:`(yas-selected-text)`} OK?")
247 (should (string= (yas--buffer-contents) "Look ma! He)}o world! OK?")))))
249 (ert-deftest example-for-issue-271 ()
252 (let ((yas-selected-text "aaa")
253 (snippet "if ${1:condition}\n`yas-selected-text`\nelse\n$3\nend"))
254 (yas-expand-snippet snippet)
256 (yas-mock-insert "bbb")
257 (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
259 (defmacro yas--with-font-locked-temp-buffer (&rest body)
260 "Like `with-temp-buffer', but ensure `font-lock-mode'."
261 (declare (indent 0) (debug t))
262 (let ((temp-buffer (make-symbol "temp-buffer")))
263 ;; NOTE: buffer name must not start with a space, otherwise
264 ;; `font-lock-mode' doesn't turn on.
265 `(let ((,temp-buffer (generate-new-buffer "*yas-temp*")))
266 (with-current-buffer ,temp-buffer
267 ;; pretend we're interactive so `font-lock-mode' turns on
268 (let ((noninteractive nil)
269 ;; turn on font locking after major mode change
270 (change-major-mode-after-body-hook #'font-lock-mode))
272 (progn (require 'font-lock)
273 ;; turn on font locking before major mode change
276 (and (buffer-name ,temp-buffer)
277 (kill-buffer ,temp-buffer))))))))
279 (defmacro yas-saving-variables (&rest body)
280 `(yas-call-with-saving-variables #'(lambda () ,@body)))
282 (defmacro yas-with-snippet-dirs (dirs &rest body)
283 (declare (indent defun))
284 `(yas-call-with-snippet-dirs ,dirs
288 (ert-deftest example-for-issue-474 ()
289 (yas--with-font-locked-temp-buffer
292 (insert "#include <foo>\n")
293 (let ((yas-good-grace nil)) (yas-expand-snippet "`\"TODO: \"`"))
294 (should (string= (yas--buffer-contents) "#include <foo>\nTODO: "))))
296 (ert-deftest example-for-issue-404 ()
297 (yas--with-font-locked-temp-buffer
300 (insert "#include <foo>\n")
301 (let ((yas-good-grace nil)) (yas-expand-snippet "main"))
302 (should (string= (yas--buffer-contents) "#include <foo>\nmain"))))
304 (ert-deftest example-for-issue-404-c-mode ()
305 (yas--with-font-locked-temp-buffer
308 (insert "#include <foo>\n")
309 (let ((yas-good-grace nil)) (yas-expand-snippet "main"))
310 (should (string= (yas--buffer-contents) "#include <foo>\nmain"))))
312 (ert-deftest middle-of-buffer-snippet-insertion ()
316 (save-excursion (insert "end"))
317 (yas-expand-snippet "-middle-")
318 (should (string= (yas--buffer-contents) "beginning-middle-end"))))
320 (ert-deftest another-example-for-issue-271 ()
321 ;; expect this to fail in batch mode since `region-active-p' doesn't
322 ;; used by `yas-expand-snippet' doesn't make sense in that context.
324 :expected-result (if noninteractive
329 (let ((snippet "\\${${1:1}:`yas-selected-text`}"))
333 (yas-expand-snippet snippet)
334 (should (string= (yas--buffer-contents) "aaa${1:bbb}ccc")))))
336 (ert-deftest string-match-with-subregexp-in-embedded-elisp ()
339 ;; the rule here is: To use regexps in embedded `(elisp)` expressions, write
340 ;; it like you would normal elisp, i.e. no need to escape the backslashes.
341 (let ((snippet "`(if (string-match \"foo\\\\(ba+r\\\\)foo\" \"foobaaaaaaaaaarfoo\")
344 (yas-expand-snippet snippet))
345 (should (string= (yas--buffer-contents) "ok"))))
347 (ert-deftest string-match-with-subregexp-in-mirror-transformations ()
350 ;; the rule here is: To use regexps in embedded `(elisp)` expressions,
351 ;; escape backslashes once, i.e. to use \\( \\) constructs, write \\\\( \\\\).
352 (let ((snippet "$1${1:$(if (string-match \"foo\\\\\\\\(ba+r\\\\\\\\)baz\" yas-text)
355 (yas-expand-snippet snippet)
356 (should (string= (yas--buffer-contents) "fail"))
357 (yas-mock-insert "foobaaar")
358 (should (string= (yas--buffer-contents) "foobaaarfail"))
359 (yas-mock-insert "baz")
360 (should (string= (yas--buffer-contents) "foobaaarbazok")))))
365 (ert-deftest protection-overlay-no-cheating ()
366 "Protection overlays at the very end of the buffer are dealt
367 with by cheatingly inserting a newline!
369 TODO: correct this bug!"
370 :expected-result :failed
373 (yas-expand-snippet "${2:brother} from another ${1:mother}")
374 (should (string= (yas--buffer-contents)
375 "brother from another mother") ;; no newline should be here!
379 (defvar yas--foobarbaz)
381 ;; See issue #497. To understand this test, follow the example of the
382 ;; `yas-key-syntaxes' docstring.
384 (ert-deftest complicated-yas-key-syntaxes ()
386 (yas-saving-variables
387 (yas-with-snippet-dirs
388 '((".emacs.d/snippets"
390 ("foo-barbaz" . "# condition: yas--foobarbaz\n# --\nOKfoo-barbazOK")
391 ("barbaz" . "# condition: yas--barbaz\n# --\nOKbarbazOK")
393 ("'quote" . "OKquoteOK"))))
397 (let ((yas-key-syntaxes '("w" "w_")))
398 (let ((yas--barbaz t))
399 (yas-should-expand '(("foo-barbaz" . "foo-OKbarbazOK")
400 ("barbaz" . "OKbarbazOK"))))
401 (let ((yas--foobarbaz t))
402 (yas-should-expand '(("foo-barbaz" . "OKfoo-barbazOK"))))
403 (let ((yas-key-syntaxes
404 (cons #'(lambda (_start-point)
405 (unless (looking-back "-")
410 (yas-should-expand '(("foo-barbaz" . "foo-barOKbazOK")))))
411 (let ((yas-key-syntaxes '(yas-try-key-from-whitespace)))
412 (yas-should-expand '(("xxx\n'quote" . "xxx\nOKquoteOK")
413 ("xxx 'quote" . "xxx OKquoteOK"))))
414 (let ((yas-key-syntaxes '(yas-shortest-key-until-whitespace))
415 (yas--foobarbaz t) (yas--barbaz t))
416 (yas-should-expand '(("foo-barbaz" . "foo-barOKbazOK")))
417 (setq yas-key-syntaxes '(yas-longest-key-from-whitespace))
418 (yas-should-expand '(("foo-barbaz" . "OKfoo-barbazOK")
419 ("foo " . "foo "))))))))
424 (defun yas--call-with-temporary-redefinitions (function
425 &rest function-names-and-overriding-functions)
426 (let* ((overrides (remove-if-not #'(lambda (fdef)
427 (fboundp (first fdef)))
428 function-names-and-overriding-functions))
429 (definition-names (mapcar #'first overrides))
430 (overriding-functions (mapcar #'second overrides))
431 (saved-functions (mapcar #'symbol-function definition-names)))
432 ;; saving all definitions before overriding anything ensures FDEFINITION
433 ;; errors don't cause accidental permanent redefinitions.
435 (cl-labels ((set-fdefinitions (names functions)
436 (loop for name in names
439 (set-fdefinitions definition-names overriding-functions)
440 (unwind-protect (funcall function)
441 (set-fdefinitions definition-names saved-functions)))))
443 (defmacro yas--with-temporary-redefinitions (fdefinitions &rest body)
444 ;; "Temporarily (but globally) redefine each function in FDEFINITIONS.
445 ;; E.g.: (yas--with-temporary-redefinitions ((foo (x) ...)
447 ;; ;; code that eventually calls foo, bar of (setf foo)
449 ;; FIXME: This is hideous! Better use defadvice (or at least letf).
450 `(yas--call-with-temporary-redefinitions
452 ,@(mapcar #'(lambda (thingy)
453 `(list ',(first thingy)
454 (lambda ,@(rest thingy))))
457 (defmacro yas-with-overriden-buffer-list (&rest body)
458 (let ((saved-sym (make-symbol "yas--buffer-list")))
459 `(let ((,saved-sym (symbol-function 'buffer-list)))
460 (yas--with-temporary-redefinitions
462 (remove-if #'(lambda (buf)
463 (with-current-buffer buf
464 (eq major-mode 'lisp-interaction-mode)))
465 (funcall ,saved-sym))))
469 (defmacro yas-with-some-interesting-snippet-dirs (&rest body)
470 `(yas-saving-variables
471 (yas-with-overriden-buffer-list
472 (yas-with-snippet-dirs
473 '((".emacs.d/snippets"
475 (".yas-parents" . "cc-mode")
476 ("printf" . "printf($1);")) ;; notice the overriding for issue #281
477 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
478 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
481 (".yas-parents" . "c++-mode")
482 ("printf" . "printf"))
483 ("cc-mode" ("def" . "# define"))
484 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
485 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
488 (ert-deftest snippet-lookup ()
489 "Test `yas-lookup-snippet'."
490 (yas-with-some-interesting-snippet-dirs
491 (yas-reload-all 'no-jit)
492 (should (equal (yas-lookup-snippet "printf" 'c-mode) "printf($1);"))
493 (should (equal (yas-lookup-snippet "def" 'c-mode) "# define"))
494 (should-not (yas-lookup-snippet "no such snippet" nil 'noerror))
495 (should-not (yas-lookup-snippet "printf" 'emacs-lisp-mode 'noerror))))
497 (ert-deftest basic-jit-loading ()
498 "Test basic loading and expansion of snippets"
499 (yas-with-some-interesting-snippet-dirs
501 (yas--basic-jit-loading-1)))
503 (ert-deftest basic-jit-loading-with-compiled-snippets ()
504 "Test basic loading and expansion of compiled snippets"
505 (yas-with-some-interesting-snippet-dirs
508 (yas--with-temporary-redefinitions ((yas--load-directory-2
510 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
512 (yas--basic-jit-loading-1))))
514 (ert-deftest visiting-compiled-snippets ()
515 "Test snippet visiting for compiled snippets."
516 (yas-with-some-interesting-snippet-dirs
518 (yas-reload-all 'no-jit) ; must be loaded for `yas-lookup-snippet' to work.
519 (yas--with-temporary-redefinitions ((find-file-noselect
521 (throw 'yas-snippet-file filename)))
522 (should (string-suffix-p
524 (catch 'yas-snippet-file
525 (yas--visit-snippet-file-1
526 (yas--lookup-snippet-1 "def" 'cc-mode))))))))
528 (ert-deftest loading-with-cyclic-parenthood ()
529 "Test loading when cyclic parenthood is setup."
530 (yas-saving-variables
531 (yas-with-snippet-dirs '((".emacs.d/snippets"
533 (".yas-parents" . "cc-mode"))
535 (".yas-parents" . "yet-another-c-mode and-that-one"))
536 ("yet-another-c-mode"
537 (".yas-parents" . "c-mode and-also-this-one lisp-interaction-mode"))))
540 (let* ((major-mode 'c-mode)
546 ;; prog-mode doesn't exist in emacs 24.3
547 ,@(if (fboundp 'prog-mode)
550 lisp-interaction-mode))
551 (observed (yas--modes-to-activate)))
552 (should (equal major-mode (car observed)))
553 (should (equal (sort expected #'string<) (sort observed #'string<))))))))
555 (ert-deftest extra-modes-parenthood ()
556 "Test activation of parents of `yas--extra-modes'."
557 (yas-saving-variables
558 (yas-with-snippet-dirs '((".emacs.d/snippets"
560 (".yas-parents" . "cc-mode"))
561 ("yet-another-c-mode"
562 (".yas-parents" . "c-mode and-also-this-one lisp-interaction-mode"))))
565 (yas-activate-extra-mode 'c-mode)
566 (yas-activate-extra-mode 'yet-another-c-mode)
567 (yas-activate-extra-mode 'and-that-one)
568 (let* ((expected-first `(and-that-one
572 (expected-rest `(cc-mode
573 ;; prog-mode doesn't exist in emacs 24.3
574 ,@(if (fboundp 'prog-mode)
578 lisp-interaction-mode))
579 (observed (yas--modes-to-activate)))
580 (should (equal expected-first
581 (cl-subseq observed 0 (length expected-first))))
582 (should (equal (sort expected-rest #'string<)
583 (sort (cl-subseq observed (length expected-first)) #'string<))))))))
585 (defalias 'yas--phony-c-mode 'c-mode)
587 (ert-deftest issue-492-and-494 ()
588 (define-derived-mode yas--test-mode yas--phony-c-mode "Just a test mode")
589 (yas-with-snippet-dirs '((".emacs.d/snippets"
593 (let* ((major-mode 'yas--test-mode)
595 ,@(if (fboundp 'prog-mode)
599 (observed (yas--modes-to-activate)))
600 (should (null (cl-set-exclusive-or expected observed)))
601 (should (= (length expected)
602 (length observed)))))))
604 (define-derived-mode yas--test-mode c-mode "Just a test mode")
605 (define-derived-mode yas--another-test-mode c-mode "Another test mode")
607 (ert-deftest issue-504-tricky-jit ()
608 (yas-with-snippet-dirs
609 '((".emacs.d/snippets"
610 ("yas--another-test-mode"
611 (".yas-parents" . "yas--test-mode"))
613 (let ((b (with-current-buffer (generate-new-buffer "*yas-test*")
614 (yas--another-test-mode)
619 (should (= 0 (hash-table-count yas--scheduled-jit-loads))))
622 (defun yas--basic-jit-loading-1 ()
624 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
625 (should (= 0 (hash-table-count yas--tables)))
626 (lisp-interaction-mode)
628 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
629 (should (= 2 (hash-table-count yas--tables)))
630 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
631 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
632 (yas-should-expand '(("sc" . "brother from another mother")
633 ("dolist" . "(dolist)")
634 ("ert-deftest" . "(ert-deftest name () )")))
637 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
638 (should (= 4 (hash-table-count yas--tables)))
639 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
640 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
641 (yas-should-expand '(("printf" . "printf();")
642 ("def" . "# define")))
643 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
648 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
649 `(yas-saving-variables
650 (yas-with-snippet-dirs
651 `((".emacs.d/snippets"
653 (".yas-make-groups" . "")
654 ("printf" . "printf($1);")
656 ("fnprintf" . "fprintf($1);")
657 ("snprintf" . "snprintf($1);"))
659 ("strcmp" . "strecmp($1);")
660 ("strcasecmp" . "strcasecmp($1);")))
661 ("lisp-interaction-mode"
662 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
664 ("a-guy" . "# uuid: 999\n# --\nyo!")
665 ("a-sir" . "# uuid: 12345\n# --\nindeed!")
666 ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
667 ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
668 ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
669 (".yas-setup.el" . , (pp-to-string
670 '(yas-define-menu 'fancy-mode
671 '((yas-ignore-item "0101")
674 ((yas-item "12345")))
675 (yas-submenu "ladies"
676 ((yas-item "54321"))))
680 (ert-deftest test-yas-define-menu ()
681 (let ((yas-use-menu t))
682 (yas-with-even-more-interesting-snippet-dirs
683 (yas-reload-all 'no-jit)
684 (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
685 (should (eql 4 (length menu)))
686 (dolist (item '("a-guy" "a-beggar"))
687 (should (find item menu :key #'third :test #'string=)))
688 (should-not (find "an-outcast" menu :key #'third :test #'string=))
689 (dolist (submenu '("sirs" "ladies"))
692 (find submenu menu :key #'third :test #'string=)))))
695 (ert-deftest test-group-menus ()
696 "Test group-based menus using .yas-make-groups and the group directive"
697 (let ((yas-use-menu t))
698 (yas-with-even-more-interesting-snippet-dirs
699 (yas-reload-all 'no-jit)
700 ;; first the subdir-based groups
702 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
703 (should (eql 3 (length menu)))
704 (dolist (item '("printf" "foo-group-a" "foo-group-b"))
705 (should (find item menu :key #'third :test #'string=)))
706 (dolist (submenu '("foo-group-a" "foo-group-b"))
709 (find submenu menu :key #'third :test #'string=))))))
710 ;; now group directives
712 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
713 (should (eql 1 (length menu)))
714 (should (find "barbar" menu :key #'third :test #'string=))
717 (find "barbar" menu :key #'third :test #'string=))))))))
719 (ert-deftest test-group-menus-twisted ()
720 "Same as similarly named test, but be mean.
723 (let ((yas-use-menu t))
724 (yas-with-even-more-interesting-snippet-dirs
725 ;; add a group directive conflicting with the subdir and watch
728 (insert "# group: foo-group-c\n# --\nstrecmp($1)")
729 (write-region nil nil (concat (first (yas-snippet-dirs))
730 "/c-mode/foo-group-b/strcmp")))
731 (yas-reload-all 'no-jit)
732 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
733 (should (eql 4 (length menu)))
734 (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
735 (should (find item menu :key #'third :test #'string=)))
736 (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
739 (find submenu menu :key #'third :test #'string=))))))
740 ;; delete the .yas-make-groups file and watch behaviour
742 (delete-file (concat (first (yas-snippet-dirs))
743 "/c-mode/.yas-make-groups"))
744 (yas-reload-all 'no-jit)
745 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
746 (should (eql 5 (length menu))))
747 ;; Change a group directive and reload
749 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
750 (should (find "barbar" menu :key #'third :test #'string=)))
753 (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
754 (write-region nil nil (concat (first (yas-snippet-dirs))
755 "/lisp-interaction-mode/ert-deftest")))
756 (yas-reload-all 'no-jit)
757 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
758 (should (eql 1 (length menu)))
759 (should (find "foofoo" menu :key #'third :test #'string=))
762 (find "foofoo" menu :key #'third :test #'string=))))))))
765 ;;; The infamous and problematic tab keybinding
767 (ert-deftest test-yas-tab-binding ()
770 (should (not (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand)))
772 (should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))
773 (yas-expand-snippet "$1 $2 $3")
774 (should (eq (key-binding [(tab)]) 'yas-next-field-or-maybe-expand))
775 (should (eq (key-binding (kbd "TAB")) 'yas-next-field-or-maybe-expand))
776 (should (eq (key-binding [(shift tab)]) 'yas-prev-field))
777 (should (eq (key-binding [backtab]) 'yas-prev-field))))
779 (ert-deftest test-rebindings ()
782 (define-key yas-minor-mode-map [tab] nil)
783 (define-key yas-minor-mode-map (kbd "TAB") nil)
784 (define-key yas-minor-mode-map (kbd "SPC") 'yas-expand)
787 (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
788 (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))
790 (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
791 (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))))
792 ;; FIXME: actually should restore to whatever saved values where there.
794 (define-key yas-minor-mode-map [tab] 'yas-expand)
795 (define-key yas-minor-mode-map (kbd "TAB") 'yas-expand)
796 (define-key yas-minor-mode-map (kbd "SPC") nil)))
798 (ert-deftest test-yas-in-org ()
802 (should (eq (key-binding [(tab)]) 'yas-expand))
803 (should (eq (key-binding (kbd "TAB")) 'yas-expand))))
805 (ert-deftest test-yas-activate-extra-modes ()
806 "Given a symbol, `yas-activate-extra-mode' should be able to
807 add the snippets associated with the given mode."
809 (yas-saving-variables
810 (yas-with-snippet-dirs
811 '((".emacs.d/snippets"
815 ("car" . "(car )"))))
819 (yas-activate-extra-mode 'markdown-mode)
820 (should (eq 'markdown-mode (car yas--extra-modes)))
821 (yas-should-expand '(("_" . "_Text_ ")))
822 (yas-should-expand '(("car" . "(car )")))
823 (yas-deactivate-extra-mode 'markdown-mode)
824 (should-not (eq 'markdown-mode (car yas--extra-modes)))
825 (yas-should-not-expand '("_"))
826 (yas-should-expand '(("car" . "(car )")))))))
831 (defun yas-should-expand (keys-and-expansions)
832 (dolist (key-and-expansion keys-and-expansions)
833 (yas-exit-all-snippets)
834 (narrow-to-region (point) (point))
835 (insert (car key-and-expansion))
836 (let ((yas-fallback-behavior nil))
837 (ert-simulate-command '(yas-expand)))
838 (unless (string= (yas--buffer-contents) (cdr key-and-expansion))
839 (ert-fail (format "\"%s\" should have expanded to \"%s\" but got \"%s\""
840 (car key-and-expansion)
841 (cdr key-and-expansion)
842 (yas--buffer-contents)))))
843 (yas-exit-all-snippets))
845 (defun yas-should-not-expand (keys)
847 (yas-exit-all-snippets)
848 (narrow-to-region (point) (point))
850 (let ((yas-fallback-behavior nil))
851 (ert-simulate-command '(yas-expand)))
852 (unless (string= (yas--buffer-contents) key)
853 (ert-fail (format "\"%s\" should have stayed put, but instead expanded to \"%s\""
855 (yas--buffer-contents))))))
857 (defun yas-mock-insert (string)
858 (dotimes (i (length string))
859 (let ((last-command-event (aref string i)))
860 (ert-simulate-command '(self-insert-command 1)))))
862 (defun yas-mock-yank (string)
863 (let ((interprogram-paste-function (lambda () string)))
864 (ert-simulate-command '(yank nil))))
866 (defun yas-make-file-or-dirs (ass)
867 (let ((file-or-dir-name (car ass))
869 (cond ((listp content)
870 (make-directory file-or-dir-name 'parents)
871 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
872 (mapc #'yas-make-file-or-dirs content)))
876 (write-region nil nil file-or-dir-name nil 'nomessage)))
878 (message "[yas] oops don't know this content")))))
881 (defun yas-variables ()
883 (mapatoms #'(lambda (sym)
884 (if (and (string-match "^yas-[^/]" (symbol-name sym))
889 (defun yas-call-with-saving-variables (fn)
890 (let* ((vars (yas-variables))
891 (saved-values (mapcar #'symbol-value vars)))
894 (loop for var in vars
895 for saved in saved-values
896 do (set var saved)))))
898 (defun yas-call-with-snippet-dirs (dirs fn)
899 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
900 (yas-snippet-dirs (mapcar #'car dirs)))
901 (with-temp-message ""
904 (mapc #'yas-make-file-or-dirs dirs)
906 (when (>= emacs-major-version 24)
907 (delete-directory default-directory 'recursive))))))
911 (unless (fboundp 'special-mode)
912 ;; FIXME: Why provide this default definition here?!?
913 (defalias 'special-mode 'fundamental))
915 (unless (fboundp 'string-suffix-p)
916 ;; introduced in Emacs 24.4
917 (defun string-suffix-p (suffix string &optional ignore-case)
918 "Return non-nil if SUFFIX is a suffix of STRING.
919 If IGNORE-CASE is non-nil, the comparison is done without paying
920 attention to case differences."
921 (let ((start-pos (- (length string) (length suffix))))
922 (and (>= start-pos 0)
923 (eq t (compare-strings suffix nil nil
924 string start-pos nil ignore-case))))))
926 ;;; btw to test this in emacs22 mac osx:
927 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
928 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
929 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
932 (put 'yas-saving-variables 'edebug-form-spec t)
933 (put 'yas-with-snippet-dirs 'edebug-form-spec t)
934 (put 'yas-with-overriden-buffer-list 'edebug-form-spec t)
935 (put 'yas-with-some-interesting-snippet-dirs 'edebug-form-spec t)
938 (put 'yas--with-temporary-redefinitions 'lisp-indent-function 1)
939 (put 'yas--with-temporary-redefinitions 'edebug-form-spec '((&rest (defun*)) cl-declarations body))
944 (provide 'yasnippet-tests)
946 ;; indent-tabs-mode: nil
947 ;; byte-compile-warnings: (not cl-functions)
949 ;;; yasnippet-tests.el ends here