test-org-list.el 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. ;;; test-org-list.el --- Tests for org-list.el
  2. ;; Copyright (C) 2012 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (ert-deftest test-org-list/list-ending ()
  16. "Test if lists end at the right place."
  17. ;; With two blank lines.
  18. (org-test-with-temp-text "- item\n\n\n Text"
  19. (goto-line 4)
  20. (should-not (org-in-item-p)))
  21. ;; With text less indented than top items.
  22. (org-test-with-temp-text "- item\nText"
  23. (goto-line 2)
  24. (should-not (org-in-item-p)))
  25. ;; Though, blank lines and text indentation is ignored in blocks.
  26. (org-test-with-temp-text
  27. "- item\n #+begin_quote\n\n\nText at column 0\n #+end_quote\n Text"
  28. (goto-line 7)
  29. (should (org-in-item-p))))
  30. (ert-deftest test-org-list/list-navigation ()
  31. "Test list navigation specifications."
  32. (org-test-with-temp-text "
  33. - item A
  34. - item B
  35. - item 1
  36. - item 1.1
  37. - item 1.2
  38. - item 1.3
  39. - item 2
  40. - item X
  41. - item Y"
  42. (let ((org-list-use-circular-motion nil))
  43. ;; 1. Test `org-next-item'.
  44. ;;
  45. ;; 1.1. Should return an error if at last item in
  46. ;; a list/sub-list, unless `org-list-use-circular-motion'
  47. ;; is non-nil.
  48. (goto-line 9)
  49. (should-error (org-next-item))
  50. (let ((org-list-use-circular-motion t))
  51. (should (progn (org-next-item) t)))
  52. (goto-line 14)
  53. (should-error (org-next-item))
  54. (let ((org-list-use-circular-motion t))
  55. (should (progn (org-next-item) t)))
  56. ;; 1.2. Should jump over sub-lists.
  57. (goto-line 6)
  58. (org-next-item)
  59. (should (looking-at "- item 2"))
  60. ;; 1.3. Shouldn't move to another list.
  61. (goto-line 3)
  62. (should-error (org-next-item))
  63. (should-not (looking-at "- item 1"))
  64. ;; 1.4. Should move to the list/sub-list first item when
  65. ;; `org-list-use-circular-motion' is non-nil.
  66. (let ((org-list-use-circular-motion t))
  67. (goto-line 10)
  68. (org-next-item)
  69. (should (looking-at "- item 1"))
  70. (goto-line 9)
  71. (org-next-item)
  72. (should (looking-at " - item 1.1")))
  73. ;; 2. Test `org-previous-item'.
  74. ;;
  75. ;; 2.1. Should return an error if at first item in
  76. ;; a list/sub-list, unless `org-list-use-circular-motion is
  77. ;; non-nil.
  78. (goto-line 7)
  79. (should-error (org-previous-item))
  80. (let ((org-list-use-circular-motion t))
  81. (should (progn (org-previous-item) t)))
  82. (goto-line 13)
  83. (should-error (org-previous-item))
  84. (let ((org-list-use-circular-motion t))
  85. (should (progn (org-previous-item) t)))
  86. ;; 2.2. Should ignore sub-lists.
  87. (goto-line 10)
  88. (org-previous-item)
  89. (should (looking-at "- item 1"))
  90. ;; 2.3. Shouldn't move to another list.
  91. (goto-line 6)
  92. (should-error (org-previous-item))
  93. (should-not (looking-at "- item B"))
  94. ;; 2.4. Should move to the list/sub-list last item when
  95. ;; `org-list-use-circular-motion' is non-nil.
  96. (let ((org-list-use-circular-motion t))
  97. (goto-line 6)
  98. (org-previous-item)
  99. (should (looking-at "- item 2"))
  100. (goto-line 7)
  101. (org-previous-item)
  102. (should (looking-at " - item 1.3"))))))
  103. (ert-deftest test-org-list/indent-item ()
  104. "Test `org-indent-item' specifications."
  105. ;; 1. Error when not at an item.
  106. (org-test-with-temp-text "Paragraph."
  107. (should-error (org-indent-item)))
  108. ;; 2. Error when trying to move first item of a list.
  109. (org-test-with-temp-text "
  110. - Item 1
  111. - Item 2"
  112. (forward-line)
  113. (should-error (org-indent-item)))
  114. ;; 3. Indent a single item, not its children.
  115. (org-test-with-temp-text "
  116. - Item 1
  117. - Item 2
  118. - Item 2.1"
  119. (search-forward "- Item 2")
  120. (let (org-list-demote-modify-bullet) (org-indent-item))
  121. (should (equal (buffer-string)
  122. "
  123. - Item 1
  124. - Item 2
  125. - Item 2.1")))
  126. ;; 4. Follow `org-list-demote-modify-bullet' specifications.
  127. ;;
  128. ;; 4.1. With unordered lists.
  129. (org-test-with-temp-text "
  130. - Item 1
  131. - Item 2"
  132. (search-forward "- Item 2")
  133. (let ((org-list-demote-modify-bullet '(("-" . "+")))) (org-indent-item))
  134. (should (equal (buffer-string)
  135. "
  136. - Item 1
  137. + Item 2")))
  138. ;; 4.2. and ordered lists.
  139. (org-test-with-temp-text "
  140. 1. Item 1
  141. 2. Item 2"
  142. (search-forward "2. Item 2")
  143. (let ((org-plain-list-ordered-item-terminator t)
  144. (org-list-demote-modify-bullet '(("1." . "+"))))
  145. (org-indent-item))
  146. (should (equal (buffer-string)
  147. "
  148. 1. Item 1
  149. + Item 2")))
  150. ;; 5. When a region is selected, indent every item within.
  151. (org-test-with-temp-text "
  152. - Item 1
  153. - Item 2
  154. - Item 3
  155. "
  156. (search-forward "- Item 2")
  157. (beginning-of-line)
  158. (transient-mark-mode 1)
  159. (push-mark (point) t t)
  160. (goto-char (point-max))
  161. (let (org-list-demote-modify-bullet) (org-indent-item))
  162. (should (equal (buffer-string)
  163. "
  164. - Item 1
  165. - Item 2
  166. - Item 3
  167. "))))
  168. (ert-deftest test-org-list/indent-item-tree ()
  169. "Test `org-indent-item-tree' specifications."
  170. ;; 1. Error when not at an item.
  171. (org-test-with-temp-text "Paragraph."
  172. (should-error (org-indent-item-tree)))
  173. ;; 2. Indent item along with its children.
  174. (org-test-with-temp-text "
  175. - Item 1
  176. - Item 2
  177. - Item 2.1"
  178. (search-forward "- Item 2")
  179. (let (org-list-demote-modify-bullet) (org-indent-item-tree))
  180. (should (equal (buffer-string)
  181. "
  182. - Item 1
  183. - Item 2
  184. - Item 2.1")))
  185. ;; 3. Special case: When indenting top item, move the whole list.
  186. (org-test-with-temp-text "
  187. - Item 1
  188. - Item 2"
  189. (search-forward "- Item 1")
  190. (let (org-list-demote-modify-bullet org-odd-levels-only)
  191. (org-indent-item-tree))
  192. (should (equal (buffer-string)
  193. "
  194. - Item 1
  195. - Item 2")))
  196. ;; 4. Follow `org-list-demote-modify-bullet' specifications.
  197. ;;
  198. ;; 4.1. With unordered lists.
  199. (org-test-with-temp-text "
  200. - Item 1
  201. - Item 2
  202. + Item 2.1"
  203. (search-forward "- Item 2")
  204. (let ((org-list-demote-modify-bullet '(("-" . "+") ("+" . "-"))))
  205. (org-indent-item-tree))
  206. (should (equal (buffer-string)
  207. "
  208. - Item 1
  209. + Item 2
  210. - Item 2.1")))
  211. ;; 4.2. and ordered lists.
  212. (org-test-with-temp-text "
  213. 1. Item 1
  214. 2. Item 2
  215. + Item 2.1"
  216. (search-forward "2. Item 2")
  217. (let ((org-plain-list-ordered-item-terminator t)
  218. (org-list-demote-modify-bullet '(("1." . "+") ("+" . "1."))))
  219. (org-indent-item-tree))
  220. (should (equal (buffer-string)
  221. "
  222. 1. Item 1
  223. + Item 2
  224. 1. Item 2.1")))
  225. ;; 5. When a region is selected, indent every item within.
  226. (org-test-with-temp-text "
  227. - Item 1
  228. - Item 2
  229. - Item 2.1
  230. - Item 3
  231. - Item 3.1
  232. "
  233. (search-forward "- Item 2")
  234. (beginning-of-line)
  235. (transient-mark-mode 1)
  236. (push-mark (point) t t)
  237. (goto-char (point-max))
  238. (let (org-list-demote-modify-bullet) (org-indent-item-tree))
  239. (should (equal (buffer-string)
  240. "
  241. - Item 1
  242. - Item 2
  243. - Item 2.1
  244. - Item 3
  245. - Item 3.1
  246. "))))
  247. (ert-deftest test-org-list/outdent-item ()
  248. "Test `org-outdent-item' specifications."
  249. ;; 1. Error when not at an item.
  250. (org-test-with-temp-text "Paragraph."
  251. (should-error (org-outdent-item)))
  252. ;; 2. Error when trying to move first item of a list.
  253. (org-test-with-temp-text "
  254. - Item 1
  255. - Item 2"
  256. (forward-line)
  257. (should-error (org-outdent-item)))
  258. ;; 3. Error when trying to outdent an item without its children.
  259. (org-test-with-temp-text "
  260. - Item 1
  261. - Item 1.1
  262. - Item 1.1.1"
  263. (search-forward "- Item 1.1")
  264. (should-error (org-outdent-item)))
  265. ;; 4. Error when trying to outdent before top item.
  266. (org-test-with-temp-text "
  267. - Item 1
  268. - Item 2"
  269. (search-forward "- Item 2")
  270. (should-error (org-outdent-item)))
  271. ;; 5. When a region is selected, outdent every item within.
  272. (org-test-with-temp-text "
  273. - Item 1
  274. - Item 2
  275. - Item 3
  276. "
  277. (search-forward "- Item 2")
  278. (beginning-of-line)
  279. (transient-mark-mode 1)
  280. (push-mark (point) t t)
  281. (goto-char (point-max))
  282. (let (org-list-demote-modify-bullet) (org-outdent-item))
  283. (should (equal (buffer-string)
  284. "
  285. - Item 1
  286. - Item 2
  287. - Item 3
  288. "))))
  289. (ert-deftest test-org-list/outdent-item-tree ()
  290. "Test `org-outdent-item-tree' specifications."
  291. ;; 1. Error when not at an item.
  292. (org-test-with-temp-text "Paragraph."
  293. (should-error (org-outdent-item-tree)))
  294. ;; 2. Error when trying to outdent before top item.
  295. (org-test-with-temp-text "
  296. - Item 1
  297. - Item 2"
  298. (search-forward "- Item 2")
  299. (should-error (org-outdent-item-tree)))
  300. ;; 3. Outdent item along with its children.
  301. (org-test-with-temp-text "
  302. - Item 1
  303. - Item 2
  304. - Item 2.1"
  305. (search-forward "- Item 2")
  306. (org-outdent-item-tree)
  307. (should (equal (buffer-string)
  308. "
  309. - Item 1
  310. - Item 2
  311. - Item 2.1")))
  312. ;; 3. Special case: When outdenting top item, move the whole list.
  313. (org-test-with-temp-text "
  314. - Item 1
  315. - Item 2"
  316. (search-forward "- Item 1")
  317. (let (org-odd-levels-only) (org-outdent-item-tree))
  318. (should (equal (buffer-string)
  319. "
  320. - Item 1
  321. - Item 2")))
  322. ;; 5. When a region is selected, outdent every item within.
  323. (org-test-with-temp-text "
  324. - Item 1
  325. - Item 2
  326. - Item 2.1
  327. - Item 3
  328. - Item 3.1
  329. "
  330. (search-forward "- Item 2")
  331. (beginning-of-line)
  332. (transient-mark-mode 1)
  333. (push-mark (point) t t)
  334. (goto-char (point-max))
  335. (org-outdent-item-tree)
  336. (should (equal (buffer-string)
  337. "
  338. - Item 1
  339. - Item 2
  340. - Item 2.1
  341. - Item 3
  342. - Item 3.1
  343. "))))
  344. (provide 'test-org-list)
  345. ;;; test-org-list.el ends here