]> code.delx.au - gnu-emacs-elpa/blob - yasnippet-tests.el
Closes #271: 3rd try, protect escapes after collecting backquote elisp
[gnu-emacs-elpa] / yasnippet-tests.el
1 ;;; yasnippet-tests.el --- some yasnippet tests
2
3 ;; Copyright (C) 2012 João Távora
4
5 ;; Author: João Távora <joaot@siscog.pt>
6 ;; Keywords: emulations, convenience
7
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.
12
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.
17
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/>.
20
21 ;;; Commentary:
22
23 ;; Test basic snippet mechanics and the loading system
24
25 ;;; Code:
26
27 (require 'yasnippet)
28 (require 'ert)
29 (require 'ert-x)
30
31 \f
32 ;;; Snippet mechanics
33
34 (defun yas--buffer-contents ()
35 (buffer-substring-no-properties (point-min) (point-max)))
36
37 (ert-deftest field-navigation ()
38 (with-temp-buffer
39 (yas-minor-mode 1)
40 (yas-expand-snippet "${1:brother} from another ${2:mother}")
41 (should (string= (yas--buffer-contents)
42 "brother from another mother"))
43
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"))))
49
50 (ert-deftest simple-mirror ()
51 (with-temp-buffer
52 (yas-minor-mode 1)
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"))))
59
60 (ert-deftest mirror-with-transformation ()
61 (with-temp-buffer
62 (yas-minor-mode 1)
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"))))
69
70 (ert-deftest nested-placeholders-kill-superfield ()
71 (with-temp-buffer
72 (yas-minor-mode 1)
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!"))))
79
80 (ert-deftest nested-placeholders-use-subfield ()
81 (with-temp-buffer
82 (yas-minor-mode 1)
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!"))))
88
89 ;; (ert-deftest in-snippet-undo ()
90 ;; (with-temp-buffer
91 ;; (yas-minor-mode 1)
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!"))))
98
99 \f
100 ;;; Snippet expansion and character escaping
101 ;;; Thanks to @zw963 (Billy) for the testing
102 ;;;
103 (ert-deftest escape-dollar ()
104 (with-temp-buffer
105 (yas-minor-mode 1)
106 (yas-expand-snippet "bla\\${1:bla}ble")
107 (should (string= (yas--buffer-contents) "bla${1:bla}ble"))))
108
109 (ert-deftest escape-closing-brace ()
110 (with-temp-buffer
111 (yas-minor-mode 1)
112 (yas-expand-snippet "bla${1:bla\\}}ble")
113 (should (string= (yas--buffer-contents) "blabla}ble"))
114 (should (string= (yas-field-value 1) "bla}"))))
115
116 (ert-deftest escape-backslashes ()
117 (with-temp-buffer
118 (yas-minor-mode 1)
119 (yas-expand-snippet "bla\\ble")
120 (should (string= (yas--buffer-contents) "bla\\ble"))))
121
122 (ert-deftest escape-some-elisp-with-strings ()
123 "elisp with strings and unbalance parens inside it"
124 (with-temp-buffer
125 (yas-minor-mode 1)
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
128 ;; embedded elisp.
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"))))
131
132 (ert-deftest escape-some-elisp-with-backslashes ()
133 (with-temp-buffer
134 (yas-minor-mode 1)
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"))))
139
140 (ert-deftest be-careful-when-escaping-in-yas-selected-text ()
141 (with-temp-buffer
142 (yas-minor-mode 1)
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)
147 (erase-buffer)
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)
152 (erase-buffer)
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)
157 (erase-buffer)))
158
159 (ert-deftest be-careful-when-escaping-in-yas-selected-text-2 ()
160 (with-temp-buffer
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?")))))
164
165 (ert-deftest mirror-transformation ()
166 (with-temp-buffer
167 (yas-minor-mode 1)
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)
172 (erase-buffer)
173 (yas-expand-snippet snippet)
174 (ert-simulate-command `(yas-mock-insert "bla"))
175 (should (string= (yas--buffer-contents) "bla BLAbaz")))))
176
177 (ert-deftest primary-field-transformation ()
178 (with-temp-buffer
179 (yas-minor-mode 1)
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")))))
185
186 (ert-deftest example-for-issue-271 ()
187 (with-temp-buffer
188 (yas-minor-mode 1)
189 (let ((yas-selected-text "aaa")
190 (snippet "if ${1:condition}\n`yas/selected-text`\nelse\n$3\nend"))
191 (yas-expand-snippet snippet)
192 (yas-next-field)
193 (ert-simulate-command `(yas-mock-insert "bbb"))
194 (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
195
196 (ert-deftest another-example-for-issue-271 ()
197 (with-temp-buffer
198 (yas-minor-mode 1)
199 (let ((snippet "\\${${1:1}:`yas/selected-text`}"))
200 (insert "aaabbbccc")
201 (set-mark 4)
202 (goto-char 7)
203 (yas-expand-snippet snippet)
204 (should (string= (yas--buffer-contents) "aaa${1:bbb}ccc")))))
205
206 (ert-deftest string-match-with-subregexp-in-embedded-elisp ()
207 (with-temp-buffer
208 (yas-minor-mode 1)
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"))))
214
215 (ert-deftest string-match-with-subregexp-in-mirror-transformations ()
216 (with-temp-buffer
217 (yas-minor-mode 1)
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")))))
227
228 \f
229 ;;; Misc tests
230 ;;;
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!
234
235 TODO: correct this bug!"
236 :expected-result :failed
237 (with-temp-buffer
238 (yas-minor-mode 1)
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!
242 )))
243 \f
244 ;;; Loading
245 ;;;
246 (defmacro yas-with-some-interesting-snippet-dirs (&rest body)
247 `(yas-saving-variables
248 (yas-with-snippet-dirs
249 '((".emacs.d/snippets"
250 ("c-mode"
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")))
255 ("library/snippets"
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"))))
260 ,@body)))
261
262 (ert-deftest basic-jit-loading ()
263 "Test basic loading and expansion of snippets"
264 (yas-with-some-interesting-snippet-dirs
265 (yas-reload-all)
266 (yas--basic-jit-loading-1)))
267
268 (ert-deftest basic-jit-loading-with-compiled-snippets ()
269 "Test basic loading and expansion of snippets"
270 (yas-with-some-interesting-snippet-dirs
271 (yas-reload-all)
272 (yas-recompile-all)
273 (flet ((yas--load-directory-2
274 (&rest dummies)
275 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
276 (yas-reload-all)
277 (yas--basic-jit-loading-1))))
278
279 (defun yas--basic-jit-loading-1 (&optional compile)
280 (with-temp-buffer
281 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
282 (should (= 0 (hash-table-count yas--tables)))
283 (lisp-interaction-mode)
284 (yas-minor-mode 1)
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 () )")))
292 (c-mode)
293 (yas-minor-mode 1)
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"))))
301
302 \f
303 ;;; Menu
304 ;;;
305 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
306 `(yas-saving-variables
307 (yas-with-snippet-dirs
308 `((".emacs.d/snippets"
309 ("c-mode"
310 (".yas-make-groups" . "")
311 ("printf" . "printf($1);")
312 ("foo-group-a"
313 ("fnprintf" . "fprintf($1);")
314 ("snprintf" . "snprintf($1);"))
315 ("foo-group-b"
316 ("strcmp" . "strecmp($1);")
317 ("strcasecmp" . "strcasecmp($1);")))
318 ("lisp-interaction-mode"
319 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
320 ("fancy-mode"
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")
329 (yas-item "999")
330 (yas-submenu "sirs"
331 ((yas-item "12345")))
332 (yas-submenu "ladies"
333 ((yas-item "54321"))))
334 '("666")))))))
335 ,@body)))
336
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"))
347 (should (keymapp
348 (fourth
349 (find submenu menu :key #'third :test #'string=)))))
350 ))))
351
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
358 ;;
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"))
364 (should (keymapp
365 (fourth
366 (find submenu menu :key #'third :test #'string=))))))
367 ;; now group directives
368 ;;
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=))
372 (should (keymapp
373 (fourth
374 (find "barbar" menu :key #'third :test #'string=))))))))
375
376 (ert-deftest test-group-menus-twisted ()
377 "Same as similarly named test, but be mean.
378
379 TODO: be meaner"
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
383 ;; behaviour
384 (with-temp-buffer
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"))
394 (should (keymapp
395 (fourth
396 (find submenu menu :key #'third :test #'string=))))))
397 ;; delete the .yas-make-groups file and watch behaviour
398 ;;
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
405 ;;
406 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
407 (should (find "barbar" menu :key #'third :test #'string=)))
408
409 (with-temp-buffer
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=))
417 (should (keymapp
418 (fourth
419 (find "foofoo" menu :key #'third :test #'string=))))))))
420
421 \f
422 ;;; Helpers
423 ;;;
424
425 (defun yas-should-expand (keys-and-expansions)
426 (dolist (key-and-expansion keys-and-expansions)
427 (yas-exit-all-snippets)
428 (erase-buffer)
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))
434
435 (defun yas-should-not-expand (keys)
436 (dolist (key keys)
437 (yas-exit-all-snippets)
438 (erase-buffer)
439 (insert key)
440 (let ((yas-fallback-behavior nil))
441 (ert-simulate-command '(yas-expand)))
442 (should (string= (yas--buffer-contents) key))))
443
444 (defun yas-mock-insert (string)
445 (interactive)
446 (do ((i 0 (1+ i)))
447 ((= i (length string)))
448 (insert (aref string i))))
449
450 (defun yas-make-file-or-dirs (ass)
451 (let ((file-or-dir-name (car ass))
452 (content (cdr 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)))
457 ((stringp content)
458 (with-temp-buffer
459 (insert content)
460 (write-region nil nil file-or-dir-name nil 'nomessage)))
461 (t
462 (message "[yas] oops don't know this content")))))
463
464
465 (defun yas-variables ()
466 (let ((syms))
467 (mapatoms #'(lambda (sym)
468 (if (and (string-match "^yas-[^/]" (symbol-name sym))
469 (boundp sym))
470 (push sym syms))))
471 syms))
472
473 (defun yas-call-with-saving-variables (fn)
474 (let* ((vars (yas-variables))
475 (saved-values (mapcar #'symbol-value vars)))
476 (unwind-protect
477 (funcall fn)
478 (loop for var in vars
479 for saved in saved-values
480 do (set var saved)))))
481
482 (defmacro yas-saving-variables (&rest body)
483 `(yas-call-with-saving-variables #'(lambda () ,@body)))
484
485
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 ""
490 (unwind-protect
491 (progn
492 (mapc #'yas-make-file-or-dirs dirs)
493 (funcall fn))
494 (when (>= emacs-major-version 23)
495 (delete-directory default-directory 'recursive))))))
496
497 (defmacro yas-with-snippet-dirs (dirs &rest body)
498 `(yas-call-with-snippet-dirs ,dirs
499 #'(lambda ()
500 ,@body)))
501
502 ;;; Older emacsen
503 ;;;
504 (unless (fboundp 'special-mode)
505 (define-minor-mode special-mode "Just a placeholder for something isn't in emacs 22"))
506
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
511
512
513 (provide 'yasnippet-tests)
514 ;;; yasnippet-tests.el ends here
515 ;; Local Variables:
516 ;; lexical-binding: t
517 ;; End: