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 nested-placeholders-kill-superfield ()
73 (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
74 (should (string= (yas--buffer-contents)
75 "brother from another mother!"))
76 (ert-simulate-command `(yas-mock-insert "bla"))
77 (should (string= (yas--buffer-contents)
78 "brother from bla!"))))
80 (ert-deftest nested-placeholders-use-subfield ()
83 (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
84 (ert-simulate-command '(yas-next-field-or-maybe-expand))
85 (ert-simulate-command `(yas-mock-insert "bla"))
86 (should (string= (yas--buffer-contents)
87 "brother from another bla!"))))
89 ;; (ert-deftest in-snippet-undo ()
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 ;; (ert-simulate-command '(undo))
96 ;; (should (string= (yas--buffer-contents)
97 ;; "brother from another mother!"))))
100 ;;; Snippet expansion and character escaping
101 ;;; Thanks to @zw963 (Billy) for the testing
103 (ert-deftest escape-dollar ()
106 (yas-expand-snippet "bla\\${1:bla}ble")
107 (should (string= (yas--buffer-contents) "bla${1:bla}ble"))))
109 (ert-deftest escape-closing-brace ()
112 (yas-expand-snippet "bla${1:bla\\}}ble")
113 (should (string= (yas--buffer-contents) "blabla}ble"))
114 (should (string= (yas-field-value 1) "bla}"))))
116 (ert-deftest escape-backslashes ()
119 (yas-expand-snippet "bla\\ble")
120 (should (string= (yas--buffer-contents) "bla\\ble"))))
122 (ert-deftest escape-some-elisp-with-strings ()
123 "elisp with strings and unbalance parens inside it"
126 ;; The rules here is: to output a literal `"' you need to escape
127 ;; it with one backslash. You don't need to escape them in
129 (yas-expand-snippet "soon \\\"`(concat (upcase \"(my arms\")\"\\\" were all around her\")`")
130 (should (string= (yas--buffer-contents) "soon \"(MY ARMS\" were all around her"))))
132 (ert-deftest escape-some-elisp-with-backslashes ()
135 ;; And the rule here is: to output a literal `\' inside a string
136 ;; inside embedded elisp you need a total of six `\'
137 (yas-expand-snippet "bla`(upcase \"hey\\\\\\yo\")`ble")
138 (should (string= (yas--buffer-contents) "blaHEY\\YOble"))))
140 (ert-deftest be-careful-when-escaping-in-yas-selected-text ()
143 (let ((yas/selected-text "He\\\\o world!"))
144 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
145 (should (string= (yas--buffer-contents) "Look ma! He\\\\o world!")))
146 (yas-exit-all-snippets)
148 (let ((yas/selected-text "He\"o world!"))
149 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
150 (should (string= (yas--buffer-contents) "Look ma! He\"o world!")))
151 (yas-exit-all-snippets)
153 (let ((yas/selected-text "He\"\)\\o world!"))
154 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
155 (should (string= (yas--buffer-contents) "Look ma! He\"\)\\o world!")))
156 (yas-exit-all-snippets)
159 (ert-deftest be-careful-when-escaping-in-yas-selected-text-2 ()
161 (let ((yas/selected-text "He)}o world!"))
162 (yas-expand-snippet "Look ma! ${1:`(yas/selected-text)`} OK?")
163 (should (string= (yas--buffer-contents) "Look ma! He)}o world! OK?")))))
165 (ert-deftest mirror-transformation ()
168 (let ((snippet "${1:`(concat \"foo\" \"bar\")`} ${1:$(concat (upcase yas/text) \"baz\")}"))
169 (yas-expand-snippet snippet)
170 (should (string= (yas--buffer-contents) "foobar FOOBARbaz"))
171 (yas-exit-all-snippets)
173 (yas-expand-snippet snippet)
174 (ert-simulate-command `(yas-mock-insert "bla"))
175 (should (string= (yas--buffer-contents) "bla BLAbaz")))))
177 (ert-deftest primary-field-transformation ()
180 (let ((snippet "${1:$$(upcase yas/text)}${1:$(concat \"bar\" yas/text)}"))
181 (yas-expand-snippet snippet)
182 (should (string= (yas--buffer-contents) "bar"))
183 (ert-simulate-command `(yas-mock-insert "foo"))
184 (should (string= (yas--buffer-contents) "FOObarFOO")))))
186 (ert-deftest example-for-issue-271 ()
189 (let ((yas-selected-text "aaa")
190 (snippet "if ${1:condition}\n`yas/selected-text`\nelse\n$3\nend"))
191 (yas-expand-snippet snippet)
193 (ert-simulate-command `(yas-mock-insert "bbb"))
194 (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
196 (ert-deftest another-example-for-issue-271 ()
199 (let ((snippet "\\${${1:1}:`yas/selected-text`}"))
203 (yas-expand-snippet snippet)
204 (should (string= (yas--buffer-contents) "aaa${1:bbb}ccc")))))
206 (ert-deftest string-match-with-subregexp-in-embedded-elisp ()
209 ;; the rule here is: To use regexps in embedded `(elisp)` expressions, write
210 ;; it like you would normal elisp, i.e. no need to escape the backslashes.
211 (let ((snippet "`(if (string-match \"foo\\\\(ba+r\\\\)foo\" \"foobaaaaaaaaaarfoo\") \"ok\" \"fail\")`"))
212 (yas-expand-snippet snippet))
213 (should (string= (yas--buffer-contents) "ok"))))
215 (ert-deftest string-match-with-subregexp-in-mirror-transformations ()
218 ;; the rule here is: To use regexps in embedded `(elisp)` expressions,
219 ;; escape backslashes once. i.e. to use \\( \\) constructs, write \\\\( \\\\).
220 (let ((snippet "$1${1:$(if (string-match \"foo\\\\\\\\(ba+r\\\\\\\\)baz\" yas/text) \"ok\" \"fail\")}"))
221 (yas-expand-snippet snippet)
222 (should (string= (yas--buffer-contents) "fail"))
223 (ert-simulate-command `(yas-mock-insert "foobaaar"))
224 (should (string= (yas--buffer-contents) "foobaaarfail"))
225 (ert-simulate-command `(yas-mock-insert "baz"))
226 (should (string= (yas--buffer-contents) "foobaaarbazok")))))
231 (ert-deftest protection-overlay-no-cheating ()
232 "Protection overlays at the very end of the buffer are dealt
233 with by cheatingly inserting a newline!
235 TODO: correct this bug!"
236 :expected-result :failed
239 (yas-expand-snippet "${2:brother} from another ${1:mother}")
240 (should (string= (yas--buffer-contents)
241 "brother from another mother") ;; no newline should be here!
246 (defmacro yas-with-some-interesting-snippet-dirs (&rest body)
247 `(yas-saving-variables
248 (yas-with-snippet-dirs
249 '((".emacs.d/snippets"
251 (".yas-parents" . "cc-mode")
252 ("printf" . "printf($1);"))
253 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
254 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
256 ("c-mode" (".yas-parents" . "c++-mode"))
257 ("cc-mode" ("def" . "# define"))
258 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
259 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
262 (ert-deftest basic-jit-loading ()
263 "Test basic loading and expansion of snippets"
264 (yas-with-some-interesting-snippet-dirs
266 (yas--basic-jit-loading-1)))
268 (ert-deftest basic-jit-loading-with-compiled-snippets ()
269 "Test basic loading and expansion of snippets"
270 (yas-with-some-interesting-snippet-dirs
273 (flet ((yas--load-directory-2
275 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
277 (yas--basic-jit-loading-1))))
279 (defun yas--basic-jit-loading-1 (&optional compile)
281 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
282 (should (= 0 (hash-table-count yas--tables)))
283 (lisp-interaction-mode)
285 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
286 (should (= 2 (hash-table-count yas--tables)))
287 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
288 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
289 (yas-should-expand '(("sc" . "brother from another mother")
290 ("dolist" . "(dolist)")
291 ("ert-deftest" . "(ert-deftest name () )")))
294 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
295 (should (= 4 (hash-table-count yas--tables)))
296 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
297 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
298 (yas-should-expand '(("printf" . "printf();")
299 ("def" . "# define")))
300 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
305 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
306 `(yas-saving-variables
307 (yas-with-snippet-dirs
308 `((".emacs.d/snippets"
310 (".yas-make-groups" . "")
311 ("printf" . "printf($1);")
313 ("fnprintf" . "fprintf($1);")
314 ("snprintf" . "snprintf($1);"))
316 ("strcmp" . "strecmp($1);")
317 ("strcasecmp" . "strcasecmp($1);")))
318 ("lisp-interaction-mode"
319 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
321 ("a-guy" . "# uuid: 999\n# --\nyo!")
322 ("a-sir" . "# uuid: 12345\n# --\nindeed!")
323 ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
324 ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
325 ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
326 (".yas-setup.el" . , (pp-to-string
327 '(yas-define-menu 'fancy-mode
328 '((yas-ignore-item "0101")
331 ((yas-item "12345")))
332 (yas-submenu "ladies"
333 ((yas-item "54321"))))
337 (ert-deftest test-yas-define-menu ()
338 (let ((yas-use-menu t))
339 (yas-with-even-more-interesting-snippet-dirs
340 (yas-reload-all 'no-jit)
341 (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
342 (should (eql 4 (length menu)))
343 (dolist (item '("a-guy" "a-beggar"))
344 (should (find item menu :key #'third :test #'string=)))
345 (should-not (find "an-outcast" menu :key #'third :test #'string=))
346 (dolist (submenu '("sirs" "ladies"))
349 (find submenu menu :key #'third :test #'string=)))))
352 (ert-deftest test-group-menus ()
353 "Test group-based menus using .yas-make-groups and the group directive"
354 (let ((yas-use-menu t))
355 (yas-with-even-more-interesting-snippet-dirs
356 (yas-reload-all 'no-jit)
357 ;; first the subdir-based groups
359 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
360 (should (eql 3 (length menu)))
361 (dolist (item '("printf" "foo-group-a" "foo-group-b"))
362 (should (find item menu :key #'third :test #'string=)))
363 (dolist (submenu '("foo-group-a" "foo-group-b"))
366 (find submenu menu :key #'third :test #'string=))))))
367 ;; now group directives
369 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
370 (should (eql 1 (length menu)))
371 (should (find "barbar" menu :key #'third :test #'string=))
374 (find "barbar" menu :key #'third :test #'string=))))))))
376 (ert-deftest test-group-menus-twisted ()
377 "Same as similarly named test, but be mean.
380 (let ((yas-use-menu t))
381 (yas-with-even-more-interesting-snippet-dirs
382 ;; add a group directive conflicting with the subdir and watch
385 (insert "# group: foo-group-c\n# --\nstrecmp($1)")
386 (write-region nil nil (concat (first (yas-snippet-dirs))
387 "/c-mode/foo-group-b/strcmp")))
388 (yas-reload-all 'no-jit)
389 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
390 (should (eql 4 (length menu)))
391 (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
392 (should (find item menu :key #'third :test #'string=)))
393 (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
396 (find submenu menu :key #'third :test #'string=))))))
397 ;; delete the .yas-make-groups file and watch behaviour
399 (delete-file (concat (first (yas-snippet-dirs))
400 "/c-mode/.yas-make-groups"))
401 (yas-reload-all 'no-jit)
402 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
403 (should (eql 5 (length menu))))
404 ;; Change a group directive and reload
406 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
407 (should (find "barbar" menu :key #'third :test #'string=)))
410 (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
411 (write-region nil nil (concat (first (yas-snippet-dirs))
412 "/lisp-interaction-mode/ert-deftest")))
413 (yas-reload-all 'no-jit)
414 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
415 (should (eql 1 (length menu)))
416 (should (find "foofoo" menu :key #'third :test #'string=))
419 (find "foofoo" menu :key #'third :test #'string=))))))))
425 (defun yas-should-expand (keys-and-expansions)
426 (dolist (key-and-expansion keys-and-expansions)
427 (yas-exit-all-snippets)
429 (insert (car key-and-expansion))
430 (let ((yas-fallback-behavior nil))
431 (ert-simulate-command '(yas-expand)))
432 (should (string= (yas--buffer-contents) (cdr key-and-expansion))))
433 (yas-exit-all-snippets))
435 (defun yas-should-not-expand (keys)
437 (yas-exit-all-snippets)
440 (let ((yas-fallback-behavior nil))
441 (ert-simulate-command '(yas-expand)))
442 (should (string= (yas--buffer-contents) key))))
444 (defun yas-mock-insert (string)
447 ((= i (length string)))
448 (insert (aref string i))))
450 (defun yas-make-file-or-dirs (ass)
451 (let ((file-or-dir-name (car ass))
453 (cond ((listp content)
454 (make-directory file-or-dir-name 'parents)
455 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
456 (mapc #'yas-make-file-or-dirs content)))
460 (write-region nil nil file-or-dir-name nil 'nomessage)))
462 (message "[yas] oops don't know this content")))))
465 (defun yas-variables ()
467 (mapatoms #'(lambda (sym)
468 (if (and (string-match "^yas-[^/]" (symbol-name sym))
473 (defun yas-call-with-saving-variables (fn)
474 (let* ((vars (yas-variables))
475 (saved-values (mapcar #'symbol-value vars)))
478 (loop for var in vars
479 for saved in saved-values
480 do (set var saved)))))
482 (defmacro yas-saving-variables (&rest body)
483 `(yas-call-with-saving-variables #'(lambda () ,@body)))
486 (defun yas-call-with-snippet-dirs (dirs fn)
487 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
488 (yas-snippet-dirs (mapcar #'car dirs)))
489 (with-temp-message ""
492 (mapc #'yas-make-file-or-dirs dirs)
494 (when (>= emacs-major-version 23)
495 (delete-directory default-directory 'recursive))))))
497 (defmacro yas-with-snippet-dirs (dirs &rest body)
498 `(yas-call-with-snippet-dirs ,dirs
504 (unless (fboundp 'special-mode)
505 (define-minor-mode special-mode "Just a placeholder for something isn't in emacs 22"))
507 ;;; btw to test this in emacs22 mac osx:
508 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
509 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
510 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
513 (provide 'yasnippet-tests)
514 ;;; yasnippet-tests.el ends here
516 ;; lexical-binding: t