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
117 (yas-with-snippet-dirs
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 (with-some-interesting-snippet-dirs
135 (yas--basic-jit-loading-1)))
137 (ert-deftest basic-jit-loading-with-compiled-snippets ()
138 "Test basic loading and expansion of snippets"
139 (with-some-interesting-snippet-dirs
142 (flet ((yas--load-directory-2
144 (ert-fail "yas--load-directory-2 shouldn't be called when snippets have been compiled")))
146 (yas--basic-jit-loading-1))))
148 (defun yas--basic-jit-loading-1 (&optional compile)
150 (should (= 4 (hash-table-count yas--scheduled-jit-loads)))
151 (should (= 0 (hash-table-count yas--tables)))
152 (lisp-interaction-mode)
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 () )")))
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"))))
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)))
210 (write-region nil nil file-or-dir-name nil 'nomessage)))
212 (message "[yas] oops don't know this content")))))
215 (defun yas-variables ()
217 (mapatoms #'(lambda (sym)
218 (if (and (string-match "^yas-[^/]" (symbol-name sym))
224 (defmacro yas-saving-variables (&rest body)
225 `(let ,(mapcar #'(lambda (sym)
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 ""
236 (mapc #'yas-make-file-or-dirs dirs)
238 (when (>= emacs-major-version 23)
239 (delete-directory default-directory 'recursive))))))
241 (defmacro yas-with-snippet-dirs (dirs &rest body)
242 `(yas-call-with-snippet-dirs ,dirs
248 (unless (fboundp 'special-mode)
249 (define-minor-mode special-mode "Just a placeholder for something isn't in emacs 22"))
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
256 (provide 'yasnippet-tests)
257 ;;; yasnippet-tests.el ends here