Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 513 lines (474 sloc) 13.79 kb
1b3672d @victorhge Init
authored
1 ;;; iedit-tests.el --- iedit's automatic-tests
2
3 ;; Copyright (C) 2010, 2011, 2012 Victor Ren
4
e35530f @victorhge Fix conjointed overlays overlapped problem after modification
authored
5 ;; Time-stamp: <2012-08-09 16:05:26 Victor Ren>
1b3672d @victorhge Init
authored
6 ;; Author: Victor Ren <victorhge@gmail.com>
b9d97fa @victorhge Fix iedit-blank-occurrences broken with rectangles problem
authored
7 ;; Version: 0.97
1b3672d @victorhge Init
authored
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
33 (ert-deftest iedit-compile-test ()
34 (let ((byte-compile-error-on-warn t ))
35 (should (byte-compile-file "iedit.el"))
36 (delete-file "iedit.elc" nil)))
37
38 (defun with-iedit-test-fixture (input-buffer-string body)
39 "iedit test fixture"
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
40 (let ((old-transient-mark-mode transient-mark-mode)
428be4e @victorhge Fix iedit-kill-rectangle problem
authored
41 (old-iedit-transient-sensitive iedit-transient-mark-sensitive))
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
42 (unwind-protect
43 (progn
44 (with-temp-buffer
45 (transient-mark-mode t)
428be4e @victorhge Fix iedit-kill-rectangle problem
authored
46 (setq iedit-transient-mark-sensitive t)
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
47 (insert input-buffer-string)
48 (goto-char 1)
49 (iedit-mode)
50 (funcall body))
51 (with-temp-buffer
428be4e @victorhge Fix iedit-kill-rectangle problem
authored
52 (setq iedit-transient-mark-sensitive nil)
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
53 (transient-mark-mode -1)
54 (insert input-buffer-string)
55 (goto-char 1)
56 (iedit-mode)
57 (funcall body)))
58 (transient-mark-mode old-transient-mark-mode)
428be4e @victorhge Fix iedit-kill-rectangle problem
authored
59 (setq iedit-transient-mark-sensitive old-transient-mark-mode))))
1b3672d @victorhge Init
authored
60
61 (ert-deftest iedit-mode-base-test ()
62 (with-iedit-test-fixture
63 "foo
64 foo
65 barfoo
66 foo"
67 (lambda ()
68 (should (= 3 (length iedit-occurrences-overlays)))
69 (should (string= iedit-initial-string-local "foo"))
70 (set-mark-command nil)
71 (forward-line 2)
72 (iedit-mode)
73 (should (= 2 (length iedit-occurrences-overlays)))
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
74 (should (string= iedit-initial-string-local "foo"))
1b3672d @victorhge Init
authored
75 (iedit-mode)
76 (should (null iedit-occurrences-overlays)))))
77
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
78 (ert-deftest iedit-mode-with-region-test ()
79 (with-iedit-test-fixture
80 "foobar
81 foo
82 foo
83 bar
84 foo"
85 (lambda ()
86 (iedit-mode)
87 (goto-char 1)
88 (set-mark-command nil)
89 (forward-char 3)
90 (iedit-mode)
91 (should (= 4 (length iedit-occurrences-overlays)))
92 (should (string= iedit-initial-string-local "foo"))
93 (should (null iedit-only-complete-symbol-local))
94 (goto-char 1)
95 (set-mark-command nil)
96 (forward-line 3)
97 (iedit-mode 4)
98 (should (= 1 (length iedit-occurrences-overlays))))))
e35530f @victorhge Fix conjointed overlays overlapped problem after modification
authored
99
100 (ert-deftest iedit-move-conjointed-overlays-test ()
101 (with-iedit-test-fixture
102 "foobar
103 foofoofoo
104 foofoo
105 foo"
106 (lambda ()
107 (iedit-mode)
108 (goto-char 1)
109 (set-mark-command nil)
110 (forward-char 3)
111 (iedit-mode)
112 (should (= 7 (length iedit-occurrences-overlays)))
113 (should (string= iedit-initial-string-local "foo"))
114 (should (null iedit-only-complete-symbol-local))
115 (goto-char 1)
116 (insert "123")
117 (should (string= (buffer-string)
118 "123foobar
119 123foo123foo123foo
120 123foo123foo
121 123foo"))
122 (forward-char 3)
123 (insert "456")
124 (should (string= (buffer-string)
125 "123foo456bar
126 123foo456123foo456123foo456
127 123foo456123foo456
128 123foo456")))))
129
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
130 (ert-deftest iedit-mode-start-from-isearch-test ()
131 (with-iedit-test-fixture
132 "foo
133 foo
134 barfoo
135 foo"
136 (lambda ()
137 (should (= 3 (length iedit-occurrences-overlays)))
138 (should (string= iedit-initial-string-local "foo"))
139 (iedit-mode)
140 (forward-line 2)
141 (isearch-mode t)
142 (isearch-process-search-char ?f)
143 (isearch-process-search-char ?o)
144 (isearch-process-search-char ?o)
145 (iedit-mode)
146 (should (string= iedit-initial-string-local "foo"))
147 (should (= 4 (length iedit-occurrences-overlays)))
148 (iedit-mode)
149 (should (null iedit-occurrences-overlays)))))
150
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
151 (ert-deftest iedit-mode-last-local-occurrence-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 (goto-char 15)
162 (iedit-mode 4) ; last local
163 (should (string= iedit-initial-string-local "foo"))
164 (should (= 3 (length iedit-occurrences-overlays))))))
165
166 (ert-deftest iedit-mode-last-global-occurrence-test ()
167 (with-iedit-test-fixture
168 "foo
169 foo
170 barfoo
171 foo"
172 (lambda ()
173 (should (= 3 (length iedit-occurrences-overlays)))
174 (should (string= iedit-initial-string-local "foo"))
175 (iedit-mode)
176 (with-temp-buffer
177 (insert "bar foo foo")
178 (goto-char 1)
179 (iedit-mode 16)
180 (should (string= iedit-initial-string-local "foo"))
181 (should (= 2 (length iedit-occurrences-overlays)))))))
182
183 (ert-deftest iedit-execute-last-modification-test ()
184 (with-iedit-test-fixture
185 "foo
186 foo
187 barfoo
188 foo"
189 (lambda ()
190 (should (= 3 (length iedit-occurrences-overlays)))
191 (should (string= iedit-initial-string-local "foo"))
192 (iedit-mode)
193 (with-temp-buffer
194 (insert "bar foo foo")
195 (should-error (iedit-execute-last-modification))))))
196
1b3672d @victorhge Init
authored
197 (ert-deftest iedit-movement-test ()
198 (with-iedit-test-fixture
199 "foo
200 foo
201 barfoo
202 foo "
203 (lambda ()
204 (iedit-last-occurrence)
205 (should (= (point) 24))
206 (iedit-first-occurrence)
207 (should (= (point) 1))
208 (iedit-next-occurrence)
209 (should (= (point) 7))
210 (iedit-next-occurrence)
211 (should (= (point) 24))
212 (iedit-next-occurrence)
641d062 @victorhge Update documents
authored
213 (should (= (point) 24)) ;; (should (string= (current-message) "This is the last occurrence."))
1b3672d @victorhge Init
authored
214 (iedit-next-occurrence)
641d062 @victorhge Update documents
authored
215 (should (= (point) 1)) ;; (should (string= (current-message) "Located the first occurrence."))
1b3672d @victorhge Init
authored
216 (iedit-next-occurrence)
217 (should (= (point) 7))
218 (goto-char (point-max))
219 (iedit-prev-occurrence)
220 (should (= (point) 27))
221 (iedit-prev-occurrence)
222 (should (= (point) 24))
223 (iedit-prev-occurrence)
224 (should (= (point) 7))
225 (iedit-prev-occurrence)
226 (should (= (point) 1))
227 (iedit-prev-occurrence)
641d062 @victorhge Update documents
authored
228 (should (= (point) 1)) ;; (should (string= (current-message) "This is the first occurrence."))
1b3672d @victorhge Init
authored
229 (iedit-prev-occurrence)
641d062 @victorhge Update documents
authored
230 (should (= (point) 24)) ;; (should (string= (current-message) "Located the last occurrence."))
1b3672d @victorhge Init
authored
231 )))
232
233 (ert-deftest iedit-occurrence-update-test ()
234 (with-iedit-test-fixture
235 "foo
236 foo
237 barfoo
238 foo"
239 (lambda ()
240 (insert "1")
241 (should (string= (buffer-string)
242 "1foo
243 1foo
244 barfoo
245 1foo"))
246 (backward-delete-char 1)
7479d4c @victorhge Merge branch 'topic/multi-interface'
authored
247 (should (string= (buffer-string)
248 "foo
249 foo
250 barfoo
251 foo"))
1b3672d @victorhge Init
authored
252 (capitalize-word 1)
253 (should (string= (buffer-string)
254 "Foo
255 Foo
256 barfoo
257 Foo"))
258 ;; test insert from empty
259 (iedit-delete-occurrences)
260 (insert "1")
261 (should (string= (buffer-string)
262 "1
263 1
264 barfoo
265 1")))))
266
267 (ert-deftest iedit-toggle-case-sensitive-test ()
268 (with-iedit-test-fixture
269 "foo
270 Foo
271 barfoo
272 foo"
273 (lambda ()
274 (should (= 2 (length iedit-occurrences-overlays)))
275 (iedit-toggle-case-sensitive)
276 (should (= 3 (length iedit-occurrences-overlays)))
277 (iedit-next-occurrence)
278 (iedit-toggle-case-sensitive)
279 (should (= 1 (length iedit-occurrences-overlays))))))
280
281 (ert-deftest iedit-apply-on-occurrences-test ()
282 "Test functions deal with the whole occurrences"
283 (with-iedit-test-fixture
284 "foo
285 foo
286 barfoo
287 foo"
288 (lambda ()
289 (iedit-upcase-occurrences)
290 (should (string= (buffer-string)
291 "FOO
292 FOO
293 barfoo
294 FOO"))
295 (iedit-downcase-occurrences)
7479d4c @victorhge Merge branch 'topic/multi-interface'
authored
296 (should (string= (buffer-string)
297 "foo
298 foo
299 barfoo
300 foo"))
1b3672d @victorhge Init
authored
301 (iedit-replace-occurrences "bar")
302 (should (string= (buffer-string)
303 "bar
304 bar
305 barfoo
306 bar"))
307 (iedit-number-occurrences 1)
308 (should (string= (buffer-string)
309 "1 bar
310 2 bar
311 barfoo
312 3 bar")))))
313
314
315 (ert-deftest iedit-blank-occurrences-test ()
316 "Test functions deal with the whole occurrences"
317 (with-iedit-test-fixture
318 "foo foo barfoo foo"
319 (lambda ()
320 (iedit-blank-occurrences)
321 (should (string= (buffer-string) " barfoo ")))))
322
b9d97fa @victorhge Fix iedit-blank-occurrences broken with rectangles problem
authored
323 (ert-deftest iedit-blank-occurrences-rectangle-test ()
324 "Test functions deal with the whole occurrences"
325 (with-iedit-test-fixture
326 "foo
327 foo barfoo foo"
328 (lambda ()
329 (iedit-mode) ; turn off iedit
330 (goto-char 2)
331 (set-mark-command nil)
332 (forward-char 1)
333 (forward-line 1)
334 (iedit-rectangle-mode)
335 (iedit-blank-occurrences)
336 (should (string= (buffer-string) "f o
337 oo barfoo foo")))))
338
1b3672d @victorhge Init
authored
339 (ert-deftest iedit-delete-occurrences-test ()
340 "Test functions deal with the whole occurrences"
341 (with-iedit-test-fixture
342 "foo foo barfoo foo"
343 (lambda ()
344 (iedit-delete-occurrences)
345 (should (string= (buffer-string) " barfoo ")))))
346
347 (ert-deftest iedit-toggle-buffering-test ()
348 (with-iedit-test-fixture
349 "foo
350 foo
351 barfoo
352 foo"
353 (lambda ()
354 (iedit-toggle-buffering)
355 (insert "bar")
356 (should (string= (buffer-string)
357 "barfoo
358 foo
359 barfoo
360 foo"))
361 (iedit-toggle-buffering)
362 (should (string= (buffer-string)
363 "barfoo
364 barfoo
365 barfoo
366 barfoo"))
367 (should (= (point) 4))
368 (iedit-toggle-buffering)
369 (backward-delete-char 3)
370 (should (string= (buffer-string)
371 "foo
372 barfoo
373 barfoo
374 barfoo"))
375 (goto-char 15) ;not in an occurrence
376 (should (null (iedit-find-current-occurrence-overlay)))
377 (iedit-toggle-buffering)
378 (should (string= (buffer-string)
379 "foo
380 barfoo
381 barfoo
382 barfoo")))))
383
7ebea0f @victorhge Add tests for iedit-rectangle
authored
384 (ert-deftest iedit-rectangle-start-test ()
385 (with-iedit-test-fixture
386 "foo
387 foo
388 barfoo
389 foo"
390 (lambda ()
391 (iedit-mode)
392 (set-mark-command nil)
393 (forward-char 3)
394 (forward-line 3)
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
395 (iedit-rectangle-mode)
7ebea0f @victorhge Add tests for iedit-rectangle
authored
396 (should (equal iedit-rectangle '(1 19))))))
397
428be4e @victorhge Fix iedit-kill-rectangle problem
authored
398 (ert-deftest iedit-kill-rectangle-error-test ()
399 (with-iedit-test-fixture
400 "foo
401 foo
402 barfoo
403 foo"
404 (lambda ()
405 (iedit-mode)
406 (set-mark-command nil)
407 (goto-char 22)
408 (iedit-rectangle-mode)
409 (should (iedit-same-column))
410 (should (equal iedit-rectangle '(1 22)))
411 (iedit-prev-occurrence)
412 (delete-char -1)
413 (should (not (iedit-same-column)))
414 (should-error (iedit-kill-rectangle)))))
415
7ebea0f @victorhge Add tests for iedit-rectangle
authored
416 (ert-deftest iedit-kill-rectangle-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 (goto-char 22)
428be4e @victorhge Fix iedit-kill-rectangle problem
authored
426 (iedit-rectangle-mode)
427 (should (iedit-same-column))
7ebea0f @victorhge Add tests for iedit-rectangle
authored
428 (should (equal iedit-rectangle '(1 22)))
429 (iedit-kill-rectangle)
430 (should (string= (buffer-string)
431 "
432 o
433 arfoo
434 foo"))
435 (should (equal killed-rectangle '("foo" " fo" " b" " "))))))
436
e5f083f @victorhge Add testcase for iedit-restrict-defun
authored
437 (ert-deftest iedit-restrict-defun-test ()
438 (with-iedit-test-fixture
439 "a
440 (defun foo (foo bar foo)
441 \"foo bar foobar\" nil)
442 (defun bar (bar foo bar)
443 \"bar foo barfoo\" nil)"
444 (lambda ()
445 (iedit-mode)
446 (emacs-lisp-mode)
447 (goto-char 5)
448 (iedit-mode)
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
449 (iedit-restrict-function)
e5f083f @victorhge Add testcase for iedit-restrict-defun
authored
450 (should (= 1 (length iedit-occurrences-overlays)))
451 (iedit-mode)
452 (goto-char 13)
641d062 @victorhge Update documents
authored
453 (iedit-mode-on-function)
e5f083f @victorhge Add testcase for iedit-restrict-defun
authored
454 (should (= 4 (length iedit-occurrences-overlays)))
455 (iedit-mode)
456 (iedit-mode)
457 (mark-defun)
458 (iedit-mode)
459 (should (= 4 (length iedit-occurrences-overlays))))))
460
428be4e @victorhge Fix iedit-kill-rectangle problem
authored
461 (ert-deftest iedit-transient-sensitive-test ()
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
462 (with-iedit-test-fixture
463 "a
464 (defun foo (foo bar foo)
465 \"foo bar foobar\" nil)
466 (defun bar (bar foo bar)
467 \"bar foo barfoo\" nil)"
468 (lambda ()
469 (iedit-mode)
470 (emacs-lisp-mode)
428be4e @victorhge Fix iedit-kill-rectangle problem
authored
471 (setq iedit-transient-mark-sensitive t)
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
472 (transient-mark-mode -1)
473 (goto-char 5)
474 (iedit-mode)
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
475 (iedit-restrict-function)
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
476 (should (= 1 (length iedit-occurrences-overlays)))
477 (iedit-mode)
478 (goto-char 13)
479 (iedit-mode 0)
480 (should (= 4 (length iedit-occurrences-overlays)))
481 (iedit-mode)
482 (iedit-mode)
483 (mark-defun)
484 (iedit-mode)
485 (should (= 0 (length iedit-occurrences-overlays))))))
486
1b3672d @victorhge Init
authored
487 (defvar iedit-printable-test-lists
488 '(("" "")
489 ("abc" "abc")
490 ("abc
491 bcd" "abc...")
492 ("abc\n34" "abc...")
493 ("12345678901234567890123456789012345678901234567890abc" "12345678901234567890123456789012345678901234567890...")
494 ("12345678901234567890123456789012345678901234567890abc
495 abcd" "12345678901234567890123456789012345678901234567890...")))
496
497 (ert-deftest iedit-printable-test ()
498 (dolist (test iedit-printable-test-lists)
499 (should (string= (iedit-printable (car test)) (cadr test)))))
500
501
e5f083f @victorhge Add testcase for iedit-restrict-defun
authored
502 ;; (elp-instrument-list '(insert-and-inherit
503 ;; delete-region
504 ;; goto-char
505 ;; iedit-occurrence-update
506 ;; buffer-substring-no-properties
507 ;; string=
508 ;; re-search-forward
509 ;; replace-match))
510
511
1b3672d @victorhge Init
authored
512 ;;; iedit-tests.el ends here
Something went wrong with that request. Please try again.