forked from joaotavora/yasnippet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yasnippet-tests.el
executable file
·188 lines (153 loc) · 6.61 KB
/
yasnippet-tests.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
;;; yasnippet-tests.el --- some yasnippet tests
;; Copyright (C) 2012 João Távora
;; Author: João Távora <[email protected]>
;; Keywords: emulations, convenience
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Attempt to test basic snippet mechanics and the loading system
;;; Code:
(require 'yasnippet)
(require 'ert)
(require 'ert-x)
;;; Snippet mechanics
(ert-deftest field-navigation ()
(with-temp-buffer
(yas/minor-mode 1)
(yas/expand-snippet "${1:brother} from another ${2:mother}")
(should (string= (buffer-substring-no-properties (point-min) (point-max))
"brother from another mother"))
(should (looking-at "brother"))
(ert-simulate-command '(yas/next-field-or-maybe-expand))
(should (looking-at "mother"))
(ert-simulate-command '(yas/prev-field))
(should (looking-at "brother"))))
(ert-deftest simple-mirror ()
(with-temp-buffer
(yas/minor-mode 1)
(yas/expand-snippet "${1:brother} from another $1")
(should (string= (buffer-substring-no-properties (point-min) (point-max))
"brother from another brother"))
(ert-simulate-command `(yas/mock-insert "bla"))
(should (string= (buffer-substring-no-properties (point-min) (point-max))
"bla from another bla"))))
(ert-deftest mirror-with-transformation ()
(with-temp-buffer
(yas/minor-mode 1)
(yas/expand-snippet "${1:brother} from another ${1:$(upcase yas/text)}")
(should (string= (buffer-substring-no-properties (point-min) (point-max))
"brother from another BROTHER"))
(ert-simulate-command `(yas/mock-insert "bla"))
(should (string= (buffer-substring-no-properties (point-min) (point-max))
"bla from another BLA"))))
(ert-deftest nested-placeholders-kill-superfield ()
(with-temp-buffer
(yas/minor-mode 1)
(yas/expand-snippet "brother from ${2:another ${3:mother}}!")
(should (string= (buffer-substring-no-properties (point-min) (point-max))
"brother from another mother!"))
(ert-simulate-command `(yas/mock-insert "bla"))
(should (string= (buffer-substring-no-properties (point-min) (point-max))
"brother from bla!"))))
(ert-deftest nested-placeholders-use-subfield ()
(with-temp-buffer
(yas/minor-mode 1)
(yas/expand-snippet "brother from ${2:another ${3:mother}}!")
(ert-simulate-command '(yas/next-field-or-maybe-expand))
(ert-simulate-command `(yas/mock-insert "bla"))
(should (string= (buffer-substring-no-properties (point-min) (point-max))
"brother from another bla!"))))
;; (ert-deftest in-snippet-undo ()
;; (with-temp-buffer
;; (yas/minor-mode 1)
;; (yas/expand-snippet "brother from ${2:another ${3:mother}}!")
;; (ert-simulate-command '(yas/next-field-or-maybe-expand))
;; (ert-simulate-command `(yas/mock-insert "bla"))
;; (ert-simulate-command '(undo))
;; (should (string= (buffer-substring-no-properties (point-min) (point-max))
;; "brother from another mother!"))))
;;; Misc tests
;;;
(ert-deftest protection-overlay-no-cheating ()
"Protection overlays at the very end of the buffer, are dealt by cheatingly inserting a newline!
TODO: correct this bug!"
:expected-result :failed
(with-temp-buffer
(yas/minor-mode 1)
(yas/expand-snippet "${2:brother} from another ${1:mother}")
(should (string= (buffer-substring-no-properties (point-min) (point-max))
"brother from another mother") ;; no newline should be here!
)))
;;; Loading
;;;
(ert-deftest basic-loading ()
"Test basic loading and expansion of snippets"
(yas/saving-variables
(with-snippet-dirs
'((".emacs.d/snippets"
("c-mode"
(".yas-parents" . "cc-mode")
("printf" . "printf($1);"))
("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))
("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))
("library/snippets"
("c-mode" (".yas-parents" . "c++-mode"))
("cc-mode" ("def" . "# define"))
("emacs-lisp-mode" ("dolist" . "(dolist)"))
("lisp-interaction-mode" ("sc" . "brother from another mother"))))
(yas/reload-all)
(with-temp-buffer
(lisp-interaction-mode)
(yas/minor-mode 1)
(insert "sc")
(ert-simulate-command '(yas/expand))
(should (string= (buffer-substring-no-properties (point-min) (point-max))
"brother from another mother"))))))
;;; Helpers
;;;
(defun yas/mock-insert (string)
(interactive)
(do ((i 0 (1+ i)))
((= i (length string)))
(insert (aref string i))))
(defun yas/make-file-or-dirs (ass)
(let ((file-or-dir-name (car ass))
(content (cdr ass)))
(cond ((listp content)
(make-directory file-or-dir-name 'parents)
(let ((default-directory (concat default-directory "/" file-or-dir-name)))
(mapc #'yas/make-file-or-dirs content)))
((stringp content)
(with-current-buffer (find-file file-or-dir-name)
(insert content)
(save-buffer)
(kill-buffer)))
(t
(message "[yas] oops don't know this content")))))
(defun yas/variables ()
(let ((syms))
(mapatoms #'(lambda (sym)
(if (and (string-match "^yas/[^/]" (symbol-name sym))
(boundp sym))
(push sym syms))))
syms))
(defmacro yas/saving-variables (&rest body)
`(let ,(mapcar #'(lambda (sym)
`(,sym ,sym))
(yas/variables))
,@body))
(defmacro with-snippet-dirs (dirs &rest body)
`(let ((default-directory (make-temp-file "yasnippet-fixture" t)))
(setq yas/snippet-dirs ',(mapcar #'car (cadr dirs)))
(mapc #'yas/make-file-or-dirs ,dirs)
,@body))
(provide 'yasnippet-tests)
;;; yasnippet-tests.el ends here