test-org-list.el 50 KB

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