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