test-org-list.el 51 KB

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