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