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