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
102 (ert-deftest escape-dollar ()
105 (yas-expand-snippet "bla\\${1:bla}ble")
106 (should (string= (yas--buffer-contents) "bla${1:bla}ble"))))
108 (ert-deftest escape-closing-brace ()
111 (yas-expand-snippet "bla${1:bla\\}}ble")
112 (should (string= (yas--buffer-contents) "blabla}ble"))
113 (should (string= (yas-field-value 1) "bla}"))))
115 (ert-deftest escape-backslashes ()
118 (yas-expand-snippet "bla\\ble")
119 (should (string= (yas--buffer-contents) "bla\\ble"))))
121 (ert-deftest escape-some-elisp-with-strings ()
122 "elisp with strings and unbalance parens inside it"
125 ;; The rules here is: to output a literal `"' you need to escape
126 ;; it with one backslash. You don't need to escape them in
128 (yas-expand-snippet "soon \\\"`(concat (upcase \"(my arms\")\"\\\" were all around her\")`")
129 (should (string= (yas--buffer-contents) "soon \"(MY ARMS\" were all around her"))))
131 (ert-deftest escape-some-elisp-with-backslashes ()
134 ;; And the rule here is: to output a literal `\' inside a string
135 ;; inside embedded elisp you need a total of six `\'
136 (yas-expand-snippet "bla`(upcase \"hey\\\\\\yo\")`ble")
137 (should (string= (yas--buffer-contents) "blaHEY\\YOble"))))
139 (ert-deftest be-careful-when-escaping-in-yas-selected-text ()
142 (let ((yas/selected-text "He\\\\o world!"))
143 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
144 (should (string= (yas--buffer-contents) "Look ma! He\\\\o world!")))
145 (yas-exit-all-snippets)
147 (let ((yas/selected-text "He\"o world!"))
148 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
149 (should (string= (yas--buffer-contents) "Look ma! He\"o world!")))
150 (yas-exit-all-snippets)
152 (let ((yas/selected-text "He\"\)\\o world!"))
153 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
154 (should (string= (yas--buffer-contents) "Look ma! He\"\)\\o world!")))
155 (yas-exit-all-snippets)
158 (ert-deftest be-careful-when-escaping-in-yas-selected-text-2 ()
160 (let ((yas/selected-text "He)}o world!"))
161 (yas-expand-snippet "Look ma! ${1:`(yas/selected-text)`} OK?")
162 (should (string= (yas--buffer-contents) "Look ma! He)}o world! OK?")))))
164 (ert-deftest mirror-transformation ()
167 (let ((snippet "${1:`(concat \"foo\" \"bar\")`} ${1:$(concat (upcase yas/text) \"baz\")}"))
168 (yas-expand-snippet snippet)
169 (should (string= (yas--buffer-contents) "foobar FOOBARbaz"))
170 (yas-exit-all-snippets)
172 (yas-expand-snippet snippet)
173 (ert-simulate-command `(yas-mock-insert "bla"))
174 (should (string= (yas--buffer-contents) "bla BLAbaz")))))
176 (ert-deftest primary-field-transformation ()
179 (let ((snippet "${1:$$(upcase yas/text)}${1:$(concat \"bar\" yas/text)}"))
180 (yas-expand-snippet snippet)
181 (should (string= (yas--buffer-contents) "bar"))
182 (ert-simulate-command `(yas-mock-insert "foo"))
183 (should (string= (yas--buffer-contents) "FOObarFOO")))))
185 (ert-deftest example-for-issue-271 ()
188 (let ((yas-selected-text "aaa")
189 (snippet "if ${1:condition}\n`yas/selected-text`\nelse\n$3\nend"))
190 (yas-expand-snippet snippet)
192 (ert-simulate-command `(yas-mock-insert "bbb"))
193 (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
195 (ert-deftest another-example-for-issue-271 ()
198 (let ((snippet "\\${${1:1}:`yas/selected-text`}"))
202 (yas-expand-snippet snippet)
203 (ert-simulate-command `(yas-mock-insert "bbb"))
204 (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
210 (ert-deftest protection-overlay-no-cheating ()
211 "Protection overlays at the very end of the buffer, are dealt by cheatingly inserting a newline!
213 TODO: correct this bug!"
214 :expected-result :failed
217 (yas-expand-snippet "${2:brother} from another ${1:mother}")
218 (should (string= (yas--buffer-contents)
219 "brother from another mother") ;; no newline should be here!
224 (defmacro yas-with-some-interesting-snippet-dirs (&rest body)
225 `(yas-saving-variables
226 (yas-with-snippet-dirs
227 '((".emacs.d/snippets"
229 (".yas-parents" . "cc-mode")
230 ("printf" . "printf($1);"))
231 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
232 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
234 ("c-mode" (".yas-parents" . "c++-mode"))
235 ("cc-mode" ("def" . "# define"))
236 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
237 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
240 (ert-deftest basic-jit-loading ()
241 "Test basic loading and expansion of snippets"
242 (yas-with-some-interesting-snippet-dirs
244 (yas--basic-jit-loading-1)))
246 (ert-deftest basic-jit-loading-with-compiled-snippets ()
247 "Test basic loading and expansion of snippets"
248 (yas-with-some-interesting-snippet-dirs
251 (flet ((yas--load-directory-2
253 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
255 (yas--basic-jit-loading-1))))
257 (defun yas--basic-jit-loading-1 (&optional compile)
259 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
260 (should (= 0 (hash-table-count yas--tables)))
261 (lisp-interaction-mode)
263 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
264 (should (= 2 (hash-table-count yas--tables)))
265 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
266 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
267 (yas-should-expand '(("sc" . "brother from another mother")
268 ("dolist" . "(dolist)")
269 ("ert-deftest" . "(ert-deftest name () )")))
272 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
273 (should (= 4 (hash-table-count yas--tables)))
274 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
275 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
276 (yas-should-expand '(("printf" . "printf();")
277 ("def" . "# define")))
278 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
283 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
284 `(yas-saving-variables
285 (yas-with-snippet-dirs
286 `((".emacs.d/snippets"
288 (".yas-make-groups" . "")
289 ("printf" . "printf($1);")
291 ("fnprintf" . "fprintf($1);")
292 ("snprintf" . "snprintf($1);"))
294 ("strcmp" . "strecmp($1);")
295 ("strcasecmp" . "strcasecmp($1);")))
296 ("lisp-interaction-mode"
297 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
299 ("a-guy" . "# uuid: 999\n# --\nyo!")
300 ("a-sir" . "# uuid: 12345\n# --\nindeed!")
301 ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
302 ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
303 ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
304 (".yas-setup.el" . , (pp-to-string
305 '(yas-define-menu 'fancy-mode
306 '((yas-ignore-item "0101")
309 ((yas-item "12345")))
310 (yas-submenu "ladies"
311 ((yas-item "54321"))))
315 (ert-deftest test-yas-define-menu ()
316 (let ((yas-use-menu t))
317 (yas-with-even-more-interesting-snippet-dirs
318 (yas-reload-all 'no-jit)
319 (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
320 (should (eql 4 (length menu)))
321 (dolist (item '("a-guy" "a-beggar"))
322 (should (find item menu :key #'third :test #'string=)))
323 (should-not (find "an-outcast" menu :key #'third :test #'string=))
324 (dolist (submenu '("sirs" "ladies"))
327 (find submenu menu :key #'third :test #'string=)))))
330 (ert-deftest test-group-menus ()
331 "Test group-based menus using .yas-make-groups and the group directive"
332 (let ((yas-use-menu t))
333 (yas-with-even-more-interesting-snippet-dirs
334 (yas-reload-all 'no-jit)
335 ;; first the subdir-based groups
337 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
338 (should (eql 3 (length menu)))
339 (dolist (item '("printf" "foo-group-a" "foo-group-b"))
340 (should (find item menu :key #'third :test #'string=)))
341 (dolist (submenu '("foo-group-a" "foo-group-b"))
344 (find submenu menu :key #'third :test #'string=))))))
345 ;; now group directives
347 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
348 (should (eql 1 (length menu)))
349 (should (find "barbar" menu :key #'third :test #'string=))
352 (find "barbar" menu :key #'third :test #'string=))))))))
354 (ert-deftest test-group-menus-twisted ()
355 "Same as similarly named test, but be mean.
358 (let ((yas-use-menu t))
359 (yas-with-even-more-interesting-snippet-dirs
360 ;; add a group directive conflicting with the subdir and watch
363 (insert "# group: foo-group-c\n# --\nstrecmp($1)")
364 (write-region nil nil (concat (first (yas-snippet-dirs))
365 "/c-mode/foo-group-b/strcmp")))
366 (yas-reload-all 'no-jit)
367 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
368 (should (eql 4 (length menu)))
369 (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
370 (should (find item menu :key #'third :test #'string=)))
371 (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
374 (find submenu menu :key #'third :test #'string=))))))
375 ;; delete the .yas-make-groups file and watch behaviour
377 (delete-file (concat (first (yas-snippet-dirs))
378 "/c-mode/.yas-make-groups"))
379 (yas-reload-all 'no-jit)
380 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
381 (should (eql 5 (length menu))))
382 ;; Change a group directive and reload
384 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
385 (should (find "barbar" menu :key #'third :test #'string=)))
388 (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
389 (write-region nil nil (concat (first (yas-snippet-dirs))
390 "/lisp-interaction-mode/ert-deftest")))
391 (yas-reload-all 'no-jit)
392 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
393 (should (eql 1 (length menu)))
394 (should (find "foofoo" menu :key #'third :test #'string=))
397 (find "foofoo" menu :key #'third :test #'string=))))))))
403 (defun yas-should-expand (keys-and-expansions)
404 (dolist (key-and-expansion keys-and-expansions)
405 (yas-exit-all-snippets)
407 (insert (car key-and-expansion))
408 (let ((yas-fallback-behavior nil))
409 (ert-simulate-command '(yas-expand)))
410 (should (string= (yas--buffer-contents) (cdr key-and-expansion))))
411 (yas-exit-all-snippets))
413 (defun yas-should-not-expand (keys)
415 (yas-exit-all-snippets)
418 (let ((yas-fallback-behavior nil))
419 (ert-simulate-command '(yas-expand)))
420 (should (string= (yas--buffer-contents) key))))
422 (defun yas-mock-insert (string)
425 ((= i (length string)))
426 (insert (aref string i))))
428 (defun yas-make-file-or-dirs (ass)
429 (let ((file-or-dir-name (car ass))
431 (cond ((listp content)
432 (make-directory file-or-dir-name 'parents)
433 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
434 (mapc #'yas-make-file-or-dirs content)))
438 (write-region nil nil file-or-dir-name nil 'nomessage)))
440 (message "[yas] oops don't know this content")))))
443 (defun yas-variables ()
445 (mapatoms #'(lambda (sym)
446 (if (and (string-match "^yas-[^/]" (symbol-name sym))
451 (defun yas-call-with-saving-variables (fn)
452 (let* ((vars (yas-variables))
453 (saved-values (mapcar #'symbol-value vars)))
456 (loop for var in vars
457 for saved in saved-values
458 do (set var saved)))))
460 (defmacro yas-saving-variables (&rest body)
461 `(yas-call-with-saving-variables #'(lambda () ,@body)))
464 (defun yas-call-with-snippet-dirs (dirs fn)
465 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
466 (yas-snippet-dirs (mapcar #'car dirs)))
467 (with-temp-message ""
470 (mapc #'yas-make-file-or-dirs dirs)
472 (when (>= emacs-major-version 23)
473 (delete-directory default-directory 'recursive))))))
475 (defmacro yas-with-snippet-dirs (dirs &rest body)
476 `(yas-call-with-snippet-dirs ,dirs
482 (unless (fboundp 'special-mode)
483 (define-minor-mode special-mode "Just a placeholder for something isn't in emacs 22"))
485 ;;; btw to test this in emacs22 mac osx:
486 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
487 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
488 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
491 (provide 'yasnippet-tests)
492 ;;; yasnippet-tests.el ends here
494 ;; lexical-binding: t