test-org-list.el 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  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 <http://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. (ert-deftest test-org-list/repair ()
  874. "Test `org-list-repair' specifications."
  875. ;; Repair indentation.
  876. (should
  877. (equal "- item\n - child"
  878. (org-test-with-temp-text "- item\n - child"
  879. (let ((org-list-indent-offset 0)) (org-list-repair))
  880. (buffer-string))))
  881. ;; Repair bullets and numbering.
  882. (should
  883. (equal "- a\n- b"
  884. (org-test-with-temp-text "- a\n+ b"
  885. (let ((org-list-indent-offset 0))
  886. (org-list-repair))
  887. (buffer-string))))
  888. (should
  889. (equal "1. a\n2. b"
  890. (org-test-with-temp-text "1. a\n1. b"
  891. (let ((org-list-indent-offset 0)
  892. (org-plain-list-ordered-item-terminator t))
  893. (org-list-repair))
  894. (buffer-string))))
  895. ;; Repair check-boxes.
  896. (should
  897. (equal "- [X] item\n - [X] child"
  898. (org-test-with-temp-text "- [ ] item\n - [X] child"
  899. (let ((org-list-indent-offset 0))
  900. (org-list-repair))
  901. (buffer-string))))
  902. ;; Special case: do not move contents of an item within its child.
  903. ;; Yet, preserve indentation differences within contents.
  904. (should
  905. (equal "- item\n - child\n within item"
  906. (org-test-with-temp-text "- item\n - child\n within item"
  907. (let ((org-list-indent-offset 0)) (org-list-repair))
  908. (buffer-string))))
  909. (should
  910. (equal
  911. "- item\n - child\n within item\n indented"
  912. (org-test-with-temp-text
  913. "- item\n - child\n within item\n indented"
  914. (let ((org-list-indent-offset 0)) (org-list-repair))
  915. (buffer-string)))))
  916. (ert-deftest test-org-list/update-checkbox-count ()
  917. "Test `org-update-checkbox-count' specifications."
  918. ;; From a headline.
  919. (should
  920. (string-match "\\[0/1\\]"
  921. (org-test-with-temp-text "* [/]\n- [ ] item"
  922. (org-update-checkbox-count)
  923. (buffer-string))))
  924. (should
  925. (string-match "\\[1/1\\]"
  926. (org-test-with-temp-text "* [/]\n- [X] item"
  927. (org-update-checkbox-count)
  928. (buffer-string))))
  929. (should
  930. (string-match "\\[100%\\]"
  931. (org-test-with-temp-text "* [%]\n- [X] item"
  932. (org-update-checkbox-count)
  933. (buffer-string))))
  934. ;; From a list or a sub-list.
  935. (should
  936. (string-match "\\[0/1\\]"
  937. (org-test-with-temp-text "- [/]\n - [ ] item"
  938. (org-update-checkbox-count)
  939. (buffer-string))))
  940. (should
  941. (string-match "\\[1/1\\]"
  942. (org-test-with-temp-text "- [/]\n - [X] item"
  943. (org-update-checkbox-count)
  944. (buffer-string))))
  945. (should
  946. (string-match "\\[100%\\]"
  947. (org-test-with-temp-text "- [%]\n - [X] item"
  948. (org-update-checkbox-count)
  949. (buffer-string))))
  950. (should
  951. (string-match
  952. "\\[1/1\\]"
  953. (org-test-with-temp-text "- [ ] item 1\n- [ ] item 2 [/]\n - [X] sub 1"
  954. (org-update-checkbox-count)
  955. (buffer-string))))
  956. ;; Count do not apply to sub-lists unless count is not hierarchical.
  957. ;; This state can be achieved with COOKIE_DATA node property set to
  958. ;; "recursive".
  959. (should
  960. (string-match "\\[1/1\\]"
  961. (org-test-with-temp-text "- [/]\n - item\n - [X] sub-item"
  962. (let ((org-checkbox-hierarchical-statistics nil))
  963. (org-update-checkbox-count))
  964. (buffer-string))))
  965. (should
  966. (string-match "\\[1/1\\]"
  967. (org-test-with-temp-text "
  968. <point>* H
  969. :PROPERTIES:
  970. :COOKIE_DATA: recursive
  971. :END:
  972. - [/]
  973. - item
  974. - [X] sub-item"
  975. (org-update-checkbox-count)
  976. (buffer-string))))
  977. (should
  978. (string-match "\\[0/0\\]"
  979. (org-test-with-temp-text "- [/]\n - item\n - [ ] sub-item"
  980. (org-update-checkbox-count)
  981. (buffer-string))))
  982. ;; With optional argument ALL, update all buffer.
  983. (should
  984. (= 2
  985. (org-test-with-temp-text "* [/]\n- [X] item\n* [/]\n- [X] item"
  986. (org-update-checkbox-count t)
  987. (count-matches "\\[1/1\\]"))))
  988. ;; Ignore boxes in drawers, blocks or inlinetasks when counting from
  989. ;; outside.
  990. (should
  991. (string-match "\\[2/2\\]"
  992. (org-test-with-temp-text "
  993. - [/]
  994. - [X] item1
  995. :DRAWER:
  996. - [X] item
  997. :END:
  998. - [X] item2"
  999. (let ((org-checkbox-hierarchical-statistics nil))
  1000. (org-update-checkbox-count))
  1001. (buffer-string)))))
  1002. ;;; API
  1003. (ert-deftest test-org-list/at-radio-list-p ()
  1004. "Test `org-at-radio-list-p' specifications."
  1005. (should
  1006. (org-test-with-temp-text "#+attr_org: :radio t\n<point>- foo"
  1007. (org-at-radio-list-p)))
  1008. (should
  1009. (org-test-with-temp-text "#+attr_org: :radio t\n- foo\n<point>- bar"
  1010. (org-at-radio-list-p)))
  1011. (should
  1012. (org-test-with-temp-text "#+ATTR_ORG: :radio t\n<point>- foo"
  1013. (org-at-radio-list-p)))
  1014. (should
  1015. (org-test-with-temp-text "#+attr_org: :radio bar\n<point>- foo"
  1016. (org-at-radio-list-p)))
  1017. (should-not
  1018. (org-test-with-temp-text "#+attr_org: :radio nil\n<point>- foo"
  1019. (org-at-radio-list-p)))
  1020. (should-not
  1021. (org-test-with-temp-text "<point>- foo"
  1022. (org-at-radio-list-p)))
  1023. (should-not
  1024. (org-test-with-temp-text "#+attr_org: :radio t\n- foo\n <point>bar"
  1025. (org-at-radio-list-p)))
  1026. (should-not
  1027. (org-test-with-temp-text
  1028. "#+attr_org: :radio t\n#+begin_example\n<point>- foo\n#+end_example"
  1029. (org-at-radio-list-p))))
  1030. ;;; Miscellaneous
  1031. (ert-deftest test-org-list/toggle-item ()
  1032. "Test `org-toggle-item' specifications."
  1033. ;; Convert normal lines to items.
  1034. (should
  1035. (equal "- line"
  1036. (org-test-with-temp-text "line"
  1037. (org-toggle-item nil)
  1038. (buffer-string))))
  1039. ;; Convert items to normal lines.
  1040. (should
  1041. (equal "line"
  1042. (org-test-with-temp-text "- line"
  1043. (org-toggle-item nil)
  1044. (buffer-string))))
  1045. ;; Convert headlines to items.
  1046. (should
  1047. (equal "- line"
  1048. (org-test-with-temp-text "* line"
  1049. (org-toggle-item nil)
  1050. (buffer-string))))
  1051. ;; When converting a headline to a list item, TODO keywords become
  1052. ;; checkboxes.
  1053. (should
  1054. (equal "- [X] line"
  1055. (org-test-with-temp-text "* DONE line"
  1056. (org-toggle-item nil)
  1057. (buffer-string))))
  1058. (should
  1059. (equal "- [ ] line"
  1060. (org-test-with-temp-text "* TODO line"
  1061. (org-toggle-item nil)
  1062. (buffer-string))))
  1063. ;; When turning headlines into items, make sure planning info line
  1064. ;; and properties drawers are removed. This also includes empty
  1065. ;; lines following them.
  1066. (should
  1067. (equal "- H\n"
  1068. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29 Thu>"
  1069. (org-toggle-item nil)
  1070. (buffer-string))))
  1071. (should
  1072. (equal "- H\n"
  1073. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  1074. (org-toggle-item nil)
  1075. (buffer-string))))
  1076. (should
  1077. (equal "- H\nText"
  1078. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n\nText"
  1079. (org-toggle-item nil)
  1080. (buffer-string))))
  1081. ;; When no region is marked and point is on a blank line
  1082. ;; only operate on current line.
  1083. (should
  1084. (equal " \n* H :tag:"
  1085. (org-test-with-temp-text "<point> \n* H :tag:"
  1086. (org-toggle-item nil)
  1087. (buffer-string))))
  1088. ;; When a region is marked and first line is a headline, all
  1089. ;; headlines are turned into items.
  1090. (should
  1091. (equal "- H1\n - H2"
  1092. (org-test-with-temp-text "* H1\n** H2"
  1093. (transient-mark-mode 1)
  1094. (push-mark (point) t t)
  1095. (goto-char (point-max))
  1096. (org-toggle-item nil)
  1097. (buffer-string))))
  1098. (should
  1099. (equal "- [ ] H1\n - [ ] H2"
  1100. (org-test-with-temp-text "* TODO H1\n** TODO H2"
  1101. (transient-mark-mode 1)
  1102. (push-mark (point) t t)
  1103. (goto-char (point-max))
  1104. (org-toggle-item nil)
  1105. (buffer-string))))
  1106. ;; When turning headlines into items, make sure headings contents
  1107. ;; are kept within items.
  1108. (should
  1109. (equal "- H1\n Text"
  1110. (org-test-with-temp-text "* H1\nText"
  1111. (transient-mark-mode 1)
  1112. (push-mark (point) t t)
  1113. (goto-char (point-max))
  1114. (org-toggle-item nil)
  1115. (buffer-string))))
  1116. ;; When a region is marked and first line is an item, all items are
  1117. ;; turned into normal lines.
  1118. (should
  1119. (equal "1\n 2"
  1120. (org-test-with-temp-text "- 1\n - 2"
  1121. (transient-mark-mode 1)
  1122. (push-mark (point) t t)
  1123. (goto-char (point-max))
  1124. (org-toggle-item nil)
  1125. (buffer-string))))
  1126. (should
  1127. (equal "1\n2"
  1128. (org-test-with-temp-text "- 1\n2"
  1129. (transient-mark-mode 1)
  1130. (push-mark (point) t t)
  1131. (goto-char (point-max))
  1132. (org-toggle-item nil)
  1133. (buffer-string))))
  1134. ;; When a region is marked and first line is an item, all normal
  1135. ;; lines are turned into items.
  1136. (should
  1137. (equal "- line 1\n- line 2"
  1138. (org-test-with-temp-text "line 1\nline 2"
  1139. (transient-mark-mode 1)
  1140. (push-mark (point) t t)
  1141. (goto-char (point-max))
  1142. (org-toggle-item nil)
  1143. (buffer-string))))
  1144. (should
  1145. (equal "- line 1\n- line 2"
  1146. (org-test-with-temp-text "line 1\n- line 2"
  1147. (transient-mark-mode 1)
  1148. (push-mark (point) t t)
  1149. (goto-char (point-max))
  1150. (org-toggle-item nil)
  1151. (buffer-string))))
  1152. ;; When argument ARG is non-nil, change the whole region into
  1153. ;; a single item.
  1154. (should
  1155. (equal "- line 1\n line 2"
  1156. (org-test-with-temp-text "line 1\nline 2"
  1157. (transient-mark-mode 1)
  1158. (push-mark (point) t t)
  1159. (goto-char (point-max))
  1160. (org-toggle-item t)
  1161. (buffer-string)))))
  1162. (ert-deftest test-org-list/sort ()
  1163. "Test `org-sort-list'."
  1164. ;; Sort alphabetically.
  1165. (let ((original-string-collate-lessp (symbol-function 'string-collate-lessp)))
  1166. (cl-letf (((symbol-function 'string-collate-lessp)
  1167. (lambda (s1 s2 &optional locale ignore-case)
  1168. (funcall original-string-collate-lessp
  1169. s1 s2 "C" ignore-case))))
  1170. (should
  1171. (equal "- abc\n- def\n- XYZ\n"
  1172. (org-test-with-temp-text "- def\n- XYZ\n- abc\n"
  1173. (org-sort-list nil ?a)
  1174. (buffer-string))))
  1175. (should
  1176. (equal "- XYZ\n- def\n- abc\n"
  1177. (org-test-with-temp-text "- def\n- XYZ\n- abc\n"
  1178. (org-sort-list nil ?A)
  1179. (buffer-string))))
  1180. ;; Sort alphabetically (with case).
  1181. (should
  1182. (equal "- C\n- a\n- b\n"
  1183. (org-test-with-temp-text "- b\n- C\n- a\n"
  1184. (org-sort-list t ?a)
  1185. (buffer-string))))
  1186. (should
  1187. (equal "- b\n- a\n- C\n"
  1188. (org-test-with-temp-text "- b\n- C\n- a\n"
  1189. (org-sort-list t ?A)
  1190. (buffer-string))))))
  1191. ;; Sort numerically.
  1192. (should
  1193. (equal "- 1\n- 2\n- 10\n"
  1194. (org-test-with-temp-text "- 10\n- 1\n- 2\n"
  1195. (org-sort-list nil ?n)
  1196. (buffer-string))))
  1197. (should
  1198. (equal "- 10\n- 2\n- 1\n"
  1199. (org-test-with-temp-text "- 10\n- 1\n- 2\n"
  1200. (org-sort-list nil ?N)
  1201. (buffer-string))))
  1202. ;; Sort by checked status.
  1203. (should
  1204. (equal "- [ ] xyz\n- [ ] def\n- [X] abc\n"
  1205. (org-test-with-temp-text "- [X] abc\n- [ ] xyz\n- [ ] def\n"
  1206. (org-sort-list nil ?x)
  1207. (buffer-string))))
  1208. (should
  1209. (equal "- [X] abc\n- [ ] xyz\n- [ ] def\n"
  1210. (org-test-with-temp-text "- [X] abc\n- [ ] xyz\n- [ ] def\n"
  1211. (org-sort-list nil ?X)
  1212. (buffer-string))))
  1213. ;; Sort by time stamp.
  1214. (should
  1215. (equal "- <2017-05-08 Mon>\n- <2017-05-09 Tue>\n- <2018-05-09 Wed>\n"
  1216. (org-test-with-temp-text
  1217. "- <2018-05-09 Wed>\n- <2017-05-09 Tue>\n- <2017-05-08 Mon>\n"
  1218. (org-sort-list nil ?t)
  1219. (buffer-string))))
  1220. (should
  1221. (equal "- <2018-05-09 Wed>\n- <2017-05-09 Tue>\n- <2017-05-08 Mon>\n"
  1222. (org-test-with-temp-text
  1223. "- <2018-05-09 Wed>\n- <2017-05-09 Tue>\n- <2017-05-08 Mon>\n"
  1224. (org-sort-list nil ?T)
  1225. (buffer-string))))
  1226. ;; Sort by custom function.
  1227. (should
  1228. (equal "- b\n- aa\n- ccc\n"
  1229. (org-test-with-temp-text "- ccc\n- b\n- aa\n"
  1230. (org-sort-list nil ?f
  1231. (lambda ()
  1232. (length (buffer-substring (point-at-bol)
  1233. (point-at-eol))))
  1234. #'<)
  1235. (buffer-string))))
  1236. (should
  1237. (equal "- ccc\n- aa\n- b\n"
  1238. (org-test-with-temp-text "- ccc\n- b\n- aa\n"
  1239. (org-sort-list nil ?F
  1240. (lambda ()
  1241. (length (buffer-substring (point-at-bol)
  1242. (point-at-eol))))
  1243. #'<)
  1244. (buffer-string)))))
  1245. ;;; List transformations
  1246. (ert-deftest test-org-list/to-generic ()
  1247. "Test `org-list-to-generic' specifications."
  1248. ;; Test `:ustart' and `:uend' parameters.
  1249. (should
  1250. (equal
  1251. "begin\na"
  1252. (org-test-with-temp-text "- a"
  1253. (org-list-to-generic (org-list-to-lisp) '(:ustart "begin")))))
  1254. (should-not
  1255. (equal
  1256. "begin\na"
  1257. (org-test-with-temp-text "1. a"
  1258. (org-list-to-generic (org-list-to-lisp) '(:ustart "begin")))))
  1259. (should
  1260. (equal
  1261. "a\nend"
  1262. (org-test-with-temp-text "- a"
  1263. (org-list-to-generic (org-list-to-lisp) '(:uend "end")))))
  1264. (should-not
  1265. (equal
  1266. "a\nend"
  1267. (org-test-with-temp-text "1. a"
  1268. (org-list-to-generic (org-list-to-lisp) '(:uend "end")))))
  1269. (should
  1270. (equal
  1271. "begin l1\na\nbegin l2\nb\nend l2\nend l1"
  1272. (org-test-with-temp-text "- a\n - b"
  1273. (org-list-to-generic
  1274. (org-list-to-lisp)
  1275. (list :ustart (lambda (l) (format "begin l%d" l))
  1276. :uend (lambda (l) (format "end l%d" l)))))))
  1277. ;; Test `:ostart' and `:oend' parameters.
  1278. (should
  1279. (equal
  1280. "begin\na"
  1281. (org-test-with-temp-text "1. a"
  1282. (org-list-to-generic (org-list-to-lisp) '(:ostart "begin")))))
  1283. (should-not
  1284. (equal
  1285. "begin\na"
  1286. (org-test-with-temp-text "- a"
  1287. (org-list-to-generic (org-list-to-lisp) '(:ostart "begin")))))
  1288. (should
  1289. (equal
  1290. "a\nend"
  1291. (org-test-with-temp-text "1. a"
  1292. (org-list-to-generic (org-list-to-lisp) '(:oend "end")))))
  1293. (should-not
  1294. (equal
  1295. "a\nend"
  1296. (org-test-with-temp-text "- a"
  1297. (org-list-to-generic (org-list-to-lisp) '(:oend "end")))))
  1298. (should
  1299. (equal
  1300. "begin l1\na\nbegin l2\nb\nend l2\nend l1"
  1301. (org-test-with-temp-text "1. a\n 1. b"
  1302. (org-list-to-generic
  1303. (org-list-to-lisp)
  1304. (list :ostart (lambda (l) (format "begin l%d" l))
  1305. :oend (lambda (l) (format "end l%d" l)))))))
  1306. ;; Test `:dstart' and `:dend' parameters.
  1307. (should
  1308. (equal
  1309. "begin\ntaga"
  1310. (org-test-with-temp-text "- tag :: a"
  1311. (org-list-to-generic (org-list-to-lisp) '(:dstart "begin")))))
  1312. (should-not
  1313. (equal
  1314. "begin\na"
  1315. (org-test-with-temp-text "- a"
  1316. (org-list-to-generic (org-list-to-lisp) '(:dstart "begin")))))
  1317. (should
  1318. (equal
  1319. "taga\nend"
  1320. (org-test-with-temp-text "- tag :: a"
  1321. (org-list-to-generic (org-list-to-lisp) '(:dend "end")))))
  1322. (should-not
  1323. (equal
  1324. "a\nend"
  1325. (org-test-with-temp-text "- a"
  1326. (org-list-to-generic (org-list-to-lisp) '(:dend "end")))))
  1327. (should
  1328. (equal
  1329. "begin l1\ntag1a\nbegin l2\ntag2b\nend l2\nend l1"
  1330. (org-test-with-temp-text "- tag1 :: a\n - tag2 :: b"
  1331. (org-list-to-generic
  1332. (org-list-to-lisp)
  1333. (list :dstart (lambda (l) (format "begin l%d" l))
  1334. :dend (lambda (l) (format "end l%d" l)))))))
  1335. ;; Test `:dtstart', `:dtend', `:ddstart' and `:ddend' parameters.
  1336. (should
  1337. (equal
  1338. ">tag<a"
  1339. (org-test-with-temp-text "- tag :: a"
  1340. (org-list-to-generic (org-list-to-lisp) '(:dtstart ">" :dtend "<")))))
  1341. (should
  1342. (equal
  1343. "tag>a<"
  1344. (org-test-with-temp-text "- tag :: a"
  1345. (org-list-to-generic (org-list-to-lisp) '(:ddstart ">" :ddend "<")))))
  1346. ;; Test `:istart' and `:iend' parameters.
  1347. (should
  1348. (equal
  1349. "starta"
  1350. (org-test-with-temp-text "- a"
  1351. (org-list-to-generic (org-list-to-lisp) '(:istart "start")))))
  1352. (should
  1353. (equal
  1354. "level1 a\nlevel2 b"
  1355. (org-test-with-temp-text "- a\n - b"
  1356. (org-list-to-generic (org-list-to-lisp)
  1357. '(:istart (lambda (type l) (format "level%d "l)))))))
  1358. (should
  1359. (equal
  1360. "a\nblevel2level1"
  1361. (org-test-with-temp-text "- a\n - b"
  1362. (org-list-to-generic (org-list-to-lisp)
  1363. '(:iend (lambda (type l) (format "level%d" l)))))))
  1364. ;; Test `:icount' parameter.
  1365. (should
  1366. (equal
  1367. "counta"
  1368. (org-test-with-temp-text "1. [@3] a"
  1369. (org-list-to-generic (org-list-to-lisp) '(:icount "count")))))
  1370. (should-not
  1371. (equal
  1372. "counta"
  1373. (org-test-with-temp-text "1. a"
  1374. (org-list-to-generic (org-list-to-lisp) '(:icount "count")))))
  1375. (should
  1376. (equal
  1377. "counta"
  1378. (org-test-with-temp-text "1. [@3] a"
  1379. (org-list-to-generic (org-list-to-lisp)
  1380. '(:icount "count" :istart "start")))))
  1381. (should
  1382. (equal
  1383. "level:1, counter:3 a"
  1384. (org-test-with-temp-text "1. [@3] a"
  1385. (org-list-to-generic
  1386. (org-list-to-lisp)
  1387. '(:icount (lambda (type l c) (format "level:%d, counter:%d " l c)))))))
  1388. ;; Test `:isep' parameter.
  1389. (should
  1390. (equal
  1391. "a\n--\nb"
  1392. (org-test-with-temp-text "- a\n- b"
  1393. (org-list-to-generic (org-list-to-lisp) '(:isep "--")))))
  1394. (should-not
  1395. (equal
  1396. "a\n--\nb"
  1397. (org-test-with-temp-text "- a\n - b"
  1398. (org-list-to-generic (org-list-to-lisp) '(:isep "--")))))
  1399. (should
  1400. (equal
  1401. "a\n- 1 -\nb"
  1402. (org-test-with-temp-text "- a\n- b"
  1403. (org-list-to-generic
  1404. (org-list-to-lisp)
  1405. '(:isep (lambda (type depth) (format "- %d -" depth)))))))
  1406. ;; Test `:ifmt' parameter.
  1407. (should
  1408. (equal
  1409. ">> a <<"
  1410. (org-test-with-temp-text "1. [@3] a"
  1411. (org-list-to-generic
  1412. (org-list-to-lisp)
  1413. '(:ifmt (lambda (type c) (format ">> %s <<" c)))))))
  1414. ;; Test `:cbon', `:cboff', `:cbtrans'
  1415. (should
  1416. (equal
  1417. "!a"
  1418. (org-test-with-temp-text "- [X] a"
  1419. (org-list-to-generic (org-list-to-lisp) '(:cbon "!")))))
  1420. (should-not
  1421. (equal
  1422. "!a"
  1423. (org-test-with-temp-text "- [X] a"
  1424. (org-list-to-generic (org-list-to-lisp) '(:cboff "!" :cbtrans "!")))))
  1425. (should
  1426. (equal
  1427. "!a"
  1428. (org-test-with-temp-text "- [ ] a"
  1429. (org-list-to-generic (org-list-to-lisp) '(:cboff "!")))))
  1430. (should-not
  1431. (equal
  1432. "!a"
  1433. (org-test-with-temp-text "- [ ] a"
  1434. (org-list-to-generic (org-list-to-lisp) '(:cbon "!" :cbtrans "!")))))
  1435. (should
  1436. (equal
  1437. "!a"
  1438. (org-test-with-temp-text "- [-] a"
  1439. (org-list-to-generic (org-list-to-lisp) '(:cbtrans "!")))))
  1440. (should-not
  1441. (equal
  1442. "!a"
  1443. (org-test-with-temp-text "- [-] a"
  1444. (org-list-to-generic (org-list-to-lisp) '(:cbon "!" :cboff "!")))))
  1445. ;; Test `:splice' parameter.
  1446. (should
  1447. (equal
  1448. "a"
  1449. (org-test-with-temp-text "- a"
  1450. (org-list-to-generic (org-list-to-lisp)
  1451. '(:ustart "begin" :uend "end" :splice t)))))
  1452. ;; No error on empty lists.
  1453. (should
  1454. (org-test-with-temp-text "-" (org-list-to-generic (org-list-to-lisp) nil))))
  1455. (ert-deftest test-org-list/to-html ()
  1456. "Test `org-list-to-html' specifications."
  1457. (should
  1458. (equal "<ul class=\"org-ul\">\n<li>a</li>\n</ul>"
  1459. (org-test-with-temp-text "- a"
  1460. (org-list-to-html (org-list-to-lisp) nil)))))
  1461. (ert-deftest test-org-list/to-latex ()
  1462. "Test `org-list-to-latex' specifications."
  1463. (should
  1464. (equal "\\begin{itemize}\n\\item a\n\\end{itemize}"
  1465. (org-test-with-temp-text "- a"
  1466. (org-list-to-latex (org-list-to-lisp) nil)))))
  1467. (ert-deftest test-org-list/to-texinfo ()
  1468. "Test `org-list-to-texinfo' specifications."
  1469. (should
  1470. (equal "@itemize\n@item\na\n@end itemize"
  1471. (org-test-with-temp-text "- a"
  1472. (org-list-to-texinfo (org-list-to-lisp) nil)))))
  1473. (ert-deftest test-org-list/to-org ()
  1474. "Test `org-list-to-org' specifications."
  1475. ;; Un-ordered list.
  1476. (should
  1477. (equal "- a"
  1478. (org-test-with-temp-text "- a"
  1479. (org-list-to-org (org-list-to-lisp) nil))))
  1480. ;; Ordered list.
  1481. (should
  1482. (equal "1. a"
  1483. (org-test-with-temp-text "1. a"
  1484. (org-list-to-org (org-list-to-lisp) nil))))
  1485. ;; Descriptive list.
  1486. (should
  1487. (equal "- a :: b"
  1488. (org-test-with-temp-text "- a :: b"
  1489. (org-list-to-org (org-list-to-lisp) nil))))
  1490. ;; Nested list.
  1491. (should
  1492. (equal "- a\n - b"
  1493. (org-test-with-temp-text "- a\n - b"
  1494. (org-list-to-org (org-list-to-lisp) nil))))
  1495. ;; Item spanning over multiple lines.
  1496. (should
  1497. (equal "- a\n b"
  1498. (org-test-with-temp-text "- a\n b"
  1499. (org-list-to-org (org-list-to-lisp) nil))))
  1500. ;; Item with continuation text after a sub-list.
  1501. (should
  1502. (equal "- a\n - b\n c"
  1503. (org-test-with-temp-text "- a\n - b\n c"
  1504. (org-list-to-org (org-list-to-lisp) nil)))))
  1505. (provide 'test-org-list)
  1506. ;;; test-org-list.el ends here