test-org-list.el 50 KB

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