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