]> code.delx.au - gnu-emacs-elpa/blob - yasnippet-tests.el
`yas-minor-mode' assertion broke a test. Good. Fix it.
[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
437 (ert-deftest basic-jit-loading ()
438 "Test basic loading and expansion of snippets"
439 (yas-with-some-interesting-snippet-dirs
440 (yas-reload-all)
441 (yas--basic-jit-loading-1)))
442
443 (ert-deftest basic-jit-loading-with-compiled-snippets ()
444 "Test basic loading and expansion of snippets"
445 (yas-with-some-interesting-snippet-dirs
446 (yas-reload-all)
447 (yas-recompile-all)
448 (yas--with-temporary-redefinitions ((yas--load-directory-2
449 (&rest _dummies)
450 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
451 (yas-reload-all)
452 (yas--basic-jit-loading-1))))
453
454 (ert-deftest loading-with-cyclic-parenthood ()
455 "Test loading when cyclic parenthood is setup."
456 (yas-saving-variables
457 (yas-with-snippet-dirs '((".emacs.d/snippets"
458 ("c-mode"
459 (".yas-parents" . "cc-mode"))
460 ("cc-mode"
461 (".yas-parents" . "yet-another-c-mode and-that-one"))
462 ("yet-another-c-mode"
463 (".yas-parents" . "c-mode and-also-this-one lisp-interaction-mode"))))
464 (yas-reload-all)
465 (with-temp-buffer
466 (let* ((major-mode 'c-mode)
467 (expected `(c-mode
468 cc-mode
469 yet-another-c-mode
470 and-also-this-one
471 and-that-one
472 ;; prog-mode doesn't exit in emacs 24.3
473 ,@(if (fboundp 'prog-mode)
474 '(prog-mode))
475 emacs-lisp-mode
476 lisp-interaction-mode))
477 (observed (yas--modes-to-activate)))
478 (should (null (cl-set-exclusive-or expected observed)))
479 (should (= (length expected)
480 (length observed))))))))
481
482 (ert-deftest issue-492-and-494 ()
483 (defalias 'yas--phony-c-mode 'c-mode)
484 (define-derived-mode yas--test-mode yas--phony-c-mode "Just a test mode")
485 (yas-with-snippet-dirs '((".emacs.d/snippets"
486 ("yas--test-mode")))
487 (yas-reload-all)
488 (with-temp-buffer
489 (let* ((major-mode 'yas--test-mode)
490 (expected `(c-mode
491 ,@(if (fboundp 'prog-mode)
492 '(prog-mode))
493 yas--phony-c-mode
494 yas--test-mode))
495 (observed (yas--modes-to-activate)))
496 (should (null (cl-set-exclusive-or expected observed)))
497 (should (= (length expected)
498 (length observed)))))))
499
500 (ert-deftest issue-504-tricky-jit ()
501 (define-derived-mode yas--test-mode c-mode "Just a test mode")
502 (define-derived-mode yas--another-test-mode c-mode "Another test mode")
503 (yas-with-snippet-dirs
504 '((".emacs.d/snippets"
505 ("yas--another-test-mode"
506 (".yas-parents" . "yas--test-mode"))
507 ("yas--test-mode")))
508 (let ((b (with-current-buffer (generate-new-buffer "*yas-test*")
509 (yas--another-test-mode)
510 (current-buffer))))
511 (unwind-protect
512 (progn
513 (yas-reload-all)
514 (should (= 0 (hash-table-count yas--scheduled-jit-loads))))
515 (kill-buffer b)))))
516
517 (defun yas--basic-jit-loading-1 ()
518 (with-temp-buffer
519 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
520 (should (= 0 (hash-table-count yas--tables)))
521 (lisp-interaction-mode)
522 (yas-minor-mode 1)
523 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
524 (should (= 2 (hash-table-count yas--tables)))
525 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
526 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
527 (yas-should-expand '(("sc" . "brother from another mother")
528 ("dolist" . "(dolist)")
529 ("ert-deftest" . "(ert-deftest name () )")))
530 (c-mode)
531 (yas-minor-mode 1)
532 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
533 (should (= 4 (hash-table-count yas--tables)))
534 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
535 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
536 (yas-should-expand '(("printf" . "printf();")
537 ("def" . "# define")))
538 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
539
540 \f
541 ;;; Menu
542 ;;;
543 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
544 `(yas-saving-variables
545 (yas-with-snippet-dirs
546 `((".emacs.d/snippets"
547 ("c-mode"
548 (".yas-make-groups" . "")
549 ("printf" . "printf($1);")
550 ("foo-group-a"
551 ("fnprintf" . "fprintf($1);")
552 ("snprintf" . "snprintf($1);"))
553 ("foo-group-b"
554 ("strcmp" . "strecmp($1);")
555 ("strcasecmp" . "strcasecmp($1);")))
556 ("lisp-interaction-mode"
557 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
558 ("fancy-mode"
559 ("a-guy" . "# uuid: 999\n# --\nyo!")
560 ("a-sir" . "# uuid: 12345\n# --\nindeed!")
561 ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
562 ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
563 ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
564 (".yas-setup.el" . , (pp-to-string
565 '(yas-define-menu 'fancy-mode
566 '((yas-ignore-item "0101")
567 (yas-item "999")
568 (yas-submenu "sirs"
569 ((yas-item "12345")))
570 (yas-submenu "ladies"
571 ((yas-item "54321"))))
572 '("666")))))))
573 ,@body)))
574
575 (ert-deftest test-yas-define-menu ()
576 (let ((yas-use-menu t))
577 (yas-with-even-more-interesting-snippet-dirs
578 (yas-reload-all 'no-jit)
579 (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
580 (should (eql 4 (length menu)))
581 (dolist (item '("a-guy" "a-beggar"))
582 (should (find item menu :key #'third :test #'string=)))
583 (should-not (find "an-outcast" menu :key #'third :test #'string=))
584 (dolist (submenu '("sirs" "ladies"))
585 (should (keymapp
586 (fourth
587 (find submenu menu :key #'third :test #'string=)))))
588 ))))
589
590 (ert-deftest test-group-menus ()
591 "Test group-based menus using .yas-make-groups and the group directive"
592 (let ((yas-use-menu t))
593 (yas-with-even-more-interesting-snippet-dirs
594 (yas-reload-all 'no-jit)
595 ;; first the subdir-based groups
596 ;;
597 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
598 (should (eql 3 (length menu)))
599 (dolist (item '("printf" "foo-group-a" "foo-group-b"))
600 (should (find item menu :key #'third :test #'string=)))
601 (dolist (submenu '("foo-group-a" "foo-group-b"))
602 (should (keymapp
603 (fourth
604 (find submenu menu :key #'third :test #'string=))))))
605 ;; now group directives
606 ;;
607 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
608 (should (eql 1 (length menu)))
609 (should (find "barbar" menu :key #'third :test #'string=))
610 (should (keymapp
611 (fourth
612 (find "barbar" menu :key #'third :test #'string=))))))))
613
614 (ert-deftest test-group-menus-twisted ()
615 "Same as similarly named test, but be mean.
616
617 TODO: be meaner"
618 (let ((yas-use-menu t))
619 (yas-with-even-more-interesting-snippet-dirs
620 ;; add a group directive conflicting with the subdir and watch
621 ;; behaviour
622 (with-temp-buffer
623 (insert "# group: foo-group-c\n# --\nstrecmp($1)")
624 (write-region nil nil (concat (first (yas-snippet-dirs))
625 "/c-mode/foo-group-b/strcmp")))
626 (yas-reload-all 'no-jit)
627 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
628 (should (eql 4 (length menu)))
629 (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
630 (should (find item menu :key #'third :test #'string=)))
631 (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
632 (should (keymapp
633 (fourth
634 (find submenu menu :key #'third :test #'string=))))))
635 ;; delete the .yas-make-groups file and watch behaviour
636 ;;
637 (delete-file (concat (first (yas-snippet-dirs))
638 "/c-mode/.yas-make-groups"))
639 (yas-reload-all 'no-jit)
640 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
641 (should (eql 5 (length menu))))
642 ;; Change a group directive and reload
643 ;;
644 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
645 (should (find "barbar" menu :key #'third :test #'string=)))
646
647 (with-temp-buffer
648 (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
649 (write-region nil nil (concat (first (yas-snippet-dirs))
650 "/lisp-interaction-mode/ert-deftest")))
651 (yas-reload-all 'no-jit)
652 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
653 (should (eql 1 (length menu)))
654 (should (find "foofoo" menu :key #'third :test #'string=))
655 (should (keymapp
656 (fourth
657 (find "foofoo" menu :key #'third :test #'string=))))))))
658
659 \f
660 ;;; The infamous and problematic tab keybinding
661 ;;;
662 (ert-deftest test-yas-tab-binding ()
663 (with-temp-buffer
664 (yas-minor-mode -1)
665 (should (not (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand)))
666 (yas-minor-mode 1)
667 (should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))
668 (yas-expand-snippet "$1 $2 $3")
669 (should (eq (key-binding [(tab)]) 'yas-next-field-or-maybe-expand))
670 (should (eq (key-binding (kbd "TAB")) 'yas-next-field-or-maybe-expand))
671 (should (eq (key-binding [(shift tab)]) 'yas-prev-field))
672 (should (eq (key-binding [backtab]) 'yas-prev-field))))
673
674 (ert-deftest test-rebindings ()
675 (unwind-protect
676 (progn
677 (define-key yas-minor-mode-map [tab] nil)
678 (define-key yas-minor-mode-map (kbd "TAB") nil)
679 (define-key yas-minor-mode-map (kbd "SPC") 'yas-expand)
680 (with-temp-buffer
681 (yas-minor-mode 1)
682 (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
683 (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))
684 (yas-reload-all)
685 (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
686 (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))))
687 ;; FIXME: actually should restore to whatever saved values where there.
688 ;;
689 (define-key yas-minor-mode-map [tab] 'yas-expand)
690 (define-key yas-minor-mode-map (kbd "TAB") 'yas-expand)
691 (define-key yas-minor-mode-map (kbd "SPC") nil)))
692
693 (ert-deftest test-yas-in-org ()
694 (with-temp-buffer
695 (org-mode)
696 (yas-minor-mode 1)
697 (should (eq (key-binding [(tab)]) 'yas-expand))
698 (should (eq (key-binding (kbd "TAB")) 'yas-expand))))
699
700 (ert-deftest test-yas-activate-extra-modes ()
701 "Given a symbol, `yas-activate-extra-mode' should be able to
702 add the snippets associated with the given mode."
703 (with-temp-buffer
704 (yas-saving-variables
705 (yas-with-snippet-dirs
706 '((".emacs.d/snippets"
707 ("markdown-mode"
708 ("_" . "_Text_ "))
709 ("emacs-lisp-mode"
710 ("car" . "(car )"))))
711 (yas-reload-all)
712 (emacs-lisp-mode)
713 (yas-minor-mode-on)
714 (yas-activate-extra-mode 'markdown-mode)
715 (should (eq 'markdown-mode (car yas--extra-modes)))
716 (yas-should-expand '(("_" . "_Text_ ")))
717 (yas-should-expand '(("car" . "(car )")))
718 (yas-deactivate-extra-mode 'markdown-mode)
719 (should-not (eq 'markdown-mode (car yas--extra-modes)))
720 (yas-should-not-expand '("_"))
721 (yas-should-expand '(("car" . "(car )")))))))
722
723 \f
724 ;;; Helpers
725 ;;;
726 (defun yas-should-expand (keys-and-expansions)
727 (dolist (key-and-expansion keys-and-expansions)
728 (yas-exit-all-snippets)
729 (narrow-to-region (point) (point))
730 (insert (car key-and-expansion))
731 (let ((yas-fallback-behavior nil))
732 (ert-simulate-command '(yas-expand)))
733 (unless (string= (yas--buffer-contents) (cdr key-and-expansion))
734 (ert-fail (format "\"%s\" should have expanded to \"%s\" but got \"%s\""
735 (car key-and-expansion)
736 (cdr key-and-expansion)
737 (yas--buffer-contents)))))
738 (yas-exit-all-snippets))
739
740 (defun yas-should-not-expand (keys)
741 (dolist (key keys)
742 (yas-exit-all-snippets)
743 (narrow-to-region (point) (point))
744 (insert key)
745 (let ((yas-fallback-behavior nil))
746 (ert-simulate-command '(yas-expand)))
747 (unless (string= (yas--buffer-contents) key)
748 (ert-fail (format "\"%s\" should have stayed put, but instead expanded to \"%s\""
749 key
750 (yas--buffer-contents))))))
751
752 (defun yas-mock-insert (string)
753 (interactive)
754 (do ((i 0 (1+ i)))
755 ((= i (length string)))
756 (insert (aref string i))))
757
758 (defun yas-make-file-or-dirs (ass)
759 (let ((file-or-dir-name (car ass))
760 (content (cdr ass)))
761 (cond ((listp content)
762 (make-directory file-or-dir-name 'parents)
763 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
764 (mapc #'yas-make-file-or-dirs content)))
765 ((stringp content)
766 (with-temp-buffer
767 (insert content)
768 (write-region nil nil file-or-dir-name nil 'nomessage)))
769 (t
770 (message "[yas] oops don't know this content")))))
771
772
773 (defun yas-variables ()
774 (let ((syms))
775 (mapatoms #'(lambda (sym)
776 (if (and (string-match "^yas-[^/]" (symbol-name sym))
777 (boundp sym))
778 (push sym syms))))
779 syms))
780
781 (defun yas-call-with-saving-variables (fn)
782 (let* ((vars (yas-variables))
783 (saved-values (mapcar #'symbol-value vars)))
784 (unwind-protect
785 (funcall fn)
786 (loop for var in vars
787 for saved in saved-values
788 do (set var saved)))))
789
790 (defmacro yas-saving-variables (&rest body)
791 `(yas-call-with-saving-variables #'(lambda () ,@body)))
792
793
794 (defun yas-call-with-snippet-dirs (dirs fn)
795 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
796 (yas-snippet-dirs (mapcar #'car dirs)))
797 (with-temp-message ""
798 (unwind-protect
799 (progn
800 (mapc #'yas-make-file-or-dirs dirs)
801 (funcall fn))
802 (when (>= emacs-major-version 24)
803 (delete-directory default-directory 'recursive))))))
804
805 (defmacro yas-with-snippet-dirs (dirs &rest body)
806 (declare (indent defun))
807 `(yas-call-with-snippet-dirs ,dirs
808 #'(lambda ()
809 ,@body)))
810
811 ;;; Older emacsen
812 ;;;
813 (unless (fboundp 'special-mode)
814 ;; FIXME: Why provide this default definition here?!?
815 (defalias 'special-mode 'fundamental))
816
817 ;;; btw to test this in emacs22 mac osx:
818 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
819 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
820 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
821
822
823 (put 'yas-saving-variables 'edebug-form-spec t)
824 (put 'yas-with-snippet-dirs 'edebug-form-spec t)
825 (put 'yas-with-overriden-buffer-list 'edebug-form-spec t)
826 (put 'yas-with-some-interesting-snippet-dirs 'edebug-form-spec t)
827
828
829 (put 'yas--with-temporary-redefinitions 'lisp-indent-function 1)
830 (put 'yas--with-temporary-redefinitions 'edebug-form-spec '((&rest (defun*)) cl-declarations body))
831
832
833
834
835 (provide 'yasnippet-tests)
836 ;; Local Variables:
837 ;; indent-tabs-mode: nil
838 ;; lexical-binding: t
839 ;; byte-compile-warnings: (not cl-functions)
840 ;; End:
841 ;;; yasnippet-tests.el ends here