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