Clean up read_key_sequence a bit; reread active keymaps after first event.
[emacs.git] / test / automated / undo-tests.el
blob3037db03602df9310c6c2028f2e18a3c2a3f7af3
1 ;;; undo-tests.el --- Tests of primitive-undo
3 ;; Copyright (C) 2012 Aaron S. Hawley
5 ;; Author: Aaron S. Hawley <aaron.s.hawley@gmail.com>
7 ;; This program is free software: you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License as
9 ;; published by the Free Software Foundation, either version 3 of the
10 ;; License, or (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; 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/'.
20 ;;; Commentary:
22 ;; Profiling when the code was translate from C to Lisp on 2012-12-24.
24 ;;; C
26 ;; (elp-instrument-function 'primitive-undo)
27 ;; (load-file "undo-test.elc")
28 ;; (benchmark 100 '(let ((undo-test5-error nil)) (undo-test-all)))
29 ;; Elapsed time: 305.218000s (104.841000s in 14804 GCs)
30 ;; M-x elp-results
31 ;; Function Name Call Count Elapsed Time Average Time
32 ;; primitive-undo 2600 3.4889999999 0.0013419230
34 ;;; Lisp
36 ;; (load-file "primundo.elc")
37 ;; (elp-instrument-function 'primitive-undo)
38 ;; (benchmark 100 '(undo-test-all))
39 ;; Elapsed time: 295.974000s (104.582000s in 14704 GCs)
40 ;; M-x elp-results
41 ;; Function Name Call Count Elapsed Time Average Time
42 ;; primitive-undo 2700 3.6869999999 0.0013655555
44 ;;; Code:
46 (require 'ert)
48 (ert-deftest undo-test0 ()
49 "Test basics of \\[undo]."
50 (with-temp-buffer
51 (buffer-enable-undo)
52 (condition-case err
53 (undo)
54 (error
55 (unless (string= "No further undo information"
56 (cadr err))
57 (error err))))
58 (undo-boundary)
59 (insert "This")
60 (undo-boundary)
61 (erase-buffer)
62 (undo-boundary)
63 (insert "That")
64 (undo-boundary)
65 (forward-word -1)
66 (undo-boundary)
67 (insert "With ")
68 (undo-boundary)
69 (forward-word -1)
70 (undo-boundary)
71 (kill-word 1)
72 (undo-boundary)
73 (put-text-property (point-min) (point-max) 'face 'bold)
74 (undo-boundary)
75 (remove-text-properties (point-min) (point-max) '(face default))
76 (undo-boundary)
77 (set-buffer-multibyte (not enable-multibyte-characters))
78 (undo-boundary)
79 (undo)
80 (should
81 (equal (should-error (undo-more nil))
82 '(wrong-type-argument number-or-marker-p nil)))
83 (undo-more 7)
84 (should (string-equal "" (buffer-string)))))
86 (ert-deftest undo-test1 ()
87 "Test undo of \\[undo] command (redo)."
88 (with-temp-buffer
89 (buffer-enable-undo)
90 (undo-boundary)
91 (insert "This")
92 (undo-boundary)
93 (erase-buffer)
94 (undo-boundary)
95 (insert "That")
96 (undo-boundary)
97 (forward-word -1)
98 (undo-boundary)
99 (insert "With ")
100 (undo-boundary)
101 (forward-word -1)
102 (undo-boundary)
103 (kill-word 1)
104 (undo-boundary)
105 (facemenu-add-face 'bold (point-min) (point-max))
106 (undo-boundary)
107 (set-buffer-multibyte (not enable-multibyte-characters))
108 (undo-boundary)
109 (should
110 (string-equal (buffer-string)
111 (progn
112 (undo)
113 (undo-more 4)
114 (undo)
115 ;(undo-more -4)
116 (buffer-string))))))
118 (ert-deftest undo-test2 ()
119 "Test basic redoing with \\[undo] command."
120 (with-temp-buffer
121 (buffer-enable-undo)
122 (undo-boundary)
123 (insert "One")
124 (undo-boundary)
125 (insert " Zero")
126 (undo-boundary)
127 (push-mark)
128 (delete-region (save-excursion
129 (forward-word -1)
130 (point)) (point))
131 (undo-boundary)
132 (beginning-of-line)
133 (insert "Zero")
134 (undo-boundary)
135 (undo)
136 (should
137 (string-equal (buffer-string)
138 (progn
139 (undo-more 2)
140 (undo)
141 (buffer-string))))))
143 (ert-deftest undo-test3 ()
144 "Test modtime with \\[undo] command."
145 (let ((tmpfile (make-temp-file "undo-test3")))
146 (with-temp-file tmpfile
147 (let ((buffer-file-name tmpfile))
148 (buffer-enable-undo)
149 (set (make-local-variable 'make-backup-files) nil)
150 (undo-boundary)
151 (insert ?\s)
152 (undo-boundary)
153 (basic-save-buffer)
154 (insert ?\t)
155 (undo)
156 (should
157 (string-equal (buffer-string)
158 (progn
159 (undo)
160 (buffer-string)))))
161 (delete-file tmpfile))))
163 (ert-deftest undo-test4 ()
164 "Test \\[undo] of \\[flush-lines]."
165 (with-temp-buffer
166 (buffer-enable-undo)
167 (dotimes (i 1048576)
168 (if (zerop (% i 2))
169 (insert "Evenses")
170 (insert "Oddses")))
171 (undo-boundary)
172 (should
173 ;; Avoid string-equal because ERT will save the `buffer-string'
174 ;; to the explanation. Using `not' will record nil or non-nil.
175 (not
176 (null
177 (string-equal (buffer-string)
178 (progn
179 (flush-lines "oddses" (point-min) (point-max))
180 (undo-boundary)
181 (undo)
182 (undo)
183 (buffer-string))))))))
185 (ert-deftest undo-test5 ()
186 "Test basic redoing with \\[undo] command."
187 (with-temp-buffer
188 (buffer-enable-undo)
189 (undo-boundary)
190 (insert "AYE")
191 (undo-boundary)
192 (insert " BEE")
193 (undo-boundary)
194 (setq buffer-undo-list (cons '(0.0 bogus) buffer-undo-list))
195 (push-mark)
196 (delete-region (save-excursion
197 (forward-word -1)
198 (point)) (point))
199 (undo-boundary)
200 (beginning-of-line)
201 (insert "CEE")
202 (undo-boundary)
203 (undo)
204 (setq buffer-undo-list (cons "bogus" buffer-undo-list))
205 (should
206 (string-equal
207 (buffer-string)
208 (progn
209 (if (and (boundp 'undo-test5-error) (not undo-test5-error))
210 (progn
211 (should (null (undo-more 2)))
212 (should (undo)))
213 ;; Errors are generated by new Lisp version of
214 ;; `primitive-undo' not by built-in C version.
215 (should
216 (equal (should-error (undo-more 2))
217 '(error "Unrecognized entry in undo list (0.0 bogus)")))
218 (should
219 (equal (should-error (undo))
220 '(error "Unrecognized entry in undo list \"bogus\""))))
221 (buffer-string))))))
223 (defun undo-test-all (&optional interactive)
224 "Run all tests for \\[undo]."
225 (interactive "p")
226 (if interactive
227 (ert-run-tests-interactively "^undo-")
228 (ert-run-tests-batch "^undo-")))
230 (provide 'undo-tests)
231 ;;; undo-tests.el ends here