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