test-org-list.el 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  1. ;;; test-org-list.el --- Tests for org-list.el
  2. ;; Copyright (C) 2012, 2013, 2014, 2018, 2019 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 <https://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/cycle-bullet ()
  104. "Test `org-cycle-list-bullet' specifications."
  105. ;; Error when not at an item.
  106. (should-error
  107. (org-test-with-temp-text "Paragraph"
  108. (org-cycle-list-bullet)))
  109. ;; Cycle through "-", "+", "*", "1.", "1)".
  110. (org-test-with-temp-text " - item"
  111. (org-cycle-list-bullet)
  112. (should (looking-at "[ \t]+\\+"))
  113. (org-cycle-list-bullet)
  114. (should (looking-at "[ \t]+\\*"))
  115. (let ((org-plain-list-ordered-item-terminator t))
  116. (org-cycle-list-bullet))
  117. (should (looking-at "[ \t]+1\\."))
  118. (let ((org-plain-list-ordered-item-terminator t))
  119. (org-cycle-list-bullet))
  120. (should (looking-at "[ \t]+1)")))
  121. ;; Argument is a valid bullet: cycle to that bullet directly.
  122. (should
  123. (equal "1. item"
  124. (org-test-with-temp-text "- item"
  125. (let ((org-plain-list-ordered-item-terminator t))
  126. (org-cycle-list-bullet "1.")
  127. (buffer-string)))))
  128. ;; Argument is an integer N: cycle to the Nth allowed bullet.
  129. (should
  130. (equal "+ item"
  131. (org-test-with-temp-text "1. item"
  132. (let ((org-plain-list-ordered-item-terminator t))
  133. (org-cycle-list-bullet 1)
  134. (buffer-string)))))
  135. ;; Argument is `previous': cycle backwards.
  136. (should
  137. (equal "- item"
  138. (org-test-with-temp-text "+ item"
  139. (let ((org-plain-list-ordered-item-terminator t))
  140. (org-cycle-list-bullet 'previous)
  141. (buffer-string)))))
  142. ;; Do not cycle to "*" bullets when item is at column 0.
  143. (should
  144. (equal "1. item"
  145. (org-test-with-temp-text "+ item"
  146. (let ((org-plain-list-ordered-item-terminator t))
  147. (org-cycle-list-bullet)
  148. (buffer-string)))))
  149. ;; Do not cycle to numbered bullets in a description list.
  150. (should-not
  151. (equal "1. tag :: item"
  152. (org-test-with-temp-text "+ tag :: item"
  153. (let ((org-plain-list-ordered-item-terminator t))
  154. (org-cycle-list-bullet)
  155. (buffer-string)))))
  156. ;; Do not cycle to ordered item terminators if they are not allowed
  157. ;; in `org-plain-list-ordered-item-terminator'.
  158. (should
  159. (equal " 1) item"
  160. (org-test-with-temp-text " * item"
  161. (let ((org-plain-list-ordered-item-terminator 41))
  162. (org-cycle-list-bullet)
  163. (buffer-string)))))
  164. ;; When `org-list-allow-alphabetical' is non-nil, cycle to alpha bullets.
  165. (should
  166. (equal "a. item"
  167. (org-test-with-temp-text "1) item"
  168. (let ((org-plain-list-ordered-item-terminator t)
  169. (org-list-allow-alphabetical t))
  170. (org-cycle-list-bullet)
  171. (buffer-string)))))
  172. ;; Do not cycle to alpha bullets when list has more than 26
  173. ;; elements.
  174. (should-not
  175. (equal "a. item 1"
  176. (org-test-with-temp-text "1) item 1
  177. 2) item 2
  178. 3) item 3
  179. 4) item 4
  180. 5) item 5
  181. 6) item 6
  182. 7) item 7
  183. 8) item 8
  184. 9) item 9
  185. 10) item 10
  186. 11) item 11
  187. 12) item 12
  188. 13) item 13
  189. 14) item 14
  190. 15) item 15
  191. 16) item 16
  192. 17) item 17
  193. 18) item 18
  194. 19) item 19
  195. 20) item 20
  196. 21) item 21
  197. 22) item 22
  198. 23) item 23
  199. 24) item 24
  200. 25) item 25
  201. 26) item 26
  202. 27) item 27"
  203. (let ((org-plain-list-ordered-item-terminator t)
  204. (org-list-allow-alphabetical t))
  205. (org-cycle-list-bullet)
  206. (buffer-substring (point) (line-end-position))))))
  207. ;; Preserve point position while cycling.
  208. (org-test-with-temp-text "- this is test
  209. - asd
  210. - asd
  211. <point> - this is
  212. * headline
  213. "
  214. (should (= (point) 36))
  215. (dotimes (_ 10)
  216. (org-cycle-list-bullet)
  217. (should (= 1 (- (point) (line-beginning-position))))))
  218. (org-test-with-temp-text "
  219. - this is test
  220. + asd
  221. - asd
  222. <point>+ this is
  223. * headline
  224. "
  225. (should (= (point) 37))
  226. (dotimes (_ 10)
  227. (org-cycle-list-bullet)
  228. (should (= 2 (- (point) (line-beginning-position))))))
  229. (org-test-with-temp-text "
  230. - this is test
  231. + asd
  232. - asd
  233. +<point> this is
  234. * headline
  235. "
  236. (should (= (point) 38))
  237. (dotimes (_ 10)
  238. (org-cycle-list-bullet)
  239. (should (= 3 (- (point) (line-beginning-position))))))
  240. (org-test-with-temp-text "
  241. - this is test
  242. - asd
  243. - asd
  244. - <point>this is
  245. * headline
  246. "
  247. (should (= (point) 39))
  248. (dotimes (i 5)
  249. (org-cycle-list-bullet)
  250. (should
  251. (if (or (< i 2) (= i 4))
  252. (should (= 4 (- (point) (line-beginning-position))))
  253. (should (= 5 (- (point) (line-beginning-position)))))))))
  254. (ert-deftest test-org-list/indent-item ()
  255. "Test `org-indent-item' specifications."
  256. ;; Error when not at an item.
  257. (org-test-with-temp-text "Paragraph."
  258. (should-error (org-indent-item)))
  259. ;; Error when trying to move first item of a list.
  260. (should-error
  261. (org-test-with-temp-text "
  262. - Item 1
  263. - Item 2"
  264. (forward-line)
  265. (org-indent-item)))
  266. (should-error
  267. (org-test-with-temp-text "
  268. - Item 1
  269. - Item 2"
  270. (forward-line)
  271. (let ((org-list-automatic-rules nil)) (org-indent-item))))
  272. ;; Indent a single item, not its children.
  273. (should
  274. (equal "
  275. - Item 1
  276. - Item 2
  277. - Item 2.1"
  278. (org-test-with-temp-text "
  279. - Item 1
  280. - Item 2<point>
  281. - Item 2.1"
  282. (let (org-list-demote-modify-bullet) (org-indent-item))
  283. (buffer-string))))
  284. ;; Follow `org-list-demote-modify-bullet' specifications.
  285. (should
  286. (equal "
  287. - Item 1
  288. + Item 2"
  289. (org-test-with-temp-text "
  290. - Item 1
  291. - Item 2<point>"
  292. (let ((org-list-demote-modify-bullet '(("-" . "+"))))
  293. (org-indent-item))
  294. (buffer-string))))
  295. (should
  296. (equal "
  297. - [ ] list item 1
  298. + [ ] list item 2"
  299. (org-test-with-temp-text "
  300. - [ ] list item 1
  301. - [ ] list item 2<point>"
  302. (let ((org-list-demote-modify-bullet '(("-" . "+"))))
  303. (org-indent-item)
  304. (buffer-string)))))
  305. (should
  306. (equal "
  307. 1. Item 1
  308. + Item 2"
  309. (org-test-with-temp-text "
  310. 1. Item 1
  311. 2. Item 2<point>"
  312. (let ((org-plain-list-ordered-item-terminator t)
  313. (org-list-demote-modify-bullet '(("1." . "+"))))
  314. (org-indent-item))
  315. (buffer-string))))
  316. (should
  317. (equal "
  318. a. Item 1
  319. - Item 2"
  320. (org-test-with-temp-text "
  321. a. Item 1
  322. b. Item 2<point>"
  323. (let ((org-plain-list-ordered-item-terminator t)
  324. (org-list-allow-alphabetical t)
  325. (org-list-demote-modify-bullet '(("A." . "a.") ("a." . "-"))))
  326. (org-indent-item))
  327. (buffer-string))))
  328. ;; When a region is selected, indent every item within.
  329. (should
  330. (equal "
  331. - Item 1
  332. - Item 2
  333. - Item 3
  334. "
  335. (org-test-with-temp-text "
  336. - Item 1
  337. <point>- Item 2
  338. - Item 3
  339. "
  340. (transient-mark-mode 1)
  341. (push-mark (point) t t)
  342. (goto-char (point-max))
  343. (let (org-list-demote-modify-bullet) (org-indent-item))
  344. (buffer-string))))
  345. ;; When point is right after empty item, do not move point.
  346. (should
  347. (= 13
  348. (org-test-with-temp-text "
  349. - item
  350. - <point> ::"
  351. (org-indent-item)
  352. (point))))
  353. ;; Preserve space after point upon promoting level.
  354. (org-test-with-temp-text "
  355. - item
  356. - <point> ::"
  357. (org-indent-item)
  358. (should (looking-at-p " \t"))))
  359. (ert-deftest test-org-list/indent-item-tree ()
  360. "Test `org-indent-item-tree' specifications."
  361. ;; 1. Error when not at an item.
  362. (org-test-with-temp-text "Paragraph."
  363. (should-error (org-indent-item-tree)))
  364. ;; 2. Indent item along with its children.
  365. (org-test-with-temp-text "
  366. - Item 1
  367. - Item 2
  368. - Item 2.1"
  369. (search-forward "- Item 2")
  370. (let (org-list-demote-modify-bullet) (org-indent-item-tree))
  371. (should (equal (buffer-string)
  372. "
  373. - Item 1
  374. - Item 2
  375. - Item 2.1")))
  376. ;; 3. Special case: When indenting top item, move the whole list.
  377. (org-test-with-temp-text "
  378. - Item 1
  379. - Item 2"
  380. (search-forward "- Item 1")
  381. (let (org-list-demote-modify-bullet org-odd-levels-only)
  382. (org-indent-item-tree))
  383. (should (equal (buffer-string)
  384. "
  385. - Item 1
  386. - Item 2")))
  387. ;; 4. Follow `org-list-demote-modify-bullet' specifications.
  388. ;;
  389. ;; 4.1. With unordered lists.
  390. (org-test-with-temp-text "
  391. - Item 1
  392. - Item 2
  393. + Item 2.1"
  394. (search-forward "- Item 2")
  395. (let ((org-list-demote-modify-bullet '(("-" . "+") ("+" . "-"))))
  396. (org-indent-item-tree))
  397. (should (equal (buffer-string)
  398. "
  399. - Item 1
  400. + Item 2
  401. - Item 2.1")))
  402. ;; 4.2. and ordered lists.
  403. (org-test-with-temp-text "
  404. 1. Item 1
  405. 2. Item 2
  406. + Item 2.1"
  407. (search-forward "2. Item 2")
  408. (let ((org-plain-list-ordered-item-terminator t)
  409. (org-list-demote-modify-bullet '(("1." . "+") ("+" . "1."))))
  410. (org-indent-item-tree))
  411. (should (equal (buffer-string)
  412. "
  413. 1. Item 1
  414. + Item 2
  415. 1. Item 2.1")))
  416. ;; 5. When a region is selected, indent every item within.
  417. (org-test-with-temp-text "
  418. - Item 1
  419. - Item 2
  420. - Item 2.1
  421. - Item 3
  422. - Item 3.1
  423. "
  424. (search-forward "- Item 2")
  425. (beginning-of-line)
  426. (transient-mark-mode 1)
  427. (push-mark (point) t t)
  428. (goto-char (point-max))
  429. (let (org-list-demote-modify-bullet) (org-indent-item-tree))
  430. (should (equal (buffer-string)
  431. "
  432. - Item 1
  433. - Item 2
  434. - Item 2.1
  435. - Item 3
  436. - Item 3.1
  437. "))))
  438. (ert-deftest test-org-list/outdent-item ()
  439. "Test `org-outdent-item' specifications."
  440. ;; 1. Error when not at an item.
  441. (org-test-with-temp-text "Paragraph."
  442. (should-error (org-outdent-item)))
  443. ;; 2. Error when trying to move first item of a list.
  444. (org-test-with-temp-text "
  445. - Item 1
  446. - Item 2"
  447. (forward-line)
  448. (should-error (org-outdent-item)))
  449. ;; 3. Error when trying to outdent an item without its children.
  450. (org-test-with-temp-text "
  451. - Item 1
  452. - Item 1.1
  453. - Item 1.1.1"
  454. (search-forward "- Item 1.1")
  455. (should-error (org-outdent-item)))
  456. ;; 4. Error when trying to outdent before top item.
  457. (org-test-with-temp-text "
  458. - Item 1
  459. - Item 2"
  460. (search-forward "- Item 2")
  461. (should-error (org-outdent-item)))
  462. ;; 5. When a region is selected, outdent every item within.
  463. (org-test-with-temp-text "
  464. - Item 1
  465. - Item 2
  466. - Item 3
  467. "
  468. (search-forward "- Item 2")
  469. (beginning-of-line)
  470. (transient-mark-mode 1)
  471. (push-mark (point) t t)
  472. (goto-char (point-max))
  473. (let (org-list-demote-modify-bullet) (org-outdent-item))
  474. (should (equal (buffer-string)
  475. "
  476. - Item 1
  477. - Item 2
  478. - Item 3
  479. "))))
  480. (ert-deftest test-org-list/outdent-item-tree ()
  481. "Test `org-outdent-item-tree' specifications."
  482. ;; 1. Error when not at an item.
  483. (org-test-with-temp-text "Paragraph."
  484. (should-error (org-outdent-item-tree)))
  485. ;; 2. Error when trying to outdent before top item.
  486. (org-test-with-temp-text "
  487. - Item 1
  488. - Item 2"
  489. (search-forward "- Item 2")
  490. (should-error (org-outdent-item-tree)))
  491. ;; 3. Outdent item along with its children.
  492. (org-test-with-temp-text "
  493. - Item 1
  494. - Item 2
  495. - Item 2.1"
  496. (search-forward "- Item 2")
  497. (org-outdent-item-tree)
  498. (should (equal (buffer-string)
  499. "
  500. - Item 1
  501. - Item 2
  502. - Item 2.1")))
  503. ;; 3. Special case: When outdenting top item, move the whole list.
  504. (org-test-with-temp-text "
  505. - Item 1
  506. - Item 2"
  507. (search-forward "- Item 1")
  508. (let (org-odd-levels-only) (org-outdent-item-tree))
  509. (should (equal (buffer-string)
  510. "
  511. - Item 1
  512. - Item 2")))
  513. ;; 5. When a region is selected, outdent every item within.
  514. (org-test-with-temp-text "
  515. - Item 1
  516. - Item 2
  517. - Item 2.1
  518. - Item 3
  519. - Item 3.1
  520. "
  521. (search-forward "- Item 2")
  522. (beginning-of-line)
  523. (transient-mark-mode 1)
  524. (push-mark (point) t t)
  525. (goto-char (point-max))
  526. (org-outdent-item-tree)
  527. (should (equal (buffer-string)
  528. "
  529. - Item 1
  530. - Item 2
  531. - Item 2.1
  532. - Item 3
  533. - Item 3.1
  534. "))))
  535. (ert-deftest test-org-list/cycle-item-identation ()
  536. "Test `org-list-cycle-item-indentation' specifications."
  537. ;; Refuse to indent non-empty items.
  538. (should-not
  539. (org-test-with-temp-text "- item - item2<point>"
  540. (org-cycle-item-indentation)))
  541. ;; First try to indent item.
  542. (should
  543. (equal "- item\n - sub-item\n - "
  544. (org-test-with-temp-text "- item\n - sub-item\n - <point>"
  545. (org-cycle-item-indentation)
  546. (buffer-string))))
  547. ;; If first indentation is not possible, outdent item.
  548. (should
  549. (equal "- item\n- "
  550. (org-test-with-temp-text "- item\n - <point>"
  551. (org-cycle-item-indentation)
  552. (buffer-string))))
  553. ;; Throw an error when item cannot move either way.
  554. (should-error
  555. (org-test-with-temp-text "- "
  556. (org-cycle-item-indentation)))
  557. ;; On repeated commands, cycle through all the indented positions,
  558. ;; then through all the outdented ones, then move back to initial
  559. ;; position.
  560. (should
  561. (equal '(4 6 0 2)
  562. (org-test-with-temp-text "- i0\n - i1\n - s1\n - <point>"
  563. (let ((indentations nil))
  564. (org-cycle-item-indentation)
  565. (dotimes (_ 3)
  566. (let ((last-command 'org-cycle-item-indentation))
  567. (push (current-indentation) indentations)
  568. (org-cycle-item-indentation)))
  569. (reverse (cons (current-indentation) indentations))))))
  570. ;; Refuse to indent the first item in a sub-list. Also refuse to
  571. ;; outdent an item with a next sibling.
  572. (should-error
  573. (org-test-with-temp-text "- item\n - <point>\n - sub-item 2"
  574. (org-cycle-item-indentation)))
  575. ;; When cycling back into initial position, preserve bullet type.
  576. (should
  577. (equal "1. item\n - "
  578. (org-test-with-temp-text "1. item\n - <point>"
  579. (org-cycle-item-indentation)
  580. (let ((last-command 'org-cycle-item-indentation))
  581. (org-cycle-item-indentation))
  582. (buffer-string))))
  583. (should
  584. (equal "1. item\n - tag :: "
  585. (org-test-with-temp-text "1. item\n - tag :: <point>"
  586. (org-cycle-item-indentation)
  587. (let ((last-command 'org-cycle-item-indentation))
  588. (org-cycle-item-indentation))
  589. (buffer-string))))
  590. ;; When starting at top level, never outdent.
  591. (should
  592. (org-test-with-temp-text "- item\n- <point>"
  593. (org-cycle-item-indentation)
  594. (let ((last-command 'org-cycle-item-indentation))
  595. (org-cycle-item-indentation))
  596. (buffer-string))))
  597. (ert-deftest test-org-list/move-item-down ()
  598. "Test `org-move-item-down' specifications."
  599. ;; Standard test.
  600. (should
  601. (equal "- item 2\n- item 1"
  602. (org-test-with-temp-text "- item 1\n- item 2"
  603. (org-move-item-down)
  604. (buffer-string))))
  605. ;; Keep same column in item.
  606. (should
  607. (org-test-with-temp-text "- it<point>em 1\n- item 2"
  608. (org-move-item-down)
  609. (looking-at "em 1")))
  610. ;; Move sub-items.
  611. (org-test-with-temp-text "- item 1\n - sub-item 1\n- item 2"
  612. (org-move-item-down)
  613. (should (equal (buffer-string)
  614. "- item 2\n- item 1\n - sub-item 1")))
  615. ;; Preserve blank lines.
  616. (should
  617. (equal
  618. "- item 2\n\n- item 1"
  619. (org-test-with-temp-text "- item 1\n\n- item 2"
  620. (org-move-item-down)
  621. (buffer-string))))
  622. ;; Error when trying to move the last item...
  623. (should-error
  624. (org-test-with-temp-text "- item 1\n- item 2"
  625. (forward-line)
  626. (org-move-item-down)))
  627. ;; ... unless `org-list-use-circular-motion' is non-nil. In this
  628. ;; case, move to the first item.
  629. (should
  630. (equal "- item 3\n- item 1\n- item 2\n"
  631. (org-test-with-temp-text "- item 1\n- item 2\n<point>- item 3"
  632. (let ((org-list-use-circular-motion t)) (org-move-item-down))
  633. (buffer-string))))
  634. ;; Preserve item visibility.
  635. (should
  636. (equal
  637. (make-list 2 'org-fold-outline)
  638. (let ((org-fold-core-style 'text-properties))
  639. (org-test-with-temp-text
  640. "* Headline\n<point>- item 1\n body 1\n- item 2\n body 2"
  641. (let ((org-cycle-include-plain-lists t))
  642. (org-cycle)
  643. (search-forward "- item 2")
  644. (org-cycle))
  645. (search-backward "- item 1")
  646. (org-move-item-down)
  647. (forward-line)
  648. (list (org-fold-get-folding-spec)
  649. (progn
  650. (search-backward " body 2")
  651. (org-fold-get-folding-spec)))))))
  652. (should
  653. (equal
  654. '(outline outline)
  655. (let ((org-fold-core-style 'overlays))
  656. (org-test-with-temp-text
  657. "* Headline\n<point>- item 1\n body 1\n- item 2\n body 2"
  658. (let ((org-cycle-include-plain-lists t))
  659. (org-cycle)
  660. (search-forward "- item 2")
  661. (org-cycle))
  662. (search-backward "- item 1")
  663. (org-move-item-down)
  664. (forward-line)
  665. (list (org-invisible-p2)
  666. (progn
  667. (search-backward " body 2")
  668. (org-invisible-p2)))))))
  669. ;; Preserve children visibility.
  670. (org-test-with-temp-text "* Headline
  671. - item 1
  672. - sub-item 1
  673. sub-body 1
  674. - item 2
  675. - sub-item 2
  676. sub-body 2"
  677. (let ((org-cycle-include-plain-lists t))
  678. (search-forward "- sub-item 1")
  679. (org-cycle)
  680. (search-forward "- sub-item 2")
  681. (org-cycle))
  682. (search-backward "- item 1")
  683. (org-move-item-down)
  684. (search-forward "sub-body 1")
  685. (should (org-invisible-p2))
  686. (search-backward "sub-body 2")
  687. (should (org-invisible-p2))))
  688. (ert-deftest test-org-list/move-item-down-contents-visibility ()
  689. "Preserve contents visibility."
  690. (org-test-with-temp-text "
  691. - item 1
  692. #+BEGIN_CENTER
  693. Text1
  694. #+END_CENTER
  695. - item 2
  696. #+BEGIN_CENTER
  697. Text2
  698. #+END_CENTER"
  699. (org-fold-hide-block-all)
  700. (let ((invisible-property-1
  701. (progn
  702. (search-forward "Text1")
  703. (get-char-property (point) 'invisible)))
  704. (invisible-property-2
  705. (progn
  706. (search-forward "Text2")
  707. (get-char-property (point) 'invisible))))
  708. (goto-char (point-min))
  709. (search-forward "- item 1")
  710. (org-move-item-down)
  711. (search-forward "Text1")
  712. (should (eq invisible-property-1 (get-char-property (point) 'invisible)))
  713. (search-backward "Text2")
  714. (should (eq invisible-property-2 (get-char-property (point) 'invisible))))))
  715. (ert-deftest test-org-list/move-item-up ()
  716. "Test `org-move-item-up' specifications."
  717. ;; Standard test.
  718. (org-test-with-temp-text "- item 1\n- item 2"
  719. (forward-line)
  720. (org-move-item-up)
  721. (should (equal (buffer-string)
  722. "- item 2\n- item 1")))
  723. ;; Keep same column in item.
  724. (org-test-with-temp-text "- item 1\n- item 2"
  725. (forward-line)
  726. (forward-char 4)
  727. (org-move-item-up)
  728. (should (looking-at "em 2")))
  729. ;; Move sub-items.
  730. (org-test-with-temp-text "- item 1\n- item 2\n - sub-item 2"
  731. (forward-line)
  732. (org-move-item-up)
  733. (should (equal (buffer-string)
  734. "- item 2\n - sub-item 2\n- item 1")))
  735. ;; Preserve blank lines.
  736. (should
  737. (equal
  738. "- item 2\n\n- item 1"
  739. (org-test-with-temp-text "- item 1\n\n- item 2"
  740. (search-forward "- item 2")
  741. (org-move-item-up)
  742. (buffer-string))))
  743. ;; Error when trying to move the first item...
  744. (org-test-with-temp-text "- item 1\n- item 2"
  745. (should-error (org-move-item-up)))
  746. ;; ... unless `org-list-use-circular-motion' is non-nil. In this
  747. ;; case, move to the first item.
  748. (should
  749. (equal "- item 2\n- item 3\n- item 1"
  750. (org-test-with-temp-text "- item 1\n- item 2\n- item 3"
  751. (let ((org-list-use-circular-motion t)) (org-move-item-up))
  752. (buffer-string))))
  753. ;; Preserve item visibility.
  754. (org-test-with-temp-text "* Headline\n- item 1\n body 1\n- item 2\n body 2"
  755. (let ((org-cycle-include-plain-lists t))
  756. (search-forward "- item 1")
  757. (org-cycle)
  758. (search-forward "- item 2")
  759. (org-cycle))
  760. (org-move-item-up)
  761. (forward-line)
  762. (should (org-invisible-p2))
  763. (search-forward " body 1")
  764. (should (org-invisible-p2)))
  765. ;; Preserve children visibility.
  766. (org-test-with-temp-text "* Headline
  767. - item 1
  768. - sub-item 1
  769. sub-body 1
  770. - item 2
  771. - sub-item 2
  772. sub-body 2"
  773. (let ((org-cycle-include-plain-lists t))
  774. (search-forward "- sub-item 1")
  775. (org-cycle)
  776. (search-forward "- sub-item 2")
  777. (org-cycle))
  778. (search-backward "- item 2")
  779. (org-move-item-up)
  780. (search-forward "sub-body 2")
  781. (should (org-invisible-p2))
  782. (search-forward "sub-body 1")
  783. (should (org-invisible-p2))))
  784. (ert-deftest test-org-list/move-item-up-contents-visibility ()
  785. (org-test-with-temp-text "
  786. - item 1
  787. #+BEGIN_CENTER
  788. Text1
  789. #+END_CENTER
  790. - item 2
  791. #+BEGIN_CENTER
  792. Text2
  793. #+END_CENTER"
  794. (org-hide-block-all)
  795. (let ((invisible-property-1
  796. (progn
  797. (search-forward "Text1")
  798. (get-char-property (point) 'invisible)))
  799. (invisible-property-2
  800. (progn
  801. (search-forward "Text2")
  802. (get-char-property (point) 'invisible))))
  803. (goto-char (point-min))
  804. (search-forward "- item 2")
  805. (org-move-item-up)
  806. (search-forward "Text2")
  807. (should (eq invisible-property-2 (get-char-property (point) 'invisible)))
  808. (search-forward "Text1")
  809. (should (eq invisible-property-1 (get-char-property (point) 'invisible))))))
  810. (ert-deftest test-org-list/insert-item ()
  811. "Test item insertion."
  812. ;; Blank lines specifications.
  813. ;;
  814. ;; Non-nil `org-blank-before-new-entry': insert a blank line.
  815. (should
  816. (org-test-with-temp-text "- a"
  817. (let ((org-blank-before-new-entry '((plain-list-item . t))))
  818. (end-of-line)
  819. (org-insert-item)
  820. (forward-line -1)
  821. (looking-at "$"))))
  822. ;; Nil `org-blank-before-new-entry': do not insert a blank line.
  823. (should-not
  824. (org-test-with-temp-text "- a"
  825. (let ((org-blank-before-new-entry '((plain-list-item . nil))))
  826. (end-of-line)
  827. (org-insert-item)
  828. (forward-line -1)
  829. (looking-at "$"))))
  830. ;; `org-blank-before-new-entry' set to auto: if there's no blank
  831. ;; line already in the sole item, do not insert one.
  832. (should-not
  833. (org-test-with-temp-text "- a"
  834. (let ((org-blank-before-new-entry '((plain-list-item . auto))))
  835. (end-of-line)
  836. (org-insert-item)
  837. (forward-line -1)
  838. (looking-at "$"))))
  839. ;; `org-blank-before-new-entry' set to `auto': if there's a blank
  840. ;; line in the sole item, insert another one.
  841. (should
  842. (org-test-with-temp-text "- a\n\n b<point>"
  843. (let ((org-blank-before-new-entry '((plain-list-item . auto))))
  844. (org-insert-item)
  845. (forward-line -1)
  846. (looking-at "$"))))
  847. ;; `org-blank-before-new-entry' set to `auto': if the user specified
  848. ;; a blank line, preserve it.
  849. (should
  850. (org-test-with-temp-text "- a\n\n<point>"
  851. (let ((org-blank-before-new-entry '((plain-list-item . auto))))
  852. (org-insert-item)
  853. (forward-line -1)
  854. (looking-at "$"))))
  855. ;; `org-blank-before-new-entry' set to `auto': if some items in list
  856. ;; are already separated by blank lines, insert one.
  857. (should
  858. (org-test-with-temp-text "- a\n\n- b<point>"
  859. (let ((org-blank-before-new-entry '((plain-list-item . auto))))
  860. (org-insert-item)
  861. (forward-line -1)
  862. (looking-at "$"))))
  863. (should
  864. (org-test-with-temp-text "- a\n\n- b"
  865. (let ((org-blank-before-new-entry '((plain-list-item . auto))))
  866. (org-insert-item)
  867. (forward-line)
  868. (looking-at "$"))))
  869. (should
  870. (org-test-with-temp-text
  871. "- a\n #+BEGIN_EXAMPLE\n\n x\n #+END_EXAMPLE<point>"
  872. (let ((org-blank-before-new-entry '((plain-list-item . auto))))
  873. (org-insert-item)
  874. (forward-line -1)
  875. (looking-at "$"))))
  876. ;; When called before or on the bullet, insert new item before
  877. ;; current one.
  878. (should
  879. (equal "- \n- item"
  880. (org-test-with-temp-text "- item"
  881. (org-insert-item)
  882. (buffer-string))))
  883. (should
  884. (equal "- \n- item"
  885. (org-test-with-temp-text "- <point>item"
  886. (org-insert-item)
  887. (buffer-string))))
  888. ;; When called at the very end of the list, insert new item as
  889. ;; a sibling of the very last one.
  890. (should
  891. (equal "- A\n\n - B\n\n - "
  892. (org-test-with-temp-text "- A\n\n - B\n\n<point>"
  893. (org-insert-item)
  894. (buffer-string))))
  895. (should
  896. (equal "- A\n\n - B\n\n - "
  897. (org-test-with-temp-text "- A\n\n - B\n\n <point>"
  898. (org-insert-item)
  899. (buffer-string))))
  900. ;; When called on tag in a descriptive list, insert new item before
  901. ;; current one too.
  902. (should
  903. (equal "- :: \n- tag :: item"
  904. (org-test-with-temp-text "- tag <point>:: item"
  905. (org-insert-item)
  906. (buffer-string))))
  907. (should
  908. (equal "- :: \n- tag :: item"
  909. (org-test-with-temp-text "- ta<point>g :: item"
  910. (org-insert-item)
  911. (buffer-string))))
  912. ;; Further, it splits the line or add a blank new item after it,
  913. ;; according to `org-M-RET-may-split-line'.
  914. (should
  915. (equal "- it\n- em"
  916. (org-test-with-temp-text "- it<point>em"
  917. (let ((org-M-RET-may-split-line '((default . t))))
  918. (org-insert-item))
  919. (buffer-string))))
  920. (should
  921. (equal "- item\n- "
  922. (org-test-with-temp-text "- it<point>em"
  923. (let ((org-M-RET-may-split-line '((default . nil))))
  924. (org-insert-item))
  925. (buffer-string))))
  926. ;; Re-order automatically.
  927. (should
  928. (equal "1. A\n\n2. \n\n3. \n\n4. B"
  929. (org-test-with-temp-text "1. A<point>\n\n2. \n\n3. B"
  930. (org-insert-item)
  931. (buffer-string))))
  932. (should
  933. (equal "1. a\n2. \n b\n3. c"
  934. (org-test-with-temp-text "1. a<point>\n b\n2. c"
  935. (org-insert-item)
  936. (buffer-string))))
  937. ;; Preserve list visibility when inserting an item.
  938. (should
  939. (equal
  940. `(org-fold-outline org-fold-outline)
  941. (let ((org-fold-core-style 'text-properties))
  942. (org-test-with-temp-text "- A\n - B\n- C\n - D"
  943. (let ((org-cycle-include-plain-lists t))
  944. (org-cycle)
  945. (forward-line 2)
  946. (org-cycle)
  947. (org-insert-item)
  948. (list (org-fold-get-folding-spec nil (line-beginning-position 0))
  949. (org-fold-get-folding-spec nil (line-end-position 2))))))))
  950. (should
  951. (equal
  952. '(outline outline)
  953. (let ((org-fold-core-style 'overlays))
  954. (org-test-with-temp-text "- A\n - B\n- C\n - D"
  955. (let ((org-cycle-include-plain-lists t))
  956. (org-cycle)
  957. (forward-line 2)
  958. (org-cycle)
  959. (org-insert-item)
  960. (list (get-char-property (line-beginning-position 0) 'invisible)
  961. (get-char-property (line-end-position 2) 'invisible)))))))
  962. ;; Test insertion in area after a sub-list. In particular, if point
  963. ;; is right at the end of the previous sub-list, still insert
  964. ;; a sub-item in that list.
  965. (should
  966. (= 2
  967. (org-test-with-temp-text "- item\n - sub-list\n<point> resume item"
  968. (org-insert-item)
  969. (current-indentation))))
  970. (should
  971. (= 0
  972. (org-test-with-temp-text "- item\n - sub-list\n resume item<point>"
  973. (org-insert-item)
  974. (current-indentation))))
  975. ;; Test splitting with blanks around.
  976. (should
  977. (equal "- A\n B\n- C\n - D\n- [ ] E"
  978. (org-test-with-temp-text "- A\n B <point> C\n - D\n- [ ] E"
  979. (org-insert-item)
  980. (buffer-string)))))
  981. (ert-deftest test-org-list/send-item ()
  982. "Test `org-list-send-item' specifications."
  983. ;; Move to beginning
  984. (should
  985. (equal "- item3\n- item1\n- item2\n"
  986. (org-test-with-temp-text
  987. "- item1\n- item2\n- item3\n"
  988. (org-list-send-item (caar (last (org-list-struct)))
  989. 'begin (org-list-struct))
  990. (buffer-string))))
  991. ;; Move to beginning with child item
  992. (should
  993. (equal "- item3\n - item4\n- item1\n- item2\n"
  994. (org-test-with-temp-text
  995. "- item1\n- item2\n- item3\n - item4\n"
  996. (org-list-send-item (car (nth 2 (org-list-struct)))
  997. 'begin (org-list-struct))
  998. (buffer-string))))
  999. ;; Move to end
  1000. (should
  1001. (equal "- item2\n- item3\n - item4\n- item1\n - item1child\n"
  1002. (org-test-with-temp-text
  1003. "- item1\n - item1child\n- item2\n- item3\n - item4\n"
  1004. (org-list-send-item (car (nth 0 (org-list-struct)))
  1005. 'end (org-list-struct))
  1006. (buffer-string))))
  1007. ;; Move to item number by string should move the item before the specified one
  1008. (should
  1009. (equal "- item2\n- item1\n - item1child\n- item3\n- item4\n- item5\n"
  1010. (org-test-with-temp-text
  1011. "- item1\n - item1child\n- item2\n- item3\n- item4\n- item5\n"
  1012. (org-list-send-item (car (nth 0 (org-list-struct)))
  1013. "3" (org-list-struct))
  1014. (buffer-string))))
  1015. ;; Move to item number by position should move the item before the specified one
  1016. (should
  1017. (equal "- item2\n- item1\n - item1child\n- item3\n- item4\n- item5\n"
  1018. (org-test-with-temp-text
  1019. "- item1\n - item1child\n- item2\n- item3\n- item4\n- item5\n"
  1020. (re-search-forward "item3")
  1021. (org-list-send-item (car (nth 0 (org-list-struct)))
  1022. (point-at-bol) (org-list-struct))
  1023. (buffer-string))))
  1024. ;; Delete
  1025. (should
  1026. (equal "- item1\n - item1child\n- item2\n- item4\n- item5\n"
  1027. (org-test-with-temp-text
  1028. "- item1\n - item1child\n- item2\n- item3\n- item4\n- item5\n"
  1029. (re-search-forward "item3")
  1030. (org-list-send-item (point-at-bol)
  1031. 'delete (org-list-struct))
  1032. (buffer-string))))
  1033. ;; Kill
  1034. (let ((kill-ring nil))
  1035. (org-test-with-temp-text
  1036. "- item1\n - item1child\n- item2\n- item3\n - item3child\n- item4\n- item5\n"
  1037. (re-search-forward "item3")
  1038. (org-list-send-item (point-at-bol)
  1039. 'kill (org-list-struct))
  1040. (should (equal "- item1\n - item1child\n- item2\n- item4\n- item5\n"
  1041. (buffer-string)))
  1042. (should (equal "item3\n - item3child"
  1043. (current-kill 0 t))))))
  1044. (ert-deftest test-org-list/repair ()
  1045. "Test `org-list-repair' specifications."
  1046. ;; Repair indentation.
  1047. (should
  1048. (equal "- item\n - child"
  1049. (org-test-with-temp-text "- item\n - child"
  1050. (let ((org-list-indent-offset 0)) (org-list-repair))
  1051. (buffer-string))))
  1052. ;; Repair bullets and numbering.
  1053. (should
  1054. (equal "- a\n- b"
  1055. (org-test-with-temp-text "- a\n+ b"
  1056. (let ((org-list-indent-offset 0))
  1057. (org-list-repair))
  1058. (buffer-string))))
  1059. (should
  1060. (equal "1. a\n2. b"
  1061. (org-test-with-temp-text "1. a\n1. b"
  1062. (let ((org-list-indent-offset 0)
  1063. (org-plain-list-ordered-item-terminator t))
  1064. (org-list-repair))
  1065. (buffer-string))))
  1066. ;; Repair check-boxes.
  1067. (should
  1068. (equal "- [X] item\n - [X] child"
  1069. (org-test-with-temp-text "- [ ] item\n - [X] child"
  1070. (let ((org-list-indent-offset 0))
  1071. (org-list-repair))
  1072. (buffer-string))))
  1073. ;; Special case: do not move contents of an item within its child.
  1074. ;; Yet, preserve indentation differences within contents.
  1075. (should
  1076. (equal "- item\n - child\n within item"
  1077. (org-test-with-temp-text "- item\n - child\n within item"
  1078. (let ((org-list-indent-offset 0)) (org-list-repair))
  1079. (buffer-string))))
  1080. (should
  1081. (equal
  1082. "- item\n - child\n within item\n indented"
  1083. (org-test-with-temp-text
  1084. "- item\n - child\n within item\n indented"
  1085. (let ((org-list-indent-offset 0)) (org-list-repair))
  1086. (buffer-string)))))
  1087. (ert-deftest test-org-list/update-checkbox-count ()
  1088. "Test `org-update-checkbox-count' specifications."
  1089. ;; From a headline.
  1090. (should
  1091. (string-match "\\[0/1\\]"
  1092. (org-test-with-temp-text "* [/]\n- [ ] item"
  1093. (org-update-checkbox-count)
  1094. (buffer-string))))
  1095. (should
  1096. (string-match "\\[1/1\\]"
  1097. (org-test-with-temp-text "* [/]\n- [X] item"
  1098. (org-update-checkbox-count)
  1099. (buffer-string))))
  1100. (should
  1101. (string-match "\\[100%\\]"
  1102. (org-test-with-temp-text "* [%]\n- [X] item"
  1103. (org-update-checkbox-count)
  1104. (buffer-string))))
  1105. ;; From a list or a sub-list.
  1106. (should
  1107. (string-match "\\[0/1\\]"
  1108. (org-test-with-temp-text "- [/]\n - [ ] item"
  1109. (org-update-checkbox-count)
  1110. (buffer-string))))
  1111. (should
  1112. (string-match "\\[1/1\\]"
  1113. (org-test-with-temp-text "- [/]\n - [X] item"
  1114. (org-update-checkbox-count)
  1115. (buffer-string))))
  1116. (should
  1117. (string-match "\\[100%\\]"
  1118. (org-test-with-temp-text "- [%]\n - [X] item"
  1119. (org-update-checkbox-count)
  1120. (buffer-string))))
  1121. (should
  1122. (string-match
  1123. "\\[1/1\\]"
  1124. (org-test-with-temp-text "- [ ] item 1\n- [ ] item 2 [/]\n - [X] sub 1"
  1125. (org-update-checkbox-count)
  1126. (buffer-string))))
  1127. ;; Count do not apply to sub-lists unless count is not hierarchical.
  1128. ;; This state can be achieved with COOKIE_DATA node property set to
  1129. ;; "recursive".
  1130. (should
  1131. (string-match "\\[1/1\\]"
  1132. (org-test-with-temp-text "- [/]\n - item\n - [X] sub-item"
  1133. (let ((org-checkbox-hierarchical-statistics nil))
  1134. (org-update-checkbox-count))
  1135. (buffer-string))))
  1136. (should
  1137. (string-match "\\[1/1\\]"
  1138. (org-test-with-temp-text "
  1139. <point>* H
  1140. :PROPERTIES:
  1141. :COOKIE_DATA: recursive
  1142. :END:
  1143. - [/]
  1144. - item
  1145. - [X] sub-item"
  1146. (org-update-checkbox-count)
  1147. (buffer-string))))
  1148. (should
  1149. (string-match "\\[0/0\\]"
  1150. (org-test-with-temp-text "- [/]\n - item\n - [ ] sub-item"
  1151. (org-update-checkbox-count)
  1152. (buffer-string))))
  1153. ;; With optional argument ALL, update all buffer.
  1154. (should
  1155. (= 2
  1156. (org-test-with-temp-text "* [/]\n- [X] item\n* [/]\n- [X] item"
  1157. (org-update-checkbox-count t)
  1158. (count-matches "\\[1/1\\]"))))
  1159. ;; Ignore boxes in drawers, blocks or inlinetasks when counting from
  1160. ;; outside.
  1161. (should
  1162. (string-match "\\[2/2\\]"
  1163. (org-test-with-temp-text "
  1164. - [/]
  1165. - [X] item1
  1166. :DRAWER:
  1167. - [X] item
  1168. :END:
  1169. - [X] item2"
  1170. (let ((org-checkbox-hierarchical-statistics nil))
  1171. (org-update-checkbox-count))
  1172. (buffer-string)))))
  1173. ;;; API
  1174. (ert-deftest test-org-list/at-radio-list-p ()
  1175. "Test `org-at-radio-list-p' specifications."
  1176. (should
  1177. (org-test-with-temp-text "#+attr_org: :radio t\n<point>- foo"
  1178. (org-at-radio-list-p)))
  1179. (should
  1180. (org-test-with-temp-text "#+attr_org: :radio t\n- foo\n<point>- bar"
  1181. (org-at-radio-list-p)))
  1182. (should
  1183. (org-test-with-temp-text "#+ATTR_ORG: :radio t\n<point>- foo"
  1184. (org-at-radio-list-p)))
  1185. (should
  1186. (org-test-with-temp-text "#+attr_org: :radio bar\n<point>- foo"
  1187. (org-at-radio-list-p)))
  1188. (should-not
  1189. (org-test-with-temp-text "#+attr_org: :radio nil\n<point>- foo"
  1190. (org-at-radio-list-p)))
  1191. (should-not
  1192. (org-test-with-temp-text "<point>- foo"
  1193. (org-at-radio-list-p)))
  1194. (should-not
  1195. (org-test-with-temp-text "#+attr_org: :radio t\n- foo\n <point>bar"
  1196. (org-at-radio-list-p)))
  1197. (should-not
  1198. (org-test-with-temp-text
  1199. "#+attr_org: :radio t\n#+begin_example\n<point>- foo\n#+end_example"
  1200. (org-at-radio-list-p))))
  1201. ;;; Miscellaneous
  1202. (ert-deftest test-org-list/toggle-item ()
  1203. "Test `org-toggle-item' specifications."
  1204. ;; Convert normal lines to items.
  1205. (should
  1206. (equal "- line"
  1207. (org-test-with-temp-text "line"
  1208. (org-toggle-item nil)
  1209. (buffer-string))))
  1210. ;; Convert items to normal lines.
  1211. (should
  1212. (equal "line"
  1213. (org-test-with-temp-text "- line"
  1214. (org-toggle-item nil)
  1215. (buffer-string))))
  1216. ;; Convert headlines to items.
  1217. (should
  1218. (equal "- line"
  1219. (org-test-with-temp-text "* line"
  1220. (org-toggle-item nil)
  1221. (buffer-string))))
  1222. ;; When converting a headline to a list item, TODO keywords become
  1223. ;; checkboxes.
  1224. (should
  1225. (equal "- [X] line"
  1226. (org-test-with-temp-text "* DONE line"
  1227. (org-toggle-item nil)
  1228. (buffer-string))))
  1229. (should
  1230. (equal "- [ ] line"
  1231. (org-test-with-temp-text "* TODO line"
  1232. (org-toggle-item nil)
  1233. (buffer-string))))
  1234. ;; When turning headlines into items, make sure planning info line
  1235. ;; and properties drawers are removed. This also includes empty
  1236. ;; lines following them.
  1237. (should
  1238. (equal "- H\n"
  1239. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29 Thu>"
  1240. (org-toggle-item nil)
  1241. (buffer-string))))
  1242. (should
  1243. (equal "- H\n"
  1244. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  1245. (org-toggle-item nil)
  1246. (buffer-string))))
  1247. (should
  1248. (equal "- H\nText"
  1249. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n\nText"
  1250. (org-toggle-item nil)
  1251. (buffer-string))))
  1252. ;; When no region is marked and point is on a blank line
  1253. ;; only operate on current line.
  1254. (should
  1255. (equal " \n* H :tag:"
  1256. (org-test-with-temp-text "<point> \n* H :tag:"
  1257. (org-toggle-item nil)
  1258. (buffer-string))))
  1259. ;; When a region is marked and first line is a headline, all
  1260. ;; headlines are turned into items.
  1261. (should
  1262. (equal "- H1\n - H2"
  1263. (org-test-with-temp-text "* H1\n** H2"
  1264. (transient-mark-mode 1)
  1265. (push-mark (point) t t)
  1266. (goto-char (point-max))
  1267. (org-toggle-item nil)
  1268. (buffer-string))))
  1269. (should
  1270. (equal "- [ ] H1\n - [ ] H2"
  1271. (org-test-with-temp-text "* TODO H1\n** TODO H2"
  1272. (transient-mark-mode 1)
  1273. (push-mark (point) t t)
  1274. (goto-char (point-max))
  1275. (org-toggle-item nil)
  1276. (buffer-string))))
  1277. ;; When turning headlines into items, make sure headings contents
  1278. ;; are kept within items.
  1279. (should
  1280. (equal "- H1\n Text"
  1281. (org-test-with-temp-text "* H1\nText"
  1282. (transient-mark-mode 1)
  1283. (push-mark (point) t t)
  1284. (goto-char (point-max))
  1285. (org-toggle-item nil)
  1286. (buffer-string))))
  1287. ;; When a region is marked and first line is an item, all items are
  1288. ;; turned into normal lines.
  1289. (should
  1290. (equal "1\n 2"
  1291. (org-test-with-temp-text "- 1\n - 2"
  1292. (transient-mark-mode 1)
  1293. (push-mark (point) t t)
  1294. (goto-char (point-max))
  1295. (org-toggle-item nil)
  1296. (buffer-string))))
  1297. (should
  1298. (equal "1\n2"
  1299. (org-test-with-temp-text "- 1\n2"
  1300. (transient-mark-mode 1)
  1301. (push-mark (point) t t)
  1302. (goto-char (point-max))
  1303. (org-toggle-item nil)
  1304. (buffer-string))))
  1305. ;; When a region is marked and first line is an item, all normal
  1306. ;; lines are turned into items.
  1307. (should
  1308. (equal "- line 1\n- line 2"
  1309. (org-test-with-temp-text "line 1\nline 2"
  1310. (transient-mark-mode 1)
  1311. (push-mark (point) t t)
  1312. (goto-char (point-max))
  1313. (org-toggle-item nil)
  1314. (buffer-string))))
  1315. (should
  1316. (equal "- line 1\n- line 2"
  1317. (org-test-with-temp-text "line 1\n- line 2"
  1318. (transient-mark-mode 1)
  1319. (push-mark (point) t t)
  1320. (goto-char (point-max))
  1321. (org-toggle-item nil)
  1322. (buffer-string))))
  1323. ;; When argument ARG is non-nil, change the whole region into
  1324. ;; a single item.
  1325. (should
  1326. (equal "- line 1\n line 2"
  1327. (org-test-with-temp-text "line 1\nline 2"
  1328. (transient-mark-mode 1)
  1329. (push-mark (point) t t)
  1330. (goto-char (point-max))
  1331. (org-toggle-item t)
  1332. (buffer-string)))))
  1333. (ert-deftest test-org-list/sort ()
  1334. "Test `org-sort-list'."
  1335. ;; Sort alphabetically.
  1336. (let ((original-string-collate-lessp (symbol-function 'string-collate-lessp)))
  1337. (cl-letf (((symbol-function 'string-collate-lessp)
  1338. (lambda (s1 s2 &optional locale ignore-case)
  1339. (funcall original-string-collate-lessp
  1340. s1 s2 "C" ignore-case))))
  1341. (should
  1342. (equal "- abc\n- def\n- XYZ\n"
  1343. (org-test-with-temp-text "- def\n- XYZ\n- abc\n"
  1344. (org-sort-list nil ?a)
  1345. (buffer-string))))
  1346. (should
  1347. (equal "- XYZ\n- def\n- abc\n"
  1348. (org-test-with-temp-text "- def\n- XYZ\n- abc\n"
  1349. (org-sort-list nil ?A)
  1350. (buffer-string))))
  1351. ;; Sort alphabetically (with case).
  1352. (should
  1353. (equal "- C\n- a\n- b\n"
  1354. (org-test-with-temp-text "- b\n- C\n- a\n"
  1355. (org-sort-list t ?a)
  1356. (buffer-string))))
  1357. (should
  1358. (equal "- b\n- a\n- C\n"
  1359. (org-test-with-temp-text "- b\n- C\n- a\n"
  1360. (org-sort-list t ?A)
  1361. (buffer-string))))))
  1362. ;; Sort numerically.
  1363. (should
  1364. (equal "- 1\n- 2\n- 10\n"
  1365. (org-test-with-temp-text "- 10\n- 1\n- 2\n"
  1366. (org-sort-list nil ?n)
  1367. (buffer-string))))
  1368. (should
  1369. (equal "- 10\n- 2\n- 1\n"
  1370. (org-test-with-temp-text "- 10\n- 1\n- 2\n"
  1371. (org-sort-list nil ?N)
  1372. (buffer-string))))
  1373. ;; Sort by checked status.
  1374. (should
  1375. (equal "- [ ] xyz\n- [ ] def\n- [X] abc\n"
  1376. (org-test-with-temp-text "- [X] abc\n- [ ] xyz\n- [ ] def\n"
  1377. (org-sort-list nil ?x)
  1378. (buffer-string))))
  1379. (should
  1380. (equal "- [X] abc\n- [ ] xyz\n- [ ] def\n"
  1381. (org-test-with-temp-text "- [X] abc\n- [ ] xyz\n- [ ] def\n"
  1382. (org-sort-list nil ?X)
  1383. (buffer-string))))
  1384. ;; Sort by time stamp.
  1385. (should
  1386. (equal "- <2017-05-08 Mon>\n- <2017-05-09 Tue>\n- <2018-05-09 Wed>\n"
  1387. (org-test-with-temp-text
  1388. "- <2018-05-09 Wed>\n- <2017-05-09 Tue>\n- <2017-05-08 Mon>\n"
  1389. (org-sort-list nil ?t)
  1390. (buffer-string))))
  1391. (should
  1392. (equal "- <2018-05-09 Wed>\n- <2017-05-09 Tue>\n- <2017-05-08 Mon>\n"
  1393. (org-test-with-temp-text
  1394. "- <2018-05-09 Wed>\n- <2017-05-09 Tue>\n- <2017-05-08 Mon>\n"
  1395. (org-sort-list nil ?T)
  1396. (buffer-string))))
  1397. ;; Sort by custom function.
  1398. (should
  1399. (equal "- b\n- aa\n- ccc\n"
  1400. (org-test-with-temp-text "- ccc\n- b\n- aa\n"
  1401. (org-sort-list nil ?f
  1402. (lambda ()
  1403. (length (buffer-substring (point-at-bol)
  1404. (point-at-eol))))
  1405. #'<)
  1406. (buffer-string))))
  1407. (should
  1408. (equal "- ccc\n- aa\n- b\n"
  1409. (org-test-with-temp-text "- ccc\n- b\n- aa\n"
  1410. (org-sort-list nil ?F
  1411. (lambda ()
  1412. (length (buffer-substring (point-at-bol)
  1413. (point-at-eol))))
  1414. #'<)
  1415. (buffer-string)))))
  1416. ;;; List transformations
  1417. (ert-deftest test-org-list/to-generic ()
  1418. "Test `org-list-to-generic' specifications."
  1419. ;; Test `:ustart' and `:uend' parameters.
  1420. (should
  1421. (equal
  1422. "begin\na"
  1423. (org-test-with-temp-text "- a"
  1424. (org-list-to-generic (org-list-to-lisp) '(:ustart "begin")))))
  1425. (should-not
  1426. (equal
  1427. "begin\na"
  1428. (org-test-with-temp-text "1. a"
  1429. (org-list-to-generic (org-list-to-lisp) '(:ustart "begin")))))
  1430. (should
  1431. (equal
  1432. "a\nend"
  1433. (org-test-with-temp-text "- a"
  1434. (org-list-to-generic (org-list-to-lisp) '(:uend "end")))))
  1435. (should-not
  1436. (equal
  1437. "a\nend"
  1438. (org-test-with-temp-text "1. a"
  1439. (org-list-to-generic (org-list-to-lisp) '(:uend "end")))))
  1440. (should
  1441. (equal
  1442. "begin l1\na\nbegin l2\nb\nend l2\nend l1"
  1443. (org-test-with-temp-text "- a\n - b"
  1444. (org-list-to-generic
  1445. (org-list-to-lisp)
  1446. (list :ustart (lambda (l) (format "begin l%d" l))
  1447. :uend (lambda (l) (format "end l%d" l)))))))
  1448. ;; Test `:ostart' and `:oend' parameters.
  1449. (should
  1450. (equal
  1451. "begin\na"
  1452. (org-test-with-temp-text "1. a"
  1453. (org-list-to-generic (org-list-to-lisp) '(:ostart "begin")))))
  1454. (should-not
  1455. (equal
  1456. "begin\na"
  1457. (org-test-with-temp-text "- a"
  1458. (org-list-to-generic (org-list-to-lisp) '(:ostart "begin")))))
  1459. (should
  1460. (equal
  1461. "a\nend"
  1462. (org-test-with-temp-text "1. a"
  1463. (org-list-to-generic (org-list-to-lisp) '(:oend "end")))))
  1464. (should-not
  1465. (equal
  1466. "a\nend"
  1467. (org-test-with-temp-text "- a"
  1468. (org-list-to-generic (org-list-to-lisp) '(:oend "end")))))
  1469. (should
  1470. (equal
  1471. "begin l1\na\nbegin l2\nb\nend l2\nend l1"
  1472. (org-test-with-temp-text "1. a\n 1. b"
  1473. (org-list-to-generic
  1474. (org-list-to-lisp)
  1475. (list :ostart (lambda (l) (format "begin l%d" l))
  1476. :oend (lambda (l) (format "end l%d" l)))))))
  1477. ;; Test `:dstart' and `:dend' parameters.
  1478. (should
  1479. (equal
  1480. "begin\ntaga"
  1481. (org-test-with-temp-text "- tag :: a"
  1482. (org-list-to-generic (org-list-to-lisp) '(:dstart "begin")))))
  1483. (should-not
  1484. (equal
  1485. "begin\na"
  1486. (org-test-with-temp-text "- a"
  1487. (org-list-to-generic (org-list-to-lisp) '(:dstart "begin")))))
  1488. (should
  1489. (equal
  1490. "taga\nend"
  1491. (org-test-with-temp-text "- tag :: a"
  1492. (org-list-to-generic (org-list-to-lisp) '(:dend "end")))))
  1493. (should-not
  1494. (equal
  1495. "a\nend"
  1496. (org-test-with-temp-text "- a"
  1497. (org-list-to-generic (org-list-to-lisp) '(:dend "end")))))
  1498. (should
  1499. (equal
  1500. "begin l1\ntag1a\nbegin l2\ntag2b\nend l2\nend l1"
  1501. (org-test-with-temp-text "- tag1 :: a\n - tag2 :: b"
  1502. (org-list-to-generic
  1503. (org-list-to-lisp)
  1504. (list :dstart (lambda (l) (format "begin l%d" l))
  1505. :dend (lambda (l) (format "end l%d" l)))))))
  1506. ;; Test `:dtstart', `:dtend', `:ddstart' and `:ddend' parameters.
  1507. (should
  1508. (equal
  1509. ">tag<a"
  1510. (org-test-with-temp-text "- tag :: a"
  1511. (org-list-to-generic (org-list-to-lisp) '(:dtstart ">" :dtend "<")))))
  1512. (should
  1513. (equal
  1514. "tag>a<"
  1515. (org-test-with-temp-text "- tag :: a"
  1516. (org-list-to-generic (org-list-to-lisp) '(:ddstart ">" :ddend "<")))))
  1517. ;; Test `:istart' and `:iend' parameters.
  1518. (should
  1519. (equal
  1520. "starta"
  1521. (org-test-with-temp-text "- a"
  1522. (org-list-to-generic (org-list-to-lisp) '(:istart "start")))))
  1523. (should
  1524. (equal
  1525. "level1 a\nlevel2 b"
  1526. (org-test-with-temp-text "- a\n - b"
  1527. (org-list-to-generic (org-list-to-lisp)
  1528. '(:istart (lambda (type l) (format "level%d "l)))))))
  1529. (should
  1530. (equal
  1531. "a\nblevel2level1"
  1532. (org-test-with-temp-text "- a\n - b"
  1533. (org-list-to-generic (org-list-to-lisp)
  1534. '(:iend (lambda (type l) (format "level%d" l)))))))
  1535. ;; Test `:icount' parameter.
  1536. (should
  1537. (equal
  1538. "counta"
  1539. (org-test-with-temp-text "1. [@3] a"
  1540. (org-list-to-generic (org-list-to-lisp) '(:icount "count")))))
  1541. (should-not
  1542. (equal
  1543. "counta"
  1544. (org-test-with-temp-text "1. a"
  1545. (org-list-to-generic (org-list-to-lisp) '(:icount "count")))))
  1546. (should
  1547. (equal
  1548. "counta"
  1549. (org-test-with-temp-text "1. [@3] a"
  1550. (org-list-to-generic (org-list-to-lisp)
  1551. '(:icount "count" :istart "start")))))
  1552. (should
  1553. (equal
  1554. "level:1, counter:3 a"
  1555. (org-test-with-temp-text "1. [@3] a"
  1556. (org-list-to-generic
  1557. (org-list-to-lisp)
  1558. '(:icount (lambda (type l c) (format "level:%d, counter:%d " l c)))))))
  1559. ;; Test `:isep' parameter.
  1560. (should
  1561. (equal
  1562. "a\n--\nb"
  1563. (org-test-with-temp-text "- a\n- b"
  1564. (org-list-to-generic (org-list-to-lisp) '(:isep "--")))))
  1565. (should-not
  1566. (equal
  1567. "a\n--\nb"
  1568. (org-test-with-temp-text "- a\n - b"
  1569. (org-list-to-generic (org-list-to-lisp) '(:isep "--")))))
  1570. (should
  1571. (equal
  1572. "a\n- 1 -\nb"
  1573. (org-test-with-temp-text "- a\n- b"
  1574. (org-list-to-generic
  1575. (org-list-to-lisp)
  1576. '(:isep (lambda (type depth) (format "- %d -" depth)))))))
  1577. ;; Test `:ifmt' parameter.
  1578. (should
  1579. (equal
  1580. ">> a <<"
  1581. (org-test-with-temp-text "1. [@3] a"
  1582. (org-list-to-generic
  1583. (org-list-to-lisp)
  1584. '(:ifmt (lambda (type c) (format ">> %s <<" c)))))))
  1585. ;; Test `:cbon', `:cboff', `:cbtrans'
  1586. (should
  1587. (equal
  1588. "!a"
  1589. (org-test-with-temp-text "- [X] a"
  1590. (org-list-to-generic (org-list-to-lisp) '(:cbon "!")))))
  1591. (should-not
  1592. (equal
  1593. "!a"
  1594. (org-test-with-temp-text "- [X] a"
  1595. (org-list-to-generic (org-list-to-lisp) '(:cboff "!" :cbtrans "!")))))
  1596. (should
  1597. (equal
  1598. "!a"
  1599. (org-test-with-temp-text "- [ ] a"
  1600. (org-list-to-generic (org-list-to-lisp) '(:cboff "!")))))
  1601. (should-not
  1602. (equal
  1603. "!a"
  1604. (org-test-with-temp-text "- [ ] a"
  1605. (org-list-to-generic (org-list-to-lisp) '(:cbon "!" :cbtrans "!")))))
  1606. (should
  1607. (equal
  1608. "!a"
  1609. (org-test-with-temp-text "- [-] a"
  1610. (org-list-to-generic (org-list-to-lisp) '(:cbtrans "!")))))
  1611. (should-not
  1612. (equal
  1613. "!a"
  1614. (org-test-with-temp-text "- [-] a"
  1615. (org-list-to-generic (org-list-to-lisp) '(:cbon "!" :cboff "!")))))
  1616. ;; Test `:splice' parameter.
  1617. (should
  1618. (equal
  1619. "a"
  1620. (org-test-with-temp-text "- a"
  1621. (org-list-to-generic (org-list-to-lisp)
  1622. '(:ustart "begin" :uend "end" :splice t)))))
  1623. ;; No error on empty lists.
  1624. (should
  1625. (org-test-with-temp-text "-" (org-list-to-generic (org-list-to-lisp) nil))))
  1626. (ert-deftest test-org-list/to-html ()
  1627. "Test `org-list-to-html' specifications."
  1628. (should
  1629. (equal "<ul class=\"org-ul\">\n<li>a</li>\n</ul>"
  1630. (org-test-with-temp-text "- a"
  1631. (org-list-to-html (org-list-to-lisp) nil)))))
  1632. (ert-deftest test-org-list/to-latex ()
  1633. "Test `org-list-to-latex' specifications."
  1634. (should
  1635. (equal "\\begin{itemize}\n\\item a\n\\end{itemize}"
  1636. (org-test-with-temp-text "- a"
  1637. (org-list-to-latex (org-list-to-lisp) nil)))))
  1638. (ert-deftest test-org-list/to-texinfo ()
  1639. "Test `org-list-to-texinfo' specifications."
  1640. (should
  1641. (equal "@itemize\n@item\na\n@end itemize"
  1642. (org-test-with-temp-text "- a"
  1643. (org-list-to-texinfo (org-list-to-lisp) nil)))))
  1644. (ert-deftest test-org-list/to-org ()
  1645. "Test `org-list-to-org' specifications."
  1646. ;; Un-ordered list.
  1647. (should
  1648. (equal "- a"
  1649. (org-test-with-temp-text "- a"
  1650. (org-list-to-org (org-list-to-lisp) nil))))
  1651. ;; Ordered list.
  1652. (should
  1653. (equal "1. a"
  1654. (org-test-with-temp-text "1. a"
  1655. (org-list-to-org (org-list-to-lisp) nil))))
  1656. ;; Descriptive list.
  1657. (should
  1658. (equal "- a :: b"
  1659. (org-test-with-temp-text "- a :: b"
  1660. (org-list-to-org (org-list-to-lisp) nil))))
  1661. ;; Nested list.
  1662. (should
  1663. (equal "- a\n - b"
  1664. (org-test-with-temp-text "- a\n - b"
  1665. (org-list-to-org (org-list-to-lisp) nil))))
  1666. ;; Item spanning over multiple lines.
  1667. (should
  1668. (equal "- a\n b"
  1669. (org-test-with-temp-text "- a\n b"
  1670. (org-list-to-org (org-list-to-lisp) nil))))
  1671. ;; Item with continuation text after a sub-list.
  1672. (should
  1673. (equal "- a\n - b\n c"
  1674. (org-test-with-temp-text "- a\n - b\n c"
  1675. (org-list-to-org (org-list-to-lisp) nil)))))
  1676. (provide 'test-org-list)
  1677. ;;; test-org-list.el ends here