1 ;;; iedit-tests.el --- iedit's automatic-tests
3 ;; Copyright (C) 2010, 2011, 2012 Victor Ren
5 ;; Time-stamp: <2012-10-22 14:01:57 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.
35 (ert-deftest iedit-compile-test ()
36 (let ((byte-compile-error-on-warn t ))
37 (should (byte-compile-file "iedit.el"))
38 (delete-file "iedit.elc" nil)))
40 (defun with-iedit-test-fixture (input-buffer-string body)
42 (let ((old-transient-mark-mode transient-mark-mode)
43 (old-iedit-transient-sensitive iedit-transient-mark-sensitive))
47 (transient-mark-mode t)
48 (setq iedit-transient-mark-sensitive t)
49 (insert input-buffer-string)
54 (setq iedit-transient-mark-sensitive nil)
55 (transient-mark-mode -1)
56 (insert input-buffer-string)
60 (transient-mark-mode old-transient-mark-mode)
61 (setq iedit-transient-mark-sensitive old-transient-mark-mode))))
63 (ert-deftest iedit-mode-base-test ()
64 (with-iedit-test-fixture
70 (should (= 3 (length iedit-occurrences-overlays)))
71 (should (string= iedit-initial-string-local "foo"))
72 (set-mark-command nil)
75 (should (= 2 (length iedit-occurrences-overlays)))
76 (should (string= iedit-initial-string-local "foo"))
78 (should (null iedit-occurrences-overlays)))))
80 (ert-deftest iedit-mode-with-region-test ()
81 (with-iedit-test-fixture
90 (set-mark-command nil)
93 (should (= 4 (length iedit-occurrences-overlays)))
94 (should (string= iedit-initial-string-local "foo"))
95 (should (null iedit-only-complete-symbol-local))
97 (set-mark-command nil)
100 (should (= 1 (length iedit-occurrences-overlays))))))
102 (ert-deftest iedit-move-conjointed-overlays-test ()
103 (with-iedit-test-fixture
111 (set-mark-command nil)
114 (should (= 7 (length iedit-occurrences-overlays)))
115 (should (string= iedit-initial-string-local "foo"))
116 (should (null iedit-only-complete-symbol-local))
119 (should (string= (buffer-string)
126 (should (string= (buffer-string)
128 123foo456123foo456123foo456
132 (ert-deftest iedit-overlay-at-end-of-buffer ()
133 (with-iedit-test-fixture
138 (highlight-changes-mode 1)
139 (goto-char (point-min))
140 (goto-char (point-at-eol))
142 (delete-region (point) (1- (point)))
143 (should (string= (buffer-string)
147 (should (string= (buffer-string)
151 (ert-deftest iedit-mode-start-from-isearch-test ()
152 (with-iedit-test-fixture
158 (should (= 3 (length iedit-occurrences-overlays)))
159 (should (string= iedit-initial-string-local "foo"))
163 (isearch-process-search-char ?f)
164 (isearch-process-search-char ?o)
165 (isearch-process-search-char ?o)
166 (call-interactively 'iedit-mode-from-isearch)
167 (should (string= iedit-initial-string-local "foo"))
168 (should (= 4 (length iedit-occurrences-overlays)))
170 (should (null iedit-occurrences-overlays)))))
172 (ert-deftest iedit-mode-last-local-occurrence-test ()
173 (with-iedit-test-fixture
179 (should (= 3 (length iedit-occurrences-overlays)))
180 (should (string= iedit-initial-string-local "foo"))
183 (iedit-mode 4) ; last local
184 (should (string= iedit-initial-string-local "foo"))
185 (should (= 3 (length iedit-occurrences-overlays))))))
187 (ert-deftest iedit-mode-last-global-occurrence-test ()
188 (with-iedit-test-fixture
194 (should (= 3 (length iedit-occurrences-overlays)))
195 (should (string= iedit-initial-string-local "foo"))
198 (insert "bar foo foo")
201 (should (string= iedit-initial-string-local "foo"))
202 (should (= 2 (length iedit-occurrences-overlays)))))))
204 (ert-deftest iedit-execute-last-modification-test ()
205 (with-iedit-test-fixture
211 (should (= 3 (length iedit-occurrences-overlays)))
212 (should (string= iedit-initial-string-local "foo"))
215 (insert "bar foo foo")
216 (should-error (iedit-execute-last-modification))))))
218 (ert-deftest iedit-movement-test ()
219 (with-iedit-test-fixture
225 (iedit-last-occurrence)
226 (should (= (point) 24))
227 (iedit-first-occurrence)
228 (should (= (point) 1))
229 (iedit-next-occurrence)
230 (should (= (point) 7))
231 (iedit-next-occurrence)
232 (should (= (point) 24))
233 (iedit-next-occurrence)
234 (should (= (point) 24)) ;; (should (string= (current-message) "This is the last occurrence."))
235 (iedit-next-occurrence)
236 (should (= (point) 1)) ;; (should (string= (current-message) "Located the first occurrence."))
237 (iedit-next-occurrence)
238 (should (= (point) 7))
239 (goto-char (point-max))
240 (iedit-prev-occurrence)
241 (should (= (point) 27))
242 (iedit-prev-occurrence)
243 (should (= (point) 24))
244 (iedit-prev-occurrence)
245 (should (= (point) 7))
246 (iedit-prev-occurrence)
247 (should (= (point) 1))
248 (iedit-prev-occurrence)
249 (should (= (point) 1)) ;; (should (string= (current-message) "This is the first occurrence."))
250 (iedit-prev-occurrence)
251 (should (= (point) 24)) ;; (should (string= (current-message) "Located the last occurrence."))
254 (ert-deftest iedit-occurrence-update-test ()
255 (with-iedit-test-fixture
262 (should (string= (buffer-string)
267 (backward-delete-char 1)
268 (should (string= (buffer-string)
274 (should (string= (buffer-string)
279 ;; test insert from empty
280 (iedit-delete-occurrences)
282 (should (string= (buffer-string)
288 (ert-deftest iedit-aborting-test ()
289 (with-iedit-test-fixture
295 (kill-region (point) (+ 4 (point)))
296 (should (string= (buffer-string)
301 (ert-deftest iedit-toggle-case-sensitive-test ()
302 (with-iedit-test-fixture
308 (should (= 2 (length iedit-occurrences-overlays)))
309 (iedit-toggle-case-sensitive)
310 (should (= 3 (length iedit-occurrences-overlays)))
311 (iedit-next-occurrence)
312 (iedit-toggle-case-sensitive)
313 (should (= 1 (length iedit-occurrences-overlays))))))
315 (ert-deftest iedit-apply-on-occurrences-test ()
316 "Test functions deal with the whole occurrences"
317 (with-iedit-test-fixture
323 (iedit-upcase-occurrences)
324 (should (string= (buffer-string)
329 (iedit-downcase-occurrences)
330 (should (string= (buffer-string)
335 (iedit-replace-occurrences "bar")
336 (should (string= (buffer-string)
341 (iedit-number-occurrences 1)
342 (should (string= (buffer-string)
348 (ert-deftest iedit-blank-occurrences-test ()
349 "Test functions deal with the whole occurrences"
350 (with-iedit-test-fixture
353 (iedit-blank-occurrences)
354 (should (string= (buffer-string) " barfoo ")))))
356 (ert-deftest iedit-blank-occurrences-rectangle-test ()
357 "Test functions deal with the whole occurrences"
358 (with-iedit-test-fixture
362 (iedit-mode) ; turn off iedit
364 (set-mark-command nil)
366 (iedit-rectangle-mode)
367 (iedit-blank-occurrences)
368 (should (string= (buffer-string) "f o
371 (ert-deftest iedit-delete-occurrences-test ()
372 "Test functions deal with the whole occurrences"
373 (with-iedit-test-fixture
376 (iedit-delete-occurrences)
377 (should (string= (buffer-string) " barfoo ")))))
379 (ert-deftest iedit-toggle-buffering-test ()
380 (with-iedit-test-fixture
386 (iedit-toggle-buffering)
388 (should (string= (buffer-string)
393 (iedit-toggle-buffering)
394 (should (string= (buffer-string)
399 (should (= (point) 4))
400 (iedit-toggle-buffering)
401 (backward-delete-char 3)
402 (should (string= (buffer-string)
407 (goto-char 15) ;not in an occurrence
408 (should (null (iedit-find-current-occurrence-overlay)))
409 (iedit-toggle-buffering)
410 (should (string= (buffer-string)
416 (ert-deftest iedit-rectangle-start-test ()
417 (with-iedit-test-fixture
424 (set-mark-command nil)
427 (iedit-rectangle-mode)
428 (should (equal iedit-rectangle '(1 19))))))
430 (ert-deftest iedit-kill-rectangle-error-test ()
431 (with-iedit-test-fixture
438 (set-mark-command nil)
440 (iedit-rectangle-mode)
441 (should (iedit-same-column))
442 (should (equal iedit-rectangle '(1 22)))
443 (iedit-prev-occurrence)
445 (should (not (iedit-same-column)))
446 (should-error (iedit-kill-rectangle)))))
448 (ert-deftest iedit-kill-rectangle-test ()
449 (with-iedit-test-fixture
456 (set-mark-command nil)
458 (iedit-rectangle-mode)
459 (should (iedit-same-column))
460 (should (equal iedit-rectangle '(1 22)))
461 (iedit-kill-rectangle)
462 (should (string= (buffer-string)
467 (should (equal killed-rectangle '("foo" " fo" " b" " "))))))
469 (ert-deftest iedit-restrict-defun-test ()
470 (with-iedit-test-fixture
472 (defun foo (foo bar foo)
473 \"foo bar foobar\" nil)
474 (defun bar (bar foo bar)
475 \"bar foo barfoo\" nil)"
481 (iedit-restrict-function)
482 (should (= 1 (length iedit-occurrences-overlays)))
485 (iedit-mode-toggle-on-function)
486 (should (= 4 (length iedit-occurrences-overlays)))
491 (should (= 4 (length iedit-occurrences-overlays))))))
493 (ert-deftest iedit-transient-sensitive-test ()
494 (with-iedit-test-fixture
496 (defun foo (foo bar foo)
497 \"foo bar foobar\" nil)
498 (defun bar (bar foo bar)
499 \"bar foo barfoo\" nil)"
503 (setq iedit-transient-mark-sensitive t)
504 (transient-mark-mode -1)
507 (iedit-restrict-function)
508 (should (= 1 (length iedit-occurrences-overlays)))
512 (should (= 4 (length iedit-occurrences-overlays)))
513 (iedit-mode) ;;turn off iedit mode
517 (should (= 0 (length iedit-occurrences-overlays))))))
519 (defvar iedit-printable-test-lists
525 ("12345678901234567890123456789012345678901234567890abc" "12345678901234567890123456789012345678901234567890...")
526 ("12345678901234567890123456789012345678901234567890abc
527 abcd" "12345678901234567890123456789012345678901234567890...")))
529 (ert-deftest iedit-printable-test ()
530 (dolist (test iedit-printable-test-lists)
531 (should (string= (iedit-printable (car test)) (cadr test)))))
534 ;; (elp-instrument-list '(insert-and-inherit
537 ;; iedit-occurrence-update
538 ;; buffer-substring-no-properties
544 ;;; iedit-tests.el ends here