]> git.donarmstrong.com Git - lib.git/blob - emacs_el/iedit-tests.el
06d5fc90c171adc530d951c449eeb46da2906e16
[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-08-09 17:17:03 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
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)))
38
39 (defun with-iedit-test-fixture (input-buffer-string body)
40   "iedit test fixture"
41   (let ((old-transient-mark-mode transient-mark-mode)
42         (old-iedit-transient-sensitive iedit-transient-mark-sensitive))
43     (unwind-protect
44         (progn
45           (with-temp-buffer
46             (transient-mark-mode t)
47             (setq iedit-transient-mark-sensitive t)
48             (insert input-buffer-string)
49             (goto-char 1)
50             (iedit-mode)
51             (funcall body))
52           (with-temp-buffer
53             (setq iedit-transient-mark-sensitive nil)
54             (transient-mark-mode -1)
55             (insert input-buffer-string)
56             (goto-char 1)
57             (iedit-mode)
58             (funcall body)))
59       (transient-mark-mode old-transient-mark-mode)
60       (setq iedit-transient-mark-sensitive old-transient-mark-mode))))
61
62 (ert-deftest iedit-mode-base-test ()
63   (with-iedit-test-fixture
64 "foo
65   foo
66    barfoo
67    foo"
68    (lambda ()
69      (should (= 3 (length iedit-occurrences-overlays)))
70      (should (string= iedit-initial-string-local "foo"))
71      (set-mark-command nil)
72      (forward-line 2)
73      (iedit-mode)
74      (should (= 2 (length iedit-occurrences-overlays)))
75      (should (string= iedit-initial-string-local "foo"))
76      (iedit-mode)
77      (should (null iedit-occurrences-overlays)))))
78
79 (ert-deftest iedit-mode-with-region-test ()
80   (with-iedit-test-fixture
81 "foobar
82  foo
83  foo
84  bar
85 foo"
86    (lambda ()
87      (iedit-mode)
88      (goto-char 1)
89      (set-mark-command nil)
90      (forward-char 3)
91      (iedit-mode)
92      (should (= 4 (length iedit-occurrences-overlays)))
93      (should (string= iedit-initial-string-local "foo"))
94      (should (null iedit-only-complete-symbol-local))
95      (goto-char 1)
96      (set-mark-command nil)
97      (forward-line 3)
98      (iedit-mode 4)
99      (should (= 1 (length iedit-occurrences-overlays))))))
100
101 (ert-deftest iedit-move-conjointed-overlays-test ()
102   (with-iedit-test-fixture
103 "foobar
104  foofoofoo
105  foofoo
106  foo"
107    (lambda ()
108      (iedit-mode)
109      (goto-char 1)
110      (set-mark-command nil)
111      (forward-char 3)
112      (iedit-mode)
113      (should (= 7 (length iedit-occurrences-overlays)))
114      (should (string= iedit-initial-string-local "foo"))
115      (should (null iedit-only-complete-symbol-local))
116      (goto-char 1)
117      (insert "123")
118      (should (string= (buffer-string)
119 "123foobar
120  123foo123foo123foo
121  123foo123foo
122  123foo"))
123      (forward-char 3)
124      (insert "456")
125      (should (string= (buffer-string)
126 "123foo456bar
127  123foo456123foo456123foo456
128  123foo456123foo456
129  123foo456")))))
130
131 (ert-deftest iedit-mode-start-from-isearch-test ()
132   (with-iedit-test-fixture
133 "foo
134   foo
135    barfoo
136    foo"
137    (lambda ()
138      (should (= 3 (length iedit-occurrences-overlays)))
139      (should (string= iedit-initial-string-local "foo"))
140      (iedit-mode)
141      (forward-line 2)
142      (isearch-mode t)
143      (isearch-process-search-char ?f)
144      (isearch-process-search-char ?o)
145      (isearch-process-search-char ?o)
146      (iedit-mode)
147      (should (string= iedit-initial-string-local "foo"))
148      (should (= 4 (length iedit-occurrences-overlays)))
149      (iedit-mode)
150      (should (null iedit-occurrences-overlays)))))
151
152 (ert-deftest iedit-mode-last-local-occurrence-test ()
153   (with-iedit-test-fixture
154 "foo
155   foo
156    barfoo
157    foo"
158    (lambda ()
159      (should (= 3 (length iedit-occurrences-overlays)))
160      (should (string= iedit-initial-string-local "foo"))
161      (iedit-mode)
162      (goto-char 15)
163      (iedit-mode 4) ; last local
164      (should (string= iedit-initial-string-local "foo"))
165      (should (= 3 (length iedit-occurrences-overlays))))))
166
167 (ert-deftest iedit-mode-last-global-occurrence-test ()
168   (with-iedit-test-fixture
169 "foo
170   foo
171    barfoo
172    foo"
173    (lambda ()
174      (should (= 3 (length iedit-occurrences-overlays)))
175      (should (string= iedit-initial-string-local "foo"))
176      (iedit-mode)
177      (with-temp-buffer
178        (insert "bar foo foo")
179        (goto-char 1)
180        (iedit-mode 16)
181      (should (string= iedit-initial-string-local "foo"))
182      (should (= 2 (length iedit-occurrences-overlays)))))))
183
184 (ert-deftest iedit-execute-last-modification-test ()
185   (with-iedit-test-fixture
186 "foo
187   foo
188    barfoo
189    foo"
190    (lambda ()
191      (should (= 3 (length iedit-occurrences-overlays)))
192      (should (string= iedit-initial-string-local "foo"))
193      (iedit-mode)
194      (with-temp-buffer
195        (insert "bar foo foo")
196        (should-error (iedit-execute-last-modification))))))
197
198 (ert-deftest iedit-movement-test ()
199   (with-iedit-test-fixture
200 "foo
201   foo
202    barfoo
203    foo "
204    (lambda ()
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."))
232      )))
233
234 (ert-deftest iedit-occurrence-update-test ()
235   (with-iedit-test-fixture
236 "foo
237   foo
238    barfoo
239    foo"
240    (lambda ()
241      (insert "1")
242      (should (string= (buffer-string)
243 "1foo
244   1foo
245    barfoo
246    1foo"))
247      (backward-delete-char 1)
248      (should (string= (buffer-string)
249 "foo
250   foo
251    barfoo
252    foo"))
253      (capitalize-word 1)
254      (should (string= (buffer-string)
255 "Foo
256   Foo
257    barfoo
258    Foo"))
259      ;; test insert from empty
260      (iedit-delete-occurrences)
261      (insert "1")
262      (should (string= (buffer-string)
263 "1
264   1
265    barfoo
266    1")))))
267
268 (ert-deftest iedit-aborting-test ()
269   (with-iedit-test-fixture
270 "foo
271   foo
272    barfoo
273    foo"
274    (lambda ()
275      (kill-region (point) (+ 4 (point)))
276      (should (string= (buffer-string)
277 "  foo
278    barfoo
279    foo")))))
280
281 (ert-deftest iedit-toggle-case-sensitive-test ()
282   (with-iedit-test-fixture
283 "foo
284   Foo
285    barfoo
286    foo"
287    (lambda ()
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))))))
294
295 (ert-deftest iedit-apply-on-occurrences-test ()
296   "Test functions deal with the whole occurrences"
297   (with-iedit-test-fixture
298 "foo
299   foo
300    barfoo
301    foo"
302    (lambda ()
303      (iedit-upcase-occurrences)
304      (should (string= (buffer-string)
305 "FOO
306   FOO
307    barfoo
308    FOO"))
309      (iedit-downcase-occurrences)
310      (should (string= (buffer-string)
311 "foo
312   foo
313    barfoo
314    foo"))
315      (iedit-replace-occurrences "bar")
316      (should (string= (buffer-string)
317 "bar
318   bar
319    barfoo
320    bar"))
321      (iedit-number-occurrences 1)
322      (should (string= (buffer-string)
323 "1 bar
324   2 bar
325    barfoo
326    3 bar")))))
327
328 (ert-deftest iedit-blank-occurrences-test ()
329   "Test functions deal with the whole occurrences"
330   (with-iedit-test-fixture
331 "foo foo barfoo foo"
332    (lambda ()
333      (iedit-blank-occurrences)
334      (should (string= (buffer-string) "        barfoo    ")))))
335
336 (ert-deftest iedit-blank-occurrences-rectangle-test ()
337   "Test functions deal with the whole occurrences"
338   (with-iedit-test-fixture
339 "foo
340  foo barfoo foo"
341    (lambda ()
342      (iedit-mode) ; turn off iedit
343      (goto-char 2)
344      (set-mark-command nil)
345      (forward-char 1)
346      (forward-line 1)
347      (iedit-rectangle-mode)
348      (iedit-blank-occurrences)
349      (should (string= (buffer-string) "f o
350   oo barfoo foo")))))
351
352 (ert-deftest iedit-delete-occurrences-test ()
353   "Test functions deal with the whole occurrences"
354   (with-iedit-test-fixture
355 "foo foo barfoo foo"
356    (lambda ()
357      (iedit-delete-occurrences)
358      (should (string= (buffer-string) "  barfoo ")))))
359
360 (ert-deftest iedit-toggle-buffering-test ()
361   (with-iedit-test-fixture
362 "foo
363  foo
364   barfoo
365     foo"
366    (lambda ()
367      (iedit-toggle-buffering)
368      (insert "bar")
369      (should (string= (buffer-string)
370 "barfoo
371  foo
372   barfoo
373     foo"))
374      (iedit-toggle-buffering)
375      (should (string= (buffer-string)
376 "barfoo
377  barfoo
378   barfoo
379     barfoo"))
380      (should (= (point) 4))
381      (iedit-toggle-buffering)
382      (backward-delete-char 3)
383      (should (string= (buffer-string)
384 "foo
385  barfoo
386   barfoo
387     barfoo"))
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)
392 "foo
393  barfoo
394   barfoo
395     barfoo")))))
396
397 (ert-deftest iedit-rectangle-start-test ()
398   (with-iedit-test-fixture
399 "foo
400  foo
401   barfoo
402     foo"
403    (lambda ()
404    (iedit-mode)
405    (set-mark-command nil)
406    (forward-char 3)
407    (forward-line 3)
408    (iedit-rectangle-mode)
409    (should (equal iedit-rectangle '(1 19))))))
410
411 (ert-deftest iedit-kill-rectangle-error-test ()
412   (with-iedit-test-fixture
413 "foo
414  foo
415   barfoo
416     foo"
417    (lambda ()
418    (iedit-mode)
419    (set-mark-command nil)
420    (goto-char 22)
421    (iedit-rectangle-mode)
422    (should (iedit-same-column))
423    (should (equal iedit-rectangle '(1 22)))
424    (iedit-prev-occurrence)
425    (delete-char -1)
426    (should (not (iedit-same-column)))
427    (should-error (iedit-kill-rectangle)))))
428
429 (ert-deftest iedit-kill-rectangle-test ()
430   (with-iedit-test-fixture
431 "foo
432  foo
433   barfoo
434     foo"
435    (lambda ()
436    (iedit-mode)
437    (set-mark-command nil)
438    (goto-char 22)
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)
444 "
445 o
446 arfoo
447  foo"))
448  (should (equal killed-rectangle '("foo" " fo" "  b" "   "))))))
449
450 (ert-deftest iedit-restrict-defun-test ()
451   (with-iedit-test-fixture
452 "a
453 (defun foo (foo bar foo)
454 \"foo bar foobar\" nil)
455 (defun bar (bar foo bar)
456   \"bar foo barfoo\" nil)"
457    (lambda ()
458       (iedit-mode)
459       (emacs-lisp-mode)
460       (goto-char 5)
461       (iedit-mode)
462       (iedit-restrict-function)
463       (should (= 1 (length iedit-occurrences-overlays)))
464       (iedit-mode)
465       (goto-char 13)
466       (iedit-mode-on-function)
467       (should (= 4 (length iedit-occurrences-overlays)))
468       (iedit-mode)
469       (iedit-mode)
470       (mark-defun)
471       (iedit-mode)
472       (should (= 4 (length iedit-occurrences-overlays))))))
473
474 (ert-deftest iedit-transient-sensitive-test ()
475   (with-iedit-test-fixture
476 "a
477 (defun foo (foo bar foo)
478 \"foo bar foobar\" nil)
479 (defun bar (bar foo bar)
480   \"bar foo barfoo\" nil)"
481    (lambda ()
482       (iedit-mode)
483       (emacs-lisp-mode)
484       (setq iedit-transient-mark-sensitive t)
485       (transient-mark-mode -1)
486       (goto-char 5)
487       (iedit-mode)
488       (iedit-restrict-function)
489       (should (= 1 (length iedit-occurrences-overlays)))
490       (iedit-mode)
491       (goto-char 13)
492       (iedit-mode 0)
493       (should (= 4 (length iedit-occurrences-overlays)))
494       (iedit-mode) ;;turn off iedit mode
495       (iedit-mode)
496       (mark-defun)
497       (iedit-mode)
498       (should (= 0 (length iedit-occurrences-overlays))))))
499
500 (defvar iedit-printable-test-lists
501   '(("" "")
502     ("abc" "abc")
503     ("abc
504 bcd" "abc...")
505     ("abc\n34" "abc...")
506     ("12345678901234567890123456789012345678901234567890abc" "12345678901234567890123456789012345678901234567890...")
507     ("12345678901234567890123456789012345678901234567890abc
508 abcd" "12345678901234567890123456789012345678901234567890...")))
509
510 (ert-deftest iedit-printable-test ()
511   (dolist (test iedit-printable-test-lists)
512     (should (string= (iedit-printable (car test)) (cadr test)))))
513
514
515 ;; (elp-instrument-list '(insert-and-inherit
516 ;;                        delete-region
517 ;;                        goto-char
518 ;;                        iedit-occurrence-update
519 ;;                        buffer-substring-no-properties
520 ;;                        string=
521 ;;                        re-search-forward
522 ;;                        replace-match))
523
524
525 ;;; iedit-tests.el ends here