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 in-snippet-undo ()
110 ;; (yas-minor-mode 1)
111 ;; (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
112 ;; (ert-simulate-command '(yas-next-field-or-maybe-expand))
113 ;; (ert-simulate-command `(yas-mock-insert "bla"))
114 ;; (ert-simulate-command '(undo))
115 ;; (should (string= (yas--buffer-contents)
116 ;; "brother from another mother!"))))
119 ;;; Snippet expansion and character escaping
120 ;;; Thanks to @zw963 (Billy) for the testing
122 (ert-deftest escape-dollar ()
125 (yas-expand-snippet "bla\\${1:bla}ble")
126 (should (string= (yas--buffer-contents) "bla${1:bla}ble"))))
128 (ert-deftest escape-closing-brace ()
131 (yas-expand-snippet "bla${1:bla\\}}ble")
132 (should (string= (yas--buffer-contents) "blabla}ble"))
133 (should (string= (yas-field-value 1) "bla}"))))
135 (ert-deftest escape-backslashes ()
138 (yas-expand-snippet "bla\\ble")
139 (should (string= (yas--buffer-contents) "bla\\ble"))))
141 (ert-deftest escape-backquotes ()
144 (yas-expand-snippet "bla`(upcase \"foo\\`bar\")`ble")
145 (should (string= (yas--buffer-contents) "blaFOO`BARble"))))
147 (ert-deftest escape-some-elisp-with-strings ()
148 "elisp with strings and unbalance parens inside it"
151 ;; The rules here is: to output a literal `"' you need to escape
152 ;; it with one backslash. You don't need to escape them in
154 (yas-expand-snippet "soon \\\"`(concat (upcase \"(my arms\")\"\\\" were all around her\")`")
155 (should (string= (yas--buffer-contents) "soon \"(MY ARMS\" were all around her"))))
157 (ert-deftest escape-some-elisp-with-backslashes ()
160 ;; And the rule here is: to output a literal `\' inside a string
161 ;; inside embedded elisp you need a total of six `\'
162 (yas-expand-snippet "bla`(upcase \"hey\\\\\\yo\")`ble")
163 (should (string= (yas--buffer-contents) "blaHEY\\YOble"))))
165 (ert-deftest be-careful-when-escaping-in-yas-selected-text ()
168 (let ((yas/selected-text "He\\\\o world!"))
169 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
170 (should (string= (yas--buffer-contents) "Look ma! He\\\\o world!")))
171 (yas-exit-all-snippets)
173 (let ((yas/selected-text "He\"o world!"))
174 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
175 (should (string= (yas--buffer-contents) "Look ma! He\"o world!")))
176 (yas-exit-all-snippets)
178 (let ((yas/selected-text "He\"\)\\o world!"))
179 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
180 (should (string= (yas--buffer-contents) "Look ma! He\"\)\\o world!")))
181 (yas-exit-all-snippets)
184 (ert-deftest be-careful-when-escaping-in-yas-selected-text-2 ()
186 (let ((yas/selected-text "He)}o world!"))
187 (yas-expand-snippet "Look ma! ${1:`(yas/selected-text)`} OK?")
188 (should (string= (yas--buffer-contents) "Look ma! He)}o world! OK?")))))
190 (ert-deftest example-for-issue-271 ()
193 (let ((yas-selected-text "aaa")
194 (snippet "if ${1:condition}\n`yas/selected-text`\nelse\n$3\nend"))
195 (yas-expand-snippet snippet)
197 (ert-simulate-command `(yas-mock-insert "bbb"))
198 (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
200 (ert-deftest another-example-for-issue-271 ()
201 ;; expect this to fail in batch mode since `region-active-p' doesn't
202 ;; used by `yas-expand-snippet' doesn't make sense in that context.
204 :expected-result (if noninteractive
209 (let ((snippet "\\${${1:1}:`yas/selected-text`}"))
213 (yas-expand-snippet snippet)
214 (should (string= (yas--buffer-contents) "aaa${1:bbb}ccc")))))
216 (ert-deftest string-match-with-subregexp-in-embedded-elisp ()
219 ;; the rule here is: To use regexps in embedded `(elisp)` expressions, write
220 ;; it like you would normal elisp, i.e. no need to escape the backslashes.
221 (let ((snippet "`(if (string-match \"foo\\\\(ba+r\\\\)foo\" \"foobaaaaaaaaaarfoo\")
224 (yas-expand-snippet snippet))
225 (should (string= (yas--buffer-contents) "ok"))))
227 (ert-deftest string-match-with-subregexp-in-mirror-transformations ()
230 ;; the rule here is: To use regexps in embedded `(elisp)` expressions,
231 ;; escape backslashes once, i.e. to use \\( \\) constructs, write \\\\( \\\\).
232 (let ((snippet "$1${1:$(if (string-match \"foo\\\\\\\\(ba+r\\\\\\\\)baz\" yas/text)
235 (yas-expand-snippet snippet)
236 (should (string= (yas--buffer-contents) "fail"))
237 (ert-simulate-command `(yas-mock-insert "foobaaar"))
238 (should (string= (yas--buffer-contents) "foobaaarfail"))
239 (ert-simulate-command `(yas-mock-insert "baz"))
240 (should (string= (yas--buffer-contents) "foobaaarbazok")))))
245 (ert-deftest protection-overlay-no-cheating ()
246 "Protection overlays at the very end of the buffer are dealt
247 with by cheatingly inserting a newline!
249 TODO: correct this bug!"
250 :expected-result :failed
253 (yas-expand-snippet "${2:brother} from another ${1:mother}")
254 (should (string= (yas--buffer-contents)
255 "brother from another mother") ;; no newline should be here!
260 (defmacro yas-with-overriden-buffer-list (&rest body)
261 (let ((saved-sym (gensym)))
262 `(let ((,saved-sym (symbol-function 'buffer-list)))
263 (flet ((buffer-list ()
264 (remove-if #'(lambda (buf)
265 (with-current-buffer buf
266 (eq major-mode 'lisp-interaction-mode)))
267 (funcall ,saved-sym))))
270 (defmacro yas-with-some-interesting-snippet-dirs (&rest body)
271 `(yas-saving-variables
272 (yas-with-overriden-buffer-list
273 (yas-with-snippet-dirs
274 '((".emacs.d/snippets"
276 (".yas-parents" . "cc-mode")
277 ("printf" . "printf($1);")) ;; notice the overriding for issue #281
278 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
279 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
282 (".yas-parents" . "c++-mode")
283 ("printf" . "printf"))
284 ("cc-mode" ("def" . "# define"))
285 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
286 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
289 (ert-deftest basic-jit-loading ()
290 "Test basic loading and expansion of snippets"
291 (yas-with-some-interesting-snippet-dirs
293 (yas--basic-jit-loading-1)))
295 (ert-deftest basic-jit-loading-with-compiled-snippets ()
296 "Test basic loading and expansion of snippets"
297 (yas-with-some-interesting-snippet-dirs
300 (yas--with-temporary-redefinitions ((yas--load-directory-2
302 (declare (ignore dummies))
303 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
305 (yas--basic-jit-loading-1))))
307 (defun yas--basic-jit-loading-1 (&optional compile)
309 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
310 (should (= 0 (hash-table-count yas--tables)))
311 (lisp-interaction-mode)
313 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
314 (should (= 2 (hash-table-count yas--tables)))
315 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
316 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
317 (yas-should-expand '(("sc" . "brother from another mother")
318 ("dolist" . "(dolist)")
319 ("ert-deftest" . "(ert-deftest name () )")))
322 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
323 (should (= 4 (hash-table-count yas--tables)))
324 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
325 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
326 (yas-should-expand '(("printf" . "printf();")
327 ("def" . "# define")))
328 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
333 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
334 `(yas-saving-variables
335 (yas-with-snippet-dirs
336 `((".emacs.d/snippets"
338 (".yas-make-groups" . "")
339 ("printf" . "printf($1);")
341 ("fnprintf" . "fprintf($1);")
342 ("snprintf" . "snprintf($1);"))
344 ("strcmp" . "strecmp($1);")
345 ("strcasecmp" . "strcasecmp($1);")))
346 ("lisp-interaction-mode"
347 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
349 ("a-guy" . "# uuid: 999\n# --\nyo!")
350 ("a-sir" . "# uuid: 12345\n# --\nindeed!")
351 ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
352 ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
353 ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
354 (".yas-setup.el" . , (pp-to-string
355 '(yas-define-menu 'fancy-mode
356 '((yas-ignore-item "0101")
359 ((yas-item "12345")))
360 (yas-submenu "ladies"
361 ((yas-item "54321"))))
365 (ert-deftest test-yas-define-menu ()
366 (let ((yas-use-menu t))
367 (yas-with-even-more-interesting-snippet-dirs
368 (yas-reload-all 'no-jit)
369 (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
370 (should (eql 4 (length menu)))
371 (dolist (item '("a-guy" "a-beggar"))
372 (should (find item menu :key #'third :test #'string=)))
373 (should-not (find "an-outcast" menu :key #'third :test #'string=))
374 (dolist (submenu '("sirs" "ladies"))
377 (find submenu menu :key #'third :test #'string=)))))
380 (ert-deftest test-group-menus ()
381 "Test group-based menus using .yas-make-groups and the group directive"
382 (let ((yas-use-menu t))
383 (yas-with-even-more-interesting-snippet-dirs
384 (yas-reload-all 'no-jit)
385 ;; first the subdir-based groups
387 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
388 (should (eql 3 (length menu)))
389 (dolist (item '("printf" "foo-group-a" "foo-group-b"))
390 (should (find item menu :key #'third :test #'string=)))
391 (dolist (submenu '("foo-group-a" "foo-group-b"))
394 (find submenu menu :key #'third :test #'string=))))))
395 ;; now group directives
397 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
398 (should (eql 1 (length menu)))
399 (should (find "barbar" menu :key #'third :test #'string=))
402 (find "barbar" menu :key #'third :test #'string=))))))))
404 (ert-deftest test-group-menus-twisted ()
405 "Same as similarly named test, but be mean.
408 (let ((yas-use-menu t))
409 (yas-with-even-more-interesting-snippet-dirs
410 ;; add a group directive conflicting with the subdir and watch
413 (insert "# group: foo-group-c\n# --\nstrecmp($1)")
414 (write-region nil nil (concat (first (yas-snippet-dirs))
415 "/c-mode/foo-group-b/strcmp")))
416 (yas-reload-all 'no-jit)
417 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
418 (should (eql 4 (length menu)))
419 (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
420 (should (find item menu :key #'third :test #'string=)))
421 (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
424 (find submenu menu :key #'third :test #'string=))))))
425 ;; delete the .yas-make-groups file and watch behaviour
427 (delete-file (concat (first (yas-snippet-dirs))
428 "/c-mode/.yas-make-groups"))
429 (yas-reload-all 'no-jit)
430 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
431 (should (eql 5 (length menu))))
432 ;; Change a group directive and reload
434 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
435 (should (find "barbar" menu :key #'third :test #'string=)))
438 (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
439 (write-region nil nil (concat (first (yas-snippet-dirs))
440 "/lisp-interaction-mode/ert-deftest")))
441 (yas-reload-all 'no-jit)
442 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
443 (should (eql 1 (length menu)))
444 (should (find "foofoo" menu :key #'third :test #'string=))
447 (find "foofoo" menu :key #'third :test #'string=))))))))
450 ;;; The infamous and problematic tab keybinding
452 (ert-deftest test-yas-tab-binding ()
455 (should (not (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand)))
457 (should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))
458 (yas-expand-snippet "$1 $2 $3")
459 (should (eq (key-binding [(tab)]) 'yas-next-field-or-maybe-expand))
460 (should (eq (key-binding (kbd "TAB")) 'yas-next-field-or-maybe-expand))
461 (should (eq (key-binding [(shift tab)]) 'yas-prev-field))
462 (should (eq (key-binding [backtab]) 'yas-prev-field))))
464 (ert-deftest test-yas-in-org ()
468 (should (eq (key-binding [(tab)]) 'yas-expand))
469 (should (eq (key-binding (kbd "TAB")) 'yas-expand))))
477 (flet ((message (&rest args)
478 (declare (ignore args))
480 (ert t (buffer-name (current-buffer)))
481 (princ (buffer-string)))))
484 (defun yas-should-expand (keys-and-expansions)
485 (dolist (key-and-expansion keys-and-expansions)
486 (yas-exit-all-snippets)
488 (insert (car key-and-expansion))
489 (let ((yas-fallback-behavior nil))
490 (ert-simulate-command '(yas-expand)))
491 (should (string= (yas--buffer-contents) (cdr key-and-expansion))))
492 (yas-exit-all-snippets))
494 (defun yas-should-not-expand (keys)
496 (yas-exit-all-snippets)
499 (let ((yas-fallback-behavior nil))
500 (ert-simulate-command '(yas-expand)))
501 (should (string= (yas--buffer-contents) key))))
503 (defun yas-mock-insert (string)
506 ((= i (length string)))
507 (insert (aref string i))))
509 (defun yas-make-file-or-dirs (ass)
510 (let ((file-or-dir-name (car ass))
512 (cond ((listp content)
513 (make-directory file-or-dir-name 'parents)
514 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
515 (mapc #'yas-make-file-or-dirs content)))
519 (write-region nil nil file-or-dir-name nil 'nomessage)))
521 (message "[yas] oops don't know this content")))))
524 (defun yas-variables ()
526 (mapatoms #'(lambda (sym)
527 (if (and (string-match "^yas-[^/]" (symbol-name sym))
532 (defun yas-call-with-saving-variables (fn)
533 (let* ((vars (yas-variables))
534 (saved-values (mapcar #'symbol-value vars)))
537 (loop for var in vars
538 for saved in saved-values
539 do (set var saved)))))
541 (defmacro yas-saving-variables (&rest body)
542 `(yas-call-with-saving-variables #'(lambda () ,@body)))
545 (defun yas-call-with-snippet-dirs (dirs fn)
546 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
547 (yas-snippet-dirs (mapcar #'car dirs)))
548 (with-temp-message ""
551 (mapc #'yas-make-file-or-dirs dirs)
553 (when (>= emacs-major-version 24)
554 (delete-directory default-directory 'recursive))))))
556 (defmacro yas-with-snippet-dirs (dirs &rest body)
557 `(yas-call-with-snippet-dirs ,dirs
563 (unless (fboundp 'special-mode)
564 (define-minor-mode special-mode "Just a placeholder for something isn't in emacs 22"))
566 ;;; btw to test this in emacs22 mac osx:
567 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
568 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
569 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
572 (provide 'yasnippet-tests)
573 ;;; yasnippet-tests.el ends here
575 ;; lexical-binding: t