test-org-list.el 48 KB

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