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