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