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