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