]> code.delx.au - gnu-emacs/blob - test/automated/simple-test.el
07b5eaa93e4dd8726c8406742ecc4c32084f57ce
[gnu-emacs] / test / automated / simple-test.el
1 ;;; simple-test.el --- Tests for simple.el -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
6
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.
11
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.
16
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/>.
19
20 ;;; Code:
21
22 (require 'ert)
23
24 (defmacro simple-test--dummy-buffer (&rest body)
25 (declare (indent 0)
26 (debug t))
27 `(with-temp-buffer
28 (emacs-lisp-mode)
29 (setq indent-tabs-mode nil)
30 (insert "(a b")
31 (save-excursion (insert " c d)"))
32 ,@body
33 (cons (buffer-substring (point-min) (point))
34 (buffer-substring (point) (point-max)))))
35
36
37 (defmacro simple-test--transpositions (&rest body)
38 (declare (indent 0)
39 (debug t))
40 `(with-temp-buffer
41 (emacs-lisp-mode)
42 (insert "(s1) (s2) (s3) (s4) (s5)")
43 (backward-sexp 1)
44 ,@body
45 (cons (buffer-substring (point-min) (point))
46 (buffer-substring (point) (point-max)))))
47
48 \f
49 ;;; `newline'
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
65 (forward-char 1)
66 (newline 1))
67 '("(a b \n" . "c d)"))))
68
69 (ert-deftest newline-indent ()
70 (should (equal (simple-test--dummy-buffer
71 (electric-indent-local-mode 1)
72 (newline 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
94 (forward-char 1)
95 (electric-indent-local-mode 1)
96 (newline 1 'interactive))
97 '("(a b\n " . "c d)"))))
98
99 \f
100 ;;; `open-line'
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
117 (forward-char 1)
118 (open-line 1))
119 '("(a b " . "\nc d)"))))
120
121 (ert-deftest open-line-margin-and-prefix ()
122 (should (equal (simple-test--dummy-buffer
123 (let ((left-margin 10))
124 (open-line 3)))
125 '("(a b" . "\n\n\n c d)")))
126 (should (equal (simple-test--dummy-buffer
127 (forward-line 0)
128 (let ((left-margin 2))
129 (open-line 1)))
130 '(" " . "\n (a b c d)")))
131 (should (equal (simple-test--dummy-buffer
132 (let ((fill-prefix "- - "))
133 (open-line 1)))
134 '("(a b" . "\n c d)")))
135 (should (equal (simple-test--dummy-buffer
136 (forward-line 0)
137 (let ((fill-prefix "- - "))
138 (open-line 1)))
139 '("- - " . "\n(a b c d)"))))
140
141 (ert-deftest open-line-indent ()
142 (should (equal (simple-test--dummy-buffer
143 (electric-indent-local-mode 1)
144 (open-line 1))
145 '("(a b" . "\n c d)")))
146 (should (equal (simple-test--dummy-buffer
147 (electric-indent-local-mode 1)
148 (open-line 1 'interactive))
149 '("(a b" . "\n c d)")))
150 (should (equal (simple-test--dummy-buffer
151 (electric-indent-local-mode 1)
152 (let ((current-prefix-arg nil))
153 (call-interactively #'open-line)
154 (call-interactively #'open-line)))
155 '("(a b" . "\n\n c d)")))
156 (should (equal (simple-test--dummy-buffer
157 (electric-indent-local-mode 1)
158 (open-line 5 'interactive))
159 '("(a b" . "\n\n\n\n\n c d)")))
160 (should (equal (simple-test--dummy-buffer
161 (electric-indent-local-mode 1)
162 (let ((current-prefix-arg 5))
163 (call-interactively #'open-line)))
164 '("(a b" . "\n\n\n\n\n c d)")))
165 (should (equal (simple-test--dummy-buffer
166 (forward-char 1)
167 (electric-indent-local-mode 1)
168 (open-line 1 'interactive))
169 '("(a b" . "\n c d)"))))
170
171 (ert-deftest open-line-hook ()
172 (let* ((x 0)
173 (inc (lambda () (setq x (1+ x)))))
174 (simple-test--dummy-buffer
175 (add-hook 'post-self-insert-hook inc nil 'local)
176 (open-line 1))
177 (should (= x 0))
178 (simple-test--dummy-buffer
179 (add-hook 'post-self-insert-hook inc nil 'local)
180 (open-line 1 'interactive))
181 (should (= x 1))
182
183 (unwind-protect
184 (progn
185 (add-hook 'post-self-insert-hook inc)
186 (simple-test--dummy-buffer
187 (open-line 1))
188 (should (= x 1))
189 (simple-test--dummy-buffer
190 (open-line 10 'interactive))
191 (should (= x 2)))
192 (remove-hook 'post-self-insert-hook inc))))
193
194 \f
195 ;;; `delete-trailing-whitespace'
196 (ert-deftest simple-delete-trailing-whitespace ()
197 "Test bug#21766: delete-whitespace sometimes deletes non-whitespace."
198 (defvar python-indent-guess-indent-offset) ; to avoid a warning
199 (let ((python (featurep 'python))
200 (python-indent-guess-indent-offset nil)
201 (delete-trailing-lines t))
202 (unwind-protect
203 (with-temp-buffer
204 (python-mode)
205 (insert (concat "query = \"\"\"WITH filtered AS \n"
206 "WHERE \n"
207 "\"\"\".format(fv_)\n"
208 "\n"
209 "\n"))
210 (delete-trailing-whitespace)
211 (should (equal (count-lines (point-min) (point-max)) 3)))
212 ;; Let's clean up if running interactive
213 (unless (or noninteractive python)
214 (unload-feature 'python)))))
215
216
217 ;;; auto-boundary tests
218 (ert-deftest undo-auto--boundary-timer ()
219 (should
220 undo-auto--current-boundary-timer))
221
222 (ert-deftest undo-auto--boundaries-added ()
223 ;; The change in the buffer should have caused addition
224 ;; to undo-auto--undoably-changed-buffers.
225 (should
226 (with-temp-buffer
227 (setq buffer-undo-list nil)
228 (insert "hello")
229 (member (current-buffer) undo-auto--undoably-changed-buffers)))
230 ;; The head of buffer-undo-list should be the insertion event, and
231 ;; therefore not nil
232 (should
233 (with-temp-buffer
234 (setq buffer-undo-list nil)
235 (insert "hello")
236 (car buffer-undo-list)))
237 ;; Now the head of the buffer-undo-list should be a boundary and so
238 ;; nil. We have to call auto-boundary explicitly because we are out
239 ;; of the command loop
240 (should-not
241 (with-temp-buffer
242 (setq buffer-undo-list nil)
243 (insert "hello")
244 (car buffer-undo-list)
245 (undo-auto--boundaries 'test))))
246
247 ;;; Transposition with negative args (bug#20698, bug#21885)
248 (ert-deftest simple-transpose-subr ()
249 (should (equal (simple-test--transpositions (transpose-sexps -1))
250 '("(s1) (s2) (s4)" . " (s3) (s5)")))
251 (should (equal (simple-test--transpositions (transpose-sexps -2))
252 '("(s1) (s4)" . " (s2) (s3) (s5)"))))
253
254
255 (provide 'simple-test)
256 ;;; simple-test.el ends here