Give '$' punctuation syntax in make-mode (Bug#24477)
[emacs.git] / test / lisp / files-x-tests.el
blob7bd69bda01697662d5a38bf607c109259da682cb
1 ;;; files-x-tests.el --- tests for files-x.el.
3 ;; Copyright (C) 2016-2018 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
22 ;;; Code:
24 (require 'ert)
25 (require 'files-x)
27 (defconst files-x-test--variables1
28 '((remote-shell-file-name . "/bin/bash")
29 (remote-shell-command-switch . "-c")
30 (remote-shell-interactive-switch . "-i")
31 (remote-shell-login-switch . "-l")))
32 (defconst files-x-test--variables2
33 '((remote-shell-file-name . "/bin/ksh")))
34 (defconst files-x-test--variables3
35 '((remote-null-device . "/dev/null")))
36 (defconst files-x-test--variables4
37 '((remote-null-device . "null")))
39 (defconst files-x-test--application '(:application 'my-application))
40 (defconst files-x-test--another-application
41 '(:application 'another-application))
42 (defconst files-x-test--protocol '(:protocol "my-protocol"))
43 (defconst files-x-test--user '(:user "my-user"))
44 (defconst files-x-test--machine '(:machine "my-machine"))
46 (defvar files-x-test--criteria nil)
47 (defconst files-x-test--criteria1
48 (append files-x-test--application files-x-test--protocol
49 files-x-test--user files-x-test--machine))
50 (defconst files-x-test--criteria2
51 (append files-x-test--another-application files-x-test--protocol
52 files-x-test--user files-x-test--machine))
54 (ert-deftest files-x-test-connection-local-set-profile-variables ()
55 "Test setting connection-local profile variables."
57 ;; Declare (PROFILE VARIABLES) objects.
58 (let (connection-local-profile-alist connection-local-criteria-alist)
59 (connection-local-set-profile-variables
60 'remote-bash files-x-test--variables1)
61 (should
62 (equal
63 (connection-local-get-profile-variables 'remote-bash)
64 files-x-test--variables1))
66 (connection-local-set-profile-variables
67 'remote-ksh files-x-test--variables2)
68 (should
69 (equal
70 (connection-local-get-profile-variables 'remote-ksh)
71 files-x-test--variables2))
73 (connection-local-set-profile-variables
74 'remote-nullfile files-x-test--variables3)
75 (should
76 (equal
77 (connection-local-get-profile-variables 'remote-nullfile)
78 files-x-test--variables3))
80 ;; A redefinition overwrites existing values.
81 (connection-local-set-profile-variables
82 'remote-nullfile files-x-test--variables4)
83 (should
84 (equal
85 (connection-local-get-profile-variables 'remote-nullfile)
86 files-x-test--variables4))))
88 (ert-deftest files-x-test-connection-local-set-profiles ()
89 "Test setting connection-local profiles."
91 ;; Declare (CRITERIA PROFILES) objects.
92 (let (connection-local-profile-alist connection-local-criteria-alist)
93 (connection-local-set-profile-variables
94 'remote-bash files-x-test--variables1)
95 (connection-local-set-profile-variables
96 'remote-ksh files-x-test--variables2)
97 (connection-local-set-profile-variables
98 'remote-nullfile files-x-test--variables3)
100 ;; Use a criteria with all properties.
101 (setq files-x-test--criteria
102 (append files-x-test--application files-x-test--protocol
103 files-x-test--user files-x-test--machine))
104 ;; An empty variable list is accepted (but makes no sense).
105 (connection-local-set-profiles files-x-test--criteria)
106 (should-not (connection-local-get-profiles files-x-test--criteria))
107 (connection-local-set-profiles
108 files-x-test--criteria 'remote-bash 'remote-ksh)
109 (should
110 (equal
111 (connection-local-get-profiles files-x-test--criteria)
112 '(remote-bash remote-ksh)))
113 ;; Changing the order of properties doesn't matter.
114 (setq files-x-test--criteria
115 (append files-x-test--protocol files-x-test--application
116 files-x-test--machine files-x-test--user))
117 (should
118 (equal
119 (connection-local-get-profiles files-x-test--criteria)
120 '(remote-bash remote-ksh)))
121 ;; A further call adds profiles.
122 (connection-local-set-profiles files-x-test--criteria 'remote-nullfile)
123 (should
124 (equal
125 (connection-local-get-profiles files-x-test--criteria)
126 '(remote-bash remote-ksh remote-nullfile)))
127 ;; Adding existing profiles doesn't matter.
128 (connection-local-set-profiles
129 files-x-test--criteria 'remote-bash 'remote-nullfile)
130 (should
131 (equal
132 (connection-local-get-profiles files-x-test--criteria)
133 '(remote-bash remote-ksh remote-nullfile)))
135 ;; Use a criteria without application.
136 (setq files-x-test--criteria
137 (append files-x-test--protocol
138 files-x-test--user files-x-test--machine))
139 (connection-local-set-profiles files-x-test--criteria 'remote-ksh)
140 (should
141 (equal
142 (connection-local-get-profiles files-x-test--criteria)
143 '(remote-ksh)))
144 ;; An application not used in any registered criteria matches also this.
145 (setq files-x-test--criteria
146 (append files-x-test--another-application files-x-test--protocol
147 files-x-test--user files-x-test--machine))
148 (should
149 (equal
150 (connection-local-get-profiles files-x-test--criteria)
151 '(remote-ksh)))
153 ;; Using a nil criteria also works. Duplicate profiles are trashed.
154 (connection-local-set-profiles
155 nil 'remote-bash 'remote-ksh 'remote-ksh 'remote-bash)
156 (should
157 (equal
158 (connection-local-get-profiles nil)
159 '(remote-bash remote-ksh)))
161 ;; A criteria other than plist is wrong.
162 (should-error (connection-local-set-profiles 'dummy))))
164 (ert-deftest files-x-test-hack-connection-local-variables-apply ()
165 "Test setting connection-local variables."
167 (let (connection-local-profile-alist connection-local-criteria-alist)
169 (connection-local-set-profile-variables
170 'remote-bash files-x-test--variables1)
171 (connection-local-set-profile-variables
172 'remote-ksh files-x-test--variables2)
173 (connection-local-set-profile-variables
174 'remote-nullfile files-x-test--variables3)
176 (connection-local-set-profiles
177 files-x-test--criteria1 'remote-bash 'remote-ksh)
178 (connection-local-set-profiles
179 files-x-test--criteria2 'remote-ksh 'remote-nullfile)
181 ;; Apply the variables.
182 (with-temp-buffer
183 (let ((enable-connection-local-variables t))
184 (should-not connection-local-variables-alist)
185 (should-not (local-variable-p 'remote-shell-file-name))
186 (should-not (boundp 'remote-shell-file-name))
187 (hack-connection-local-variables-apply files-x-test--criteria1)
188 ;; All connection-local variables are set. They apply in
189 ;; reverse order in `connection-local-variables-alist'. The
190 ;; settings from `remote-ksh' are not contained, because they
191 ;; declare same variables as in `remote-bash'.
192 (should
193 (equal connection-local-variables-alist
194 (nreverse (copy-tree files-x-test--variables1))))
195 ;; The variables exist also as local variables.
196 (should (local-variable-p 'remote-shell-file-name))
197 ;; The proper variable value is set.
198 (should
199 (string-equal (symbol-value 'remote-shell-file-name) "/bin/bash"))))
201 ;; The second test case.
202 (with-temp-buffer
203 (let ((enable-connection-local-variables t))
204 (should-not connection-local-variables-alist)
205 (should-not (local-variable-p 'remote-shell-file-name))
206 (should-not (boundp 'remote-shell-file-name))
207 (hack-connection-local-variables-apply files-x-test--criteria2)
208 ;; All connection-local variables are set. They apply in
209 ;; reverse order in `connection-local-variables-alist'.
210 (should
211 (equal connection-local-variables-alist
212 (append
213 (nreverse (copy-tree files-x-test--variables3))
214 (nreverse (copy-tree files-x-test--variables2)))))
215 ;; The variables exist also as local variables.
216 (should (local-variable-p 'remote-shell-file-name))
217 ;; The proper variable value is set.
218 (should
219 (string-equal (symbol-value 'remote-shell-file-name) "/bin/ksh"))))
221 ;; The third test case. Both criteria `files-x-test--criteria1'
222 ;; and `files-x-test--criteria2' apply, but there are no double
223 ;; entries.
224 (connection-local-set-profiles
225 nil 'remote-bash 'remote-ksh)
226 (with-temp-buffer
227 (let ((enable-connection-local-variables t))
228 (should-not connection-local-variables-alist)
229 (should-not (local-variable-p 'remote-shell-file-name))
230 (should-not (boundp 'remote-shell-file-name))
231 (hack-connection-local-variables-apply nil)
232 ;; All connection-local variables are set. They apply in
233 ;; reverse order in `connection-local-variables-alist'. The
234 ;; settings from `remote-ksh' are not contained, because they
235 ;; declare same variables as in `remote-bash'.
236 (should
237 (equal connection-local-variables-alist
238 (nreverse (copy-tree files-x-test--variables1))))
239 ;; The variables exist also as local variables.
240 (should (local-variable-p 'remote-shell-file-name))
241 ;; The proper variable value is set.
242 (should
243 (string-equal (symbol-value 'remote-shell-file-name) "/bin/bash"))))
245 ;; When `enable-connection-local-variables' is nil, nothing happens.
246 (with-temp-buffer
247 (let ((enable-connection-local-variables nil))
248 (should-not connection-local-variables-alist)
249 (should-not (local-variable-p 'remote-shell-file-name))
250 (should-not (boundp 'remote-shell-file-name))
251 (hack-connection-local-variables-apply nil)
252 (should-not connection-local-variables-alist)
253 (should-not (local-variable-p 'remote-shell-file-name))
254 (should-not (boundp 'remote-shell-file-name))))))
256 (ert-deftest files-x-test-with-connection-local-profiles ()
257 "Test setting connection-local variables."
259 (let (connection-local-profile-alist connection-local-criteria-alist)
260 (connection-local-set-profile-variables
261 'remote-bash files-x-test--variables1)
262 (connection-local-set-profile-variables
263 'remote-ksh files-x-test--variables2)
264 (connection-local-set-profile-variables
265 'remote-nullfile files-x-test--variables3)
267 (connection-local-set-profiles
268 nil 'remote-ksh 'remote-nullfile)
270 (with-temp-buffer
271 (let ((enable-connection-local-variables t))
272 (hack-connection-local-variables-apply nil)
274 ;; All connection-local variables are set. They apply in
275 ;; reverse order in `connection-local-variables-alist'.
276 (should
277 (equal connection-local-variables-alist
278 (append
279 (nreverse (copy-tree files-x-test--variables3))
280 (nreverse (copy-tree files-x-test--variables2)))))
281 ;; The variables exist also as local variables.
282 (should (local-variable-p 'remote-shell-file-name))
283 (should (local-variable-p 'remote-null-device))
284 ;; The proper variable values are set.
285 (should
286 (string-equal (symbol-value 'remote-shell-file-name) "/bin/ksh"))
287 (should
288 (string-equal (symbol-value 'remote-null-device) "/dev/null"))
290 ;; A candidate connection-local variable is not bound yet.
291 (should-not (local-variable-p 'remote-shell-command-switch))
293 ;; Use the macro.
294 (with-connection-local-profiles '(remote-bash remote-ksh)
295 ;; All connection-local variables are set. They apply in
296 ;; reverse order in `connection-local-variables-alist'.
297 ;; This variable keeps only the variables to be set inside
298 ;; the macro.
299 (should
300 (equal connection-local-variables-alist
301 (nreverse (copy-tree files-x-test--variables1))))
302 ;; The variables exist also as local variables.
303 (should (local-variable-p 'remote-shell-file-name))
304 (should (local-variable-p 'remote-shell-command-switch))
305 ;; The proper variable values are set. The settings from
306 ;; `remote-bash' overwrite the same variables as in
307 ;; `remote-ksh'.
308 (should
309 (string-equal (symbol-value 'remote-shell-file-name) "/bin/bash"))
310 (should
311 (string-equal (symbol-value 'remote-shell-command-switch) "-c")))
313 ;; Everything is rewound. The old variable values are reset.
314 (should
315 (equal connection-local-variables-alist
316 (append
317 (nreverse (copy-tree files-x-test--variables3))
318 (nreverse (copy-tree files-x-test--variables2)))))
319 ;; The variables exist also as local variables.
320 (should (local-variable-p 'remote-shell-file-name))
321 (should (local-variable-p 'remote-null-device))
322 ;; The proper variable values are set. The settings from
323 ;; `remote-ksh' are back.
324 (should
325 (string-equal (symbol-value 'remote-shell-file-name) "/bin/ksh"))
326 (should
327 (string-equal (symbol-value 'remote-null-device) "/dev/null"))
329 ;; The variable set temporarily is not unbound, again.
330 (should-not (local-variable-p 'remote-shell-command-switch))))))
332 (provide 'files-x-tests)
333 ;;; files-x-tests.el ends here