test-org-list.el 52 KB

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