1 ;;; yasnippet-tests.el --- some yasnippet tests
3 ;; Copyright (C) 2012 João Távora
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 delete-numberless-inner-snippet-issue-562 ()
118 (yas-expand-snippet "${3:${test}bla}$0${2:ble}")
119 (ert-simulate-command '(yas-next-field-or-maybe-expand))
120 (should (looking-at "testblable"))
121 (ert-simulate-command '(yas-next-field-or-maybe-expand))
122 (ert-simulate-command '(yas-skip-and-clear-or-delete-char))
123 (should (looking-at "ble"))
124 (should (null (yas--snippets-at-point)))))
126 ;; (ert-deftest in-snippet-undo ()
128 ;; (yas-minor-mode 1)
129 ;; (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
130 ;; (ert-simulate-command '(yas-next-field-or-maybe-expand))
131 ;; (ert-simulate-command `(yas-mock-insert "bla"))
132 ;; (ert-simulate-command '(undo))
133 ;; (should (string= (yas--buffer-contents)
134 ;; "brother from another mother!"))))
137 ;;; Snippet expansion and character escaping
138 ;;; Thanks to @zw963 (Billy) for the testing
140 (ert-deftest escape-dollar ()
143 (yas-expand-snippet "bla\\${1:bla}ble")
144 (should (string= (yas--buffer-contents) "bla${1:bla}ble"))))
146 (ert-deftest escape-closing-brace ()
149 (yas-expand-snippet "bla${1:bla\\}}ble")
150 (should (string= (yas--buffer-contents) "blabla}ble"))
151 (should (string= (yas-field-value 1) "bla}"))))
153 (ert-deftest escape-backslashes ()
156 (yas-expand-snippet "bla\\ble")
157 (should (string= (yas--buffer-contents) "bla\\ble"))))
159 (ert-deftest escape-backquotes ()
162 (yas-expand-snippet "bla`(upcase \"foo\\`bar\")`ble")
163 (should (string= (yas--buffer-contents) "blaFOO`BARble"))))
165 (ert-deftest escape-some-elisp-with-strings ()
166 "elisp with strings and unbalance parens inside it"
169 ;; The rules here is: to output a literal `"' you need to escape
170 ;; it with one backslash. You don't need to escape them in
172 (yas-expand-snippet "soon \\\"`(concat (upcase \"(my arms\")\"\\\" were all around her\")`")
173 (should (string= (yas--buffer-contents) "soon \"(MY ARMS\" were all around her"))))
175 (ert-deftest escape-some-elisp-with-backslashes ()
178 ;; And the rule here is: to output a literal `\' inside a string
179 ;; inside embedded elisp you need a total of six `\'
180 (yas-expand-snippet "bla`(upcase \"hey\\\\\\yo\")`ble")
181 (should (string= (yas--buffer-contents) "blaHEY\\YOble"))))
183 (ert-deftest be-careful-when-escaping-in-yas-selected-text ()
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)
191 (let ((yas-selected-text "He\"o world!"))
192 (yas-expand-snippet "Look ma! `(yas-selected-text)`")
193 (should (string= (yas--buffer-contents) "Look ma! He\"o world!")))
194 (yas-exit-all-snippets)
196 (let ((yas-selected-text "He\"\)\\o world!"))
197 (yas-expand-snippet "Look ma! `(yas-selected-text)`")
198 (should (string= (yas--buffer-contents) "Look ma! He\"\)\\o world!")))
199 (yas-exit-all-snippets)
202 (ert-deftest be-careful-when-escaping-in-yas-selected-text-2 ()
205 (let ((yas-selected-text "He)}o world!"))
206 (yas-expand-snippet "Look ma! ${1:`(yas-selected-text)`} OK?")
207 (should (string= (yas--buffer-contents) "Look ma! He)}o world! OK?")))))
209 (ert-deftest example-for-issue-271 ()
212 (let ((yas-selected-text "aaa")
213 (snippet "if ${1:condition}\n`yas-selected-text`\nelse\n$3\nend"))
214 (yas-expand-snippet snippet)
216 (ert-simulate-command `(yas-mock-insert "bbb"))
217 (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
219 (defmacro yas--with-font-locked-temp-buffer (&rest body)
220 "Like `with-temp-buffer', but ensure `font-lock-mode'."
221 (declare (indent 0) (debug t))
222 (let ((temp-buffer (make-symbol "temp-buffer")))
223 ;; NOTE: buffer name must not start with a space, otherwise
224 ;; `font-lock-mode' doesn't turn on.
225 `(let ((,temp-buffer (generate-new-buffer "*yas-temp*")))
226 (with-current-buffer ,temp-buffer
227 ;; pretend we're interactive so `font-lock-mode' turns on
228 (let ((noninteractive nil)
229 ;; turn on font locking after major mode change
230 (change-major-mode-after-body-hook #'font-lock-mode))
232 (progn (require 'font-lock)
233 ;; turn on font locking before major mode change
236 (and (buffer-name ,temp-buffer)
237 (kill-buffer ,temp-buffer))))))))
239 (ert-deftest example-for-issue-474 ()
240 (yas--with-font-locked-temp-buffer
243 (insert "#include <foo>\n")
244 (let ((yas-good-grace nil)) (yas-expand-snippet "`\"TODO: \"`"))
245 (should (string= (yas--buffer-contents) "#include <foo>\nTODO: "))))
247 (ert-deftest example-for-issue-404 ()
248 (yas--with-font-locked-temp-buffer
251 (insert "#include <foo>\n")
252 (let ((yas-good-grace nil)) (yas-expand-snippet "main"))
253 (should (string= (yas--buffer-contents) "#include <foo>\nmain"))))
255 (ert-deftest example-for-issue-404-c-mode ()
256 (yas--with-font-locked-temp-buffer
259 (insert "#include <foo>\n")
260 (let ((yas-good-grace nil)) (yas-expand-snippet "main"))
261 (should (string= (yas--buffer-contents) "#include <foo>\nmain"))))
263 (ert-deftest middle-of-buffer-snippet-insertion ()
267 (save-excursion (insert "end"))
268 (yas-expand-snippet "-middle-")
269 (should (string= (yas--buffer-contents) "beginning-middle-end"))))
271 (ert-deftest another-example-for-issue-271 ()
272 ;; expect this to fail in batch mode since `region-active-p' doesn't
273 ;; used by `yas-expand-snippet' doesn't make sense in that context.
275 :expected-result (if noninteractive
280 (let ((snippet "\\${${1:1}:`yas-selected-text`}"))
284 (yas-expand-snippet snippet)
285 (should (string= (yas--buffer-contents) "aaa${1:bbb}ccc")))))
287 (ert-deftest string-match-with-subregexp-in-embedded-elisp ()
290 ;; the rule here is: To use regexps in embedded `(elisp)` expressions, write
291 ;; it like you would normal elisp, i.e. no need to escape the backslashes.
292 (let ((snippet "`(if (string-match \"foo\\\\(ba+r\\\\)foo\" \"foobaaaaaaaaaarfoo\")
295 (yas-expand-snippet snippet))
296 (should (string= (yas--buffer-contents) "ok"))))
298 (ert-deftest string-match-with-subregexp-in-mirror-transformations ()
301 ;; the rule here is: To use regexps in embedded `(elisp)` expressions,
302 ;; escape backslashes once, i.e. to use \\( \\) constructs, write \\\\( \\\\).
303 (let ((snippet "$1${1:$(if (string-match \"foo\\\\\\\\(ba+r\\\\\\\\)baz\" yas-text)
306 (yas-expand-snippet snippet)
307 (should (string= (yas--buffer-contents) "fail"))
308 (ert-simulate-command `(yas-mock-insert "foobaaar"))
309 (should (string= (yas--buffer-contents) "foobaaarfail"))
310 (ert-simulate-command `(yas-mock-insert "baz"))
311 (should (string= (yas--buffer-contents) "foobaaarbazok")))))
316 (ert-deftest protection-overlay-no-cheating ()
317 "Protection overlays at the very end of the buffer are dealt
318 with by cheatingly inserting a newline!
320 TODO: correct this bug!"
321 :expected-result :failed
324 (yas-expand-snippet "${2:brother} from another ${1:mother}")
325 (should (string= (yas--buffer-contents)
326 "brother from another mother") ;; no newline should be here!
329 ;; See issue #497. To understand this test, follow the example of the
330 ;; `yas-key-syntaxes' docstring.
332 (ert-deftest complicated-yas-key-syntaxes ()
334 (yas-saving-variables
335 (yas-with-snippet-dirs
336 '((".emacs.d/snippets"
338 ("foo-barbaz" . "# condition: yas--foobarbaz\n# --\nOKfoo-barbazOK")
339 ("barbaz" . "# condition: yas--barbaz\n# --\nOKbarbazOK")
341 ("'quote" . "OKquoteOK"))))
345 (let ((yas-key-syntaxes '("w" "w_")))
346 (let ((yas--barbaz t))
347 (yas-should-expand '(("foo-barbaz" . "foo-OKbarbazOK")
348 ("barbaz" . "OKbarbazOK"))))
349 (let ((yas--foobarbaz t))
350 (yas-should-expand '(("foo-barbaz" . "OKfoo-barbazOK"))))
351 (let ((yas-key-syntaxes
352 (cons #'(lambda (_start-point)
353 (unless (looking-back "-")
358 (yas-should-expand '(("foo-barbaz" . "foo-barOKbazOK")))))
359 (let ((yas-key-syntaxes '(yas-try-key-from-whitespace)))
360 (yas-should-expand '(("xxx\n'quote" . "xxx\nOKquoteOK")
361 ("xxx 'quote" . "xxx OKquoteOK"))))
362 (let ((yas-key-syntaxes '(yas-shortest-key-until-whitespace))
363 (yas--foobarbaz t) (yas--barbaz t))
364 (yas-should-expand '(("foo-barbaz" . "foo-barOKbazOK")))
365 (setq yas-key-syntaxes '(yas-longest-key-from-whitespace))
366 (yas-should-expand '(("foo-barbaz" . "OKfoo-barbazOK")
367 ("foo " . "foo "))))))))
372 (defun yas--call-with-temporary-redefinitions (function
373 &rest function-names-and-overriding-functions)
374 (let* ((overrides (remove-if-not #'(lambda (fdef)
375 (fboundp (first fdef)))
376 function-names-and-overriding-functions))
377 (definition-names (mapcar #'first overrides))
378 (overriding-functions (mapcar #'second overrides))
379 (saved-functions (mapcar #'symbol-function definition-names)))
380 ;; saving all definitions before overriding anything ensures FDEFINITION
381 ;; errors don't cause accidental permanent redefinitions.
383 (cl-labels ((set-fdefinitions (names functions)
384 (loop for name in names
387 (set-fdefinitions definition-names overriding-functions)
388 (unwind-protect (funcall function)
389 (set-fdefinitions definition-names saved-functions)))))
391 (defmacro yas--with-temporary-redefinitions (fdefinitions &rest body)
392 ;; "Temporarily (but globally) redefine each function in FDEFINITIONS.
393 ;; E.g.: (yas--with-temporary-redefinitions ((foo (x) ...)
395 ;; ;; code that eventually calls foo, bar of (setf foo)
397 ;; FIXME: This is hideous! Better use defadvice (or at least letf).
398 `(yas--call-with-temporary-redefinitions
400 ,@(mapcar #'(lambda (thingy)
401 `(list ',(first thingy)
402 (lambda ,@(rest thingy))))
405 (defmacro yas-with-overriden-buffer-list (&rest body)
406 (let ((saved-sym (make-symbol "yas--buffer-list")))
407 `(let ((,saved-sym (symbol-function 'buffer-list)))
408 (yas--with-temporary-redefinitions
410 (remove-if #'(lambda (buf)
411 (with-current-buffer buf
412 (eq major-mode 'lisp-interaction-mode)))
413 (funcall ,saved-sym))))
417 (defmacro yas-with-some-interesting-snippet-dirs (&rest body)
418 `(yas-saving-variables
419 (yas-with-overriden-buffer-list
420 (yas-with-snippet-dirs
421 '((".emacs.d/snippets"
423 (".yas-parents" . "cc-mode")
424 ("printf" . "printf($1);")) ;; notice the overriding for issue #281
425 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
426 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
429 (".yas-parents" . "c++-mode")
430 ("printf" . "printf"))
431 ("cc-mode" ("def" . "# define"))
432 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
433 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
437 (ert-deftest basic-jit-loading ()
438 "Test basic loading and expansion of snippets"
439 (yas-with-some-interesting-snippet-dirs
441 (yas--basic-jit-loading-1)))
443 (ert-deftest basic-jit-loading-with-compiled-snippets ()
444 "Test basic loading and expansion of snippets"
445 (yas-with-some-interesting-snippet-dirs
448 (yas--with-temporary-redefinitions ((yas--load-directory-2
450 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
452 (yas--basic-jit-loading-1))))
454 (ert-deftest loading-with-cyclic-parenthood ()
455 "Test loading when cyclic parenthood is setup."
456 (yas-saving-variables
457 (yas-with-snippet-dirs '((".emacs.d/snippets"
459 (".yas-parents" . "cc-mode"))
461 (".yas-parents" . "yet-another-c-mode and-that-one"))
462 ("yet-another-c-mode"
463 (".yas-parents" . "c-mode and-also-this-one lisp-interaction-mode"))))
466 (let* ((major-mode 'c-mode)
472 ;; prog-mode doesn't exit in emacs 24.3
473 ,@(if (fboundp 'prog-mode)
476 lisp-interaction-mode))
477 (observed (yas--modes-to-activate)))
478 (should (null (cl-set-exclusive-or expected observed)))
479 (should (= (length expected)
480 (length observed))))))))
482 (ert-deftest issue-492-and-494 ()
483 (defalias 'yas--phony-c-mode 'c-mode)
484 (define-derived-mode yas--test-mode yas--phony-c-mode "Just a test mode")
485 (yas-with-snippet-dirs '((".emacs.d/snippets"
489 (let* ((major-mode 'yas--test-mode)
491 ,@(if (fboundp 'prog-mode)
495 (observed (yas--modes-to-activate)))
496 (should (null (cl-set-exclusive-or expected observed)))
497 (should (= (length expected)
498 (length observed)))))))
500 (ert-deftest issue-504-tricky-jit ()
501 (define-derived-mode yas--test-mode c-mode "Just a test mode")
502 (define-derived-mode yas--another-test-mode c-mode "Another test mode")
503 (yas-with-snippet-dirs
504 '((".emacs.d/snippets"
505 ("yas--another-test-mode"
506 (".yas-parents" . "yas--test-mode"))
508 (let ((b (with-current-buffer (generate-new-buffer "*yas-test*")
509 (yas--another-test-mode)
514 (should (= 0 (hash-table-count yas--scheduled-jit-loads))))
517 (defun yas--basic-jit-loading-1 ()
519 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
520 (should (= 0 (hash-table-count yas--tables)))
521 (lisp-interaction-mode)
523 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
524 (should (= 2 (hash-table-count yas--tables)))
525 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
526 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
527 (yas-should-expand '(("sc" . "brother from another mother")
528 ("dolist" . "(dolist)")
529 ("ert-deftest" . "(ert-deftest name () )")))
532 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
533 (should (= 4 (hash-table-count yas--tables)))
534 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
535 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
536 (yas-should-expand '(("printf" . "printf();")
537 ("def" . "# define")))
538 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
543 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
544 `(yas-saving-variables
545 (yas-with-snippet-dirs
546 `((".emacs.d/snippets"
548 (".yas-make-groups" . "")
549 ("printf" . "printf($1);")
551 ("fnprintf" . "fprintf($1);")
552 ("snprintf" . "snprintf($1);"))
554 ("strcmp" . "strecmp($1);")
555 ("strcasecmp" . "strcasecmp($1);")))
556 ("lisp-interaction-mode"
557 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
559 ("a-guy" . "# uuid: 999\n# --\nyo!")
560 ("a-sir" . "# uuid: 12345\n# --\nindeed!")
561 ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
562 ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
563 ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
564 (".yas-setup.el" . , (pp-to-string
565 '(yas-define-menu 'fancy-mode
566 '((yas-ignore-item "0101")
569 ((yas-item "12345")))
570 (yas-submenu "ladies"
571 ((yas-item "54321"))))
575 (ert-deftest test-yas-define-menu ()
576 (let ((yas-use-menu t))
577 (yas-with-even-more-interesting-snippet-dirs
578 (yas-reload-all 'no-jit)
579 (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
580 (should (eql 4 (length menu)))
581 (dolist (item '("a-guy" "a-beggar"))
582 (should (find item menu :key #'third :test #'string=)))
583 (should-not (find "an-outcast" menu :key #'third :test #'string=))
584 (dolist (submenu '("sirs" "ladies"))
587 (find submenu menu :key #'third :test #'string=)))))
590 (ert-deftest test-group-menus ()
591 "Test group-based menus using .yas-make-groups and the group directive"
592 (let ((yas-use-menu t))
593 (yas-with-even-more-interesting-snippet-dirs
594 (yas-reload-all 'no-jit)
595 ;; first the subdir-based groups
597 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
598 (should (eql 3 (length menu)))
599 (dolist (item '("printf" "foo-group-a" "foo-group-b"))
600 (should (find item menu :key #'third :test #'string=)))
601 (dolist (submenu '("foo-group-a" "foo-group-b"))
604 (find submenu menu :key #'third :test #'string=))))))
605 ;; now group directives
607 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
608 (should (eql 1 (length menu)))
609 (should (find "barbar" menu :key #'third :test #'string=))
612 (find "barbar" menu :key #'third :test #'string=))))))))
614 (ert-deftest test-group-menus-twisted ()
615 "Same as similarly named test, but be mean.
618 (let ((yas-use-menu t))
619 (yas-with-even-more-interesting-snippet-dirs
620 ;; add a group directive conflicting with the subdir and watch
623 (insert "# group: foo-group-c\n# --\nstrecmp($1)")
624 (write-region nil nil (concat (first (yas-snippet-dirs))
625 "/c-mode/foo-group-b/strcmp")))
626 (yas-reload-all 'no-jit)
627 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
628 (should (eql 4 (length menu)))
629 (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
630 (should (find item menu :key #'third :test #'string=)))
631 (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
634 (find submenu menu :key #'third :test #'string=))))))
635 ;; delete the .yas-make-groups file and watch behaviour
637 (delete-file (concat (first (yas-snippet-dirs))
638 "/c-mode/.yas-make-groups"))
639 (yas-reload-all 'no-jit)
640 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
641 (should (eql 5 (length menu))))
642 ;; Change a group directive and reload
644 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
645 (should (find "barbar" menu :key #'third :test #'string=)))
648 (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
649 (write-region nil nil (concat (first (yas-snippet-dirs))
650 "/lisp-interaction-mode/ert-deftest")))
651 (yas-reload-all 'no-jit)
652 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
653 (should (eql 1 (length menu)))
654 (should (find "foofoo" menu :key #'third :test #'string=))
657 (find "foofoo" menu :key #'third :test #'string=))))))))
660 ;;; The infamous and problematic tab keybinding
662 (ert-deftest test-yas-tab-binding ()
665 (should (not (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand)))
667 (should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))
668 (yas-expand-snippet "$1 $2 $3")
669 (should (eq (key-binding [(tab)]) 'yas-next-field-or-maybe-expand))
670 (should (eq (key-binding (kbd "TAB")) 'yas-next-field-or-maybe-expand))
671 (should (eq (key-binding [(shift tab)]) 'yas-prev-field))
672 (should (eq (key-binding [backtab]) 'yas-prev-field))))
674 (ert-deftest test-rebindings ()
677 (define-key yas-minor-mode-map [tab] nil)
678 (define-key yas-minor-mode-map (kbd "TAB") nil)
679 (define-key yas-minor-mode-map (kbd "SPC") 'yas-expand)
682 (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
683 (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))
685 (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
686 (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))))
687 ;; FIXME: actually should restore to whatever saved values where there.
689 (define-key yas-minor-mode-map [tab] 'yas-expand)
690 (define-key yas-minor-mode-map (kbd "TAB") 'yas-expand)
691 (define-key yas-minor-mode-map (kbd "SPC") nil)))
693 (ert-deftest test-yas-in-org ()
697 (should (eq (key-binding [(tab)]) 'yas-expand))
698 (should (eq (key-binding (kbd "TAB")) 'yas-expand))))
700 (ert-deftest test-yas-activate-extra-modes ()
701 "Given a symbol, `yas-activate-extra-mode' should be able to
702 add the snippets associated with the given mode."
704 (yas-saving-variables
705 (yas-with-snippet-dirs
706 '((".emacs.d/snippets"
710 ("car" . "(car )"))))
714 (yas-activate-extra-mode 'markdown-mode)
715 (should (eq 'markdown-mode (car yas--extra-modes)))
716 (yas-should-expand '(("_" . "_Text_ ")))
717 (yas-should-expand '(("car" . "(car )")))
718 (yas-deactivate-extra-mode 'markdown-mode)
719 (should-not (eq 'markdown-mode (car yas--extra-modes)))
720 (yas-should-not-expand '("_"))
721 (yas-should-expand '(("car" . "(car )")))))))
726 (defun yas-should-expand (keys-and-expansions)
727 (dolist (key-and-expansion keys-and-expansions)
728 (yas-exit-all-snippets)
729 (narrow-to-region (point) (point))
730 (insert (car key-and-expansion))
731 (let ((yas-fallback-behavior nil))
732 (ert-simulate-command '(yas-expand)))
733 (unless (string= (yas--buffer-contents) (cdr key-and-expansion))
734 (ert-fail (format "\"%s\" should have expanded to \"%s\" but got \"%s\""
735 (car key-and-expansion)
736 (cdr key-and-expansion)
737 (yas--buffer-contents)))))
738 (yas-exit-all-snippets))
740 (defun yas-should-not-expand (keys)
742 (yas-exit-all-snippets)
743 (narrow-to-region (point) (point))
745 (let ((yas-fallback-behavior nil))
746 (ert-simulate-command '(yas-expand)))
747 (unless (string= (yas--buffer-contents) key)
748 (ert-fail (format "\"%s\" should have stayed put, but instead expanded to \"%s\""
750 (yas--buffer-contents))))))
752 (defun yas-mock-insert (string)
755 ((= i (length string)))
756 (insert (aref string i))))
758 (defun yas-make-file-or-dirs (ass)
759 (let ((file-or-dir-name (car ass))
761 (cond ((listp content)
762 (make-directory file-or-dir-name 'parents)
763 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
764 (mapc #'yas-make-file-or-dirs content)))
768 (write-region nil nil file-or-dir-name nil 'nomessage)))
770 (message "[yas] oops don't know this content")))))
773 (defun yas-variables ()
775 (mapatoms #'(lambda (sym)
776 (if (and (string-match "^yas-[^/]" (symbol-name sym))
781 (defun yas-call-with-saving-variables (fn)
782 (let* ((vars (yas-variables))
783 (saved-values (mapcar #'symbol-value vars)))
786 (loop for var in vars
787 for saved in saved-values
788 do (set var saved)))))
790 (defmacro yas-saving-variables (&rest body)
791 `(yas-call-with-saving-variables #'(lambda () ,@body)))
794 (defun yas-call-with-snippet-dirs (dirs fn)
795 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
796 (yas-snippet-dirs (mapcar #'car dirs)))
797 (with-temp-message ""
800 (mapc #'yas-make-file-or-dirs dirs)
802 (when (>= emacs-major-version 24)
803 (delete-directory default-directory 'recursive))))))
805 (defmacro yas-with-snippet-dirs (dirs &rest body)
806 (declare (indent defun))
807 `(yas-call-with-snippet-dirs ,dirs
813 (unless (fboundp 'special-mode)
814 ;; FIXME: Why provide this default definition here?!?
815 (defalias 'special-mode 'fundamental))
817 ;;; btw to test this in emacs22 mac osx:
818 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
819 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
820 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
823 (put 'yas-saving-variables 'edebug-form-spec t)
824 (put 'yas-with-snippet-dirs 'edebug-form-spec t)
825 (put 'yas-with-overriden-buffer-list 'edebug-form-spec t)
826 (put 'yas-with-some-interesting-snippet-dirs 'edebug-form-spec t)
829 (put 'yas--with-temporary-redefinitions 'lisp-indent-function 1)
830 (put 'yas--with-temporary-redefinitions 'edebug-form-spec '((&rest (defun*)) cl-declarations body))
835 (provide 'yasnippet-tests)
837 ;; indent-tabs-mode: nil
838 ;; lexical-binding: t
839 ;; byte-compile-warnings: (not cl-functions)
841 ;;; yasnippet-tests.el ends here