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