test-org-list.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. (ert-deftest test-org-list/move-item-down ()
  345. "Test `org-move-item-down' specifications."
  346. ;; Standard test.
  347. (org-test-with-temp-text "- item 1\n- item 2"
  348. (org-move-item-down)
  349. (should (equal (buffer-string)
  350. "- item 2\n- item 1")))
  351. ;; Keep same column in item.
  352. (org-test-with-temp-text "- item 1\n- item 2"
  353. (forward-char 4)
  354. (org-move-item-down)
  355. (should (looking-at "em 1")))
  356. ;; Move sub-items.
  357. (org-test-with-temp-text "- item 1\n - sub-item 1\n- item 2"
  358. (org-move-item-down)
  359. (should (equal (buffer-string)
  360. "- item 2\n- item 1\n - sub-item 1")))
  361. ;; Preserve blank lines.
  362. (org-test-with-temp-text "- item 1\n\n- item 2"
  363. (let ((org-empty-line-terminates-plain-lists nil)) (org-move-item-down))
  364. (should (equal (buffer-string) "- item 2\n\n- item 1")))
  365. ;; Error when trying to move the last item...
  366. (org-test-with-temp-text "- item 1\n- item 2"
  367. (forward-line)
  368. (should-error (org-move-item-down)))
  369. ;; ... unless `org-list-use-circular-motion' is non-nil. In this
  370. ;; case, move to the first item.
  371. (org-test-with-temp-text "- item 1\n- item 2\n- item 3"
  372. (forward-line 2)
  373. (let ((org-list-use-circular-motion t)) (org-move-item-down))
  374. (should (equal (buffer-string) "- item 3\n- item 1\n- item 2\n")))
  375. ;; Preserve item visibility.
  376. (org-test-with-temp-text "* Headline\n- item 1\n body 1\n- item 2\n body 2"
  377. (let ((org-cycle-include-plain-lists t))
  378. (search-forward "- item 1")
  379. (org-cycle)
  380. (search-forward "- item 2")
  381. (org-cycle))
  382. (search-backward "- item 1")
  383. (org-move-item-down)
  384. (forward-line)
  385. (should (org-invisible-p2))
  386. (search-backward " body 2")
  387. (should (org-invisible-p2)))
  388. ;; Preserve children visibility.
  389. (org-test-with-temp-text "* Headline
  390. - item 1
  391. - sub-item 1
  392. sub-body 1
  393. - item 2
  394. - sub-item 2
  395. sub-body 2"
  396. (let ((org-cycle-include-plain-lists t))
  397. (search-forward "- sub-item 1")
  398. (org-cycle)
  399. (search-forward "- sub-item 2")
  400. (org-cycle))
  401. (search-backward "- item 1")
  402. (org-move-item-down)
  403. (search-forward "sub-body 1")
  404. (should (org-invisible-p2))
  405. (search-backward "sub-body 2")
  406. (should (org-invisible-p2)))
  407. ;; Preserve contents visibility.
  408. (org-test-with-temp-text "
  409. - item 1
  410. #+BEGIN_CENTER
  411. Text1
  412. #+END_CENTER
  413. - item 2
  414. #+BEGIN_CENTER
  415. Text2
  416. #+END_CENTER"
  417. (org-hide-block-all)
  418. (search-forward "- item 1")
  419. (org-move-item-down)
  420. (search-forward "Text1")
  421. (should (org-invisible-p2))
  422. (search-backward "Text2")
  423. (should (org-invisible-p2))))
  424. (ert-deftest test-org-list/move-item-up ()
  425. "Test `org-move-item-up' specifications."
  426. ;; Standard test.
  427. (org-test-with-temp-text "- item 1\n- item 2"
  428. (forward-line)
  429. (org-move-item-up)
  430. (should (equal (buffer-string)
  431. "- item 2\n- item 1")))
  432. ;; Keep same column in item.
  433. (org-test-with-temp-text "- item 1\n- item 2"
  434. (forward-line)
  435. (forward-char 4)
  436. (org-move-item-up)
  437. (should (looking-at "em 2")))
  438. ;; Move sub-items.
  439. (org-test-with-temp-text "- item 1\n- item 2\n - sub-item 2"
  440. (forward-line)
  441. (org-move-item-up)
  442. (should (equal (buffer-string)
  443. "- item 2\n - sub-item 2\n- item 1")))
  444. ;; Preserve blank lines.
  445. (org-test-with-temp-text "- item 1\n\n- item 2"
  446. (search-forward "- item 2")
  447. (let ((org-empty-line-terminates-plain-lists nil)) (org-move-item-up))
  448. (should (equal (buffer-string) "- item 2\n\n- item 1")))
  449. ;; Error when trying to move the first item...
  450. (org-test-with-temp-text "- item 1\n- item 2"
  451. (should-error (org-move-item-up)))
  452. ;; ... unless `org-list-use-circular-motion' is non-nil. In this
  453. ;; case, move to the first item.
  454. (org-test-with-temp-text "- item 1\n- item 2\n- item 3"
  455. (let ((org-list-use-circular-motion t)) (org-move-item-up))
  456. (should (equal (buffer-string) "- item 2\n- item 3\n- item 1")))
  457. ;; Preserve item visibility.
  458. (org-test-with-temp-text "* Headline\n- item 1\n body 1\n- item 2\n body 2"
  459. (let ((org-cycle-include-plain-lists t))
  460. (search-forward "- item 1")
  461. (org-cycle)
  462. (search-forward "- item 2")
  463. (org-cycle))
  464. (org-move-item-up)
  465. (forward-line)
  466. (should (org-invisible-p2))
  467. (search-forward " body 1")
  468. (should (org-invisible-p2)))
  469. ;; Preserve children visibility.
  470. (org-test-with-temp-text "* Headline
  471. - item 1
  472. - sub-item 1
  473. sub-body 1
  474. - item 2
  475. - sub-item 2
  476. sub-body 2"
  477. (let ((org-cycle-include-plain-lists t))
  478. (search-forward "- sub-item 1")
  479. (org-cycle)
  480. (search-forward "- sub-item 2")
  481. (org-cycle))
  482. (search-backward "- item 2")
  483. (org-move-item-up)
  484. (search-forward "sub-body 2")
  485. (should (org-invisible-p2))
  486. (search-forward "sub-body 1")
  487. (should (org-invisible-p2)))
  488. ;; Preserve contents visibility.
  489. (org-test-with-temp-text "
  490. - item 1
  491. #+BEGIN_CENTER
  492. Text1
  493. #+END_CENTER
  494. - item 2
  495. #+BEGIN_CENTER
  496. Text2
  497. #+END_CENTER"
  498. (org-hide-block-all)
  499. (search-forward "- item 2")
  500. (org-move-item-up)
  501. (search-forward "Text2")
  502. (should (org-invisible-p2))
  503. (search-forward "Text1")
  504. (should (org-invisible-p2))))
  505. (provide 'test-org-list)
  506. ;;; test-org-list.el ends here