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
35 (ert-deftest field-navigation ()
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"))
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"))))
48 (ert-deftest simple-mirror ()
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"))))
58 (ert-deftest mirror-with-transformation ()
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"))))
68 (ert-deftest nested-placeholders-kill-superfield ()
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!"))))
78 (ert-deftest nested-placeholders-use-subfield ()
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!"))))
87 ;; (ert-deftest in-snippet-undo ()
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!"))))
101 (ert-deftest protection-overlay-no-cheating ()
102 "Protection overlays at the very end of the buffer, are dealt by cheatingly inserting a newline!
104 TODO: correct this bug!"
105 :expected-result :failed
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!
115 (defmacro with-some-interesting-snippet-dirs (&rest body)
116 `(yas/saving-variables
118 '((".emacs.d/snippets"
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")))
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"))))
131 (ert-deftest basic-jit-loading ()
132 "Test basic loading and expansion of snippets"
133 (yas/basic-jit-loading-1))
135 (ert-deftest basic-jit-loading-with-compiled-snippets ()
136 "Test basic loading and expansion of snippets"
137 (yas/basic-jit-loading-1 'compile))
139 (defun yas/basic-jit-loading-1 (&optional compile)
140 (yas/saving-variables
142 '((".emacs.d/snippets"
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")))
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"))))
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 () )")))
167 (yas/should-expand '(("printf" . "printf();")
168 ("def" . "# define")))
169 (yas/should-not-expand '("sc" "dolist" "ert-deftest"))))))
174 (defun yas/should-expand (keys-and-expansions)
175 (dolist (key-and-expansion keys-and-expansions)
176 (yas/exit-all-snippets)
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))
185 (defun yas/should-not-expand (keys)
187 (yas/exit-all-snippets)
190 (let ((yas/fallback-behavior nil))
191 (ert-simulate-command '(yas/expand)))
192 (should (string= (buffer-substring-no-properties (point-min) (point-max)) key))))
194 (defun yas/mock-insert (string)
197 ((= i (length string)))
198 (insert (aref string i))))
200 (defun yas/make-file-or-dirs (ass)
201 (let ((file-or-dir-name (car 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)))
208 (with-current-buffer (find-file file-or-dir-name)
211 (kill-buffer (current-buffer))))
213 (message "[yas] oops don't know this content")))))
216 (defun yas/variables ()
218 (mapatoms #'(lambda (sym)
219 (if (and (string-match "^yas/[^/]" (symbol-name sym))
225 (defmacro yas/saving-variables (&rest body)
226 `(let ,(mapcar #'(lambda (sym)
231 (defmacro with-snippet-dirs (dirs &rest body)
232 `(let ((default-directory (make-temp-file "yasnippet-fixture" t)))
235 (setq yas/snippet-dirs ',(mapcar #'car (cadr dirs)))
236 (mapc #'yas/make-file-or-dirs ,dirs)
238 (when (>= emacs-major-version 23)
239 (delete-directory default-directory 'recursive)))))
243 (unless (fboundp 'special-mode)
244 (define-minor-mode special-mode "Just a placeholder for something isn't in emacs 22"))
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
251 (provide 'yasnippet-tests)
252 ;;; yasnippet-tests.el ends here