aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhil Sainty2019-06-14 16:51:26 +1200
committerPhil Sainty2019-07-13 18:51:55 +1200
commitc1b43688091c22dfb6008d452dad7dc74b71273d (patch)
tree85ca496f94626b0ee125d3f963280cc6a3f9d55b
parent4ac905f88f10439ca0795b217a046e3a62895fc4 (diff)
downloademacs-c1b43688091c22dfb6008d452dad7dc74b71273d.tar.gz
emacs-c1b43688091c22dfb6008d452dad7dc74b71273d.zip
Add tests for so-long.el
-rw-r--r--test/lisp/so-long-tests/autoload-longlines-mode-tests.el51
-rw-r--r--test/lisp/so-long-tests/autoload-major-mode-tests.el46
-rw-r--r--test/lisp/so-long-tests/autoload-minor-mode-tests.el50
-rw-r--r--test/lisp/so-long-tests/so-long-tests-helpers.el113
-rw-r--r--test/lisp/so-long-tests/so-long-tests.el448
5 files changed, 708 insertions, 0 deletions
diff --git a/test/lisp/so-long-tests/autoload-longlines-mode-tests.el b/test/lisp/so-long-tests/autoload-longlines-mode-tests.el
new file mode 100644
index 00000000000..5a57e049fb5
--- /dev/null
+++ b/test/lisp/so-long-tests/autoload-longlines-mode-tests.el
@@ -0,0 +1,51 @@
1;;; autoload-longlines-mode-tests.el --- Test suite for so-long.el -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2019 Free Software Foundation, Inc.
4
5;; Author: Phil Sainty <psainty@orcon.net.nz>
6;; Keywords: convenience
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
22
23;;; Code:
24
25(require 'ert)
26(load (expand-file-name "so-long-tests-helpers"
27 (file-name-directory (or load-file-name
28 default-directory))))
29
30(declare-function so-long-tests-remember "so-long-tests-helpers")
31(declare-function so-long-tests-assert-and-revert "so-long-tests-helpers")
32(defvar so-long-action)
33
34;; We're testing the initial state. We could alternatively use
35;; `unload-feature' to revert to that, but this option is simple.
36
37(autoload 'so-long "so-long")
38(autoload 'longlines-mode "longlines")
39
40(ert-deftest so-long-tests-autoload-longlines-mode ()
41 "File-local -*- so-long-action: longlines-mode; eval: (so-long) -*-"
42 (with-temp-buffer
43 (so-long-tests-remember)
44 (insert "-*- so-long-action: longlines-mode; eval: (so-long) -*-\n")
45 (put 'so-long-action 'safe-local-variable #'symbolp)
46 (push '(eval . (so-long)) safe-local-variable-values)
47 (hack-local-variables)
48 (should (eq so-long-action 'longlines-mode))
49 (so-long-tests-assert-and-revert 'longlines-mode)))
50
51;;; autoload-longlines-mode-tests.el ends here
diff --git a/test/lisp/so-long-tests/autoload-major-mode-tests.el b/test/lisp/so-long-tests/autoload-major-mode-tests.el
new file mode 100644
index 00000000000..d82cb59750c
--- /dev/null
+++ b/test/lisp/so-long-tests/autoload-major-mode-tests.el
@@ -0,0 +1,46 @@
1;;; autoload-major-mode-tests.el --- Test suite for so-long.el -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2019 Free Software Foundation, Inc.
4
5;; Author: Phil Sainty <psainty@orcon.net.nz>
6;; Keywords: convenience
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
22
23;;; Code:
24
25(require 'ert)
26(load (expand-file-name "so-long-tests-helpers"
27 (file-name-directory (or load-file-name
28 default-directory))))
29
30(declare-function so-long-tests-remember "so-long-tests-helpers")
31(declare-function so-long-tests-assert-and-revert "so-long-tests-helpers")
32
33;; We're testing the initial state. We could alternatively use
34;; `unload-feature' to revert to that, but this option is simple.
35
36(autoload 'so-long-mode "so-long")
37
38(ert-deftest so-long-tests-autoload-major-mode ()
39 "File-local -*- so-long -*-"
40 (with-temp-buffer
41 (so-long-tests-remember)
42 (insert "-*- so-long -*-\n")
43 (normal-mode)
44 (so-long-tests-assert-and-revert 'so-long-mode)))
45
46;;; autoload-major-mode-tests.el ends here
diff --git a/test/lisp/so-long-tests/autoload-minor-mode-tests.el b/test/lisp/so-long-tests/autoload-minor-mode-tests.el
new file mode 100644
index 00000000000..67f1903c09c
--- /dev/null
+++ b/test/lisp/so-long-tests/autoload-minor-mode-tests.el
@@ -0,0 +1,50 @@
1;;; autoload-minor-mode-tests.el --- Test suite for so-long.el -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2019 Free Software Foundation, Inc.
4
5;; Author: Phil Sainty <psainty@orcon.net.nz>
6;; Keywords: convenience
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
22
23;;; Code:
24
25(require 'ert)
26(load (expand-file-name "so-long-tests-helpers"
27 (file-name-directory (or load-file-name
28 default-directory))))
29
30(declare-function so-long-tests-remember "so-long-tests-helpers")
31(declare-function so-long-tests-assert-and-revert "so-long-tests-helpers")
32(defvar so-long-action)
33
34;; We're testing the initial state. We could alternatively use
35;; `unload-feature' to revert to that, but this option is simple.
36
37(autoload 'so-long "so-long")
38
39(ert-deftest so-long-tests-autoload-minor-mode ()
40 "File-local -*- so-long-action: so-long-minor-mode; eval: (so-long) -*-"
41 (with-temp-buffer
42 (so-long-tests-remember)
43 (insert "-*- so-long-action: so-long-minor-mode; eval: (so-long) -*-\n")
44 (put 'so-long-action 'safe-local-variable #'symbolp)
45 (push '(eval . (so-long)) safe-local-variable-values)
46 (hack-local-variables)
47 (should (eq so-long-action 'so-long-minor-mode))
48 (so-long-tests-assert-and-revert 'so-long-minor-mode)))
49
50;;; autoload-minor-mode-tests.el ends here
diff --git a/test/lisp/so-long-tests/so-long-tests-helpers.el b/test/lisp/so-long-tests/so-long-tests-helpers.el
new file mode 100644
index 00000000000..1a8ba451610
--- /dev/null
+++ b/test/lisp/so-long-tests/so-long-tests-helpers.el
@@ -0,0 +1,113 @@
1;;; so-long-tests-helpers.el --- Test suite for so-long.el -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2019 Free Software Foundation, Inc.
4
5;; Author: Phil Sainty <psainty@orcon.net.nz>
6;; Keywords: convenience
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
22
23;;; Code:
24
25(require 'ert)
26(require 'so-long)
27
28(defvar longlines-mode)
29(declare-function longlines-mode "longlines")
30
31(defvar so-long-tests-memory nil
32 "Original values of minor modes and variables.")
33
34(defun so-long-tests-assert-active (action)
35 "Assert that ACTION is active."
36 (cl-destructuring-bind (_key _label actionfunc revertfunc)
37 (assq action so-long-action-alist)
38 (should (eq so-long-function actionfunc))
39 (should (eq so-long-revert-function revertfunc))
40 (should (eq so-long-enabled t))
41 (should (eq so-long--active t))
42 ;; pcase fails here in Emacs 24.
43 (cl-case action
44 ('so-long-mode
45 (should (eq major-mode 'so-long-mode))
46 (so-long-tests-assert-overrides))
47 ('so-long-minor-mode
48 (should (eq so-long-minor-mode t))
49 (so-long-tests-assert-overrides))
50 ('longlines-mode
51 (should (eq longlines-mode t))))))
52
53(defun so-long-tests-assert-reverted (action)
54 "Assert that ACTION has been reverted."
55 (cl-destructuring-bind (_key _label actionfunc revertfunc)
56 (assq action so-long-action-alist)
57 (should (eq so-long-function actionfunc))
58 (should (eq so-long-revert-function revertfunc))
59 (should (eq so-long-enabled t))
60 (should (eq so-long--active nil))
61 ;; pcase fails here in Emacs 24.
62 (cl-case action
63 ('so-long-mode
64 (should-not (eq major-mode 'so-long-mode))
65 (so-long-tests-assert-overrides-reverted))
66 ('so-long-minor-mode
67 (should-not (eq so-long-minor-mode t))
68 (so-long-tests-assert-overrides-reverted))
69 ('longlines-mode
70 (should-not (eq longlines-mode t))))))
71
72(defun so-long-tests-assert-and-revert (action)
73 "Assert ACTION, revert it, and then assert the revert."
74 (so-long-tests-assert-active action)
75 (so-long-revert)
76 (so-long-tests-assert-reverted action))
77
78(defun so-long-tests-assert-overrides ()
79 "Assert that overridden modes and variables have their expected values."
80 (dolist (ovar so-long-variable-overrides)
81 (when (boundp (car ovar))
82 (should (equal (symbol-value (car ovar)) (cdr ovar)))))
83 (dolist (mode so-long-minor-modes)
84 (when (boundp mode)
85 (should (eq (symbol-value mode) nil)))))
86
87(defun so-long-tests-assert-overrides-reverted ()
88 "Assert that each remembered variable has its original value."
89 (dolist (ovar so-long-tests-memory)
90 (when (boundp (car ovar))
91 (should (equal (symbol-value (car ovar)) (cdr ovar))))))
92
93(defun so-long-tests-remember ()
94 "Remember the original states of modes and variables.
95
96Call this after setting up a buffer in the normal (not so-long)
97state for its major mode, so that after triggering a so-long
98action we can call `so-long-revert' and compare the reverted
99state against this remembered state."
100 (setq so-long-tests-memory nil)
101 (push (cons 'major-mode major-mode)
102 so-long-tests-memory)
103 (dolist (ovar so-long-variable-overrides)
104 (when (boundp (car ovar))
105 (push (cons (car ovar) (symbol-value (car ovar)))
106 so-long-tests-memory)))
107 (dolist (mode so-long-minor-modes)
108 (when (boundp mode)
109 (push (cons mode (symbol-value mode))
110 so-long-tests-memory))))
111
112(provide 'so-long-tests-helpers)
113;;; so-long-tests-helpers.el ends here
diff --git a/test/lisp/so-long-tests/so-long-tests.el b/test/lisp/so-long-tests/so-long-tests.el
new file mode 100644
index 00000000000..b1e0cb90d00
--- /dev/null
+++ b/test/lisp/so-long-tests/so-long-tests.el
@@ -0,0 +1,448 @@
1;;; so-long-tests.el --- Test suite for so-long.el -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2019 Free Software Foundation, Inc.
4
5;; Author: Phil Sainty <psainty@orcon.net.nz>
6;; Keywords: convenience
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
22
23;;; Commentary:
24
25;; Most of these tests use the shebang #!emacs to get `normal-mode' to
26;; select `emacs-lisp-mode', as using a file-local mode variable would
27;; usually trigger `so-long-file-local-mode-function'. In cases where
28;; we need to `hack-local-variables', we instead set `buffer-file-name'.
29;; (We could consistently use the latter, but the mixture of approaches
30;; means that we're testing more things.)
31
32;; Running the tests with "make lisp/so-long-tests" is like:
33;;
34;; HOME=/nonexistent EMACSLOADPATH= LC_ALL=C \
35;; EMACS_TEST_DIRECTORY=/home/phil/emacs/trunk/repository/test \
36;; "../src/emacs" --no-init-file --no-site-file --no-site-lisp \
37;; -L ":." -l ert -l lisp/so-long-tests.el --batch --eval \
38;; '(ert-run-tests-batch-and-exit (quote (not (tag :unstable))))'
39;;
40;; See also `ert-run-tests-batch-and-exit'.
41
42;;; Code:
43
44(require 'ert)
45(require 'so-long)
46(load (expand-file-name "so-long-tests-helpers"
47 (file-name-directory (or load-file-name
48 default-directory))))
49
50(declare-function so-long-tests-remember "so-long-tests-helpers")
51(declare-function so-long-tests-assert-active "so-long-tests-helpers")
52(declare-function so-long-tests-assert-reverted "so-long-tests-helpers")
53(declare-function so-long-tests-assert-and-revert "so-long-tests-helpers")
54
55;; Enable the automated behaviour for all tests.
56(global-so-long-mode 1)
57
58(ert-deftest so-long-tests-threshold-under ()
59 "Under line length threshold."
60 (with-temp-buffer
61 (insert "#!emacs\n")
62 (insert (make-string (1- so-long-threshold) ?x))
63 (normal-mode)
64 (should (eq major-mode 'emacs-lisp-mode))))
65
66(ert-deftest so-long-tests-threshold-at ()
67 "At line length threshold."
68 (with-temp-buffer
69 (insert "#!emacs\n")
70 (insert (make-string (1- so-long-threshold) ?x))
71 (normal-mode)
72 (should (eq major-mode 'emacs-lisp-mode))))
73
74(ert-deftest so-long-tests-threshold-over ()
75 "Over line length threshold."
76 (with-temp-buffer
77 (insert "#!emacs\n")
78 (normal-mode)
79 (so-long-tests-remember)
80 (insert (make-string (1+ so-long-threshold) ?x))
81 (normal-mode)
82 (so-long-tests-assert-and-revert 'so-long-mode)))
83
84(ert-deftest so-long-tests-skip-comments ()
85 "Skip leading shebang, whitespace, and comments."
86 ;; Long comment, no newline.
87 (with-temp-buffer
88 (insert "#!emacs\n")
89 (insert (make-string (1+ so-long-threshold) ?\;))
90 (normal-mode)
91 (should (eq major-mode 'emacs-lisp-mode)))
92 ;; Long comment, with newline.
93 (with-temp-buffer
94 (insert "#!emacs\n")
95 (insert (make-string (1+ so-long-threshold) ?\;))
96 (insert "\n")
97 (normal-mode)
98 (should (eq major-mode 'emacs-lisp-mode)))
99 ;; Long comment, with short text following.
100 (with-temp-buffer
101 (insert "#!emacs\n")
102 (insert (make-string (1+ so-long-threshold) ?\;))
103 (insert "\n")
104 (insert (make-string so-long-threshold ?x))
105 (normal-mode)
106 (should (eq major-mode 'emacs-lisp-mode)))
107 ;; Long comment, with long text following.
108 (with-temp-buffer
109 (insert "#!emacs\n")
110 (insert (make-string (1+ so-long-threshold) ?\;))
111 (insert "\n")
112 (insert (make-string (1+ so-long-threshold) ?x))
113 (normal-mode)
114 (should (eq major-mode 'so-long-mode))))
115
116(ert-deftest so-long-tests-max-lines ()
117 "Give up after `so-long-max-lines'."
118 (with-temp-buffer
119 (insert "#!emacs\n")
120 ;; Insert exactly `so-long-max-lines' non-comment lines, followed
121 ;; by a long line.
122 (dotimes (_ so-long-max-lines)
123 (insert "x\n"))
124 (insert (make-string (1+ so-long-threshold) ?x))
125 (normal-mode)
126 (should (eq major-mode 'emacs-lisp-mode))
127 ;; If `so-long-max-lines' is nil, don't give up the search.
128 (let ((so-long-max-lines nil))
129 (normal-mode)
130 (should (eq major-mode 'so-long-mode)))
131 ;; If `so-long-skip-leading-comments' is nil, all lines are
132 ;; counted, and so the shebang line counts, which makes the
133 ;; long line one line further away.
134 (let ((so-long-skip-leading-comments nil)
135 (so-long-max-lines (1+ so-long-max-lines)))
136 (normal-mode)
137 (should (eq major-mode 'emacs-lisp-mode))
138 (let ((so-long-max-lines (1+ so-long-max-lines)))
139 (normal-mode)
140 (should (eq major-mode 'so-long-mode))))))
141
142(ert-deftest so-long-tests-actions ()
143 "Test each of the standard actions."
144 (dolist (action (mapcar #'car so-long-action-alist))
145 (with-temp-buffer
146 (insert "#!emacs\n")
147 (normal-mode)
148 (so-long-tests-remember)
149 (insert (make-string (1+ so-long-threshold) ?x))
150 (let ((so-long-action action))
151 (normal-mode)
152 (so-long-tests-assert-and-revert action)))))
153
154(ert-deftest so-long-tests-command-so-long ()
155 "Test the `so-long' command."
156 ;; Includes argument of nil, meaning the default `so-long-mode' action.
157 (dolist (action (cons nil (mapcar #'car so-long-action-alist)))
158 (with-temp-buffer
159 (insert "#!emacs\n")
160 (normal-mode)
161 (so-long-tests-remember)
162 (insert (make-string (1+ so-long-threshold) ?x))
163 (so-long action)
164 (so-long-tests-assert-and-revert (or action 'so-long-mode)))))
165
166(ert-deftest so-long-tests-so-long-menu-item-replace-action ()
167 "Test using the `so-long-menu-item-replace-action' menu item."
168 (with-temp-buffer
169 ;; Due to (with-selected-window (so-long-menu-click-window) ...)
170 ;; (used by `so-long-menu-item-replace-action'), our temp buffer
171 ;; must be in the selected window.
172 (set-window-buffer nil (current-buffer))
173 (insert "#!emacs\n")
174 (normal-mode)
175 (so-long-tests-remember)
176 (insert (make-string (1+ so-long-threshold) ?x))
177 (let (action)
178 (dolist (item so-long-action-alist)
179 ;; n.b. Any existing action is first reverted.
180 (so-long-menu-item-replace-action item)
181 (setq action (car item))
182 (so-long-tests-assert-active action))
183 ;; After all actions have been used, revert to normal and assert
184 ;; that the most recent action to have been applied is the one
185 ;; we have just reverted.
186 (so-long-menu-item-revert)
187 (so-long-tests-assert-reverted action))))
188
189(ert-deftest so-long-tests-major-mode ()
190 "Test calling `so-long-mode' directly."
191 (with-temp-buffer
192 (insert "#!emacs\n")
193 (normal-mode)
194 (so-long-tests-remember)
195 (so-long-mode)
196 (so-long-tests-assert-and-revert 'so-long-mode)))
197
198(ert-deftest so-long-tests-minor-mode ()
199 "Test calling `so-long-minor-mode' directly."
200 (with-temp-buffer
201 (insert "#!emacs\n")
202 (normal-mode)
203 (so-long-tests-remember)
204 (so-long-minor-mode 1)
205 (so-long-tests-assert-active 'so-long-minor-mode)
206 (so-long-minor-mode 0)
207 (so-long-tests-assert-reverted 'so-long-minor-mode)))
208
209(ert-deftest so-long-tests-target-modes ()
210 "Targeted major modes."
211 ;; Test the `so-long-target-modes' user option.
212 (with-temp-buffer
213 (insert "#!emacs\n")
214 (insert (make-string (1+ so-long-threshold) ?x))
215 ;; Nil target modes.
216 (let ((so-long-target-modes nil))
217 (normal-mode)
218 (should (eq major-mode 'emacs-lisp-mode)))
219 ;; Non-matching target modes.
220 (let ((so-long-target-modes '(text-mode)))
221 (normal-mode)
222 (should (eq major-mode 'emacs-lisp-mode)))
223 ;; Matching mode (direct).
224 (let ((so-long-target-modes '(emacs-lisp-mode)))
225 (normal-mode)
226 (should (eq major-mode 'so-long-mode)))
227 ;; Matching mode (indirect).
228 (let ((so-long-target-modes '(prog-mode)))
229 (normal-mode)
230 (should (eq major-mode 'so-long-mode)))))
231
232(ert-deftest so-long-tests-predicate ()
233 "Custom predicate function."
234 ;; Test the `so-long-predicate' user option.
235 (with-temp-buffer
236 (insert "#!emacs\n")
237 ;; Always false.
238 (let ((so-long-predicate #'ignore))
239 (normal-mode)
240 (should (eq major-mode 'emacs-lisp-mode)))
241 ;; Always true.
242 (let ((so-long-predicate (lambda () t)))
243 (normal-mode)
244 (should (eq major-mode 'so-long-mode)))))
245
246(ert-deftest so-long-tests-file-local-action ()
247 "File-local action."
248 ;; Test `so-long-action' as a file-local variable.
249 ;; Only valid in Emacs26+. Refer to "Caveats" in the so-long.el Commentary.
250 (unless (version< emacs-version "26")
251 (with-temp-buffer
252 (insert "#!emacs\n")
253 (normal-mode)
254 (so-long-tests-remember))
255 ;; n.b. `run-mode-hooks' *only* runs `hack-local-variables' when there's a
256 ;; (buffer-file-name), so the #!emacs approach is insufficient here. It's
257 ;; valid for the file-locals to be on the second line after the shebang,
258 ;; but with the *.el filename we no longer need the shebang.
259 (with-temp-buffer
260 (setq buffer-file-name (expand-file-name "so-long-tests-data.el"))
261 (insert ";; -*- so-long-action:so-long-minor-mode; -*-\n")
262 (put 'so-long-action 'safe-local-variable #'symbolp)
263 (insert (make-string (1+ so-long-threshold) ?x))
264 (normal-mode)
265 (so-long-tests-assert-and-revert 'so-long-minor-mode))))
266
267(ert-deftest so-long-tests-file-local-action-eval-so-long ()
268 "File-local action and eval:(so-long)."
269 ;; As per previous test, but using file-local `eval' to call `so-long'.
270 ;; Only valid in Emacs26+. Refer to "Caveats" in the so-long.el Commentary.
271 ;; See also `so-long-tests-file-local-action' above.
272 (unless (version< emacs-version "26")
273 (with-temp-buffer
274 (insert "#!emacs\n")
275 (normal-mode)
276 (so-long-tests-remember))
277 (with-temp-buffer
278 (setq buffer-file-name (concat (make-temp-name "so-long-tests-") ".el"))
279 (insert ";; -*- so-long-action:so-long-minor-mode; eval:(so-long) -*-\n")
280 (put 'so-long-action 'safe-local-variable #'symbolp)
281 (push '(eval . (so-long)) safe-local-variable-values)
282 (normal-mode)
283 (so-long-tests-assert-and-revert 'so-long-minor-mode))))
284
285(defvar so-long-tests-local-mode 'unset
286 "Set by `so-long-tests-file-local-mode-function'.")
287
288(defun so-long-tests-file-local-mode-function (mode)
289 "A custom value for `so-long-file-local-mode-function'."
290 (setq so-long-tests-local-mode mode))
291
292;; Test `so-long-file-local-mode-function' when the file-local major
293;; mode is `emacs-lisp-mode'.
294
295(defmacro so-long-tests-deftest-file-local-emacs-lisp-mode
296 (sym docstring prop-line &optional local-vars)
297 "Generate tests for using `emacs-lisp-mode' as a file-local mode."
298 (setq prop-line (or prop-line "")
299 local-vars (or local-vars ""))
300 `(ert-deftest ,sym ()
301 ,docstring
302 (let ((orig so-long-file-local-mode-function))
303 ;; Do nothing at all when a file-local mode is used.
304 (setq-default so-long-file-local-mode-function 'so-long-inhibit)
305 (with-temp-buffer
306 (insert ,prop-line)
307 (insert (make-string (1+ so-long-threshold) ?x))
308 (insert ,local-vars)
309 (normal-mode)
310 ;; Remember the `emacs-lisp-mode' state. The other cases
311 ;; will validate the 'reverted' state against this.
312 (so-long-tests-remember)
313 (should (eq major-mode 'emacs-lisp-mode)))
314 ;; Downgrade the action from major mode to minor mode.
315 (setq-default so-long-file-local-mode-function 'so-long-mode-downgrade)
316 (with-temp-buffer
317 (insert ,prop-line)
318 (insert (make-string (1+ so-long-threshold) ?x))
319 (insert ,local-vars)
320 (normal-mode)
321 (so-long-tests-assert-and-revert 'so-long-minor-mode))
322 ;; Do not treat the file-local mode specially.
323 (setq-default so-long-file-local-mode-function nil)
324 (with-temp-buffer
325 (insert ,prop-line)
326 (insert (make-string (1+ so-long-threshold) ?x))
327 (insert ,local-vars)
328 (normal-mode)
329 (so-long-tests-assert-and-revert 'so-long-mode))
330 ;; Custom function
331 (setq-default so-long-file-local-mode-function
332 #'so-long-tests-file-local-mode-function)
333 (with-temp-buffer
334 (insert ,prop-line)
335 (insert (make-string (1+ so-long-threshold) ?x))
336 (insert ,local-vars)
337 (let (so-long-tests-local-mode)
338 (normal-mode)
339 (should (eq so-long-tests-local-mode 'emacs-lisp-mode))
340 (so-long-tests-assert-active 'so-long-mode)))
341 ;; end
342 (setq-default so-long-file-local-mode-function orig))))
343
344(so-long-tests-deftest-file-local-emacs-lisp-mode
345 so-long-tests-file-local-emacs-lisp-mode-short-form
346 "File-local mode (short form). -*- emacs-lisp -*-"
347 ";; -*- emacs-lisp -*-\n")
348
349(so-long-tests-deftest-file-local-emacs-lisp-mode
350 so-long-tests-file-local-emacs-lisp-mode-long-form
351 "File-local mode (long form). -*- emacs-lisp -*-"
352 ";; -*- mode: emacs-lisp -*-\n")
353
354(so-long-tests-deftest-file-local-emacs-lisp-mode
355 so-long-tests-file-local-emacs-lisp-mode-long-form2
356 "File-local mode (long form). -*- emacs-lisp -*-"
357 nil "\n;; Local Variables:\n;; mode: emacs-lisp\n;; End:\n")
358
359;; Test `so-long-file-local-mode-function' when the file-local major
360;; mode is `so-long-mode'. In this case we should always end up with
361;; the major mode being `so-long-mode'.
362
363(defmacro so-long-tests-deftest-file-local-so-long-mode
364 (sym docstring prop-line &optional local-vars)
365 "Generate tests for using `so-long-mode' as a file-local mode."
366 (setq prop-line (or prop-line "")
367 local-vars (or local-vars ""))
368 `(ert-deftest ,sym ()
369 ,docstring
370 (let ((orig so-long-file-local-mode-function))
371 ;; Do nothing at all when a file-local mode is used.
372 (setq-default so-long-file-local-mode-function 'so-long-inhibit)
373 (with-temp-buffer
374 ;; Remember the new-buffer state. The other cases will
375 ;; validate the 'reverted' state against this.
376 (so-long-tests-remember)
377 (insert ,prop-line)
378 (insert (make-string (1+ so-long-threshold) ?x))
379 (insert ,local-vars)
380 (normal-mode)
381 (so-long-tests-assert-and-revert 'so-long-mode))
382 ;; Downgrade from major mode to minor mode.
383 (setq-default so-long-file-local-mode-function 'so-long-mode-downgrade)
384 (with-temp-buffer
385 (insert ,prop-line)
386 (insert (make-string (1+ so-long-threshold) ?x))
387 (insert ,local-vars)
388 (normal-mode)
389 (so-long-tests-assert-and-revert 'so-long-mode))
390 ;; Do not treat the file-local mode specially.
391 (setq-default so-long-file-local-mode-function nil)
392 (with-temp-buffer
393 (insert ,prop-line)
394 (insert (make-string (1+ so-long-threshold) ?x))
395 (insert ,local-vars)
396 (normal-mode)
397 (so-long-tests-assert-and-revert 'so-long-mode))
398 ;; Custom function.
399 (setq-default so-long-file-local-mode-function
400 #'so-long-tests-file-local-mode-function)
401 (with-temp-buffer
402 (insert ,prop-line)
403 (insert (make-string (1+ so-long-threshold) ?x))
404 (insert ,local-vars)
405 (let (so-long-tests-local-mode)
406 (normal-mode)
407 (should (eq so-long--inhibited t))
408 (should (eq so-long-tests-local-mode 'so-long-mode))
409 (so-long-tests-assert-active 'so-long-mode)))
410 ;; end
411 (setq-default so-long-file-local-mode-function orig))))
412
413(so-long-tests-deftest-file-local-so-long-mode
414 so-long-tests-file-local-so-long-mode-short-form
415 "File-local mode (short form). -*- so-long -*-"
416 ";; -*- so-long -*-\n")
417
418(so-long-tests-deftest-file-local-so-long-mode
419 so-long-tests-file-local-so-long-mode-long-form
420 "File-local mode (long form). -*- mode: so-long -*-"
421 ";; -*- mode: so-long -*-\n")
422
423(so-long-tests-deftest-file-local-so-long-mode
424 so-long-tests-file-local-so-long-mode-long-form2
425 "File-local mode (long form). -*- mode: so-long -*-"
426 nil "\n;; Local Variables:\n;; mode: so-long\n;; End:\n")
427
428(ert-deftest so-long-tests-commentary ()
429 "Test the `so-long-commentary' command."
430 (so-long-commentary)
431 (should (string= (buffer-name) "*So Long: Commentary*"))
432 (should (eq major-mode 'outline-mode))
433 (should (eq view-mode t))
434 (should (looking-at "^\\* Introduction$"))
435 (goto-char (point-min))
436 (should (looking-at "^so-long\\.el$"))
437 (should (re-search-forward "^\\* Change Log:$")))
438
439(ert-deftest so-long-tests-customize ()
440 "Test the `so-long-customize' command."
441 (so-long-customize)
442 (should (string= (buffer-name) "*Customize Group: So Long*"))
443 (should (eq major-mode 'Custom-mode)))
444
445;; Page break to prevent the local vars strings above from
446;; being misinterpreted as actual local vars declarations.
447
448;;; so-long-tests.el ends here