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 ;; Attempt to test basic snippet mechanics and the loading system
34 (ert-deftest field-navigation ()
37 (yas/expand-snippet "${1:brother} from another ${2:mother}")
38 (should (string= (buffer-substring-no-properties (point-min) (point-max))
39 "brother from another mother"))
41 (should (looking-at "brother"))
42 (ert-simulate-command '(yas/next-field-or-maybe-expand))
43 (should (looking-at "mother"))
44 (ert-simulate-command '(yas/prev-field))
45 (should (looking-at "brother"))))
47 (ert-deftest simple-mirror ()
50 (yas/expand-snippet "${1:brother} from another $1")
51 (should (string= (buffer-substring-no-properties (point-min) (point-max))
52 "brother from another brother"))
53 (ert-simulate-command `(yas/mock-insert "bla"))
54 (should (string= (buffer-substring-no-properties (point-min) (point-max))
55 "bla from another bla"))))
57 (ert-deftest mirror-with-transformation ()
60 (yas/expand-snippet "${1:brother} from another ${1:$(upcase yas/text)}")
61 (should (string= (buffer-substring-no-properties (point-min) (point-max))
62 "brother from another BROTHER"))
63 (ert-simulate-command `(yas/mock-insert "bla"))
64 (should (string= (buffer-substring-no-properties (point-min) (point-max))
65 "bla from another BLA"))))
67 (ert-deftest nested-placeholders-kill-superfield ()
70 (yas/expand-snippet "brother from ${2:another ${3:mother}}!")
71 (should (string= (buffer-substring-no-properties (point-min) (point-max))
72 "brother from another mother!"))
73 (ert-simulate-command `(yas/mock-insert "bla"))
74 (should (string= (buffer-substring-no-properties (point-min) (point-max))
75 "brother from bla!"))))
77 (ert-deftest nested-placeholders-use-subfield ()
80 (yas/expand-snippet "brother from ${2:another ${3:mother}}!")
81 (ert-simulate-command '(yas/next-field-or-maybe-expand))
82 (ert-simulate-command `(yas/mock-insert "bla"))
83 (should (string= (buffer-substring-no-properties (point-min) (point-max))
84 "brother from another bla!"))))
86 ;; (ert-deftest in-snippet-undo ()
89 ;; (yas/expand-snippet "brother from ${2:another ${3:mother}}!")
90 ;; (ert-simulate-command '(yas/next-field-or-maybe-expand))
91 ;; (ert-simulate-command `(yas/mock-insert "bla"))
92 ;; (ert-simulate-command '(undo))
93 ;; (should (string= (buffer-substring-no-properties (point-min) (point-max))
94 ;; "brother from another mother!"))))
100 (ert-deftest protection-overlay-no-cheating ()
101 "Protection overlays at the very end of the buffer, are dealt by cheatingly inserting a newline!
103 TODO: correct this bug!"
104 :expected-result :failed
107 (yas/expand-snippet "${2:brother} from another ${1:mother}")
108 (should (string= (buffer-substring-no-properties (point-min) (point-max))
109 "brother from another mother") ;; no newline should be here!
114 (ert-deftest basic-loading ()
115 "Test basic loading and expansion of snippets"
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 (lisp-interaction-mode)
134 (ert-simulate-command '(yas/expand))
135 (should (string= (buffer-substring-no-properties (point-min) (point-max))
136 "brother from another mother"))))))
143 (defun yas/mock-insert (string)
146 ((= i (length string)))
147 (insert (aref string i))))
149 (defun yas/make-file-or-dirs (ass)
150 (let ((file-or-dir-name (car ass))
152 (cond ((listp content)
153 (make-directory file-or-dir-name 'parents)
154 (let ((default-directory (concat default-directory "/" file-or-dir-name)))
155 (mapc #'yas/make-file-or-dirs content)))
157 (with-current-buffer (find-file file-or-dir-name)
162 (message "[yas] oops don't know this content")))))
165 (defun yas/variables ()
167 (mapatoms #'(lambda (sym)
168 (if (and (string-match "^yas/[^/]" (symbol-name sym))
174 (defmacro yas/saving-variables (&rest body)
175 `(let ,(mapcar #'(lambda (sym)
180 (defmacro with-snippet-dirs (dirs &rest body)
181 `(let ((default-directory (make-temp-file "yasnippet-fixture" t)))
182 (setq yas/snippet-dirs ',(mapcar #'car (cadr dirs)))
183 (mapc #'yas/make-file-or-dirs ,dirs)
187 (provide 'yasnippet-tests)
188 ;;; yasnippet-tests.el ends here