Prefer HTTPS to HTTP for gnu.org
[emacs.git] / test / lisp / replace-tests.el
blob06b6dd8a0a9b130151507a82ad76e1eabe62fed3
1 ;;; replace-tests.el --- tests for replace.el.
3 ;; Copyright (C) 2010-2017 Free Software Foundation, Inc.
5 ;; Author: Nicolas Richard <youngfrog@members.fsf.org>
6 ;; Author: Juri Linkov <juri@jurta.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ;;; Code:
25 (require 'ert)
27 (ert-deftest query-replace--split-string-tests ()
28 (let ((sep (propertize "\0" 'separator t)))
29 (dolist (before '("" "b"))
30 (dolist (after '("" "a"))
31 (should (equal
32 (query-replace--split-string (concat before sep after))
33 (cons before after)))
34 (should (equal
35 (query-replace--split-string (concat before "\0" after))
36 (concat before "\0" after)))))))
38 (defconst replace-occur-tests
40 ;; * Test one-line matches (at bob, eob, bol, eol).
41 ("x" 0 "\
46 xex
48 " "\
49 6 matches in 5 lines for \"x\" in buffer: *test-occur*
50 1:xa
51 3:cx
52 4:xd
53 5:xex
54 6:fx
56 ;; * Test multi-line matches, this is the first test from
57 ;; https://lists.gnu.org/archive/html/emacs-devel/2005-06/msg01008.html
58 ;; where numbers are replaced with letters.
59 ("a\na" 0 "\
65 " "\
66 2 matches for \"a\na\" in buffer: *test-occur*
67 1:a
69 3:a
72 ;; * Test multi-line matches, this is the second test from
73 ;; https://lists.gnu.org/archive/html/emacs-devel/2005-06/msg01008.html
74 ;; where numbers are replaced with letters.
75 ("a\nb" 0 "\
81 " "\
82 2 matches for \"a\nb\" in buffer: *test-occur*
83 1:a
85 4:a
88 ;; * Test line numbers for multi-line matches with empty last match line.
89 ("a\n" 0 "\
95 " "\
96 2 matches for \"a\n\" in buffer: *test-occur*
97 1:a
99 4:a
102 ;; * Test multi-line matches with 3 match lines.
103 ("x\n.x\n" 0 "\
110 " "\
111 2 matches for \"x\n.x\n\" in buffer: *test-occur*
112 1:ax
115 5:ex
119 ;; * Test non-overlapping context lines with matches at bob/eob.
120 ("x" 1 "\
129 " "\
130 3 matches for \"x\" in buffer: *test-occur*
131 1:ax
133 -------
135 5:ex
137 -------
139 8:hx
141 ;; * Test non-overlapping context lines with matches not at bob/eob.
142 ("x" 1 "\
149 " "\
150 2 matches for \"x\" in buffer: *test-occur*
152 2:bx
154 -------
156 5:ex
159 ;; * Test overlapping context lines with matches at bob/eob.
160 ("x" 2 "\
172 " "\
173 5 matches for \"x\" in buffer: *test-occur*
174 1:ax
175 2:bx
177 4:dx
180 7:gx
184 11:kx
186 ;; * Test overlapping context lines with matches not at bob/eob.
187 ("x" 2 "\
197 " "\
198 2 matches for \"x\" in buffer: *test-occur*
201 3:cx
205 7:gx
209 ;; * Test overlapping context lines with empty first and last line..
210 ("x" 2 "\
220 " "\
221 2 matches for \"x\" in buffer: *test-occur*
224 3:cx
228 7:gx
232 ;; * Test multi-line overlapping context lines.
233 ("x\n.x" 2 "\
245 " "\
246 3 matches for \"x\n.x\" in buffer: *test-occur*
247 1:ax
251 5:ex
256 10:jx
259 ;; * Test multi-line non-overlapping context lines.
260 ("x\n.x" 2 "\
269 " "\
270 2 matches for \"x\n.x\" in buffer: *test-occur*
271 1:ax
275 -------
278 7:gx
281 ;; * Test non-overlapping negative (before-context) lines.
282 ("x" -2 "\
292 " "\
293 3 matches for \"x\" in buffer: *test-occur*
295 2:bx
296 -------
299 6:fx
300 -------
303 9:ix
305 ;; * Test overlapping negative (before-context) lines.
306 ("x" -3 "\
315 " "\
316 3 matches for \"x\" in buffer: *test-occur*
318 2:bx
320 4:dx
323 7:gx
327 "List of tests for `occur'.
328 Each element has the format:
329 \(REGEXP NLINES INPUT-BUFFER-STRING OUTPUT-BUFFER-STRING).")
331 (defun replace-occur-test-case (test)
332 (let ((regexp (nth 0 test))
333 (nlines (nth 1 test))
334 (input-buffer-string (nth 2 test))
335 (temp-buffer (get-buffer-create " *test-occur*")))
336 (unwind-protect
337 (save-window-excursion
338 (with-current-buffer temp-buffer
339 (erase-buffer)
340 (insert input-buffer-string)
341 (occur regexp nlines)
342 (with-current-buffer "*Occur*"
343 (buffer-substring-no-properties (point-min) (point-max)))))
344 (and (buffer-name temp-buffer)
345 (kill-buffer temp-buffer)))))
347 (defun replace-occur-test-create (n)
348 "Create a test for element N of the `replace-occur-tests' constant."
349 (let ((testname (intern (format "occur-test-%.2d" n)))
350 (testdoc (format "Test element %d of `replace-occur-tests'." n)))
351 (eval
352 `(ert-deftest ,testname ()
353 ,testdoc
354 (let (replace-occur-hook)
355 (should (equal (replace-occur-test-case (nth ,n replace-occur-tests))
356 (nth 3 (nth ,n replace-occur-tests)))))))))
358 (dotimes (i (length replace-occur-tests))
359 (replace-occur-test-create i))
361 (defun replace-tests--query-replace-undo (&optional comma)
362 (with-temp-buffer
363 (insert "111")
364 (goto-char 1)
365 (let ((count 0))
366 ;; Don't wait for user input.
367 (cl-letf (((symbol-function 'read-event)
368 (lambda (&rest args)
369 (cl-incf count)
370 (let ((val (pcase count
371 ('2 (if comma ?, ?\s)) ; replace and: ',' no move; '\s' go next
372 ('3 ?u) ; undo
373 ('4 ?q) ; exit
374 (_ ?\s)))) ; replace current and go next
375 val))))
376 (perform-replace "1" "2" t nil nil)))
377 (buffer-string)))
379 (ert-deftest query-replace--undo ()
380 (should (string= "211" (replace-tests--query-replace-undo)))
381 (should (string= "211" (replace-tests--query-replace-undo 'comma))))
383 ;;; replace-tests.el ends here