1 ;;; yasnippet-tests.el --- some yasnippet tests
3 ;; Copyright (C) 2012 João Távora
5 ;; Author: João Távora <joaot@siscog.pt>
6 ;; Keywords: emulations, convenience
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.
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.
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/>.
23 ;; Test basic snippet mechanics and the loading system
34 (ert-deftest field-navigation ()
37 (yas-expand-snippet "${1:brother} from another ${2:mother}")
38 (should (string= (buffer-substring-no-properties (point-min) (point-max))
39 "brother from another mother"))
41 (should (looking-at "brother"))
42 (ert-simulate-command '(yas-next-field-or-maybe-expand))
43 (should (looking-at "mother"))
44 (ert-simulate-command '(yas-prev-field))
45 (should (looking-at "brother"))))
47 (ert-deftest simple-mirror ()
50 (yas-expand-snippet "${1:brother} from another $1")
51 (should (string= (buffer-substring-no-properties (point-min) (point-max))
52 "brother from another brother"))
53 (ert-simulate-command `(yas-mock-insert "bla"))
54 (should (string= (buffer-substring-no-properties (point-min) (point-max))
55 "bla from another bla"))))
57 (ert-deftest mirror-with-transformation ()
60 (yas-expand-snippet "${1:brother} from another ${1:$(upcase yas-text)}")
61 (should (string= (buffer-substring-no-properties (point-min) (point-max))
62 "brother from another BROTHER"))
63 (ert-simulate-command `(yas-mock-insert "bla"))
64 (should (string= (buffer-substring-no-properties (point-min) (point-max))
65 "bla from another BLA"))))
67 (ert-deftest nested-placeholders-kill-superfield ()
70 (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
71 (should (string= (buffer-substring-no-properties (point-min) (point-max))
72 "brother from another mother!"))
73 (ert-simulate-command `(yas-mock-insert "bla"))
74 (should (string= (buffer-substring-no-properties (point-min) (point-max))
75 "brother from bla!"))))
77 (ert-deftest nested-placeholders-use-subfield ()
80 (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
81 (ert-simulate-command '(yas-next-field-or-maybe-expand))
82 (ert-simulate-command `(yas-mock-insert "bla"))
83 (should (string= (buffer-substring-no-properties (point-min) (point-max))
84 "brother from another bla!"))))
86 ;; (ert-deftest in-snippet-undo ()
89 ;; (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
90 ;; (ert-simulate-command '(yas-next-field-or-maybe-expand))
91 ;; (ert-simulate-command `(yas-mock-insert "bla"))
92 ;; (ert-simulate-command '(undo))
93 ;; (should (string= (buffer-substring-no-properties (point-min) (point-max))
94 ;; "brother from another mother!"))))
100 (ert-deftest protection-overlay-no-cheating ()
101 "Protection overlays at the very end of the buffer, are dealt by cheatingly inserting a newline!
103 TODO: correct this bug!"
104 :expected-result :failed
107 (yas-expand-snippet "${2:brother} from another ${1:mother}")
108 (should (string= (buffer-substring-no-properties (point-min) (point-max))
109 "brother from another mother") ;; no newline should be here!
114 (defmacro yas-with-some-interesting-snippet-dirs (&rest body)
115 `(yas-saving-variables
116 (yas-with-snippet-dirs
117 '((".emacs.d/snippets"
119 (".yas-parents" . "cc-mode")
120 ("printf" . "printf($1);"))
121 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
122 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
124 ("c-mode" (".yas-parents" . "c++-mode"))
125 ("cc-mode" ("def" . "# define"))
126 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
127 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
130 (ert-deftest basic-jit-loading ()
131 "Test basic loading and expansion of snippets"
132 (yas-with-some-interesting-snippet-dirs
134 (yas--basic-jit-loading-1)))
136 (ert-deftest basic-jit-loading-with-compiled-snippets ()
137 "Test basic loading and expansion of snippets"
138 (yas-with-some-interesting-snippet-dirs
141 (flet ((yas--load-directory-2
143 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
145 (yas--basic-jit-loading-1))))
147 (defun yas--basic-jit-loading-1 (&optional compile)
149 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
150 (should (= 0 (hash-table-count yas--tables)))
151 (lisp-interaction-mode)
153 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
154 (should (= 2 (hash-table-count yas--tables)))
155 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
156 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
157 (yas-should-expand '(("sc" . "brother from another mother")
158 ("dolist" . "(dolist)")
159 ("ert-deftest" . "(ert-deftest name () )")))
162 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
163 (should (= 4 (hash-table-count yas--tables)))
164 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
165 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
166 (yas-should-expand '(("printf" . "printf();")
167 ("def" . "# define")))
168 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
173 (defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
174 `(yas-saving-variables
175 (yas-with-snippet-dirs
176 `((".emacs.d/snippets"
178 (".yas-make-groups" . "")
179 ("printf" . "printf($1);")
181 ("fnprintf" . "fprintf($1);")
182 ("snprintf" . "snprintf($1);"))
184 ("strcmp" . "strecmp($1);")
185 ("strcasecmp" . "strcasecmp($1);")))
186 ("lisp-interaction-mode"
187 ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
189 ("a-guy" . "# uuid: 999\n# --\nyo!")
190 ("a-sir" . "# uuid: 12345\n# --\nindeed!")
191 ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
192 ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
193 ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
194 (".yas-setup.el" . , (pp-to-string
195 '(yas-define-menu 'fancy-mode
196 '((yas-ignore-item "0101")
199 ((yas-item "12345")))
200 (yas-submenu "ladies"
201 ((yas-item "54321"))))
205 (ert-deftest test-yas-define-menu ()
206 (let ((yas-use-menu t))
207 (yas-with-even-more-interesting-snippet-dirs
208 (yas-reload-all 'no-jit)
209 (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
210 (should (eql 4 (length menu)))
211 (dolist (item '("a-guy" "a-beggar"))
212 (should (find item menu :key #'third :test #'string=)))
213 (should-not (find "an-outcast" menu :key #'third :test #'string=))
214 (dolist (submenu '("sirs" "ladies"))
217 (find submenu menu :key #'third :test #'string=)))))
220 (ert-deftest test-group-menus ()
221 "Test group-based menus using .yas-make-groups and the group directive"
222 (let ((yas-use-menu t))
223 (yas-with-even-more-interesting-snippet-dirs
224 (yas-reload-all 'no-jit)
225 ;; first the subdir-based groups
227 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
228 (should (eql 3 (length menu)))
229 (dolist (item '("printf" "foo-group-a" "foo-group-b"))
230 (should (find item menu :key #'third :test #'string=)))
231 (dolist (submenu '("foo-group-a" "foo-group-b"))
234 (find submenu menu :key #'third :test #'string=))))))
235 ;; now group directives
237 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
238 (should (eql 1 (length menu)))
239 (should (find "barbar" menu :key #'third :test #'string=))
242 (find "barbar" menu :key #'third :test #'string=))))))))
244 (ert-deftest test-group-menus-twisted ()
245 "Same as similarly named test, but be mean.
248 (let ((yas-use-menu t))
249 (yas-with-even-more-interesting-snippet-dirs
250 ;; add a group directive conflicting with the subdir and watch
253 (insert "# group: foo-group-c\n# --\nstrecmp($1)")
254 (write-region nil nil (concat (first (yas-snippet-dirs))
255 "/c-mode/foo-group-b/strcmp")))
256 (yas-reload-all 'no-jit)
257 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
258 (should (eql 4 (length menu)))
259 (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
260 (should (find item menu :key #'third :test #'string=)))
261 (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
264 (find submenu menu :key #'third :test #'string=))))))
265 ;; delete the .yas-make-groups file and watch behaviour
267 (delete-file (concat (first (yas-snippet-dirs))
268 "/c-mode/.yas-make-groups"))
269 (yas-reload-all 'no-jit)
270 (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
271 (should (eql 5 (length menu))))
272 ;; Change a group directive and reload
274 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
275 (should (find "barbar" menu :key #'third :test #'string=)))
278 (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
279 (write-region nil nil (concat (first (yas-snippet-dirs))
280 "/lisp-interaction-mode/ert-deftest")))
281 (yas-reload-all 'no-jit)
282 (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
283 (should (eql 1 (length menu)))
284 (should (find "foofoo" menu :key #'third :test #'string=))
287 (find "foofoo" menu :key #'third :test #'string=))))))))
293 (defun yas-should-expand (keys-and-expansions)
294 (dolist (key-and-expansion keys-and-expansions)
295 (yas-exit-all-snippets)
297 (insert (car key-and-expansion))
298 (let ((yas-fallback-behavior nil))
299 (ert-simulate-command '(yas-expand)))
300 (should (string= (buffer-substring-no-properties (point-min) (point-max))
301 (cdr key-and-expansion))))
302 (yas-exit-all-snippets))
304 (defun yas-should-not-expand (keys)
306 (yas-exit-all-snippets)
309 (let ((yas-fallback-behavior nil))
310 (ert-simulate-command '(yas-expand)))
311 (should (string= (buffer-substring-no-properties (point-min) (point-max)) key))))
313 (defun yas-mock-insert (string)
316 ((= i (length string)))
317 (insert (aref string i))))
319 (defun yas-make-file-or-dirs (ass)
320 (let ((file-or-dir-name (car ass))
322 (cond ((listp content)
323 (make-directory file-or-dir-name 'parents)
324 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
325 (mapc #'yas-make-file-or-dirs content)))
329 (write-region nil nil file-or-dir-name nil 'nomessage)))
331 (message "[yas] oops don't know this content")))))
334 (defun yas-variables ()
336 (mapatoms #'(lambda (sym)
337 (if (and (string-match "^yas-[^/]" (symbol-name sym))
342 (defun yas-call-with-saving-variables (fn)
343 (let* ((vars (yas-variables))
344 (saved-values (mapcar #'symbol-value vars)))
347 (loop for var in vars
348 for saved in saved-values
349 do (set var saved)))))
351 (defmacro yas-saving-variables (&rest body)
352 `(yas-call-with-saving-variables #'(lambda () ,@body)))
355 (defun yas-call-with-snippet-dirs (dirs fn)
356 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
357 (yas-snippet-dirs (mapcar #'car dirs)))
358 (with-temp-message ""
361 (mapc #'yas-make-file-or-dirs dirs)
363 (when (>= emacs-major-version 23)
364 (delete-directory default-directory 'recursive))))))
366 (defmacro yas-with-snippet-dirs (dirs &rest body)
367 `(yas-call-with-snippet-dirs ,dirs
373 (unless (fboundp 'special-mode)
374 (define-minor-mode special-mode "Just a placeholder for something isn't in emacs 22"))
376 ;;; btw to test this in emacs22 mac osx:
377 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
378 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
379 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
382 (provide 'yasnippet-tests)
383 ;;; yasnippet-tests.el ends here
385 ;; lexical-binding: t