]> code.delx.au - gnu-emacs-elpa/blob - yasnippet-tests.el
Rename all non-external symbols to `yas--`.
[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
32
33 ;;; Snippet mechanics
34
35 (ert-deftest field-navigation ()
36 (with-temp-buffer
37 (yas-minor-mode 1)
38 (yas-expand-snippet "${1:brother} from another ${2:mother}")
39 (should (string= (buffer-substring-no-properties (point-min) (point-max))
40 "brother from another mother"))
41
42 (should (looking-at "brother"))
43 (ert-simulate-command '(yas-next-field-or-maybe-expand))
44 (should (looking-at "mother"))
45 (ert-simulate-command '(yas-prev-field))
46 (should (looking-at "brother"))))
47
48 (ert-deftest simple-mirror ()
49 (with-temp-buffer
50 (yas-minor-mode 1)
51 (yas-expand-snippet "${1:brother} from another $1")
52 (should (string= (buffer-substring-no-properties (point-min) (point-max))
53 "brother from another brother"))
54 (ert-simulate-command `(yas-mock-insert "bla"))
55 (should (string= (buffer-substring-no-properties (point-min) (point-max))
56 "bla from another bla"))))
57
58 (ert-deftest mirror-with-transformation ()
59 (with-temp-buffer
60 (yas-minor-mode 1)
61 (yas-expand-snippet "${1:brother} from another ${1:$(upcase yas-text)}")
62 (should (string= (buffer-substring-no-properties (point-min) (point-max))
63 "brother from another BROTHER"))
64 (ert-simulate-command `(yas-mock-insert "bla"))
65 (should (string= (buffer-substring-no-properties (point-min) (point-max))
66 "bla from another BLA"))))
67
68 (ert-deftest nested-placeholders-kill-superfield ()
69 (with-temp-buffer
70 (yas-minor-mode 1)
71 (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
72 (should (string= (buffer-substring-no-properties (point-min) (point-max))
73 "brother from another mother!"))
74 (ert-simulate-command `(yas-mock-insert "bla"))
75 (should (string= (buffer-substring-no-properties (point-min) (point-max))
76 "brother from bla!"))))
77
78 (ert-deftest nested-placeholders-use-subfield ()
79 (with-temp-buffer
80 (yas-minor-mode 1)
81 (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
82 (ert-simulate-command '(yas-next-field-or-maybe-expand))
83 (ert-simulate-command `(yas-mock-insert "bla"))
84 (should (string= (buffer-substring-no-properties (point-min) (point-max))
85 "brother from another bla!"))))
86
87 ;; (ert-deftest in-snippet-undo ()
88 ;; (with-temp-buffer
89 ;; (yas-minor-mode 1)
90 ;; (yas-expand-snippet "brother from ${2:another ${3:mother}}!")
91 ;; (ert-simulate-command '(yas-next-field-or-maybe-expand))
92 ;; (ert-simulate-command `(yas-mock-insert "bla"))
93 ;; (ert-simulate-command '(undo))
94 ;; (should (string= (buffer-substring-no-properties (point-min) (point-max))
95 ;; "brother from another mother!"))))
96
97
98 ;;; Misc tests
99 ;;;
100
101 (ert-deftest protection-overlay-no-cheating ()
102 "Protection overlays at the very end of the buffer, are dealt by cheatingly inserting a newline!
103
104 TODO: correct this bug!"
105 :expected-result :failed
106 (with-temp-buffer
107 (yas-minor-mode 1)
108 (yas-expand-snippet "${2:brother} from another ${1:mother}")
109 (should (string= (buffer-substring-no-properties (point-min) (point-max))
110 "brother from another mother") ;; no newline should be here!
111 )))
112
113 ;;; Loading
114 ;;;
115 (defmacro with-some-interesting-snippet-dirs (&rest body)
116 `(yas-saving-variables
117 (yas-with-snippet-dirs
118 '((".emacs.d/snippets"
119 ("c-mode"
120 (".yas-parents" . "cc-mode")
121 ("printf" . "printf($1);"))
122 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
123 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
124 ("library/snippets"
125 ("c-mode" (".yas-parents" . "c++-mode"))
126 ("cc-mode" ("def" . "# define"))
127 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
128 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
129 ,@body)))
130
131 (ert-deftest basic-jit-loading ()
132 "Test basic loading and expansion of snippets"
133 (with-some-interesting-snippet-dirs
134 (yas-reload-all)
135 (yas--basic-jit-loading-1)))
136
137 (ert-deftest basic-jit-loading-with-compiled-snippets ()
138 "Test basic loading and expansion of snippets"
139 (with-some-interesting-snippet-dirs
140 (yas-reload-all)
141 (yas-recompile-all)
142 (flet ((yas--load-directory-2
143 (&rest dummies)
144 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
145 (yas-reload-all)
146 (yas--basic-jit-loading-1))))
147
148 (defun yas--basic-jit-loading-1 (&optional compile)
149 (with-temp-buffer
150 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
151 (should (= 0 (hash-table-count yas--tables)))
152 (lisp-interaction-mode)
153 (yas-minor-mode 1)
154 (should (= 2 (hash-table-count yas--scheduled-jit-loads)))
155 (should (= 2 (hash-table-count yas--tables)))
156 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'lisp-interaction-mode yas--tables)))))
157 (should (= 2 (hash-table-count (yas--table-uuidhash (gethash 'emacs-lisp-mode yas--tables)))))
158 (yas-should-expand '(("sc" . "brother from another mother")
159 ("dolist" . "(dolist)")
160 ("ert-deftest" . "(ert-deftest name () )")))
161 (c-mode)
162 (yas-minor-mode 1)
163 (should (= 0 (hash-table-count yas--scheduled-jit-loads)))
164 (should (= 4 (hash-table-count yas--tables)))
165 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'c-mode yas--tables)))))
166 (should (= 1 (hash-table-count (yas--table-uuidhash (gethash 'cc-mode yas--tables)))))
167 (yas-should-expand '(("printf" . "printf();")
168 ("def" . "# define")))
169 (yas-should-not-expand '("sc" "dolist" "ert-deftest"))))
170
171 ;;; Helpers
172 ;;;
173
174 (defun yas-should-expand (keys-and-expansions)
175 (dolist (key-and-expansion keys-and-expansions)
176 (yas-exit-all-snippets)
177 (erase-buffer)
178 (insert (car key-and-expansion))
179 (let ((yas-fallback-behavior nil))
180 (ert-simulate-command '(yas-expand)))
181 (should (string= (buffer-substring-no-properties (point-min) (point-max))
182 (cdr key-and-expansion))))
183 (yas-exit-all-snippets))
184
185 (defun yas-should-not-expand (keys)
186 (dolist (key keys)
187 (yas-exit-all-snippets)
188 (erase-buffer)
189 (insert key)
190 (let ((yas-fallback-behavior nil))
191 (ert-simulate-command '(yas-expand)))
192 (should (string= (buffer-substring-no-properties (point-min) (point-max)) key))))
193
194 (defun yas-mock-insert (string)
195 (interactive)
196 (do ((i 0 (1+ i)))
197 ((= i (length string)))
198 (insert (aref string i))))
199
200 (defun yas-make-file-or-dirs (ass)
201 (let ((file-or-dir-name (car ass))
202 (content (cdr ass)))
203 (cond ((listp content)
204 (make-directory file-or-dir-name 'parents)
205 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
206 (mapc #'yas-make-file-or-dirs content)))
207 ((stringp content)
208 (with-temp-buffer
209 (insert content)
210 (write-region nil nil file-or-dir-name nil 'nomessage)))
211 (t
212 (message "[yas] oops don't know this content")))))
213
214
215 (defun yas-variables ()
216 (let ((syms))
217 (mapatoms #'(lambda (sym)
218 (if (and (string-match "^yas-[^/]" (symbol-name sym))
219 (boundp sym))
220 (push sym syms))))
221 syms))
222
223
224 (defmacro yas-saving-variables (&rest body)
225 `(let ,(mapcar #'(lambda (sym)
226 `(,sym ,sym))
227 (yas-variables))
228 ,@body))
229
230 (defun yas-call-with-snippet-dirs (dirs fn)
231 (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
232 (yas-snippet-dirs (mapcar #'car dirs)))
233 (with-temp-message ""
234 (unwind-protect
235 (progn
236 (mapc #'yas-make-file-or-dirs dirs)
237 (funcall fn))
238 (when (>= emacs-major-version 23)
239 (delete-directory default-directory 'recursive))))))
240
241 (defmacro yas-with-snippet-dirs (dirs &rest body)
242 `(yas-call-with-snippet-dirs ,dirs
243 #'(lambda ()
244 ,@body)))
245
246 ;;; Older emacsen
247 ;;;
248 (unless (fboundp 'special-mode)
249 (define-minor-mode special-mode "Just a placeholder for something isn't in emacs 22"))
250
251 ;;; btw to test this in emacs22 mac osx:
252 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
253 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
254 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
255
256 (provide 'yasnippet-tests)
257 ;;; yasnippet-tests.el ends here