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 in-snippet-undo ()
100 ;; (yas-minor-mode 1)
101 ;; (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
102 ;; (ert-simulate-command '(yas-next-field-or-maybe-expand))
103 ;; (ert-simulate-command `(yas-mock-insert "bla"))
104 ;; (ert-simulate-command '(undo))
105 ;; (should (string= (yas--buffer-contents)
106 ;; "brother from another mother!"))))
109 ;;; Snippet expansion and character escaping
110 ;;; Thanks to @zw963 (Billy) for the testing
112 (ert-deftest escape-dollar ()
115 (yas-expand-snippet "bla\\${1:bla}ble")
116 (should (string= (yas--buffer-contents) "bla${1:bla}ble"))))
118 (ert-deftest escape-closing-brace ()
121 (yas-expand-snippet "bla${1:bla\\}}ble")
122 (should (string= (yas--buffer-contents) "blabla}ble"))
123 (should (string= (yas-field-value 1) "bla}"))))
125 (ert-deftest escape-backslashes ()
128 (yas-expand-snippet "bla\\ble")
129 (should (string= (yas--buffer-contents) "bla\\ble"))))
131 (ert-deftest escape-backquotes ()
134 (yas-expand-snippet "bla`(upcase \"foo\\`bar\")`ble")
135 (should (string= (yas--buffer-contents) "blaFOO`BARble"))))
137 (ert-deftest escape-some-elisp-with-strings ()
138 "elisp with strings and unbalance parens inside it"
141 ;; The rules here is: to output a literal `"' you need to escape
142 ;; it with one backslash. You don't need to escape them in
144 (yas-expand-snippet "soon \\\"`(concat (upcase \"(my arms\")\"\\\" were all around her\")`")
145 (should (string= (yas--buffer-contents) "soon \"(MY ARMS\" were all around her"))))
147 (ert-deftest escape-some-elisp-with-backslashes ()
150 ;; And the rule here is: to output a literal `\' inside a string
151 ;; inside embedded elisp you need a total of six `\'
152 (yas-expand-snippet "bla`(upcase \"hey\\\\\\yo\")`ble")
153 (should (string= (yas--buffer-contents) "blaHEY\\YOble"))))
155 (ert-deftest be-careful-when-escaping-in-yas-selected-text ()
158 (let ((yas/selected-text "He\\\\o world!"))
159 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
160 (should (string= (yas--buffer-contents) "Look ma! He\\\\o world!")))
161 (yas-exit-all-snippets)
163 (let ((yas/selected-text "He\"o world!"))
164 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
165 (should (string= (yas--buffer-contents) "Look ma! He\"o world!")))
166 (yas-exit-all-snippets)
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)
174 (ert-deftest be-careful-when-escaping-in-yas-selected-text-2 ()
176 (let ((yas/selected-text "He)}o world!"))
177 (yas-expand-snippet "Look ma! ${1:`(yas/selected-text)`} OK?")
178 (should (string= (yas--buffer-contents) "Look ma! He)}o world! OK?")))))
180 (ert-deftest example-for-issue-271 ()
183 (let ((yas-selected-text "aaa")
184 (snippet "if ${1:condition}\n`yas/selected-text`\nelse\n$3\nend"))
185 (yas-expand-snippet snippet)
187 (ert-simulate-command `(yas-mock-insert "bbb"))
188 (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
190 (ert-deftest another-example-for-issue-271 ()
193 (let ((snippet "\\${${1:1}:`yas/selected-text`}"))
197 (yas-expand-snippet snippet)
198 (should (string= (yas--buffer-contents) "aaa${1:bbb}ccc")))))
200 (ert-deftest string-match-with-subregexp-in-embedded-elisp ()
203 ;; the rule here is: To use regexps in embedded `(elisp)` expressions, write
204 ;; it like you would normal elisp, i.e. no need to escape the backslashes.
205 (let ((snippet "`(if (string-match \"foo\\\\(ba+r\\\\)foo\" \"foobaaaaaaaaaarfoo\")
208 (yas-expand-snippet snippet))
209 (should (string= (yas--buffer-contents) "ok"))))
211 (ert-deftest string-match-with-subregexp-in-mirror-transformations ()
214 ;; the rule here is: To use regexps in embedded `(elisp)` expressions,
215 ;; escape backslashes once, i.e. to use \\( \\) constructs, write \\\\( \\\\).
216 (let ((snippet "$1${1:$(if (string-match \"foo\\\\\\\\(ba+r\\\\\\\\)baz\" yas/text)
219 (yas-expand-snippet snippet)
220 (should (string= (yas--buffer-contents) "fail"))
221 (ert-simulate-command `(yas-mock-insert "foobaaar"))
222 (should (string= (yas--buffer-contents) "foobaaarfail"))
223 (ert-simulate-command `(yas-mock-insert "baz"))
224 (should (string= (yas--buffer-contents) "foobaaarbazok")))))
229 (ert-deftest protection-overlay-no-cheating ()
230 "Protection overlays at the very end of the buffer are dealt
231 with by cheatingly inserting a newline!
233 TODO: correct this bug!"
234 :expected-result :failed
237 (yas-expand-snippet "${2:brother} from another ${1:mother}")
238 (should (string= (yas--buffer-contents)
239 "brother from another mother") ;; no newline should be here!
244 (defmacro yas-with-some-interesting-snippet-dirs (&rest body)
245 `(yas-saving-variables
246 (yas-with-snippet-dirs
247 '((".emacs.d/snippets"
249 (".yas-parents" . "cc-mode")
250 ("printf" . "printf($1);"))
251 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
252 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
254 ("c-mode" (".yas-parents" . "c++-mode"))
255 ("cc-mode" ("def" . "# define"))
256 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
257 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
260 (ert-deftest basic-jit-loading ()
261 "Test basic loading and expansion of snippets"
262 (yas-with-some-interesting-snippet-dirs
264 (yas--basic-jit-loading-1)))
266 (ert-deftest basic-jit-loading-with-compiled-snippets ()
267 "Test basic loading and expansion of snippets"
268 (yas-with-some-interesting-snippet-dirs
271 (flet ((yas--load-directory-2
273 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
275 (yas--basic-jit-loading-1))))
277 (defun yas--basic-jit-loading-1 (&optional compile)
279 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
280 (should (= 0 (hash-table-count yas--tables)))
281 (lisp-interaction-mode)
283 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
284 (should (= 2 (hash-table-count yas--tables)))
285 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
286 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
287 (yas-should-expand '(("sc" . "brother from another mother")
288 ("dolist" . "(dolist)")
289 ("ert-deftest" . "(ert-deftest name () )")))
292 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
293 (should (= 4 (hash-table-count yas--tables)))
294 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
295 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
296 (yas-should-expand '(("printf" . "printf();")
297 ("def" . "# define")))
298 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
303 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
304 `(yas-saving-variables
305 (yas-with-snippet-dirs
306 `((".emacs.d/snippets"
308 (".yas-make-groups" . "")
309 ("printf" . "printf($1);")
311 ("fnprintf" . "fprintf($1);")
312 ("snprintf" . "snprintf($1);"))
314 ("strcmp" . "strecmp($1);")
315 ("strcasecmp" . "strcasecmp($1);")))
316 ("lisp-interaction-mode"
317 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
319 ("a-guy" . "# uuid: 999\n# --\nyo!")
320 ("a-sir" . "# uuid: 12345\n# --\nindeed!")
321 ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
322 ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
323 ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
324 (".yas-setup.el" . , (pp-to-string
325 '(yas-define-menu 'fancy-mode
326 '((yas-ignore-item "0101")
329 ((yas-item "12345")))
330 (yas-submenu "ladies"
331 ((yas-item "54321"))))
335 (ert-deftest test-yas-define-menu ()
336 (let ((yas-use-menu t))
337 (yas-with-even-more-interesting-snippet-dirs
338 (yas-reload-all 'no-jit)
339 (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
340 (should (eql 4 (length menu)))
341 (dolist (item '("a-guy" "a-beggar"))
342 (should (find item menu :key #'third :test #'string=)))
343 (should-not (find "an-outcast" menu :key #'third :test #'string=))
344 (dolist (submenu '("sirs" "ladies"))
347 (find submenu menu :key #'third :test #'string=)))))
350 (ert-deftest test-group-menus ()
351 "Test group-based menus using .yas-make-groups and the group directive"
352 (let ((yas-use-menu t))
353 (yas-with-even-more-interesting-snippet-dirs
354 (yas-reload-all 'no-jit)
355 ;; first the subdir-based groups
357 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
358 (should (eql 3 (length menu)))
359 (dolist (item '("printf" "foo-group-a" "foo-group-b"))
360 (should (find item menu :key #'third :test #'string=)))
361 (dolist (submenu '("foo-group-a" "foo-group-b"))
364 (find submenu menu :key #'third :test #'string=))))))
365 ;; now group directives
367 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
368 (should (eql 1 (length menu)))
369 (should (find "barbar" menu :key #'third :test #'string=))
372 (find "barbar" menu :key #'third :test #'string=))))))))
374 (ert-deftest test-group-menus-twisted ()
375 "Same as similarly named test, but be mean.
378 (let ((yas-use-menu t))
379 (yas-with-even-more-interesting-snippet-dirs
380 ;; add a group directive conflicting with the subdir and watch
383 (insert "# group: foo-group-c\n# --\nstrecmp($1)")
384 (write-region nil nil (concat (first (yas-snippet-dirs))
385 "/c-mode/foo-group-b/strcmp")))
386 (yas-reload-all 'no-jit)
387 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
388 (should (eql 4 (length menu)))
389 (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
390 (should (find item menu :key #'third :test #'string=)))
391 (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
394 (find submenu menu :key #'third :test #'string=))))))
395 ;; delete the .yas-make-groups file and watch behaviour
397 (delete-file (concat (first (yas-snippet-dirs))
398 "/c-mode/.yas-make-groups"))
399 (yas-reload-all 'no-jit)
400 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
401 (should (eql 5 (length menu))))
402 ;; Change a group directive and reload
404 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
405 (should (find "barbar" menu :key #'third :test #'string=)))
408 (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
409 (write-region nil nil (concat (first (yas-snippet-dirs))
410 "/lisp-interaction-mode/ert-deftest")))
411 (yas-reload-all 'no-jit)
412 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
413 (should (eql 1 (length menu)))
414 (should (find "foofoo" menu :key #'third :test #'string=))
417 (find "foofoo" menu :key #'third :test #'string=))))))))
420 ;;; The infamous and problematic tab keybinding
422 (ert-deftest test-yas-tab-binding ()
425 (should (not (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand)))
427 (should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))
428 (yas-expand-snippet "$1 $2 $3")
429 (dolist (k (if (listp yas-next-field-key)
431 (list yas-next-field-key)))
432 (should (eq (key-binding (yas--read-keybinding k)) 'yas-next-field-or-maybe-expand)))
433 (dolist (k (if (listp yas-prev-field-key)
435 (list yas-prev-field-key)))
436 (should (eq (key-binding (yas--read-keybinding k)) 'yas-prev-field)))))
438 (ert-deftest test-yas-in-org ()
442 (should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))))
447 (defun yas-should-expand (keys-and-expansions)
448 (dolist (key-and-expansion keys-and-expansions)
449 (yas-exit-all-snippets)
451 (insert (car key-and-expansion))
452 (let ((yas-fallback-behavior nil))
453 (ert-simulate-command '(yas-expand)))
454 (should (string= (yas--buffer-contents) (cdr key-and-expansion))))
455 (yas-exit-all-snippets))
457 (defun yas-should-not-expand (keys)
459 (yas-exit-all-snippets)
462 (let ((yas-fallback-behavior nil))
463 (ert-simulate-command '(yas-expand)))
464 (should (string= (yas--buffer-contents) key))))
466 (defun yas-mock-insert (string)
469 ((= i (length string)))
470 (insert (aref string i))))
472 (defun yas-make-file-or-dirs (ass)
473 (let ((file-or-dir-name (car ass))
475 (cond ((listp content)
476 (make-directory file-or-dir-name 'parents)
477 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
478 (mapc #'yas-make-file-or-dirs content)))
482 (write-region nil nil file-or-dir-name nil 'nomessage)))
484 (message "[yas] oops don't know this content")))))
487 (defun yas-variables ()
489 (mapatoms #'(lambda (sym)
490 (if (and (string-match "^yas-[^/]" (symbol-name sym))
495 (defun yas-call-with-saving-variables (fn)
496 (let* ((vars (yas-variables))
497 (saved-values (mapcar #'symbol-value vars)))
500 (loop for var in vars
501 for saved in saved-values
502 do (set var saved)))))
504 (defmacro yas-saving-variables (&rest body)
505 `(yas-call-with-saving-variables #'(lambda () ,@body)))
508 (defun yas-call-with-snippet-dirs (dirs fn)
509 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
510 (yas-snippet-dirs (mapcar #'car dirs)))
511 (with-temp-message ""
514 (mapc #'yas-make-file-or-dirs dirs)
516 (when (>= emacs-major-version 24)
517 (delete-directory default-directory 'recursive))))))
519 (defmacro yas-with-snippet-dirs (dirs &rest body)
520 `(yas-call-with-snippet-dirs ,dirs
526 (unless (fboundp 'special-mode)
527 (define-minor-mode special-mode "Just a placeholder for something isn't in emacs 22"))
529 ;;; btw to test this in emacs22 mac osx:
530 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
531 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
532 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
535 (provide 'yasnippet-tests)
536 ;;; yasnippet-tests.el ends here
538 ;; lexical-binding: t