1 ;;; simple-test.el --- Tests for simple.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
7 ;; This program 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 ;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
24 (defmacro simple-test--dummy-buffer
(&rest body
)
29 (setq indent-tabs-mode nil
)
31 (save-excursion (insert " c d)"))
33 (cons (buffer-substring (point-min) (point))
34 (buffer-substring (point) (point-max)))))
37 (defmacro simple-test--transpositions
(&rest body
)
42 (insert "(s1) (s2) (s3) (s4) (s5)")
45 (cons (buffer-substring (point-min) (point))
46 (buffer-substring (point) (point-max)))))
50 (ert-deftest newline
()
51 (should-error (newline -
1))
52 (should (equal (simple-test--dummy-buffer (newline 1))
53 '("(a b\n" .
" c d)")))
54 (should (equal (simple-test--dummy-buffer
55 (electric-indent-mode -
1)
56 (call-interactively #'newline
))
57 '("(a b\n" .
" c d)")))
58 (should (equal (simple-test--dummy-buffer
59 (let ((current-prefix-arg 5))
60 (call-interactively #'newline
)))
61 '("(a b\n\n\n\n\n" .
" c d)")))
62 (should (equal (simple-test--dummy-buffer (newline 5))
63 '("(a b\n\n\n\n\n" .
" c d)")))
64 (should (equal (simple-test--dummy-buffer
67 '("(a b \n" .
"c d)"))))
69 (ert-deftest newline-indent
()
70 (should (equal (simple-test--dummy-buffer
71 (electric-indent-local-mode 1)
73 '("(a b\n" .
" c d)")))
74 (should (equal (simple-test--dummy-buffer
75 (electric-indent-local-mode 1)
76 (newline 1 'interactive
))
77 '("(a b\n " .
"c d)")))
78 (should (equal (simple-test--dummy-buffer
79 (electric-indent-local-mode 1)
80 (let ((current-prefix-arg nil
))
81 (call-interactively #'newline
)
82 (call-interactively #'newline
)))
83 '("(a b\n\n " .
"c d)")))
84 (should (equal (simple-test--dummy-buffer
85 (electric-indent-local-mode 1)
86 (newline 5 'interactive
))
87 '("(a b\n\n\n\n\n " .
"c d)")))
88 (should (equal (simple-test--dummy-buffer
89 (electric-indent-local-mode 1)
90 (let ((current-prefix-arg 5))
91 (call-interactively #'newline
)))
92 '("(a b\n\n\n\n\n " .
"c d)")))
93 (should (equal (simple-test--dummy-buffer
95 (electric-indent-local-mode 1)
96 (newline 1 'interactive
))
97 '("(a b\n " .
"c d)"))))
101 (ert-deftest open-line
()
102 (should-error (open-line -
1))
103 (should-error (open-line))
104 (should (equal (simple-test--dummy-buffer (open-line 1))
105 '("(a b" .
"\n c d)")))
106 (should (equal (simple-test--dummy-buffer
107 (electric-indent-mode -
1)
108 (call-interactively #'open-line
))
109 '("(a b" .
"\n c d)")))
110 (should (equal (simple-test--dummy-buffer
111 (let ((current-prefix-arg 5))
112 (call-interactively #'open-line
)))
113 '("(a b" .
"\n\n\n\n\n c d)")))
114 (should (equal (simple-test--dummy-buffer (open-line 5))
115 '("(a b" .
"\n\n\n\n\n c d)")))
116 (should (equal (simple-test--dummy-buffer
119 '("(a b " .
"\nc d)"))))
121 (ert-deftest open-line-margin-and-prefix
()
122 (should (equal (simple-test--dummy-buffer
123 (let ((left-margin 10))
125 '("(a b" .
"\n\n\n c d)")))
126 (should (equal (simple-test--dummy-buffer
128 (let ((left-margin 2))
130 '(" " .
"\n (a b c d)")))
131 (should (equal (simple-test--dummy-buffer
132 (let ((fill-prefix "- - "))
134 '("(a b" .
"\n c d)")))
135 (should (equal (simple-test--dummy-buffer
137 (let ((fill-prefix "- - "))
139 '("- - " .
"\n(a b c d)"))))
141 ;; For a while, from 24 Oct - 21 Nov 2015, `open-line' in the Emacs
142 ;; development tree became sensitive to `electric-indent-mode', which
143 ;; it had not been before. This sensitivity was reverted for the
144 ;; Emacs 25 release, so it could be discussed further (see thread
145 ;; "Questioning the new behavior of `open-line'." on the Emacs Devel
146 ;; mailing list, and bug #21884).
147 (ert-deftest open-line-indent
()
148 (should (equal (simple-test--dummy-buffer
149 (electric-indent-local-mode 1)
151 '("(a b" .
"\n c d)")))
152 (should (equal (simple-test--dummy-buffer
153 (electric-indent-local-mode 1)
155 '("(a b" .
"\n c d)")))
156 (should (equal (simple-test--dummy-buffer
157 (electric-indent-local-mode 1)
158 (let ((current-prefix-arg nil
))
159 (call-interactively #'open-line
)
160 (call-interactively #'open-line
)))
161 '("(a b" .
"\n\n c d)")))
162 (should (equal (simple-test--dummy-buffer
163 (electric-indent-local-mode 1)
165 '("(a b" .
"\n\n\n\n\n c d)")))
166 (should (equal (simple-test--dummy-buffer
167 (electric-indent-local-mode 1)
168 (let ((current-prefix-arg 5))
169 (call-interactively #'open-line
)))
170 '("(a b" .
"\n\n\n\n\n c d)")))
171 (should (equal (simple-test--dummy-buffer
173 (electric-indent-local-mode 1)
175 '("(a b " .
"\nc d)"))))
177 ;; From 24 Oct - 21 Nov 2015, `open-line' took a second argument
178 ;; INTERACTIVE and ran `post-self-insert-hook' if the argument was
179 ;; true. This test tested that. Currently, however, `open-line'
180 ;; does not run run `post-self-insert-hook' at all, so for now
181 ;; this test just makes sure that it doesn't.
182 (ert-deftest open-line-hook
()
184 (inc (lambda () (setq x
(1+ x
)))))
185 (simple-test--dummy-buffer
186 (add-hook 'post-self-insert-hook inc nil
'local
)
189 (simple-test--dummy-buffer
190 (add-hook 'post-self-insert-hook inc nil
'local
)
196 (add-hook 'post-self-insert-hook inc
)
197 (simple-test--dummy-buffer
200 (simple-test--dummy-buffer
203 (remove-hook 'post-self-insert-hook inc
))))
206 ;;; `delete-trailing-whitespace'
207 (ert-deftest simple-delete-trailing-whitespace
()
208 "Test bug#21766: delete-whitespace sometimes deletes non-whitespace."
209 (defvar python-indent-guess-indent-offset
) ; to avoid a warning
210 (let ((python (featurep 'python
))
211 (python-indent-guess-indent-offset nil
)
212 (delete-trailing-lines t
))
216 (insert (concat "query = \"\"\"WITH filtered AS \n"
218 "\"\"\".format(fv_)\n"
221 (delete-trailing-whitespace)
222 (should (equal (count-lines (point-min) (point-max)) 3)))
223 ;; Let's clean up if running interactive
224 (unless (or noninteractive python
)
225 (unload-feature 'python
)))))
228 ;;; auto-boundary tests
229 (ert-deftest undo-auto-boundary-timer
()
231 undo-auto-current-boundary-timer
))
233 (ert-deftest undo-auto--boundaries-added
()
234 ;; The change in the buffer should have caused addition
235 ;; to undo-auto--undoably-changed-buffers.
238 (setq buffer-undo-list nil
)
240 (member (current-buffer) undo-auto--undoably-changed-buffers
)))
241 ;; The head of buffer-undo-list should be the insertion event, and
245 (setq buffer-undo-list nil
)
247 (car buffer-undo-list
)))
248 ;; Now the head of the buffer-undo-list should be a boundary and so
249 ;; nil. We have to call auto-boundary explicitly because we are out
250 ;; of the command loop
253 (setq buffer-undo-list nil
)
255 (car buffer-undo-list
)
256 (undo-auto--boundaries 'test
))))
258 ;;; Transposition with negative args (bug#20698, bug#21885)
259 (ert-deftest simple-transpose-subr
()
260 (should (equal (simple-test--transpositions (transpose-sexps -
1))
261 '("(s1) (s2) (s4)" .
" (s3) (s5)")))
262 (should (equal (simple-test--transpositions (transpose-sexps -
2))
263 '("(s1) (s4)" .
" (s2) (s3) (s5)"))))
266 ;; Test for a regression introduced by undo-auto--boundaries changes.
267 ;; https://lists.gnu.org/archive/html/emacs-devel/2015-11/msg01652.html
268 (defun undo-test-kill-c-a-then-undo ()
270 (switch-to-buffer (current-buffer))
271 (setq buffer-undo-list nil
)
272 (insert "a\nb\n\c\n")
273 (goto-char (point-max))
274 ;; We use a keyboard macro because it adds undo events in the same
275 ;; way as if a user were involved.
276 (kmacro-call-macro nil nil nil
288 (defun undo-test-point-after-forward-kill ()
290 (switch-to-buffer (current-buffer))
291 (setq buffer-undo-list nil
)
292 (insert "kill word forward")
293 ;; Move to word "word".
295 (kmacro-call-macro nil nil nil
304 (ert-deftest undo-point-in-wrong-place
()
306 ;; returns 5 with the bug
308 (undo-test-kill-c-a-then-undo)))
311 (undo-test-point-after-forward-kill))))
314 (provide 'simple-test
)
315 ;;; simple-test.el ends here