replace-tests.el 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. ;;; replace-tests.el --- tests for replace.el.
  2. ;; Copyright (C) 2010-2017 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Richard <youngfrog@members.fsf.org>
  4. ;; Author: Juri Linkov <juri@jurta.org>
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Code:
  17. (require 'ert)
  18. (ert-deftest query-replace--split-string-tests ()
  19. (let ((sep (propertize "\0" 'separator t)))
  20. (dolist (before '("" "b"))
  21. (dolist (after '("" "a"))
  22. (should (equal
  23. (query-replace--split-string (concat before sep after))
  24. (cons before after)))
  25. (should (equal
  26. (query-replace--split-string (concat before "\0" after))
  27. (concat before "\0" after)))))))
  28. (defconst replace-occur-tests
  29. '(
  30. ;; * Test one-line matches (at bob, eob, bol, eol).
  31. ("x" 0 "\
  32. xa
  33. b
  34. cx
  35. xd
  36. xex
  37. fx
  38. " "\
  39. 6 matches in 5 lines for \"x\" in buffer: *test-occur*
  40. 1:xa
  41. 3:cx
  42. 4:xd
  43. 5:xex
  44. 6:fx
  45. ")
  46. ;; * Test multi-line matches, this is the first test from
  47. ;; http://lists.gnu.org/archive/html/emacs-devel/2005-06/msg01008.html
  48. ;; where numbers are replaced with letters.
  49. ("a\na" 0 "\
  50. a
  51. a
  52. a
  53. a
  54. a
  55. " "\
  56. 2 matches for \"a\na\" in buffer: *test-occur*
  57. 1:a
  58. :a
  59. 3:a
  60. :a
  61. ")
  62. ;; * Test multi-line matches, this is the second test from
  63. ;; http://lists.gnu.org/archive/html/emacs-devel/2005-06/msg01008.html
  64. ;; where numbers are replaced with letters.
  65. ("a\nb" 0 "\
  66. a
  67. b
  68. c
  69. a
  70. b
  71. " "\
  72. 2 matches for \"a\nb\" in buffer: *test-occur*
  73. 1:a
  74. :b
  75. 4:a
  76. :b
  77. ")
  78. ;; * Test line numbers for multi-line matches with empty last match line.
  79. ("a\n" 0 "\
  80. a
  81. c
  82. a
  83. " "\
  84. 2 matches for \"a\n\" in buffer: *test-occur*
  85. 1:a
  86. :
  87. 4:a
  88. :
  89. ")
  90. ;; * Test multi-line matches with 3 match lines.
  91. ("x\n.x\n" 0 "\
  92. ax
  93. bx
  94. c
  95. d
  96. ex
  97. fx
  98. " "\
  99. 2 matches for \"x\n.x\n\" in buffer: *test-occur*
  100. 1:ax
  101. :bx
  102. :c
  103. 5:ex
  104. :fx
  105. :
  106. ")
  107. ;; * Test non-overlapping context lines with matches at bob/eob.
  108. ("x" 1 "\
  109. ax
  110. b
  111. c
  112. d
  113. ex
  114. f
  115. g
  116. hx
  117. " "\
  118. 3 matches for \"x\" in buffer: *test-occur*
  119. 1:ax
  120. :b
  121. -------
  122. :d
  123. 5:ex
  124. :f
  125. -------
  126. :g
  127. 8:hx
  128. ")
  129. ;; * Test non-overlapping context lines with matches not at bob/eob.
  130. ("x" 1 "\
  131. a
  132. bx
  133. c
  134. d
  135. ex
  136. f
  137. " "\
  138. 2 matches for \"x\" in buffer: *test-occur*
  139. :a
  140. 2:bx
  141. :c
  142. -------
  143. :d
  144. 5:ex
  145. :f
  146. ")
  147. ;; * Test overlapping context lines with matches at bob/eob.
  148. ("x" 2 "\
  149. ax
  150. bx
  151. c
  152. dx
  153. e
  154. f
  155. gx
  156. h
  157. i
  158. j
  159. kx
  160. " "\
  161. 5 matches for \"x\" in buffer: *test-occur*
  162. 1:ax
  163. 2:bx
  164. :c
  165. 4:dx
  166. :e
  167. :f
  168. 7:gx
  169. :h
  170. :i
  171. :j
  172. 11:kx
  173. ")
  174. ;; * Test overlapping context lines with matches not at bob/eob.
  175. ("x" 2 "\
  176. a
  177. b
  178. cx
  179. d
  180. e
  181. f
  182. gx
  183. h
  184. i
  185. " "\
  186. 2 matches for \"x\" in buffer: *test-occur*
  187. :a
  188. :b
  189. 3:cx
  190. :d
  191. :e
  192. :f
  193. 7:gx
  194. :h
  195. :i
  196. ")
  197. ;; * Test overlapping context lines with empty first and last line..
  198. ("x" 2 "\
  199. b
  200. cx
  201. d
  202. e
  203. f
  204. gx
  205. h
  206. " "\
  207. 2 matches for \"x\" in buffer: *test-occur*
  208. :
  209. :b
  210. 3:cx
  211. :d
  212. :e
  213. :f
  214. 7:gx
  215. :h
  216. :
  217. ")
  218. ;; * Test multi-line overlapping context lines.
  219. ("x\n.x" 2 "\
  220. ax
  221. bx
  222. c
  223. d
  224. ex
  225. fx
  226. g
  227. h
  228. i
  229. jx
  230. kx
  231. " "\
  232. 3 matches for \"x\n.x\" in buffer: *test-occur*
  233. 1:ax
  234. :bx
  235. :c
  236. :d
  237. 5:ex
  238. :fx
  239. :g
  240. :h
  241. :i
  242. 10:jx
  243. :kx
  244. ")
  245. ;; * Test multi-line non-overlapping context lines.
  246. ("x\n.x" 2 "\
  247. ax
  248. bx
  249. c
  250. d
  251. e
  252. f
  253. gx
  254. hx
  255. " "\
  256. 2 matches for \"x\n.x\" in buffer: *test-occur*
  257. 1:ax
  258. :bx
  259. :c
  260. :d
  261. -------
  262. :e
  263. :f
  264. 7:gx
  265. :hx
  266. ")
  267. ;; * Test non-overlapping negative (before-context) lines.
  268. ("x" -2 "\
  269. a
  270. bx
  271. c
  272. d
  273. e
  274. fx
  275. g
  276. h
  277. ix
  278. " "\
  279. 3 matches for \"x\" in buffer: *test-occur*
  280. :a
  281. 2:bx
  282. -------
  283. :d
  284. :e
  285. 6:fx
  286. -------
  287. :g
  288. :h
  289. 9:ix
  290. ")
  291. ;; * Test overlapping negative (before-context) lines.
  292. ("x" -3 "\
  293. a
  294. bx
  295. c
  296. dx
  297. e
  298. f
  299. gx
  300. h
  301. " "\
  302. 3 matches for \"x\" in buffer: *test-occur*
  303. :a
  304. 2:bx
  305. :c
  306. 4:dx
  307. :e
  308. :f
  309. 7:gx
  310. ")
  311. )
  312. "List of tests for `occur'.
  313. Each element has the format:
  314. \(REGEXP NLINES INPUT-BUFFER-STRING OUTPUT-BUFFER-STRING).")
  315. (defun replace-occur-test-case (test)
  316. (let ((regexp (nth 0 test))
  317. (nlines (nth 1 test))
  318. (input-buffer-string (nth 2 test))
  319. (temp-buffer (get-buffer-create " *test-occur*")))
  320. (unwind-protect
  321. (save-window-excursion
  322. (with-current-buffer temp-buffer
  323. (erase-buffer)
  324. (insert input-buffer-string)
  325. (occur regexp nlines)
  326. (with-current-buffer "*Occur*"
  327. (buffer-substring-no-properties (point-min) (point-max)))))
  328. (and (buffer-name temp-buffer)
  329. (kill-buffer temp-buffer)))))
  330. (defun replace-occur-test-create (n)
  331. "Create a test for element N of the `replace-occur-tests' constant."
  332. (let ((testname (intern (format "occur-test-%.2d" n)))
  333. (testdoc (format "Test element %d of `replace-occur-tests'." n)))
  334. (eval
  335. `(ert-deftest ,testname ()
  336. ,testdoc
  337. (let (replace-occur-hook)
  338. (should (equal (replace-occur-test-case (nth ,n replace-occur-tests))
  339. (nth 3 (nth ,n replace-occur-tests)))))))))
  340. (dotimes (i (length replace-occur-tests))
  341. (replace-occur-test-create i))
  342. ;;; replace-tests.el ends here