]> code.delx.au - gnu-emacs-elpa/blob - yasnippet-tests.el
bugfix: also exit all snippets at end of should-expand test
[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 (ert-deftest basic-loading ()
116 "Test basic loading and expansion of snippets"
117 (yas/saving-variables
118 (with-snippet-dirs
119 '((".emacs.d/snippets"
120 ("c-mode"
121 (".yas-parents" . "cc-mode")
122 ("printf" . "printf($1);"))
123 ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
124 ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
125 ("library/snippets"
126 ("c-mode" (".yas-parents" . "c++-mode"))
127 ("cc-mode" ("def" . "# define"))
128 ("emacs-lisp-mode" ("dolist" . "(dolist)"))
129 ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
130 (yas/reload-all 'with-jit)
131 (with-temp-buffer
132 (should (= 4 (hash-table-count yas/scheduled-jit-loads)))
133 (should (= 0 (hash-table-count yas/tables)))
134 (lisp-interaction-mode) (yas/minor-mode 1) ;; either one will load two tables depending on yas/global-mode (FIXME)
135 (should (= 2 (hash-table-count yas/scheduled-jit-loads)))
136 (should (= 2 (hash-table-count yas/tables)))
137 (should (= 1 (hash-table-count (yas/table-uuidhash (gethash 'lisp-interaction-mode yas/tables)))))
138 (should (= 2 (hash-table-count (yas/table-uuidhash (gethash 'emacs-lisp-mode yas/tables)))))
139 (yas/should-expand '(("sc" . "brother from another mother")
140 ("dolist" . "(dolist)")
141 ("ert-deftest" . "(ert-deftest name () )")))
142 (c-mode)
143 (yas/should-expand '(("printf" . "printf();")
144 ("def" . "# define")))
145 (yas/should-not-expand '("sc" "dolist" "ert-deftest"))))))
146
147 ;;; Helpers
148 ;;;
149
150 (defun yas/should-expand (keys-and-expansions)
151 (dolist (key-and-expansion keys-and-expansions)
152 (yas/exit-all-snippets)
153 (erase-buffer)
154 (insert (car key-and-expansion))
155 (let ((yas/fallback-behavior nil))
156 (ert-simulate-command '(yas/expand)))
157 (should (string= (buffer-substring-no-properties (point-min) (point-max))
158 (cdr key-and-expansion))))
159 (yas/exit-all-snippets))
160
161 (defun yas/should-not-expand (keys)
162 (dolist (key keys)
163 (yas/exit-all-snippets)
164 (erase-buffer)
165 (insert key)
166 (let ((yas/fallback-behavior nil))
167 (ert-simulate-command '(yas/expand)))
168 (should (string= (buffer-substring-no-properties (point-min) (point-max)) key))))
169
170 (defun yas/mock-insert (string)
171 (interactive)
172 (do ((i 0 (1+ i)))
173 ((= i (length string)))
174 (insert (aref string i))))
175
176 (defun yas/make-file-or-dirs (ass)
177 (let ((file-or-dir-name (car ass))
178 (content (cdr ass)))
179 (cond ((listp content)
180 (make-directory file-or-dir-name 'parents)
181 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
182 (mapc #'yas/make-file-or-dirs content)))
183 ((stringp content)
184 (with-current-buffer (find-file file-or-dir-name)
185 (insert content)
186 (save-buffer)
187 (kill-buffer (current-buffer))))
188 (t
189 (message "[yas] oops don't know this content")))))
190
191
192 (defun yas/variables ()
193 (let ((syms))
194 (mapatoms #'(lambda (sym)
195 (if (and (string-match "^yas/[^/]" (symbol-name sym))
196 (boundp sym))
197 (push sym syms))))
198 syms))
199
200
201 (defmacro yas/saving-variables (&rest body)
202 `(let ,(mapcar #'(lambda (sym)
203 `(,sym ,sym))
204 (yas/variables))
205 ,@body))
206
207 (defmacro with-snippet-dirs (dirs &rest body)
208 `(let ((default-directory (make-temp-file "yasnippet-fixture" t)))
209 (unwind-protect
210 (progn
211 (setq yas/snippet-dirs ',(mapcar #'car (cadr dirs)))
212 (mapc #'yas/make-file-or-dirs ,dirs)
213 ,@body)
214 (when (>= emacs-major-version 23)
215 (delete-directory default-directory 'recursive)))))
216
217 ;;; Older emacsen
218 ;;;
219 (unless (fboundp 'special-mode)
220 (define-minor-mode special-mode "Just a placeholder for something isn't in emacs 22"))
221
222 ;;; btw to test this in emacs22 mac osx:
223 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
224 ;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
225 ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert
226
227 (provide 'yasnippet-tests)
228 ;;; yasnippet-tests.el ends here