]> code.delx.au - gnu-emacs-elpa/blob - yasnippet-tests.el
* yasnippet-tests.el (string-suffix-p): Define for older Emacsen.
[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 exit 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 (null (cl-set-exclusive-or expected observed)))
501 (should (= (length expected)
502 (length observed))))))))
503
504 (ert-deftest issue-492-and-494 ()
505 (defalias 'yas--phony-c-mode 'c-mode)
506 (define-derived-mode yas--test-mode yas--phony-c-mode "Just a test mode")
507 (yas-with-snippet-dirs '((".emacs.d/snippets"
508 ("yas--test-mode")))
509 (yas-reload-all)
510 (with-temp-buffer
511 (let* ((major-mode 'yas--test-mode)
512 (expected `(c-mode
513 ,@(if (fboundp 'prog-mode)
514 '(prog-mode))
515 yas--phony-c-mode
516 yas--test-mode))
517 (observed (yas--modes-to-activate)))
518 (should (null (cl-set-exclusive-or expected observed)))
519 (should (= (length expected)
520 (length observed)))))))
521
522 (ert-deftest issue-504-tricky-jit ()
523 (define-derived-mode yas--test-mode c-mode "Just a test mode")
524 (define-derived-mode yas--another-test-mode c-mode "Another test mode")
525 (yas-with-snippet-dirs
526 '((".emacs.d/snippets"
527 ("yas--another-test-mode"
528 (".yas-parents" . "yas--test-mode"))
529 ("yas--test-mode")))
530 (let ((b (with-current-buffer (generate-new-buffer "*yas-test*")
531 (yas--another-test-mode)
532 (current-buffer))))
533 (unwind-protect
534 (progn
535 (yas-reload-all)
536 (should (= 0 (hash-table-count yas--scheduled-jit-loads))))
537 (kill-buffer b)))))
538
539 (defun yas--basic-jit-loading-1 ()
540 (with-temp-buffer
541 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
542 (should (= 0 (hash-table-count yas--tables)))
543 (lisp-interaction-mode)
544 (yas-minor-mode 1)
545 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
546 (should (= 2 (hash-table-count yas--tables)))
547 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
548 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
549 (yas-should-expand '(("sc" . "brother from another mother")
550 ("dolist" . "(dolist)")
551 ("ert-deftest" . "(ert-deftest name () )")))
552 (c-mode)
553 (yas-minor-mode 1)
554 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
555 (should (= 4 (hash-table-count yas--tables)))
556 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
557 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
558 (yas-should-expand '(("printf" . "printf();")
559 ("def" . "# define")))
560 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
561
562 \f
563 ;;; Menu
564 ;;;
565 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
566 `(yas-saving-variables
567 (yas-with-snippet-dirs
568 `((".emacs.d/snippets"
569 ("c-mode"
570 (".yas-make-groups" . "")
571 ("printf" . "printf($1);")
572 ("foo-group-a"
573 ("fnprintf" . "fprintf($1);")
574 ("snprintf" . "snprintf($1);"))
575 ("foo-group-b"
576 ("strcmp" . "strecmp($1);")
577 ("strcasecmp" . "strcasecmp($1);")))
578 ("lisp-interaction-mode"
579 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
580 ("fancy-mode"
581 ("a-guy" . "# uuid: 999\n# --\nyo!")
582 ("a-sir" . "# uuid: 12345\n# --\nindeed!")
583 ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
584 ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
585 ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
586 (".yas-setup.el" . , (pp-to-string
587 '(yas-define-menu 'fancy-mode
588 '((yas-ignore-item "0101")
589 (yas-item "999")
590 (yas-submenu "sirs"
591 ((yas-item "12345")))
592 (yas-submenu "ladies"
593 ((yas-item "54321"))))
594 '("666")))))))
595 ,@body)))
596
597 (ert-deftest test-yas-define-menu ()
598 (let ((yas-use-menu t))
599 (yas-with-even-more-interesting-snippet-dirs
600 (yas-reload-all 'no-jit)
601 (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
602 (should (eql 4 (length menu)))
603 (dolist (item '("a-guy" "a-beggar"))
604 (should (find item menu :key #'third :test #'string=)))
605 (should-not (find "an-outcast" menu :key #'third :test #'string=))
606 (dolist (submenu '("sirs" "ladies"))
607 (should (keymapp
608 (fourth
609 (find submenu menu :key #'third :test #'string=)))))
610 ))))
611
612 (ert-deftest test-group-menus ()
613 "Test group-based menus using .yas-make-groups and the group directive"
614 (let ((yas-use-menu t))
615 (yas-with-even-more-interesting-snippet-dirs
616 (yas-reload-all 'no-jit)
617 ;; first the subdir-based groups
618 ;;
619 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
620 (should (eql 3 (length menu)))
621 (dolist (item '("printf" "foo-group-a" "foo-group-b"))
622 (should (find item menu :key #'third :test #'string=)))
623 (dolist (submenu '("foo-group-a" "foo-group-b"))
624 (should (keymapp
625 (fourth
626 (find submenu menu :key #'third :test #'string=))))))
627 ;; now group directives
628 ;;
629 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
630 (should (eql 1 (length menu)))
631 (should (find "barbar" menu :key #'third :test #'string=))
632 (should (keymapp
633 (fourth
634 (find "barbar" menu :key #'third :test #'string=))))))))
635
636 (ert-deftest test-group-menus-twisted ()
637 "Same as similarly named test, but be mean.
638
639 TODO: be meaner"
640 (let ((yas-use-menu t))
641 (yas-with-even-more-interesting-snippet-dirs
642 ;; add a group directive conflicting with the subdir and watch
643 ;; behaviour
644 (with-temp-buffer
645 (insert "# group: foo-group-c\n# --\nstrecmp($1)")
646 (write-region nil nil (concat (first (yas-snippet-dirs))
647 "/c-mode/foo-group-b/strcmp")))
648 (yas-reload-all 'no-jit)
649 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
650 (should (eql 4 (length menu)))
651 (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
652 (should (find item menu :key #'third :test #'string=)))
653 (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
654 (should (keymapp
655 (fourth
656 (find submenu menu :key #'third :test #'string=))))))
657 ;; delete the .yas-make-groups file and watch behaviour
658 ;;
659 (delete-file (concat (first (yas-snippet-dirs))
660 "/c-mode/.yas-make-groups"))
661 (yas-reload-all 'no-jit)
662 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
663 (should (eql 5 (length menu))))
664 ;; Change a group directive and reload
665 ;;
666 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
667 (should (find "barbar" menu :key #'third :test #'string=)))
668
669 (with-temp-buffer
670 (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
671 (write-region nil nil (concat (first (yas-snippet-dirs))
672 "/lisp-interaction-mode/ert-deftest")))
673 (yas-reload-all 'no-jit)
674 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
675 (should (eql 1 (length menu)))
676 (should (find "foofoo" menu :key #'third :test #'string=))
677 (should (keymapp
678 (fourth
679 (find "foofoo" menu :key #'third :test #'string=))))))))
680
681 \f
682 ;;; The infamous and problematic tab keybinding
683 ;;;
684 (ert-deftest test-yas-tab-binding ()
685 (with-temp-buffer
686 (yas-minor-mode -1)
687 (should (not (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand)))
688 (yas-minor-mode 1)
689 (should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))
690 (yas-expand-snippet "$1 $2 $3")
691 (should (eq (key-binding [(tab)]) 'yas-next-field-or-maybe-expand))
692 (should (eq (key-binding (kbd "TAB")) 'yas-next-field-or-maybe-expand))
693 (should (eq (key-binding [(shift tab)]) 'yas-prev-field))
694 (should (eq (key-binding [backtab]) 'yas-prev-field))))
695
696 (ert-deftest test-rebindings ()
697 (unwind-protect
698 (progn
699 (define-key yas-minor-mode-map [tab] nil)
700 (define-key yas-minor-mode-map (kbd "TAB") nil)
701 (define-key yas-minor-mode-map (kbd "SPC") 'yas-expand)
702 (with-temp-buffer
703 (yas-minor-mode 1)
704 (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
705 (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))
706 (yas-reload-all)
707 (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
708 (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))))
709 ;; FIXME: actually should restore to whatever saved values where there.
710 ;;
711 (define-key yas-minor-mode-map [tab] 'yas-expand)
712 (define-key yas-minor-mode-map (kbd "TAB") 'yas-expand)
713 (define-key yas-minor-mode-map (kbd "SPC") nil)))
714
715 (ert-deftest test-yas-in-org ()
716 (with-temp-buffer
717 (org-mode)
718 (yas-minor-mode 1)
719 (should (eq (key-binding [(tab)]) 'yas-expand))
720 (should (eq (key-binding (kbd "TAB")) 'yas-expand))))
721
722 (ert-deftest test-yas-activate-extra-modes ()
723 "Given a symbol, `yas-activate-extra-mode' should be able to
724 add the snippets associated with the given mode."
725 (with-temp-buffer
726 (yas-saving-variables
727 (yas-with-snippet-dirs
728 '((".emacs.d/snippets"
729 ("markdown-mode"
730 ("_" . "_Text_ "))
731 ("emacs-lisp-mode"
732 ("car" . "(car )"))))
733 (yas-reload-all)
734 (emacs-lisp-mode)
735 (yas-minor-mode-on)
736 (yas-activate-extra-mode 'markdown-mode)
737 (should (eq 'markdown-mode (car yas--extra-modes)))
738 (yas-should-expand '(("_" . "_Text_ ")))
739 (yas-should-expand '(("car" . "(car )")))
740 (yas-deactivate-extra-mode 'markdown-mode)
741 (should-not (eq 'markdown-mode (car yas--extra-modes)))
742 (yas-should-not-expand '("_"))
743 (yas-should-expand '(("car" . "(car )")))))))
744
745 \f
746 ;;; Helpers
747 ;;;
748 (defun yas-should-expand (keys-and-expansions)
749 (dolist (key-and-expansion keys-and-expansions)
750 (yas-exit-all-snippets)
751 (narrow-to-region (point) (point))
752 (insert (car key-and-expansion))
753 (let ((yas-fallback-behavior nil))
754 (ert-simulate-command '(yas-expand)))
755 (unless (string= (yas--buffer-contents) (cdr key-and-expansion))
756 (ert-fail (format "\"%s\" should have expanded to \"%s\" but got \"%s\""
757 (car key-and-expansion)
758 (cdr key-and-expansion)
759 (yas--buffer-contents)))))
760 (yas-exit-all-snippets))
761
762 (defun yas-should-not-expand (keys)
763 (dolist (key keys)
764 (yas-exit-all-snippets)
765 (narrow-to-region (point) (point))
766 (insert key)
767 (let ((yas-fallback-behavior nil))
768 (ert-simulate-command '(yas-expand)))
769 (unless (string= (yas--buffer-contents) key)
770 (ert-fail (format "\"%s\" should have stayed put, but instead expanded to \"%s\""
771 key
772 (yas--buffer-contents))))))
773
774 (defun yas-mock-insert (string)
775 (interactive)
776 (do ((i 0 (1+ i)))
777 ((= i (length string)))
778 (insert (aref string i))))
779
780 (defun yas-make-file-or-dirs (ass)
781 (let ((file-or-dir-name (car ass))
782 (content (cdr ass)))
783 (cond ((listp content)
784 (make-directory file-or-dir-name 'parents)
785 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
786 (mapc #'yas-make-file-or-dirs content)))
787 ((stringp content)
788 (with-temp-buffer
789 (insert content)
790 (write-region nil nil file-or-dir-name nil 'nomessage)))
791 (t
792 (message "[yas] oops don't know this content")))))
793
794
795 (defun yas-variables ()
796 (let ((syms))
797 (mapatoms #'(lambda (sym)
798 (if (and (string-match "^yas-[^/]" (symbol-name sym))
799 (boundp sym))
800 (push sym syms))))
801 syms))
802
803 (defun yas-call-with-saving-variables (fn)
804 (let* ((vars (yas-variables))
805 (saved-values (mapcar #'symbol-value vars)))
806 (unwind-protect
807 (funcall fn)
808 (loop for var in vars
809 for saved in saved-values
810 do (set var saved)))))
811
812 (defmacro yas-saving-variables (&rest body)
813 `(yas-call-with-saving-variables #'(lambda () ,@body)))
814
815
816 (defun yas-call-with-snippet-dirs (dirs fn)
817 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
818 (yas-snippet-dirs (mapcar #'car dirs)))
819 (with-temp-message ""
820 (unwind-protect
821 (progn
822 (mapc #'yas-make-file-or-dirs dirs)
823 (funcall fn))
824 (when (>= emacs-major-version 24)
825 (delete-directory default-directory 'recursive))))))
826
827 (defmacro yas-with-snippet-dirs (dirs &rest body)
828 (declare (indent defun))
829 `(yas-call-with-snippet-dirs ,dirs
830 #'(lambda ()
831 ,@body)))
832
833 ;;; Older emacsen
834 ;;;
835 (unless (fboundp 'special-mode)
836 ;; FIXME: Why provide this default definition here?!?
837 (defalias 'special-mode 'fundamental))
838
839 (unless (fboundp 'string-suffix-p)
840 ;; introduced in Emacs 24.4
841 (defun string-suffix-p (suffix string &optional ignore-case)
842 "Return non-nil if SUFFIX is a suffix of STRING.
843 If IGNORE-CASE is non-nil, the comparison is done without paying
844 attention to case differences."
845 (let ((start-pos (- (length string) (length suffix))))
846 (and (>= start-pos 0)
847 (eq t (compare-strings suffix nil nil
848 string start-pos nil ignore-case))))))
849
850 ;;; btw to test this in emacs22 mac osx:
851 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
852 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
853 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
854
855
856 (put 'yas-saving-variables 'edebug-form-spec t)
857 (put 'yas-with-snippet-dirs 'edebug-form-spec t)
858 (put 'yas-with-overriden-buffer-list 'edebug-form-spec t)
859 (put 'yas-with-some-interesting-snippet-dirs 'edebug-form-spec t)
860
861
862 (put 'yas--with-temporary-redefinitions 'lisp-indent-function 1)
863 (put 'yas--with-temporary-redefinitions 'edebug-form-spec '((&rest (defun*)) cl-declarations body))
864
865
866
867
868 (provide 'yasnippet-tests)
869 ;; Local Variables:
870 ;; indent-tabs-mode: nil
871 ;; lexical-binding: t
872 ;; byte-compile-warnings: (not cl-functions)
873 ;; End:
874 ;;; yasnippet-tests.el ends here