]> git.donarmstrong.com Git - lib.git/blob - emacs_el/iedit-tests.el
fix missing ) for org-mode
[lib.git] / emacs_el / iedit-tests.el
1 ;;; iedit-tests.el --- iedit's automatic-tests
2
3 ;; Copyright (C) 2010, 2011, 2012 Victor Ren
4
5 ;; Time-stamp: <2012-10-22 14:01:57 Victor Ren>
6 ;; Author: Victor Ren <victorhge@gmail.com>
7 ;; Version: 0.97
8 ;; X-URL: http://www.emacswiki.org/emacs/Iedit
9
10 ;; This file is not part of GNU Emacs, but it is distributed under
11 ;; the same terms as GNU Emacs.
12
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.
17
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.
22
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/>.
25
26 ;;; Commentary:
27
28 ;; This file is part of iedit.
29
30 ;;; Code:
31 (require 'ert)
32 (require 'iedit)
33 (require 'iedit-rect)
34
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)))
39
40 (defun with-iedit-test-fixture (input-buffer-string body)
41   "iedit test fixture"
42   (let ((old-transient-mark-mode transient-mark-mode)
43         (old-iedit-transient-sensitive iedit-transient-mark-sensitive))
44     (unwind-protect
45         (progn
46           (with-temp-buffer
47             (transient-mark-mode t)
48             (setq iedit-transient-mark-sensitive t)
49             (insert input-buffer-string)
50             (goto-char 1)
51             (iedit-mode)
52             (funcall body))
53           (with-temp-buffer
54             (setq iedit-transient-mark-sensitive nil)
55             (transient-mark-mode -1)
56             (insert input-buffer-string)
57             (goto-char 1)
58             (iedit-mode)
59             (funcall body)))
60       (transient-mark-mode old-transient-mark-mode)
61       (setq iedit-transient-mark-sensitive old-transient-mark-mode))))
62
63 (ert-deftest iedit-mode-base-test ()
64   (with-iedit-test-fixture
65 "foo
66   foo
67    barfoo
68    foo"
69    (lambda ()
70      (should (= 3 (length iedit-occurrences-overlays)))
71      (should (string= iedit-initial-string-local "foo"))
72      (set-mark-command nil)
73      (forward-line 2)
74      (iedit-mode)
75      (should (= 2 (length iedit-occurrences-overlays)))
76      (should (string= iedit-initial-string-local "foo"))
77      (iedit-mode)
78      (should (null iedit-occurrences-overlays)))))
79
80 (ert-deftest iedit-mode-with-region-test ()
81   (with-iedit-test-fixture
82 "foobar
83  foo
84  foo
85  bar
86 foo"
87    (lambda ()
88      (iedit-mode)
89      (goto-char 1)
90      (set-mark-command nil)
91      (forward-char 3)
92      (iedit-mode)
93      (should (= 4 (length iedit-occurrences-overlays)))
94      (should (string= iedit-initial-string-local "foo"))
95      (should (null iedit-only-complete-symbol-local))
96      (goto-char 1)
97      (set-mark-command nil)
98      (forward-line 3)
99      (iedit-mode 4)
100      (should (= 1 (length iedit-occurrences-overlays))))))
101
102 (ert-deftest iedit-move-conjointed-overlays-test ()
103   (with-iedit-test-fixture
104 "foobar
105  foofoofoo
106  foofoo
107  foo"
108    (lambda ()
109      (iedit-mode)
110      (goto-char 1)
111      (set-mark-command nil)
112      (forward-char 3)
113      (iedit-mode)
114      (should (= 7 (length iedit-occurrences-overlays)))
115      (should (string= iedit-initial-string-local "foo"))
116      (should (null iedit-only-complete-symbol-local))
117      (goto-char 1)
118      (insert "123")
119      (should (string= (buffer-string)
120 "123foobar
121  123foo123foo123foo
122  123foo123foo
123  123foo"))
124      (forward-char 3)
125      (insert "456")
126      (should (string= (buffer-string)
127 "123foo456bar
128  123foo456123foo456123foo456
129  123foo456123foo456
130  123foo456")))))
131
132 (ert-deftest iedit-overlay-at-end-of-buffer ()
133   (with-iedit-test-fixture
134    "foo
135 foo"
136    (lambda ()
137      (iedit-mode)
138      (highlight-changes-mode 1)
139      (goto-char (point-min))
140      (goto-char (point-at-eol))
141      (iedit-mode)
142      (delete-region (point) (1- (point)))
143      (should (string= (buffer-string)
144                       "fo
145 fo"))
146      (insert "b")
147      (should (string= (buffer-string)
148                       "fob
149 fob")))))
150
151 (ert-deftest iedit-mode-start-from-isearch-test ()
152   (with-iedit-test-fixture
153 "foo
154   foo
155    barfoo
156    foo"
157    (lambda ()
158      (should (= 3 (length iedit-occurrences-overlays)))
159      (should (string= iedit-initial-string-local "foo"))
160      (iedit-mode)
161      (forward-line 2)
162      (isearch-mode t)
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)))
169      (iedit-mode)
170      (should (null iedit-occurrences-overlays)))))
171
172 (ert-deftest iedit-mode-last-local-occurrence-test ()
173   (with-iedit-test-fixture
174 "foo
175   foo
176    barfoo
177    foo"
178    (lambda ()
179      (should (= 3 (length iedit-occurrences-overlays)))
180      (should (string= iedit-initial-string-local "foo"))
181      (iedit-mode)
182      (goto-char 15)
183      (iedit-mode 4) ; last local
184      (should (string= iedit-initial-string-local "foo"))
185      (should (= 3 (length iedit-occurrences-overlays))))))
186
187 (ert-deftest iedit-mode-last-global-occurrence-test ()
188   (with-iedit-test-fixture
189 "foo
190   foo
191    barfoo
192    foo"
193    (lambda ()
194      (should (= 3 (length iedit-occurrences-overlays)))
195      (should (string= iedit-initial-string-local "foo"))
196      (iedit-mode)
197      (with-temp-buffer
198        (insert "bar foo foo")
199        (goto-char 1)
200        (iedit-mode 16)
201      (should (string= iedit-initial-string-local "foo"))
202      (should (= 2 (length iedit-occurrences-overlays)))))))
203
204 (ert-deftest iedit-execute-last-modification-test ()
205   (with-iedit-test-fixture
206 "foo
207   foo
208    barfoo
209    foo"
210    (lambda ()
211      (should (= 3 (length iedit-occurrences-overlays)))
212      (should (string= iedit-initial-string-local "foo"))
213      (iedit-mode)
214      (with-temp-buffer
215        (insert "bar foo foo")
216        (should-error (iedit-execute-last-modification))))))
217
218 (ert-deftest iedit-movement-test ()
219   (with-iedit-test-fixture
220 "foo
221   foo
222    barfoo
223    foo "
224    (lambda ()
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."))
252      )))
253
254 (ert-deftest iedit-occurrence-update-test ()
255   (with-iedit-test-fixture
256 "foo
257   foo
258    barfoo
259    foo"
260    (lambda ()
261      (insert "1")
262      (should (string= (buffer-string)
263 "1foo
264   1foo
265    barfoo
266    1foo"))
267      (backward-delete-char 1)
268      (should (string= (buffer-string)
269 "foo
270   foo
271    barfoo
272    foo"))
273      (capitalize-word 1)
274      (should (string= (buffer-string)
275 "Foo
276   Foo
277    barfoo
278    Foo"))
279      ;; test insert from empty
280      (iedit-delete-occurrences)
281      (insert "1")
282      (should (string= (buffer-string)
283 "1
284   1
285    barfoo
286    1")))))
287
288 (ert-deftest iedit-aborting-test ()
289   (with-iedit-test-fixture
290 "foo
291   foo
292    barfoo
293    foo"
294    (lambda ()
295      (kill-region (point) (+ 4 (point)))
296      (should (string= (buffer-string)
297 "  foo
298    barfoo
299    foo")))))
300
301 (ert-deftest iedit-toggle-case-sensitive-test ()
302   (with-iedit-test-fixture
303 "foo
304   Foo
305    barfoo
306    foo"
307    (lambda ()
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))))))
314
315 (ert-deftest iedit-apply-on-occurrences-test ()
316   "Test functions deal with the whole occurrences"
317   (with-iedit-test-fixture
318 "foo
319   foo
320    barfoo
321    foo"
322    (lambda ()
323      (iedit-upcase-occurrences)
324      (should (string= (buffer-string)
325 "FOO
326   FOO
327    barfoo
328    FOO"))
329      (iedit-downcase-occurrences)
330      (should (string= (buffer-string)
331 "foo
332   foo
333    barfoo
334    foo"))
335      (iedit-replace-occurrences "bar")
336      (should (string= (buffer-string)
337 "bar
338   bar
339    barfoo
340    bar"))
341      (iedit-number-occurrences 1)
342      (should (string= (buffer-string)
343 "1 bar
344   2 bar
345    barfoo
346    3 bar")))))
347
348 (ert-deftest iedit-blank-occurrences-test ()
349   "Test functions deal with the whole occurrences"
350   (with-iedit-test-fixture
351 "foo foo barfoo foo"
352    (lambda ()
353      (iedit-blank-occurrences)
354      (should (string= (buffer-string) "        barfoo    ")))))
355
356 (ert-deftest iedit-blank-occurrences-rectangle-test ()
357   "Test functions deal with the whole occurrences"
358   (with-iedit-test-fixture
359 "foo
360  foo barfoo foo"
361    (lambda ()
362      (iedit-mode) ; turn off iedit
363      (goto-char 2)
364      (set-mark-command nil)
365      (goto-char 7)
366      (iedit-rectangle-mode)
367      (iedit-blank-occurrences)
368      (should (string= (buffer-string) "f o
369   oo barfoo foo")))))
370
371 (ert-deftest iedit-delete-occurrences-test ()
372   "Test functions deal with the whole occurrences"
373   (with-iedit-test-fixture
374 "foo foo barfoo foo"
375    (lambda ()
376      (iedit-delete-occurrences)
377      (should (string= (buffer-string) "  barfoo ")))))
378
379 (ert-deftest iedit-toggle-buffering-test ()
380   (with-iedit-test-fixture
381 "foo
382  foo
383   barfoo
384     foo"
385    (lambda ()
386      (iedit-toggle-buffering)
387      (insert "bar")
388      (should (string= (buffer-string)
389 "barfoo
390  foo
391   barfoo
392     foo"))
393      (iedit-toggle-buffering)
394      (should (string= (buffer-string)
395 "barfoo
396  barfoo
397   barfoo
398     barfoo"))
399      (should (= (point) 4))
400      (iedit-toggle-buffering)
401      (backward-delete-char 3)
402      (should (string= (buffer-string)
403 "foo
404  barfoo
405   barfoo
406     barfoo"))
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)
411 "foo
412  barfoo
413   barfoo
414     barfoo")))))
415
416 (ert-deftest iedit-rectangle-start-test ()
417   (with-iedit-test-fixture
418 "foo
419  foo
420   barfoo
421     foo"
422    (lambda ()
423    (iedit-mode)
424    (set-mark-command nil)
425    (forward-char 3)
426    (forward-line 3)
427    (iedit-rectangle-mode)
428    (should (equal iedit-rectangle '(1 19))))))
429
430 (ert-deftest iedit-kill-rectangle-error-test ()
431   (with-iedit-test-fixture
432 "foo
433  foo
434   barfoo
435     foo"
436    (lambda ()
437    (iedit-mode)
438    (set-mark-command nil)
439    (goto-char 22)
440    (iedit-rectangle-mode)
441    (should (iedit-same-column))
442    (should (equal iedit-rectangle '(1 22)))
443    (iedit-prev-occurrence)
444    (delete-char -1)
445    (should (not (iedit-same-column)))
446    (should-error (iedit-kill-rectangle)))))
447
448 (ert-deftest iedit-kill-rectangle-test ()
449   (with-iedit-test-fixture
450 "foo
451  foo
452   barfoo
453     foo"
454    (lambda ()
455    (iedit-mode)
456    (set-mark-command nil)
457    (goto-char 22)
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)
463 "
464 o
465 arfoo
466  foo"))
467  (should (equal killed-rectangle '("foo" " fo" "  b" "   "))))))
468
469 (ert-deftest iedit-restrict-defun-test ()
470   (with-iedit-test-fixture
471 "a
472 (defun foo (foo bar foo)
473 \"foo bar foobar\" nil)
474 (defun bar (bar foo bar)
475   \"bar foo barfoo\" nil)"
476    (lambda ()
477       (iedit-mode)
478       (emacs-lisp-mode)
479       (goto-char 5)
480       (iedit-mode)
481       (iedit-restrict-function)
482       (should (= 1 (length iedit-occurrences-overlays)))
483       (iedit-mode)
484       (goto-char 13)
485       (iedit-mode-toggle-on-function)
486       (should (= 4 (length iedit-occurrences-overlays)))
487       (iedit-mode)
488       (iedit-mode)
489       (mark-defun)
490       (iedit-mode)
491       (should (= 4 (length iedit-occurrences-overlays))))))
492
493 (ert-deftest iedit-transient-sensitive-test ()
494   (with-iedit-test-fixture
495 "a
496 (defun foo (foo bar foo)
497 \"foo bar foobar\" nil)
498 (defun bar (bar foo bar)
499   \"bar foo barfoo\" nil)"
500    (lambda ()
501       (iedit-mode)
502       (emacs-lisp-mode)
503       (setq iedit-transient-mark-sensitive t)
504       (transient-mark-mode -1)
505       (goto-char 5)
506       (iedit-mode)
507       (iedit-restrict-function)
508       (should (= 1 (length iedit-occurrences-overlays)))
509       (iedit-mode)
510       (goto-char 13)
511       (iedit-mode 0)
512       (should (= 4 (length iedit-occurrences-overlays)))
513       (iedit-mode) ;;turn off iedit mode
514       (iedit-mode)
515       (mark-defun)
516       (iedit-mode)
517       (should (= 0 (length iedit-occurrences-overlays))))))
518
519 (defvar iedit-printable-test-lists
520   '(("" "")
521     ("abc" "abc")
522     ("abc
523 bcd" "abc...")
524     ("abc\n34" "abc...")
525     ("12345678901234567890123456789012345678901234567890abc" "12345678901234567890123456789012345678901234567890...")
526     ("12345678901234567890123456789012345678901234567890abc
527 abcd" "12345678901234567890123456789012345678901234567890...")))
528
529 (ert-deftest iedit-printable-test ()
530   (dolist (test iedit-printable-test-lists)
531     (should (string= (iedit-printable (car test)) (cadr test)))))
532
533
534 ;; (elp-instrument-list '(insert-and-inherit
535 ;;                        delete-region
536 ;;                        goto-char
537 ;;                        iedit-occurrence-update
538 ;;                        buffer-substring-no-properties
539 ;;                        string=
540 ;;                        re-search-forward
541 ;;                        replace-match))
542
543
544 ;;; iedit-tests.el ends here