lisp/simple.el (define-alternatives): Rename from alternatives-define.
[emacs.git] / test / automated / files.el
blob8ce2ed7230c30a4cce5043434ff695584e31fd23
1 ;;; files.el --- tests for file handling.
3 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20 ;;; Code:
22 (require 'ert)
24 ;; Set to t if the local variable was set, `query' if the query was
25 ;; triggered.
26 (defvar files-test-result nil)
28 (defvar files-test-safe-result nil)
29 (put 'files-test-safe-result 'safe-local-variable 'booleanp)
31 (defun files-test-fun1 ()
32 (setq files-test-result t))
34 ;; Test combinations:
35 ;; `enable-local-variables' t, nil, :safe, :all, or something else.
36 ;; `enable-local-eval' t, nil, or something else.
38 (defvar files-test-local-variable-data
39 ;; Unsafe eval form
40 '((("eval: (files-test-fun1)")
41 (t t (eq files-test-result t))
42 (t nil (eq files-test-result nil))
43 (t maybe (eq files-test-result 'query))
44 (nil t (eq files-test-result nil))
45 (nil nil (eq files-test-result nil))
46 (nil maybe (eq files-test-result nil))
47 (:safe t (eq files-test-result nil))
48 (:safe nil (eq files-test-result nil))
49 (:safe maybe (eq files-test-result nil))
50 (:all t (eq files-test-result t))
51 (:all nil (eq files-test-result nil))
52 (:all maybe (eq files-test-result t)) ; This combination is ambiguous.
53 (maybe t (eq files-test-result 'query))
54 (maybe nil (eq files-test-result 'query))
55 (maybe maybe (eq files-test-result 'query)))
56 ;; Unsafe local variable value
57 (("files-test-result: t")
58 (t t (eq files-test-result 'query))
59 (t nil (eq files-test-result 'query))
60 (t maybe (eq files-test-result 'query))
61 (nil t (eq files-test-result nil))
62 (nil nil (eq files-test-result nil))
63 (nil maybe (eq files-test-result nil))
64 (:safe t (eq files-test-result nil))
65 (:safe nil (eq files-test-result nil))
66 (:safe maybe (eq files-test-result nil))
67 (:all t (eq files-test-result t))
68 (:all nil (eq files-test-result t))
69 (:all maybe (eq files-test-result t))
70 (maybe t (eq files-test-result 'query))
71 (maybe nil (eq files-test-result 'query))
72 (maybe maybe (eq files-test-result 'query)))
73 ;; Safe local variable
74 (("files-test-safe-result: t")
75 (t t (eq files-test-safe-result t))
76 (t nil (eq files-test-safe-result t))
77 (t maybe (eq files-test-safe-result t))
78 (nil t (eq files-test-safe-result nil))
79 (nil nil (eq files-test-safe-result nil))
80 (nil maybe (eq files-test-safe-result nil))
81 (:safe t (eq files-test-safe-result t))
82 (:safe nil (eq files-test-safe-result t))
83 (:safe maybe (eq files-test-safe-result t))
84 (:all t (eq files-test-safe-result t))
85 (:all nil (eq files-test-safe-result t))
86 (:all maybe (eq files-test-safe-result t))
87 (maybe t (eq files-test-result 'query))
88 (maybe nil (eq files-test-result 'query))
89 (maybe maybe (eq files-test-result 'query)))
90 ;; Safe local variable with unsafe value
91 (("files-test-safe-result: 1")
92 (t t (eq files-test-result 'query))
93 (t nil (eq files-test-result 'query))
94 (t maybe (eq files-test-result 'query))
95 (nil t (eq files-test-safe-result nil))
96 (nil nil (eq files-test-safe-result nil))
97 (nil maybe (eq files-test-safe-result nil))
98 (:safe t (eq files-test-safe-result nil))
99 (:safe nil (eq files-test-safe-result nil))
100 (:safe maybe (eq files-test-safe-result nil))
101 (:all t (eq files-test-safe-result 1))
102 (:all nil (eq files-test-safe-result 1))
103 (:all maybe (eq files-test-safe-result 1))
104 (maybe t (eq files-test-result 'query))
105 (maybe nil (eq files-test-result 'query))
106 (maybe maybe (eq files-test-result 'query))))
107 "List of file-local variable tests.
108 Each list element should have the form
110 (LOCAL-VARS-LIST . TEST-LIST)
112 where LOCAL-VARS-LISTS should be a list of local variable
113 definitions (strings) and TEST-LIST is a list of tests to
114 perform. Each entry of TEST-LIST should have the form
116 (ENABLE-LOCAL-VARIABLES ENABLE-LOCAL-EVAL FORM)
118 where ENABLE-LOCAL-VARIABLES is the value to assign to
119 `enable-local-variables', ENABLE-LOCAL-EVAL is the value to
120 assign to `enable-local-eval', and FORM is a desired `should'
121 form.")
123 (defun file-test--do-local-variables-test (str test-settings)
124 (with-temp-buffer
125 (insert str)
126 (setq files-test-result nil
127 files-test-safe-result nil)
128 (let ((enable-local-variables (nth 0 test-settings))
129 (enable-local-eval (nth 1 test-settings))
130 (files-test-queried nil))
131 (hack-local-variables)
132 (eval (nth 2 test-settings)))))
134 (ert-deftest files-test-local-variables ()
135 "Test the file-local variables implementation."
136 (unwind-protect
137 (progn
138 (defadvice hack-local-variables-confirm (around files-test activate)
139 (setq files-test-result 'query)
140 nil)
141 (dolist (test files-test-local-variable-data)
142 (let ((str (concat "text\n\n;; Local Variables:\n;; "
143 (mapconcat 'identity (car test) "\n;; ")
144 "\n;; End:\n")))
145 (dolist (subtest (cdr test))
146 (should (file-test--do-local-variables-test str subtest))))))
147 (ad-disable-advice 'hack-local-variables-confirm 'around 'files-test)))
149 ;; Stop the above "Local Var..." confusing Emacs.
152 ;;; files.el ends here