occur-tests.el 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. ;;; occur-tests.el --- Test suite for occur.
  2. ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
  3. ;; Author: Juri Linkov <juri@jurta.org>
  4. ;; Keywords: matching, internal
  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. (defconst occur-tests
  19. '(
  20. ;; * Test one-line matches (at bob, eob, bol, eol).
  21. ("x" 0 "\
  22. xa
  23. b
  24. cx
  25. xd
  26. xex
  27. fx
  28. " "\
  29. 5 matches for \"x\" in buffer: *temp*<2>
  30. 1:xa
  31. 3:cx
  32. 4:xd
  33. 5:xex
  34. 6:fx
  35. ")
  36. ;; * Test multi-line matches, this is the first test from
  37. ;; http://lists.gnu.org/archive/html/emacs-devel/2005-06/msg01008.html
  38. ;; where numbers are replaced with letters.
  39. ("a\na" 0 "\
  40. a
  41. a
  42. a
  43. a
  44. a
  45. " "\
  46. 2 matches for \"a^Ja\" in buffer: *temp*<2>
  47. 1:a
  48. :a
  49. 3:a
  50. :a
  51. ")
  52. ;; * Test multi-line matches, this is the second test from
  53. ;; http://lists.gnu.org/archive/html/emacs-devel/2005-06/msg01008.html
  54. ;; where numbers are replaced with letters.
  55. ("a\nb" 0 "\
  56. a
  57. b
  58. c
  59. a
  60. b
  61. " "\
  62. 2 matches for \"a^Jb\" in buffer: *temp*<2>
  63. 1:a
  64. :b
  65. 4:a
  66. :b
  67. ")
  68. ;; * Test line numbers for multi-line matches with empty last match line.
  69. ("a\n" 0 "\
  70. a
  71. c
  72. a
  73. " "\
  74. 2 matches for \"a^J\" in buffer: *temp*<2>
  75. 1:a
  76. :
  77. 4:a
  78. :
  79. ")
  80. ;; * Test multi-line matches with 3 match lines.
  81. ("x\n.x\n" 0 "\
  82. ax
  83. bx
  84. c
  85. d
  86. ex
  87. fx
  88. " "\
  89. 2 matches for \"x^J.x^J\" in buffer: *temp*<2>
  90. 1:ax
  91. :bx
  92. :c
  93. 5:ex
  94. :fx
  95. :
  96. ")
  97. ;; * Test non-overlapping context lines with matches at bob/eob.
  98. ("x" 1 "\
  99. ax
  100. b
  101. c
  102. d
  103. ex
  104. f
  105. g
  106. hx
  107. " "\
  108. 3 matches for \"x\" in buffer: *temp*<2>
  109. 1:ax
  110. :b
  111. -------
  112. :d
  113. 5:ex
  114. :f
  115. -------
  116. :g
  117. 8:hx
  118. ")
  119. ;; * Test non-overlapping context lines with matches not at bob/eob.
  120. ("x" 1 "\
  121. a
  122. bx
  123. c
  124. d
  125. ex
  126. f
  127. " "\
  128. 2 matches for \"x\" in buffer: *temp*<2>
  129. :a
  130. 2:bx
  131. :c
  132. -------
  133. :d
  134. 5:ex
  135. :f
  136. ")
  137. ;; * Test overlapping context lines with matches at bob/eob.
  138. ("x" 2 "\
  139. ax
  140. bx
  141. c
  142. dx
  143. e
  144. f
  145. gx
  146. h
  147. i
  148. j
  149. kx
  150. " "\
  151. 5 matches for \"x\" in buffer: *temp*<2>
  152. 1:ax
  153. 2:bx
  154. :c
  155. 4:dx
  156. :e
  157. :f
  158. 7:gx
  159. :h
  160. :i
  161. :j
  162. 11:kx
  163. ")
  164. ;; * Test overlapping context lines with matches not at bob/eob.
  165. ("x" 2 "\
  166. a
  167. b
  168. cx
  169. d
  170. e
  171. f
  172. gx
  173. h
  174. i
  175. " "\
  176. 2 matches for \"x\" in buffer: *temp*<2>
  177. :a
  178. :b
  179. 3:cx
  180. :d
  181. :e
  182. :f
  183. 7:gx
  184. :h
  185. :i
  186. ")
  187. ;; * Test overlapping context lines with empty first and last line..
  188. ("x" 2 "\
  189. b
  190. cx
  191. d
  192. e
  193. f
  194. gx
  195. h
  196. " "\
  197. 2 matches for \"x\" in buffer: *temp*<2>
  198. :
  199. :b
  200. 3:cx
  201. :d
  202. :e
  203. :f
  204. 7:gx
  205. :h
  206. :
  207. ")
  208. ;; * Test multi-line overlapping context lines.
  209. ("x\n.x" 2 "\
  210. ax
  211. bx
  212. c
  213. d
  214. ex
  215. fx
  216. g
  217. h
  218. i
  219. jx
  220. kx
  221. " "\
  222. 3 matches for \"x^J.x\" in buffer: *temp*<2>
  223. 1:ax
  224. :bx
  225. :c
  226. :d
  227. 5:ex
  228. :fx
  229. :g
  230. :h
  231. :i
  232. 10:jx
  233. :kx
  234. ")
  235. ;; * Test multi-line non-overlapping context lines.
  236. ("x\n.x" 2 "\
  237. ax
  238. bx
  239. c
  240. d
  241. e
  242. f
  243. gx
  244. hx
  245. " "\
  246. 2 matches for \"x^J.x\" in buffer: *temp*<2>
  247. 1:ax
  248. :bx
  249. :c
  250. :d
  251. -------
  252. :e
  253. :f
  254. 7:gx
  255. :hx
  256. ")
  257. ;; * Test non-overlapping negative (before-context) lines.
  258. ("x" -2 "\
  259. a
  260. bx
  261. c
  262. d
  263. e
  264. fx
  265. g
  266. h
  267. ix
  268. " "\
  269. 3 matches for \"x\" in buffer: *temp*<2>
  270. :a
  271. 2:bx
  272. -------
  273. :d
  274. :e
  275. 6:fx
  276. -------
  277. :g
  278. :h
  279. 9:ix
  280. ")
  281. ;; * Test overlapping negative (before-context) lines.
  282. ("x" -3 "\
  283. a
  284. bx
  285. c
  286. dx
  287. e
  288. f
  289. gx
  290. h
  291. " "\
  292. 3 matches for \"x\" in buffer: *temp*<2>
  293. :a
  294. 2:bx
  295. :c
  296. 4:dx
  297. :e
  298. :f
  299. 7:gx
  300. ")
  301. )
  302. "List of tests for `occur'.
  303. Each element has the format:
  304. \(REGEXP NLINES INPUT-BUFFER-STRING OUTPUT-BUFFER-STRING).")
  305. (defun occur-test-case (test)
  306. (let ((regexp (nth 0 test))
  307. (nlines (nth 1 test))
  308. (input-buffer-string (nth 2 test))
  309. (output-buffer-string (nth 3 test)))
  310. (save-window-excursion
  311. (with-temp-buffer
  312. (insert input-buffer-string)
  313. (occur regexp nlines)
  314. (equal output-buffer-string
  315. (with-current-buffer "*Occur*"
  316. (buffer-string)))))))
  317. (ert-deftest occur-tests ()
  318. "Test the functionality of `occur'.
  319. The test data is in the `occur-tests' constant."
  320. (let ((occur-hook nil))
  321. (dolist (test occur-tests)
  322. (should (occur-test-case test)))))
  323. (provide 'occur-tests)
  324. ;;; occur-tests.el ends here