]> code.delx.au - gnu-emacs-elpa/blob - yasnippet-tests.el
Fix: tests also use YAS--WITH-TEMPORARY-REDEFINITIONS to make stubs
[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 in-snippet-undo ()
109 ;; (with-temp-buffer
110 ;; (yas-minor-mode 1)
111 ;; (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
112 ;; (ert-simulate-command '(yas-next-field-or-maybe-expand))
113 ;; (ert-simulate-command `(yas-mock-insert "bla"))
114 ;; (ert-simulate-command '(undo))
115 ;; (should (string= (yas--buffer-contents)
116 ;; "brother from another mother!"))))
117
118 \f
119 ;;; Snippet expansion and character escaping
120 ;;; Thanks to @zw963 (Billy) for the testing
121 ;;;
122 (ert-deftest escape-dollar ()
123 (with-temp-buffer
124 (yas-minor-mode 1)
125 (yas-expand-snippet "bla\\${1:bla}ble")
126 (should (string= (yas--buffer-contents) "bla${1:bla}ble"))))
127
128 (ert-deftest escape-closing-brace ()
129 (with-temp-buffer
130 (yas-minor-mode 1)
131 (yas-expand-snippet "bla${1:bla\\}}ble")
132 (should (string= (yas--buffer-contents) "blabla}ble"))
133 (should (string= (yas-field-value 1) "bla}"))))
134
135 (ert-deftest escape-backslashes ()
136 (with-temp-buffer
137 (yas-minor-mode 1)
138 (yas-expand-snippet "bla\\ble")
139 (should (string= (yas--buffer-contents) "bla\\ble"))))
140
141 (ert-deftest escape-backquotes ()
142 (with-temp-buffer
143 (yas-minor-mode 1)
144 (yas-expand-snippet "bla`(upcase \"foo\\`bar\")`ble")
145 (should (string= (yas--buffer-contents) "blaFOO`BARble"))))
146
147 (ert-deftest escape-some-elisp-with-strings ()
148 "elisp with strings and unbalance parens inside it"
149 (with-temp-buffer
150 (yas-minor-mode 1)
151 ;; The rules here is: to output a literal `"' you need to escape
152 ;; it with one backslash. You don't need to escape them in
153 ;; embedded elisp.
154 (yas-expand-snippet "soon \\\"`(concat (upcase \"(my arms\")\"\\\" were all around her\")`")
155 (should (string= (yas--buffer-contents) "soon \"(MY ARMS\" were all around her"))))
156
157 (ert-deftest escape-some-elisp-with-backslashes ()
158 (with-temp-buffer
159 (yas-minor-mode 1)
160 ;; And the rule here is: to output a literal `\' inside a string
161 ;; inside embedded elisp you need a total of six `\'
162 (yas-expand-snippet "bla`(upcase \"hey\\\\\\yo\")`ble")
163 (should (string= (yas--buffer-contents) "blaHEY\\YOble"))))
164
165 (ert-deftest be-careful-when-escaping-in-yas-selected-text ()
166 (with-temp-buffer
167 (yas-minor-mode 1)
168 (let ((yas/selected-text "He\\\\o world!"))
169 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
170 (should (string= (yas--buffer-contents) "Look ma! He\\\\o world!")))
171 (yas-exit-all-snippets)
172 (erase-buffer)
173 (let ((yas/selected-text "He\"o world!"))
174 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
175 (should (string= (yas--buffer-contents) "Look ma! He\"o world!")))
176 (yas-exit-all-snippets)
177 (erase-buffer)
178 (let ((yas/selected-text "He\"\)\\o world!"))
179 (yas-expand-snippet "Look ma! `(yas/selected-text)`")
180 (should (string= (yas--buffer-contents) "Look ma! He\"\)\\o world!")))
181 (yas-exit-all-snippets)
182 (erase-buffer)))
183
184 (ert-deftest be-careful-when-escaping-in-yas-selected-text-2 ()
185 (with-temp-buffer
186 (let ((yas/selected-text "He)}o world!"))
187 (yas-expand-snippet "Look ma! ${1:`(yas/selected-text)`} OK?")
188 (should (string= (yas--buffer-contents) "Look ma! He)}o world! OK?")))))
189
190 (ert-deftest example-for-issue-271 ()
191 (with-temp-buffer
192 (yas-minor-mode 1)
193 (let ((yas-selected-text "aaa")
194 (snippet "if ${1:condition}\n`yas/selected-text`\nelse\n$3\nend"))
195 (yas-expand-snippet snippet)
196 (yas-next-field)
197 (ert-simulate-command `(yas-mock-insert "bbb"))
198 (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
199
200 (ert-deftest another-example-for-issue-271 ()
201 ;; expect this to fail in batch mode since `region-active-p' doesn't
202 ;; used by `yas-expand-snippet' doesn't make sense in that context.
203 ;;
204 :expected-result (if noninteractive
205 :failed
206 :passed)
207 (with-temp-buffer
208 (yas-minor-mode 1)
209 (let ((snippet "\\${${1:1}:`yas/selected-text`}"))
210 (insert "aaabbbccc")
211 (set-mark 4)
212 (goto-char 7)
213 (yas-expand-snippet snippet)
214 (should (string= (yas--buffer-contents) "aaa${1:bbb}ccc")))))
215
216 (ert-deftest string-match-with-subregexp-in-embedded-elisp ()
217 (with-temp-buffer
218 (yas-minor-mode 1)
219 ;; the rule here is: To use regexps in embedded `(elisp)` expressions, write
220 ;; it like you would normal elisp, i.e. no need to escape the backslashes.
221 (let ((snippet "`(if (string-match \"foo\\\\(ba+r\\\\)foo\" \"foobaaaaaaaaaarfoo\")
222 \"ok\"
223 \"fail\")`"))
224 (yas-expand-snippet snippet))
225 (should (string= (yas--buffer-contents) "ok"))))
226
227 (ert-deftest string-match-with-subregexp-in-mirror-transformations ()
228 (with-temp-buffer
229 (yas-minor-mode 1)
230 ;; the rule here is: To use regexps in embedded `(elisp)` expressions,
231 ;; escape backslashes once, i.e. to use \\( \\) constructs, write \\\\( \\\\).
232 (let ((snippet "$1${1:$(if (string-match \"foo\\\\\\\\(ba+r\\\\\\\\)baz\" yas/text)
233 \"ok\"
234 \"fail\")}"))
235 (yas-expand-snippet snippet)
236 (should (string= (yas--buffer-contents) "fail"))
237 (ert-simulate-command `(yas-mock-insert "foobaaar"))
238 (should (string= (yas--buffer-contents) "foobaaarfail"))
239 (ert-simulate-command `(yas-mock-insert "baz"))
240 (should (string= (yas--buffer-contents) "foobaaarbazok")))))
241
242 \f
243 ;;; Misc tests
244 ;;;
245 (ert-deftest protection-overlay-no-cheating ()
246 "Protection overlays at the very end of the buffer are dealt
247 with by cheatingly inserting a newline!
248
249 TODO: correct this bug!"
250 :expected-result :failed
251 (with-temp-buffer
252 (yas-minor-mode 1)
253 (yas-expand-snippet "${2:brother} from another ${1:mother}")
254 (should (string= (yas--buffer-contents)
255 "brother from another mother") ;; no newline should be here!
256 )))
257 \f
258 ;;; Loading
259 ;;;
260 (defmacro yas-with-overriden-buffer-list (&rest body)
261 (let ((saved-sym (gensym)))
262 `(let ((,saved-sym (symbol-function 'buffer-list)))
263 (yas--with-temporary-redefinitions
264 ((buffer-list ()
265 (remove-if #'(lambda (buf)
266 (with-current-buffer buf
267 (eq major-mode 'lisp-interaction-mode)))
268 (funcall ,saved-sym))))
269 ,@body))))
270
271 (defmacro yas-with-some-interesting-snippet-dirs (&rest body)
272 `(yas-saving-variables
273 (yas-with-overriden-buffer-list
274 (yas-with-snippet-dirs
275 '((".emacs.d/snippets"
276 ("c-mode"
277 (".yas-parents" . "cc-mode")
278 ("printf" . "printf($1);")) ;; notice the overriding for issue #281
279 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
280 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
281 ("library/snippets"
282 ("c-mode"
283 (".yas-parents" . "c++-mode")
284 ("printf" . "printf"))
285 ("cc-mode" ("def" . "# define"))
286 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
287 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
288 ,@body))))
289
290 (ert-deftest basic-jit-loading ()
291 "Test basic loading and expansion of snippets"
292 (yas-with-some-interesting-snippet-dirs
293 (yas-reload-all)
294 (yas--basic-jit-loading-1)))
295
296 (ert-deftest basic-jit-loading-with-compiled-snippets ()
297 "Test basic loading and expansion of snippets"
298 (yas-with-some-interesting-snippet-dirs
299 (yas-reload-all)
300 (yas-recompile-all)
301 (yas--with-temporary-redefinitions ((yas--load-directory-2
302 (&rest dummies)
303 (declare (ignore dummies))
304 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
305 (yas-reload-all)
306 (yas--basic-jit-loading-1))))
307
308 (defun yas--basic-jit-loading-1 (&optional compile)
309 (with-temp-buffer
310 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
311 (should (= 0 (hash-table-count yas--tables)))
312 (lisp-interaction-mode)
313 (yas-minor-mode 1)
314 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
315 (should (= 2 (hash-table-count yas--tables)))
316 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
317 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
318 (yas-should-expand '(("sc" . "brother from another mother")
319 ("dolist" . "(dolist)")
320 ("ert-deftest" . "(ert-deftest name () )")))
321 (c-mode)
322 (yas-minor-mode 1)
323 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
324 (should (= 4 (hash-table-count yas--tables)))
325 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
326 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
327 (yas-should-expand '(("printf" . "printf();")
328 ("def" . "# define")))
329 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
330
331 \f
332 ;;; Menu
333 ;;;
334 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
335 `(yas-saving-variables
336 (yas-with-snippet-dirs
337 `((".emacs.d/snippets"
338 ("c-mode"
339 (".yas-make-groups" . "")
340 ("printf" . "printf($1);")
341 ("foo-group-a"
342 ("fnprintf" . "fprintf($1);")
343 ("snprintf" . "snprintf($1);"))
344 ("foo-group-b"
345 ("strcmp" . "strecmp($1);")
346 ("strcasecmp" . "strcasecmp($1);")))
347 ("lisp-interaction-mode"
348 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
349 ("fancy-mode"
350 ("a-guy" . "# uuid: 999\n# --\nyo!")
351 ("a-sir" . "# uuid: 12345\n# --\nindeed!")
352 ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
353 ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
354 ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
355 (".yas-setup.el" . , (pp-to-string
356 '(yas-define-menu 'fancy-mode
357 '((yas-ignore-item "0101")
358 (yas-item "999")
359 (yas-submenu "sirs"
360 ((yas-item "12345")))
361 (yas-submenu "ladies"
362 ((yas-item "54321"))))
363 '("666")))))))
364 ,@body)))
365
366 (ert-deftest test-yas-define-menu ()
367 (let ((yas-use-menu t))
368 (yas-with-even-more-interesting-snippet-dirs
369 (yas-reload-all 'no-jit)
370 (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
371 (should (eql 4 (length menu)))
372 (dolist (item '("a-guy" "a-beggar"))
373 (should (find item menu :key #'third :test #'string=)))
374 (should-not (find "an-outcast" menu :key #'third :test #'string=))
375 (dolist (submenu '("sirs" "ladies"))
376 (should (keymapp
377 (fourth
378 (find submenu menu :key #'third :test #'string=)))))
379 ))))
380
381 (ert-deftest test-group-menus ()
382 "Test group-based menus using .yas-make-groups and the group directive"
383 (let ((yas-use-menu t))
384 (yas-with-even-more-interesting-snippet-dirs
385 (yas-reload-all 'no-jit)
386 ;; first the subdir-based groups
387 ;;
388 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
389 (should (eql 3 (length menu)))
390 (dolist (item '("printf" "foo-group-a" "foo-group-b"))
391 (should (find item menu :key #'third :test #'string=)))
392 (dolist (submenu '("foo-group-a" "foo-group-b"))
393 (should (keymapp
394 (fourth
395 (find submenu menu :key #'third :test #'string=))))))
396 ;; now group directives
397 ;;
398 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
399 (should (eql 1 (length menu)))
400 (should (find "barbar" menu :key #'third :test #'string=))
401 (should (keymapp
402 (fourth
403 (find "barbar" menu :key #'third :test #'string=))))))))
404
405 (ert-deftest test-group-menus-twisted ()
406 "Same as similarly named test, but be mean.
407
408 TODO: be meaner"
409 (let ((yas-use-menu t))
410 (yas-with-even-more-interesting-snippet-dirs
411 ;; add a group directive conflicting with the subdir and watch
412 ;; behaviour
413 (with-temp-buffer
414 (insert "# group: foo-group-c\n# --\nstrecmp($1)")
415 (write-region nil nil (concat (first (yas-snippet-dirs))
416 "/c-mode/foo-group-b/strcmp")))
417 (yas-reload-all 'no-jit)
418 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
419 (should (eql 4 (length menu)))
420 (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
421 (should (find item menu :key #'third :test #'string=)))
422 (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
423 (should (keymapp
424 (fourth
425 (find submenu menu :key #'third :test #'string=))))))
426 ;; delete the .yas-make-groups file and watch behaviour
427 ;;
428 (delete-file (concat (first (yas-snippet-dirs))
429 "/c-mode/.yas-make-groups"))
430 (yas-reload-all 'no-jit)
431 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
432 (should (eql 5 (length menu))))
433 ;; Change a group directive and reload
434 ;;
435 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
436 (should (find "barbar" menu :key #'third :test #'string=)))
437
438 (with-temp-buffer
439 (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
440 (write-region nil nil (concat (first (yas-snippet-dirs))
441 "/lisp-interaction-mode/ert-deftest")))
442 (yas-reload-all 'no-jit)
443 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
444 (should (eql 1 (length menu)))
445 (should (find "foofoo" menu :key #'third :test #'string=))
446 (should (keymapp
447 (fourth
448 (find "foofoo" menu :key #'third :test #'string=))))))))
449
450 \f
451 ;;; The infamous and problematic tab keybinding
452 ;;;
453 (ert-deftest test-yas-tab-binding ()
454 (with-temp-buffer
455 (yas-minor-mode -1)
456 (should (not (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand)))
457 (yas-minor-mode 1)
458 (should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))
459 (yas-expand-snippet "$1 $2 $3")
460 (should (eq (key-binding [(tab)]) 'yas-next-field-or-maybe-expand))
461 (should (eq (key-binding (kbd "TAB")) 'yas-next-field-or-maybe-expand))
462 (should (eq (key-binding [(shift tab)]) 'yas-prev-field))
463 (should (eq (key-binding [backtab]) 'yas-prev-field))))
464
465 (ert-deftest test-yas-in-org ()
466 (with-temp-buffer
467 (org-mode)
468 (yas-minor-mode 1)
469 (should (eq (key-binding [(tab)]) 'yas-expand))
470 (should (eq (key-binding (kbd "TAB")) 'yas-expand))))
471
472 \f
473 ;;; Helpers
474 ;;;
475 (defun yas/ert ()
476 (interactive)
477 (with-temp-buffer
478 (yas--with-temporary-redefinitions
479 ((message (&rest args) ;
480 (declare (ignore args))
481 nil))
482 (ert t (buffer-name (current-buffer)))
483 (princ (buffer-string)))))
484
485
486 (defun yas-should-expand (keys-and-expansions)
487 (dolist (key-and-expansion keys-and-expansions)
488 (yas-exit-all-snippets)
489 (erase-buffer)
490 (insert (car key-and-expansion))
491 (let ((yas-fallback-behavior nil))
492 (ert-simulate-command '(yas-expand)))
493 (should (string= (yas--buffer-contents) (cdr key-and-expansion))))
494 (yas-exit-all-snippets))
495
496 (defun yas-should-not-expand (keys)
497 (dolist (key keys)
498 (yas-exit-all-snippets)
499 (erase-buffer)
500 (insert key)
501 (let ((yas-fallback-behavior nil))
502 (ert-simulate-command '(yas-expand)))
503 (should (string= (yas--buffer-contents) key))))
504
505 (defun yas-mock-insert (string)
506 (interactive)
507 (do ((i 0 (1+ i)))
508 ((= i (length string)))
509 (insert (aref string i))))
510
511 (defun yas-make-file-or-dirs (ass)
512 (let ((file-or-dir-name (car ass))
513 (content (cdr ass)))
514 (cond ((listp content)
515 (make-directory file-or-dir-name 'parents)
516 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
517 (mapc #'yas-make-file-or-dirs content)))
518 ((stringp content)
519 (with-temp-buffer
520 (insert content)
521 (write-region nil nil file-or-dir-name nil 'nomessage)))
522 (t
523 (message "[yas] oops don't know this content")))))
524
525
526 (defun yas-variables ()
527 (let ((syms))
528 (mapatoms #'(lambda (sym)
529 (if (and (string-match "^yas-[^/]" (symbol-name sym))
530 (boundp sym))
531 (push sym syms))))
532 syms))
533
534 (defun yas-call-with-saving-variables (fn)
535 (let* ((vars (yas-variables))
536 (saved-values (mapcar #'symbol-value vars)))
537 (unwind-protect
538 (funcall fn)
539 (loop for var in vars
540 for saved in saved-values
541 do (set var saved)))))
542
543 (defmacro yas-saving-variables (&rest body)
544 `(yas-call-with-saving-variables #'(lambda () ,@body)))
545
546
547 (defun yas-call-with-snippet-dirs (dirs fn)
548 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
549 (yas-snippet-dirs (mapcar #'car dirs)))
550 (with-temp-message ""
551 (unwind-protect
552 (progn
553 (mapc #'yas-make-file-or-dirs dirs)
554 (funcall fn))
555 (when (>= emacs-major-version 24)
556 (delete-directory default-directory 'recursive))))))
557
558 (defmacro yas-with-snippet-dirs (dirs &rest body)
559 `(yas-call-with-snippet-dirs ,dirs
560 #'(lambda ()
561 ,@body)))
562
563 ;;; Older emacsen
564 ;;;
565 (unless (fboundp 'special-mode)
566 (define-minor-mode special-mode "Just a placeholder for something isn't in emacs 22"))
567
568 ;;; btw to test this in emacs22 mac osx:
569 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
570 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
571 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
572
573
574 (provide 'yasnippet-tests)
575 ;;; yasnippet-tests.el ends here
576 ;; Local Variables:
577 ;; lexical-binding: t
578 ;; End: