]> code.delx.au - gnu-emacs-elpa/blob - yasnippet-tests.el
Closes #474: fix more cc-mode font-lock conflicts
[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 primary-field-transformation ()
71 (with-temp-buffer
72 (yas-minor-mode 1)
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")))))
78
79 (ert-deftest nested-placeholders-kill-superfield ()
80 (with-temp-buffer
81 (yas-minor-mode 1)
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!"))))
88
89 (ert-deftest nested-placeholders-use-subfield ()
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 (should (string= (yas--buffer-contents)
96 "brother from another bla!"))))
97
98 (ert-deftest mirrors-adjacent-to-fields-with-nested-mirrors ()
99 (with-temp-buffer
100 (yas-minor-mode 1)
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...' %>"))))
107
108 (ert-deftest deep-nested-mirroring-issue-351 ()
109 (with-temp-buffer
110 (yas-minor-mode 1)
111 (yas-expand-snippet "${1:FOOOOOOO}${2:$1}${3:$2}${4:$3}")
112 (ert-simulate-command `(yas-mock-insert "abc"))
113 (should (string= (yas--buffer-contents) "abcabcabcabc"))))
114
115 ;; (ert-deftest in-snippet-undo ()
116 ;; (with-temp-buffer
117 ;; (yas-minor-mode 1)
118 ;; (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
119 ;; (ert-simulate-command '(yas-next-field-or-maybe-expand))
120 ;; (ert-simulate-command `(yas-mock-insert "bla"))
121 ;; (ert-simulate-command '(undo))
122 ;; (should (string= (yas--buffer-contents)
123 ;; "brother from another mother!"))))
124
125 \f
126 ;;; Snippet expansion and character escaping
127 ;;; Thanks to @zw963 (Billy) for the testing
128 ;;;
129 (ert-deftest escape-dollar ()
130 (with-temp-buffer
131 (yas-minor-mode 1)
132 (yas-expand-snippet "bla\\${1:bla}ble")
133 (should (string= (yas--buffer-contents) "bla${1:bla}ble"))))
134
135 (ert-deftest escape-closing-brace ()
136 (with-temp-buffer
137 (yas-minor-mode 1)
138 (yas-expand-snippet "bla${1:bla\\}}ble")
139 (should (string= (yas--buffer-contents) "blabla}ble"))
140 (should (string= (yas-field-value 1) "bla}"))))
141
142 (ert-deftest escape-backslashes ()
143 (with-temp-buffer
144 (yas-minor-mode 1)
145 (yas-expand-snippet "bla\\ble")
146 (should (string= (yas--buffer-contents) "bla\\ble"))))
147
148 (ert-deftest escape-backquotes ()
149 (with-temp-buffer
150 (yas-minor-mode 1)
151 (yas-expand-snippet "bla`(upcase \"foo\\`bar\")`ble")
152 (should (string= (yas--buffer-contents) "blaFOO`BARble"))))
153
154 (ert-deftest escape-some-elisp-with-strings ()
155 "elisp with strings and unbalance parens inside it"
156 (with-temp-buffer
157 (yas-minor-mode 1)
158 ;; The rules here is: to output a literal `"' you need to escape
159 ;; it with one backslash. You don't need to escape them in
160 ;; embedded elisp.
161 (yas-expand-snippet "soon \\\"`(concat (upcase \"(my arms\")\"\\\" were all around her\")`")
162 (should (string= (yas--buffer-contents) "soon \"(MY ARMS\" were all around her"))))
163
164 (ert-deftest escape-some-elisp-with-backslashes ()
165 (with-temp-buffer
166 (yas-minor-mode 1)
167 ;; And the rule here is: to output a literal `\' inside a string
168 ;; inside embedded elisp you need a total of six `\'
169 (yas-expand-snippet "bla`(upcase \"hey\\\\\\yo\")`ble")
170 (should (string= (yas--buffer-contents) "blaHEY\\YOble"))))
171
172 (ert-deftest be-careful-when-escaping-in-yas-selected-text ()
173 (with-temp-buffer
174 (yas-minor-mode 1)
175 (let ((yas-selected-text "He\\\\o world!"))
176 (yas-expand-snippet "Look ma! `(yas-selected-text)`")
177 (should (string= (yas--buffer-contents) "Look ma! He\\\\o world!")))
178 (yas-exit-all-snippets)
179 (erase-buffer)
180 (let ((yas-selected-text "He\"o world!"))
181 (yas-expand-snippet "Look ma! `(yas-selected-text)`")
182 (should (string= (yas--buffer-contents) "Look ma! He\"o world!")))
183 (yas-exit-all-snippets)
184 (erase-buffer)
185 (let ((yas-selected-text "He\"\)\\o world!"))
186 (yas-expand-snippet "Look ma! `(yas-selected-text)`")
187 (should (string= (yas--buffer-contents) "Look ma! He\"\)\\o world!")))
188 (yas-exit-all-snippets)
189 (erase-buffer)))
190
191 (ert-deftest be-careful-when-escaping-in-yas-selected-text-2 ()
192 (with-temp-buffer
193 (let ((yas-selected-text "He)}o world!"))
194 (yas-expand-snippet "Look ma! ${1:`(yas-selected-text)`} OK?")
195 (should (string= (yas--buffer-contents) "Look ma! He)}o world! OK?")))))
196
197 (ert-deftest example-for-issue-271 ()
198 (with-temp-buffer
199 (yas-minor-mode 1)
200 (let ((yas-selected-text "aaa")
201 (snippet "if ${1:condition}\n`yas-selected-text`\nelse\n$3\nend"))
202 (yas-expand-snippet snippet)
203 (yas-next-field)
204 (ert-simulate-command `(yas-mock-insert "bbb"))
205 (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
206
207 (defmacro yas--with-font-locked-temp-buffer (&rest body)
208 "Like `with-temp-buffer', but ensure `font-lock-mode'."
209 (declare (indent 0) (debug t))
210 (let ((temp-buffer (make-symbol "temp-buffer")))
211 ;; NOTE: buffer name must not start with a space, otherwise
212 ;; `font-lock-mode' doesn't turn on.
213 `(let ((,temp-buffer (generate-new-buffer "*yas-temp*")))
214 (with-current-buffer ,temp-buffer
215 ;; pretend we're interactive so `font-lock-mode' turns on
216 (let ((noninteractive nil)
217 ;; turn on font locking after major mode change
218 (change-major-mode-after-body-hook #'font-lock-mode))
219 (unwind-protect
220 (progn (require 'font-lock)
221 ;; turn on font locking before major mode change
222 (font-lock-mode +1)
223 ,@body)
224 (and (buffer-name ,temp-buffer)
225 (kill-buffer ,temp-buffer))))))))
226
227 (ert-deftest example-for-issue-474 ()
228 (yas--with-font-locked-temp-buffer
229 (c-mode)
230 (yas-minor-mode 1)
231 (insert "#include <foo>\n")
232 (let ((yas-good-grace nil)) (yas-expand-snippet "`\"TODO: \"`"))
233 (should (string= (yas--buffer-contents) "#include <foo>\nTODO: "))))
234
235 (ert-deftest example-for-issue-404 ()
236 (yas--with-font-locked-temp-buffer
237 (c++-mode)
238 (yas-minor-mode 1)
239 (insert "#include <foo>\n")
240 (let ((yas-good-grace nil)) (yas-expand-snippet "main"))
241 (should (string= (yas--buffer-contents) "#include <foo>\nmain"))))
242
243 (ert-deftest example-for-issue-404-c-mode ()
244 (yas--with-font-locked-temp-buffer
245 (c-mode)
246 (yas-minor-mode 1)
247 (insert "#include <foo>\n")
248 (let ((yas-good-grace nil)) (yas-expand-snippet "main"))
249 (should (string= (yas--buffer-contents) "#include <foo>\nmain"))))
250
251 (ert-deftest middle-of-buffer-snippet-insertion ()
252 (with-temp-buffer
253 (yas-minor-mode 1)
254 (insert "beginning")
255 (save-excursion (insert "end"))
256 (yas-expand-snippet "-middle-")
257 (should (string= (yas--buffer-contents) "beginning-middle-end"))))
258
259 (ert-deftest another-example-for-issue-271 ()
260 ;; expect this to fail in batch mode since `region-active-p' doesn't
261 ;; used by `yas-expand-snippet' doesn't make sense in that context.
262 ;;
263 :expected-result (if noninteractive
264 :failed
265 :passed)
266 (with-temp-buffer
267 (yas-minor-mode 1)
268 (let ((snippet "\\${${1:1}:`yas-selected-text`}"))
269 (insert "aaabbbccc")
270 (set-mark 4)
271 (goto-char 7)
272 (yas-expand-snippet snippet)
273 (should (string= (yas--buffer-contents) "aaa${1:bbb}ccc")))))
274
275 (ert-deftest string-match-with-subregexp-in-embedded-elisp ()
276 (with-temp-buffer
277 (yas-minor-mode 1)
278 ;; the rule here is: To use regexps in embedded `(elisp)` expressions, write
279 ;; it like you would normal elisp, i.e. no need to escape the backslashes.
280 (let ((snippet "`(if (string-match \"foo\\\\(ba+r\\\\)foo\" \"foobaaaaaaaaaarfoo\")
281 \"ok\"
282 \"fail\")`"))
283 (yas-expand-snippet snippet))
284 (should (string= (yas--buffer-contents) "ok"))))
285
286 (ert-deftest string-match-with-subregexp-in-mirror-transformations ()
287 (with-temp-buffer
288 (yas-minor-mode 1)
289 ;; the rule here is: To use regexps in embedded `(elisp)` expressions,
290 ;; escape backslashes once, i.e. to use \\( \\) constructs, write \\\\( \\\\).
291 (let ((snippet "$1${1:$(if (string-match \"foo\\\\\\\\(ba+r\\\\\\\\)baz\" yas-text)
292 \"ok\"
293 \"fail\")}"))
294 (yas-expand-snippet snippet)
295 (should (string= (yas--buffer-contents) "fail"))
296 (ert-simulate-command `(yas-mock-insert "foobaaar"))
297 (should (string= (yas--buffer-contents) "foobaaarfail"))
298 (ert-simulate-command `(yas-mock-insert "baz"))
299 (should (string= (yas--buffer-contents) "foobaaarbazok")))))
300
301 \f
302 ;;; Misc tests
303 ;;;
304 (ert-deftest protection-overlay-no-cheating ()
305 "Protection overlays at the very end of the buffer are dealt
306 with by cheatingly inserting a newline!
307
308 TODO: correct this bug!"
309 :expected-result :failed
310 (with-temp-buffer
311 (yas-minor-mode 1)
312 (yas-expand-snippet "${2:brother} from another ${1:mother}")
313 (should (string= (yas--buffer-contents)
314 "brother from another mother") ;; no newline should be here!
315 )))
316 \f
317 ;;; Loading
318 ;;;
319 (defun yas--call-with-temporary-redefinitions (function
320 &rest function-names-and-overriding-functions)
321 (let* ((overrides (remove-if-not #'(lambda (fdef)
322 (fboundp (first fdef)))
323 function-names-and-overriding-functions))
324 (definition-names (mapcar #'first overrides))
325 (overriding-functions (mapcar #'second overrides))
326 (saved-functions (mapcar #'symbol-function definition-names)))
327 ;; saving all definitions before overriding anything ensures FDEFINITION
328 ;; errors don't cause accidental permanent redefinitions.
329 ;;
330 (cl-labels ((set-fdefinitions (names functions)
331 (loop for name in names
332 for fn in functions
333 do (fset name fn))))
334 (set-fdefinitions definition-names overriding-functions)
335 (unwind-protect (funcall function)
336 (set-fdefinitions definition-names saved-functions)))))
337
338 (defmacro yas--with-temporary-redefinitions (fdefinitions &rest body)
339 ;; "Temporarily (but globally) redefine each function in FDEFINITIONS.
340 ;; E.g.: (yas--with-temporary-redefinitions ((foo (x) ...)
341 ;; (bar (x) ...))
342 ;; ;; code that eventually calls foo, bar of (setf foo)
343 ;; ...)"
344 ;; FIXME: This is hideous! Better use defadvice (or at least letf).
345 `(yas--call-with-temporary-redefinitions
346 (lambda () ,@body)
347 ,@(mapcar #'(lambda (thingy)
348 `(list ',(first thingy)
349 (lambda ,@(rest thingy))))
350 fdefinitions)))
351
352 (defmacro yas-with-overriden-buffer-list (&rest body)
353 (let ((saved-sym (make-symbol "yas--buffer-list")))
354 `(let ((,saved-sym (symbol-function 'buffer-list)))
355 (yas--with-temporary-redefinitions
356 ((buffer-list ()
357 (remove-if #'(lambda (buf)
358 (with-current-buffer buf
359 (eq major-mode 'lisp-interaction-mode)))
360 (funcall ,saved-sym))))
361 ,@body))))
362
363
364 (defmacro yas-with-some-interesting-snippet-dirs (&rest body)
365 `(yas-saving-variables
366 (yas-with-overriden-buffer-list
367 (yas-with-snippet-dirs
368 '((".emacs.d/snippets"
369 ("c-mode"
370 (".yas-parents" . "cc-mode")
371 ("printf" . "printf($1);")) ;; notice the overriding for issue #281
372 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
373 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
374 ("library/snippets"
375 ("c-mode"
376 (".yas-parents" . "c++-mode")
377 ("printf" . "printf"))
378 ("cc-mode" ("def" . "# define"))
379 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
380 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
381 ,@body))))
382
383
384 (ert-deftest basic-jit-loading ()
385 "Test basic loading and expansion of snippets"
386 (yas-with-some-interesting-snippet-dirs
387 (yas-reload-all)
388 (yas--basic-jit-loading-1)))
389
390 (ert-deftest basic-jit-loading-with-compiled-snippets ()
391 "Test basic loading and expansion of snippets"
392 (yas-with-some-interesting-snippet-dirs
393 (yas-reload-all)
394 (yas-recompile-all)
395 (yas--with-temporary-redefinitions ((yas--load-directory-2
396 (&rest _dummies)
397 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
398 (yas-reload-all)
399 (yas--basic-jit-loading-1))))
400
401 (ert-deftest loading-with-cyclic-parenthood ()
402 "Test loading when cyclic parenthood is setup."
403 (yas-saving-variables
404 (yas-with-snippet-dirs '((".emacs.d/snippets"
405 ("c-mode"
406 (".yas-parents" . "cc-mode"))
407 ("cc-mode"
408 (".yas-parents" . "yet-another-c-mode and-that-one"))
409 ("yet-another-c-mode"
410 (".yas-parents" . "c-mode and-also-this-one lisp-interaction-mode"))))
411 (yas-reload-all)
412 (with-temp-buffer
413 (let* ((major-mode 'c-mode)
414 (expected `(c-mode
415 cc-mode
416 yet-another-c-mode
417 and-also-this-one
418 and-that-one
419 ;; prog-mode doesn't exit in emacs 24.3
420 ,@(if (fboundp 'prog-mode)
421 '(prog-mode))
422 emacs-lisp-mode
423 lisp-interaction-mode))
424 (observed (yas--modes-to-activate)))
425 (should (null (cl-set-exclusive-or expected observed)))
426 (should (= (length expected)
427 (length observed))))))))
428
429 (defun yas--basic-jit-loading-1 ()
430 (with-temp-buffer
431 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
432 (should (= 0 (hash-table-count yas--tables)))
433 (lisp-interaction-mode)
434 (yas-minor-mode 1)
435 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
436 (should (= 2 (hash-table-count yas--tables)))
437 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
438 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
439 (yas-should-expand '(("sc" . "brother from another mother")
440 ("dolist" . "(dolist)")
441 ("ert-deftest" . "(ert-deftest name () )")))
442 (c-mode)
443 (yas-minor-mode 1)
444 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
445 (should (= 4 (hash-table-count yas--tables)))
446 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
447 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
448 (yas-should-expand '(("printf" . "printf();")
449 ("def" . "# define")))
450 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
451
452 \f
453 ;;; Menu
454 ;;;
455 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
456 `(yas-saving-variables
457 (yas-with-snippet-dirs
458 `((".emacs.d/snippets"
459 ("c-mode"
460 (".yas-make-groups" . "")
461 ("printf" . "printf($1);")
462 ("foo-group-a"
463 ("fnprintf" . "fprintf($1);")
464 ("snprintf" . "snprintf($1);"))
465 ("foo-group-b"
466 ("strcmp" . "strecmp($1);")
467 ("strcasecmp" . "strcasecmp($1);")))
468 ("lisp-interaction-mode"
469 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
470 ("fancy-mode"
471 ("a-guy" . "# uuid: 999\n# --\nyo!")
472 ("a-sir" . "# uuid: 12345\n# --\nindeed!")
473 ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
474 ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
475 ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
476 (".yas-setup.el" . , (pp-to-string
477 '(yas-define-menu 'fancy-mode
478 '((yas-ignore-item "0101")
479 (yas-item "999")
480 (yas-submenu "sirs"
481 ((yas-item "12345")))
482 (yas-submenu "ladies"
483 ((yas-item "54321"))))
484 '("666")))))))
485 ,@body)))
486
487 (ert-deftest test-yas-define-menu ()
488 (let ((yas-use-menu t))
489 (yas-with-even-more-interesting-snippet-dirs
490 (yas-reload-all 'no-jit)
491 (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
492 (should (eql 4 (length menu)))
493 (dolist (item '("a-guy" "a-beggar"))
494 (should (find item menu :key #'third :test #'string=)))
495 (should-not (find "an-outcast" menu :key #'third :test #'string=))
496 (dolist (submenu '("sirs" "ladies"))
497 (should (keymapp
498 (fourth
499 (find submenu menu :key #'third :test #'string=)))))
500 ))))
501
502 (ert-deftest test-group-menus ()
503 "Test group-based menus using .yas-make-groups and the group directive"
504 (let ((yas-use-menu t))
505 (yas-with-even-more-interesting-snippet-dirs
506 (yas-reload-all 'no-jit)
507 ;; first the subdir-based groups
508 ;;
509 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
510 (should (eql 3 (length menu)))
511 (dolist (item '("printf" "foo-group-a" "foo-group-b"))
512 (should (find item menu :key #'third :test #'string=)))
513 (dolist (submenu '("foo-group-a" "foo-group-b"))
514 (should (keymapp
515 (fourth
516 (find submenu menu :key #'third :test #'string=))))))
517 ;; now group directives
518 ;;
519 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
520 (should (eql 1 (length menu)))
521 (should (find "barbar" menu :key #'third :test #'string=))
522 (should (keymapp
523 (fourth
524 (find "barbar" menu :key #'third :test #'string=))))))))
525
526 (ert-deftest test-group-menus-twisted ()
527 "Same as similarly named test, but be mean.
528
529 TODO: be meaner"
530 (let ((yas-use-menu t))
531 (yas-with-even-more-interesting-snippet-dirs
532 ;; add a group directive conflicting with the subdir and watch
533 ;; behaviour
534 (with-temp-buffer
535 (insert "# group: foo-group-c\n# --\nstrecmp($1)")
536 (write-region nil nil (concat (first (yas-snippet-dirs))
537 "/c-mode/foo-group-b/strcmp")))
538 (yas-reload-all 'no-jit)
539 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
540 (should (eql 4 (length menu)))
541 (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
542 (should (find item menu :key #'third :test #'string=)))
543 (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
544 (should (keymapp
545 (fourth
546 (find submenu menu :key #'third :test #'string=))))))
547 ;; delete the .yas-make-groups file and watch behaviour
548 ;;
549 (delete-file (concat (first (yas-snippet-dirs))
550 "/c-mode/.yas-make-groups"))
551 (yas-reload-all 'no-jit)
552 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
553 (should (eql 5 (length menu))))
554 ;; Change a group directive and reload
555 ;;
556 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
557 (should (find "barbar" menu :key #'third :test #'string=)))
558
559 (with-temp-buffer
560 (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
561 (write-region nil nil (concat (first (yas-snippet-dirs))
562 "/lisp-interaction-mode/ert-deftest")))
563 (yas-reload-all 'no-jit)
564 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
565 (should (eql 1 (length menu)))
566 (should (find "foofoo" menu :key #'third :test #'string=))
567 (should (keymapp
568 (fourth
569 (find "foofoo" menu :key #'third :test #'string=))))))))
570
571 \f
572 ;;; The infamous and problematic tab keybinding
573 ;;;
574 (ert-deftest test-yas-tab-binding ()
575 (with-temp-buffer
576 (yas-minor-mode -1)
577 (should (not (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand)))
578 (yas-minor-mode 1)
579 (should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))
580 (yas-expand-snippet "$1 $2 $3")
581 (should (eq (key-binding [(tab)]) 'yas-next-field-or-maybe-expand))
582 (should (eq (key-binding (kbd "TAB")) 'yas-next-field-or-maybe-expand))
583 (should (eq (key-binding [(shift tab)]) 'yas-prev-field))
584 (should (eq (key-binding [backtab]) 'yas-prev-field))))
585
586 (ert-deftest test-rebindings ()
587 (unwind-protect
588 (progn
589 (define-key yas-minor-mode-map [tab] nil)
590 (define-key yas-minor-mode-map (kbd "TAB") nil)
591 (define-key yas-minor-mode-map (kbd "SPC") 'yas-expand)
592 (with-temp-buffer
593 (yas-minor-mode 1)
594 (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
595 (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))
596 (yas-reload-all)
597 (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
598 (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))))
599 ;; FIXME: actually should restore to whatever saved values where there.
600 ;;
601 (define-key yas-minor-mode-map [tab] 'yas-expand)
602 (define-key yas-minor-mode-map (kbd "TAB") 'yas-expand)
603 (define-key yas-minor-mode-map (kbd "SPC") nil)))
604
605 (ert-deftest test-yas-in-org ()
606 (with-temp-buffer
607 (org-mode)
608 (yas-minor-mode 1)
609 (should (eq (key-binding [(tab)]) 'yas-expand))
610 (should (eq (key-binding (kbd "TAB")) 'yas-expand))))
611
612 (ert-deftest test-yas-activate-extra-modes ()
613 "Given a symbol, `yas-activate-extra-mode' should be able to
614 add the snippets associated with the given mode."
615 (with-temp-buffer
616 (yas-saving-variables
617 (yas-with-snippet-dirs
618 '((".emacs.d/snippets"
619 ("markdown-mode"
620 ("_" . "_Text_ "))
621 ("emacs-lisp-mode"
622 ("car" . "(car )"))))
623 (yas-reload-all)
624 (emacs-lisp-mode)
625 (yas-minor-mode-on)
626 (yas-activate-extra-mode 'markdown-mode)
627 (should (eq 'markdown-mode (car yas--extra-modes)))
628 (yas-should-expand '(("_" . "_Text_ ")))
629 (yas-should-expand '(("car" . "(car )")))
630 (yas-deactivate-extra-mode 'markdown-mode)
631 (should-not (eq 'markdown-mode (car yas--extra-modes)))
632 (yas-should-not-expand '("_"))
633 (yas-should-expand '(("car" . "(car )")))))))
634
635 \f
636 ;;; Helpers
637 ;;;
638 (defun yas-should-expand (keys-and-expansions)
639 (dolist (key-and-expansion keys-and-expansions)
640 (yas-exit-all-snippets)
641 (erase-buffer)
642 (insert (car key-and-expansion))
643 (let ((yas-fallback-behavior nil))
644 (ert-simulate-command '(yas-expand)))
645 (should (string= (yas--buffer-contents) (cdr key-and-expansion))))
646 (yas-exit-all-snippets))
647
648 (defun yas-should-not-expand (keys)
649 (dolist (key keys)
650 (yas-exit-all-snippets)
651 (erase-buffer)
652 (insert key)
653 (let ((yas-fallback-behavior nil))
654 (ert-simulate-command '(yas-expand)))
655 (should (string= (yas--buffer-contents) key))))
656
657 (defun yas-mock-insert (string)
658 (interactive)
659 (do ((i 0 (1+ i)))
660 ((= i (length string)))
661 (insert (aref string i))))
662
663 (defun yas-make-file-or-dirs (ass)
664 (let ((file-or-dir-name (car ass))
665 (content (cdr ass)))
666 (cond ((listp content)
667 (make-directory file-or-dir-name 'parents)
668 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
669 (mapc #'yas-make-file-or-dirs content)))
670 ((stringp content)
671 (with-temp-buffer
672 (insert content)
673 (write-region nil nil file-or-dir-name nil 'nomessage)))
674 (t
675 (message "[yas] oops don't know this content")))))
676
677
678 (defun yas-variables ()
679 (let ((syms))
680 (mapatoms #'(lambda (sym)
681 (if (and (string-match "^yas-[^/]" (symbol-name sym))
682 (boundp sym))
683 (push sym syms))))
684 syms))
685
686 (defun yas-call-with-saving-variables (fn)
687 (let* ((vars (yas-variables))
688 (saved-values (mapcar #'symbol-value vars)))
689 (unwind-protect
690 (funcall fn)
691 (loop for var in vars
692 for saved in saved-values
693 do (set var saved)))))
694
695 (defmacro yas-saving-variables (&rest body)
696 `(yas-call-with-saving-variables #'(lambda () ,@body)))
697
698
699 (defun yas-call-with-snippet-dirs (dirs fn)
700 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
701 (yas-snippet-dirs (mapcar #'car dirs)))
702 (with-temp-message ""
703 (unwind-protect
704 (progn
705 (mapc #'yas-make-file-or-dirs dirs)
706 (funcall fn))
707 (when (>= emacs-major-version 24)
708 (delete-directory default-directory 'recursive))))))
709
710 (defmacro yas-with-snippet-dirs (dirs &rest body)
711 (declare (indent defun))
712 `(yas-call-with-snippet-dirs ,dirs
713 #'(lambda ()
714 ,@body)))
715
716 ;;; Older emacsen
717 ;;;
718 (unless (fboundp 'special-mode)
719 ;; FIXME: Why provide this default definition here?!?
720 (defalias 'special-mode 'fundamental))
721
722 ;;; btw to test this in emacs22 mac osx:
723 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
724 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
725 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
726
727
728 (put 'yas-saving-variables 'edebug-form-spec t)
729 (put 'yas-with-snippet-dirs 'edebug-form-spec t)
730 (put 'yas-with-overriden-buffer-list 'edebug-form-spec t)
731 (put 'yas-with-some-interesting-snippet-dirs 'edebug-form-spec t)
732
733
734 (put 'yas--with-temporary-redefinitions 'lisp-indent-function 1)
735 (put 'yas--with-temporary-redefinitions 'edebug-form-spec '((&rest (defun*)) cl-declarations body))
736
737
738
739
740 (provide 'yasnippet-tests)
741 ;;; yasnippet-tests.el ends here
742 ;; Local Variables:
743 ;; lexical-binding: t
744 ;; byte-compile-warnings: (not cl-functions)
745 ;; End: