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