1 ;;; iedit-tests.el --- iedit's automatic-tests
3 ;; Copyright (C) 2010, 2011, 2012 Victor Ren
5 ;; Time-stamp: <2012-08-09 17:17:03 Victor Ren>
6 ;; Author: Victor Ren <victorhge@gmail.com>
8 ;; X-URL: http://www.emacswiki.org/emacs/Iedit
10 ;; This file is not part of GNU Emacs, but it is distributed under
11 ;; the same terms as GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file is part of iedit.
34 (ert-deftest iedit-compile-test ()
35 (let ((byte-compile-error-on-warn t ))
36 (should (byte-compile-file "iedit.el"))
37 (delete-file "iedit.elc" nil)))
39 (defun with-iedit-test-fixture (input-buffer-string body)
41 (let ((old-transient-mark-mode transient-mark-mode)
42 (old-iedit-transient-sensitive iedit-transient-mark-sensitive))
46 (transient-mark-mode t)
47 (setq iedit-transient-mark-sensitive t)
48 (insert input-buffer-string)
53 (setq iedit-transient-mark-sensitive nil)
54 (transient-mark-mode -1)
55 (insert input-buffer-string)
59 (transient-mark-mode old-transient-mark-mode)
60 (setq iedit-transient-mark-sensitive old-transient-mark-mode))))
62 (ert-deftest iedit-mode-base-test ()
63 (with-iedit-test-fixture
69 (should (= 3 (length iedit-occurrences-overlays)))
70 (should (string= iedit-initial-string-local "foo"))
71 (set-mark-command nil)
74 (should (= 2 (length iedit-occurrences-overlays)))
75 (should (string= iedit-initial-string-local "foo"))
77 (should (null iedit-occurrences-overlays)))))
79 (ert-deftest iedit-mode-with-region-test ()
80 (with-iedit-test-fixture
89 (set-mark-command nil)
92 (should (= 4 (length iedit-occurrences-overlays)))
93 (should (string= iedit-initial-string-local "foo"))
94 (should (null iedit-only-complete-symbol-local))
96 (set-mark-command nil)
99 (should (= 1 (length iedit-occurrences-overlays))))))
101 (ert-deftest iedit-move-conjointed-overlays-test ()
102 (with-iedit-test-fixture
110 (set-mark-command nil)
113 (should (= 7 (length iedit-occurrences-overlays)))
114 (should (string= iedit-initial-string-local "foo"))
115 (should (null iedit-only-complete-symbol-local))
118 (should (string= (buffer-string)
125 (should (string= (buffer-string)
127 123foo456123foo456123foo456
131 (ert-deftest iedit-mode-start-from-isearch-test ()
132 (with-iedit-test-fixture
138 (should (= 3 (length iedit-occurrences-overlays)))
139 (should (string= iedit-initial-string-local "foo"))
143 (isearch-process-search-char ?f)
144 (isearch-process-search-char ?o)
145 (isearch-process-search-char ?o)
147 (should (string= iedit-initial-string-local "foo"))
148 (should (= 4 (length iedit-occurrences-overlays)))
150 (should (null iedit-occurrences-overlays)))))
152 (ert-deftest iedit-mode-last-local-occurrence-test ()
153 (with-iedit-test-fixture
159 (should (= 3 (length iedit-occurrences-overlays)))
160 (should (string= iedit-initial-string-local "foo"))
163 (iedit-mode 4) ; last local
164 (should (string= iedit-initial-string-local "foo"))
165 (should (= 3 (length iedit-occurrences-overlays))))))
167 (ert-deftest iedit-mode-last-global-occurrence-test ()
168 (with-iedit-test-fixture
174 (should (= 3 (length iedit-occurrences-overlays)))
175 (should (string= iedit-initial-string-local "foo"))
178 (insert "bar foo foo")
181 (should (string= iedit-initial-string-local "foo"))
182 (should (= 2 (length iedit-occurrences-overlays)))))))
184 (ert-deftest iedit-execute-last-modification-test ()
185 (with-iedit-test-fixture
191 (should (= 3 (length iedit-occurrences-overlays)))
192 (should (string= iedit-initial-string-local "foo"))
195 (insert "bar foo foo")
196 (should-error (iedit-execute-last-modification))))))
198 (ert-deftest iedit-movement-test ()
199 (with-iedit-test-fixture
205 (iedit-last-occurrence)
206 (should (= (point) 24))
207 (iedit-first-occurrence)
208 (should (= (point) 1))
209 (iedit-next-occurrence)
210 (should (= (point) 7))
211 (iedit-next-occurrence)
212 (should (= (point) 24))
213 (iedit-next-occurrence)
214 (should (= (point) 24)) ;; (should (string= (current-message) "This is the last occurrence."))
215 (iedit-next-occurrence)
216 (should (= (point) 1)) ;; (should (string= (current-message) "Located the first occurrence."))
217 (iedit-next-occurrence)
218 (should (= (point) 7))
219 (goto-char (point-max))
220 (iedit-prev-occurrence)
221 (should (= (point) 27))
222 (iedit-prev-occurrence)
223 (should (= (point) 24))
224 (iedit-prev-occurrence)
225 (should (= (point) 7))
226 (iedit-prev-occurrence)
227 (should (= (point) 1))
228 (iedit-prev-occurrence)
229 (should (= (point) 1)) ;; (should (string= (current-message) "This is the first occurrence."))
230 (iedit-prev-occurrence)
231 (should (= (point) 24)) ;; (should (string= (current-message) "Located the last occurrence."))
234 (ert-deftest iedit-occurrence-update-test ()
235 (with-iedit-test-fixture
242 (should (string= (buffer-string)
247 (backward-delete-char 1)
248 (should (string= (buffer-string)
254 (should (string= (buffer-string)
259 ;; test insert from empty
260 (iedit-delete-occurrences)
262 (should (string= (buffer-string)
268 (ert-deftest iedit-aborting-test ()
269 (with-iedit-test-fixture
275 (kill-region (point) (+ 4 (point)))
276 (should (string= (buffer-string)
281 (ert-deftest iedit-toggle-case-sensitive-test ()
282 (with-iedit-test-fixture
288 (should (= 2 (length iedit-occurrences-overlays)))
289 (iedit-toggle-case-sensitive)
290 (should (= 3 (length iedit-occurrences-overlays)))
291 (iedit-next-occurrence)
292 (iedit-toggle-case-sensitive)
293 (should (= 1 (length iedit-occurrences-overlays))))))
295 (ert-deftest iedit-apply-on-occurrences-test ()
296 "Test functions deal with the whole occurrences"
297 (with-iedit-test-fixture
303 (iedit-upcase-occurrences)
304 (should (string= (buffer-string)
309 (iedit-downcase-occurrences)
310 (should (string= (buffer-string)
315 (iedit-replace-occurrences "bar")
316 (should (string= (buffer-string)
321 (iedit-number-occurrences 1)
322 (should (string= (buffer-string)
328 (ert-deftest iedit-blank-occurrences-test ()
329 "Test functions deal with the whole occurrences"
330 (with-iedit-test-fixture
333 (iedit-blank-occurrences)
334 (should (string= (buffer-string) " barfoo ")))))
336 (ert-deftest iedit-blank-occurrences-rectangle-test ()
337 "Test functions deal with the whole occurrences"
338 (with-iedit-test-fixture
342 (iedit-mode) ; turn off iedit
344 (set-mark-command nil)
347 (iedit-rectangle-mode)
348 (iedit-blank-occurrences)
349 (should (string= (buffer-string) "f o
352 (ert-deftest iedit-delete-occurrences-test ()
353 "Test functions deal with the whole occurrences"
354 (with-iedit-test-fixture
357 (iedit-delete-occurrences)
358 (should (string= (buffer-string) " barfoo ")))))
360 (ert-deftest iedit-toggle-buffering-test ()
361 (with-iedit-test-fixture
367 (iedit-toggle-buffering)
369 (should (string= (buffer-string)
374 (iedit-toggle-buffering)
375 (should (string= (buffer-string)
380 (should (= (point) 4))
381 (iedit-toggle-buffering)
382 (backward-delete-char 3)
383 (should (string= (buffer-string)
388 (goto-char 15) ;not in an occurrence
389 (should (null (iedit-find-current-occurrence-overlay)))
390 (iedit-toggle-buffering)
391 (should (string= (buffer-string)
397 (ert-deftest iedit-rectangle-start-test ()
398 (with-iedit-test-fixture
405 (set-mark-command nil)
408 (iedit-rectangle-mode)
409 (should (equal iedit-rectangle '(1 19))))))
411 (ert-deftest iedit-kill-rectangle-error-test ()
412 (with-iedit-test-fixture
419 (set-mark-command nil)
421 (iedit-rectangle-mode)
422 (should (iedit-same-column))
423 (should (equal iedit-rectangle '(1 22)))
424 (iedit-prev-occurrence)
426 (should (not (iedit-same-column)))
427 (should-error (iedit-kill-rectangle)))))
429 (ert-deftest iedit-kill-rectangle-test ()
430 (with-iedit-test-fixture
437 (set-mark-command nil)
439 (iedit-rectangle-mode)
440 (should (iedit-same-column))
441 (should (equal iedit-rectangle '(1 22)))
442 (iedit-kill-rectangle)
443 (should (string= (buffer-string)
448 (should (equal killed-rectangle '("foo" " fo" " b" " "))))))
450 (ert-deftest iedit-restrict-defun-test ()
451 (with-iedit-test-fixture
453 (defun foo (foo bar foo)
454 \"foo bar foobar\" nil)
455 (defun bar (bar foo bar)
456 \"bar foo barfoo\" nil)"
462 (iedit-restrict-function)
463 (should (= 1 (length iedit-occurrences-overlays)))
466 (iedit-mode-on-function)
467 (should (= 4 (length iedit-occurrences-overlays)))
472 (should (= 4 (length iedit-occurrences-overlays))))))
474 (ert-deftest iedit-transient-sensitive-test ()
475 (with-iedit-test-fixture
477 (defun foo (foo bar foo)
478 \"foo bar foobar\" nil)
479 (defun bar (bar foo bar)
480 \"bar foo barfoo\" nil)"
484 (setq iedit-transient-mark-sensitive t)
485 (transient-mark-mode -1)
488 (iedit-restrict-function)
489 (should (= 1 (length iedit-occurrences-overlays)))
493 (should (= 4 (length iedit-occurrences-overlays)))
494 (iedit-mode) ;;turn off iedit mode
498 (should (= 0 (length iedit-occurrences-overlays))))))
500 (defvar iedit-printable-test-lists
506 ("12345678901234567890123456789012345678901234567890abc" "12345678901234567890123456789012345678901234567890...")
507 ("12345678901234567890123456789012345678901234567890abc
508 abcd" "12345678901234567890123456789012345678901234567890...")))
510 (ert-deftest iedit-printable-test ()
511 (dolist (test iedit-printable-test-lists)
512 (should (string= (iedit-printable (car test)) (cadr test)))))
515 ;; (elp-instrument-list '(insert-and-inherit
518 ;; iedit-occurrence-update
519 ;; buffer-substring-no-properties
525 ;;; iedit-tests.el ends here