]> code.delx.au - gnu-emacs-elpa/blob - yasnippet-tests.el
Merge remote-tracking branch 'origin/master' into jit-loading
[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 (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 (yas/basic-jit-loading-1))
134
135 (ert-deftest basic-jit-loading-with-compiled-snippets ()
136 "Test basic loading and expansion of snippets"
137 (yas/basic-jit-loading-1 'compile))
138
139 (defun yas/basic-jit-loading-1 (&optional compile)
140 (yas/saving-variables
141 (with-snippet-dirs
142 '((".emacs.d/snippets"
143 ("c-mode"
144 (".yas-parents" . "cc-mode")
145 ("printf" . "printf($1);"))
146 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
147 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
148 ("library/snippets"
149 ("c-mode" (".yas-parents" . "c++-mode"))
150 ("cc-mode" ("def" . "# define"))
151 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
152 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
153 (yas/reload-all)
154 (with-temp-buffer
155 (should (= 4 (hash-table-count yas/scheduled-jit-loads)))
156 (should (= 0 (hash-table-count yas/tables)))
157 (lisp-interaction-mode) (yas/minor-mode 1) ;; either one will load two tables depending on yas/global-mode (FIXME)
158 (should (= 2 (hash-table-count yas/scheduled-jit-loads)))
159 (should (= 2 (hash-table-count yas/tables)))
160 (should (= 1 (hash-table-count (yas/table-uuidhash (gethash 'lisp-interaction-mode yas/tables)))))
161 (should (= 2 (hash-table-count (yas/table-uuidhash (gethash 'emacs-lisp-mode yas/tables)))))
162 (yas/should-expand '(("sc" . "brother from another mother")
163 ("dolist" . "(dolist)")
164 ("ert-deftest" . "(ert-deftest name () )")))
165 (c-mode)
166 (yas/minor-mode 1)
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-current-buffer (find-file file-or-dir-name)
209 (insert content)
210 (save-buffer)
211 (kill-buffer (current-buffer))))
212 (t
213 (message "[yas] oops don't know this content")))))
214
215
216 (defun yas/variables ()
217 (let ((syms))
218 (mapatoms #'(lambda (sym)
219 (if (and (string-match "^yas/[^/]" (symbol-name sym))
220 (boundp sym))
221 (push sym syms))))
222 syms))
223
224
225 (defmacro yas/saving-variables (&rest body)
226 `(let ,(mapcar #'(lambda (sym)
227 `(,sym ,sym))
228 (yas/variables))
229 ,@body))
230
231 (defmacro with-snippet-dirs (dirs &rest body)
232 `(let ((default-directory (make-temp-file "yasnippet-fixture" t)))
233 (unwind-protect
234 (progn
235 (setq yas/snippet-dirs ',(mapcar #'car (cadr dirs)))
236 (mapc #'yas/make-file-or-dirs ,dirs)
237 ,@body)
238 (when (>= emacs-major-version 23)
239 (delete-directory default-directory 'recursive)))))
240
241 ;;; Older emacsen
242 ;;;
243 (unless (fboundp 'special-mode)
244 (define-minor-mode special-mode "Just a placeholder for something isn't in emacs 22"))
245
246 ;;; btw to test this in emacs22 mac osx:
247 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
248 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
249 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
250
251 (provide 'yasnippet-tests)
252 ;;; yasnippet-tests.el ends here