-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathyatemplate-tests.el
159 lines (143 loc) · 5.76 KB
/
yatemplate-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
;;; test-yatemplate.el --- Yatemplate tests -*- lexical-binding: t; -*-
;; Copyright (C) 2016
;; Author: Wieland Hoffmann <[email protected]>
;; Keywords:
;; 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:
;;
;;; Code:
(require 'buttercup)
(require 'shut-up)
(require 'yatemplate)
(defmacro yatemplate-with-file-in-dir (dir file-name &rest body)
"Call BODY after opening a buffer visiting FILE-NAME in DIR."
`(unwind-protect
(progn
(find-file (concat ,dir "/" ,file-name))
,(macroexp-progn body))
(kill-buffer (current-buffer))))
(defun yatemplate-entries ()
"Return all entries generated by yatempled in `auto-insert-alist'."
(mapcar
'car
(cl-remove-if-not
(lambda (pair)
(ignore-errors (eq 'yatemplate-expand-yas-buffer (aref (cdr pair) 1))))
auto-insert-alist)))
(defun yatemplates ()
"Regenerate `auto-insert-alist' and return all our entries."
(shut-up
(yatemplate-reload-all))
(yatemplate-entries))
(defun touch (filename)
"Touch FILENAME without any messages."
(shut-up
(write-region "a" nil filename)))
(describe
"Yatemplate"
(before-all
(setq
;; The root directory for tests
yatemplate-toplevel-test-dir (file-name-as-directory (concat temporary-file-directory "yatemplate-test"))
;; The directory in which to keep templates during tests. This doesn't
;; contain a trailing slash on purpose to ensure it's really treated as a
;; directory name throughout the code.
yatemplate-dir (concat yatemplate-toplevel-test-dir "templates")))
(before-each
(mkdir yatemplate-toplevel-test-dir t)
(mkdir yatemplate-dir t)
(setq default-directory yatemplate-dir))
(it "doesn't add any entries unless asked"
(expect (yatemplates) :not :to-be-truthy))
(it "recognizes a single file"
(touch "01:test.el")
(expect (yatemplates) :to-equal '("test.el$")))
(it "doesn't create duplicates"
(touch "01:test.el")
(expect (yatemplates) :to-equal '("test.el$"))
(expect (yatemplates) :to-equal '("test.el$")))
(it "recognizes multiple files"
(touch "01:test.el")
(touch "02:.*.py")
(expect (yatemplates) :to-equal '("test.el$" ".*.py$")))
(it "doesn't keep renamed files"
(touch "01:test.el")
(touch "02:.*.py")
(expect (yatemplates) :to-equal '("test.el$" ".*.py$"))
(rename-file "01:test.el" "10:testbla.el")
(expect (yatemplates) :to-equal '(".*.py$" "testbla.el$")))
(it "doesn't keep deleted files"
(touch "01:test.el")
(touch "02:.*.py")
(expect (yatemplates) :to-equal '("test.el$" ".*.py$"))
(delete-file "01:test.el")
(expect (yatemplates) :to-equal '(".*.py$")))
(it "ignores files without yatemplate-separator in their name"
(touch "01:test.el")
(touch "02-.*.py")
(expect (yatemplates) :to-equal '("test.el$")))
(it "only loads files in yatemplate-dir"
(touch "01:test.el")
(let ((not-template-directory (file-name-as-directory (concat yatemplate-dir "notreally"))))
(mkdir not-template-directory t)
(touch (concat not-template-directory "02:.*.py"))
(expect (yatemplates) :to-equal '("test.el$"))))
(it "does not warn about files matched by yatemplate-ignored-files-regexp"
(touch "README.md")
(touch "foo.txt")
(touch "01:test.el")
(shut-up
(let ((yatemplate-ignored-files-regexp "foo.txt$"))
(yatemplate-fill-alist))
(expect (shut-up-current-output) :not :to-match "foo.txt does not contain . exactly once$")
(expect (shut-up-current-output) :to-match "README.md does not contain . exactly once$"))
(expect (yatemplate-entries) :to-equal '("test.el$")))
(describe
"when the separator is changed"
(before-all
(setq yatemplate-separator ":"))
(it "removes no longer matching files"
(touch "01:test.el")
(touch "02:.*.py")
(expect (yatemplates) :to-equal '("test.el$" ".*.py$"))
(setq yatemplate-separator "=")
(expect (yatemplates) :not :to-be-truthy))
(it "replaces no longer matching files with now matching ones"
(touch "01:test.el")
(touch "02:.*.py")
(touch "01=bla.el")
(expect (yatemplates) :to-equal '("test.el$" ".*.py$" ))
(setq yatemplate-separator "=")
(expect (yatemplates) :to-equal '("bla.el$")))
(after-each
(setq yatemplate-separator ":")))
(it "activates snippet-mode in files in yatemplate-dir"
(yatemplate-with-file-in-dir
yatemplate-dir
"foo.el"
(expect major-mode :to-be 'snippet-mode)
(expect yas--guessed-modes :to-contain 'emacs-lisp-mode)))
(describe
"when yatemplate-unload-function is called"
(it "unloads all yatemplate hooks"
(expect find-file-hook :to-contain 'yatemplate--find-file-hook)
(expect after-save-hook :to-contain 'yatemplate--after-save-hook)
(yatemplate-unload-function)
(expect find-file-hook :not :to-contain 'yatemplate--find-file-hook)
(expect after-save-hook :not :to-contain 'yatemplate--after-save-hook)))
(after-each
(delete-directory yatemplate-toplevel-test-dir t)))
(describe "yatemplate-unload-function"
(it "should return nil"
(expect (yatemplate-unload-function) :to-equal nil)))
(provide 'test-yatemplate)
;;; test-yatemplate.el ends here