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