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