test-org-colview.el 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  1. ;;; test-org-colview.el --- Tests for org-colview.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2016, 2019 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  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. ;;; Column view
  16. (require 'cl-lib)
  17. (ert-deftest test-org-colview/get-format ()
  18. "Test `org-columns-get-format' specifications."
  19. ;; Without any clue, use `org-columns-default-format'.
  20. (should
  21. (equal "%A"
  22. (org-test-with-temp-text "* H"
  23. (let ((org-columns-default-format "%A"))
  24. (org-columns-get-format)))))
  25. ;; If COLUMNS keyword is set, use it.
  26. (should
  27. (equal "%B"
  28. (org-test-with-temp-text "#+COLUMNS: %B\n* H"
  29. (let ((org-columns-default-format "%A"))
  30. (org-columns-get-format)))))
  31. (should
  32. (equal "%B"
  33. (org-test-with-temp-text "#+columns: %B\n* H"
  34. (let ((org-columns-default-format "%A"))
  35. (org-columns-get-format)))))
  36. (should
  37. (equal "%B"
  38. (org-test-with-temp-text "* H\n#+COLUMNS: %B"
  39. (let ((org-columns-default-format "%A"))
  40. (org-columns-get-format)))))
  41. ;; When :COLUMNS: property is set somewhere in the tree, use it over
  42. ;; the previous ways.
  43. (should
  44. (equal
  45. "%C"
  46. (org-test-with-temp-text
  47. "#+COLUMNS: %B\n* H\n:PROPERTIES:\n:COLUMNS: %C\n:END:\n** S\n<point>"
  48. (let ((org-columns-default-format "%A"))
  49. (org-columns-get-format)))))
  50. ;; When optional argument is provided, prefer it.
  51. (should
  52. (equal
  53. "%D"
  54. (org-test-with-temp-text
  55. "#+COLUMNS: %B\n* H\n:PROPERTIES:\n:COLUMNS: %C\n:END:\n** S\n<point>"
  56. (let ((org-columns-default-format "%A"))
  57. (org-columns-get-format "%D"))))))
  58. (ert-deftest test-org-colview/columns-scope ()
  59. "Test `org-columns' scope."
  60. ;; Before the first headline, view all document.
  61. (should
  62. (equal
  63. '("H1" "H2" "H3")
  64. (org-test-with-temp-text "Top\n* H1\n** H2\n* H3"
  65. (let ((org-columns-default-format "%ITEM")) (org-columns))
  66. (org-map-entries
  67. (lambda () (get-char-property (point) 'org-columns-value))))))
  68. ;; When :COLUMNS: is set up in the hierarchy, view tree starting
  69. ;; there.
  70. (should
  71. (equal
  72. '(nil "H2" "H3" nil)
  73. (org-test-with-temp-text
  74. "* H1\n** H2\n:PROPERTIES:\n:COLUMNS: %ITEM\n:END:\n*** <point>H3\n* H4"
  75. (let ((org-columns-default-format "%ITEM")) (org-columns))
  76. (org-map-entries
  77. (lambda () (get-char-property (point) 'org-columns-value))))))
  78. ;; Otherwise, view tree starting at the current headline.
  79. (should
  80. (equal
  81. '(nil "H2" "H3" nil)
  82. (org-test-with-temp-text "Top\n* H1\n** <point>H2\n*** H3\n* H4"
  83. (let ((org-columns-default-format "%ITEM")) (org-columns))
  84. (org-map-entries
  85. (lambda () (get-char-property (point) 'org-columns-value))))))
  86. ;; With a non-nil prefix argument, always view all document.
  87. (should
  88. (equal
  89. '("H1" "H2" "H3" "H4")
  90. (org-test-with-temp-text
  91. "* H1\n** H2\n:PROPERTIES:\n:COLUMNS: %ITEM\n:END:\n*** <point>H3\n* H4"
  92. (let ((org-columns-default-format "%ITEM")) (org-columns t))
  93. (org-map-entries
  94. (lambda () (get-char-property (point) 'org-columns-value))))))
  95. (should
  96. (equal
  97. '("1" "1")
  98. (org-test-with-temp-text
  99. "Top\n* H1\n** <point>H2\n:PROPERTIES:\n:A: 1\n:END:"
  100. (let ((org-columns-default-format "%A{+}")) (org-columns t))
  101. (org-map-entries
  102. (lambda () (get-char-property (point) 'org-columns-value)))))))
  103. (ert-deftest test-org-colview/columns-width ()
  104. "Test `org-columns' column widths."
  105. ;; When a width is specified in the format, use it.
  106. (should
  107. (= 9
  108. (org-test-with-temp-text "* H"
  109. (let ((org-columns-default-format "%9ITEM")) (org-columns))
  110. (aref org-columns-current-maxwidths 0))))
  111. ;; Otherwise, use the width of the largest value in the column.
  112. (should
  113. (= 2
  114. (org-test-with-temp-text
  115. "* H\n:PROPERTIES:\n:P: X\n:END:\n** H2\n:PROPERTIES:\n:P: XX\n:END:"
  116. (let ((org-columns-default-format "%P")) (org-columns))
  117. (aref org-columns-current-maxwidths 0))))
  118. ;; If the title is wider than the widest value, use title width
  119. ;; instead.
  120. (should
  121. (= 4
  122. (org-test-with-temp-text "* H"
  123. (let ((org-columns-default-format "%ITEM")) (org-columns))
  124. (aref org-columns-current-maxwidths 0))))
  125. ;; Special case: stars do count for ITEM.
  126. (should
  127. (= 6
  128. (org-test-with-temp-text "* Head"
  129. (let ((org-columns-default-format "%ITEM")) (org-columns))
  130. (aref org-columns-current-maxwidths 0))))
  131. ;; Special case: width takes into account link narrowing in ITEM.
  132. (should
  133. (equal
  134. '("* 123" . 5)
  135. (org-test-with-temp-text "* [[https://orgmode.org][123]]"
  136. (let ((org-columns-default-format "%ITEM")) (org-columns))
  137. (cons (get-char-property (point) 'org-columns-value-modified)
  138. (aref org-columns-current-maxwidths 0)))))
  139. ;; When a value is too wide for the current column, add ellipses.
  140. ;; Take into consideration length of `org-columns-ellipses'.
  141. (should
  142. (equal "123.. |"
  143. (org-test-with-temp-text "* H\n:PROPERTIES:\n:P: 123456\n:END:"
  144. (let ((org-columns-default-format "%5P")
  145. (org-columns-ellipses ".."))
  146. (org-columns))
  147. (org-trim (get-char-property (point) 'display)))))
  148. (should
  149. (equal "1234… |"
  150. (org-test-with-temp-text "* H\n:PROPERTIES:\n:P: 123456\n:END:"
  151. (let ((org-columns-default-format "%5P")
  152. (org-columns-ellipses "…"))
  153. (org-columns))
  154. (org-trim (get-char-property (point) 'display))))))
  155. (ert-deftest test-org-colview/columns-summary ()
  156. "Test `org-columns' summary types."
  157. ;; {+} and {+;format} add numbers.
  158. (should
  159. (equal
  160. "3"
  161. (org-test-with-temp-text
  162. "* H
  163. ** S1
  164. :PROPERTIES:
  165. :A: 1
  166. :END:
  167. ** S1
  168. :PROPERTIES:
  169. :A: 2
  170. :END:"
  171. (let ((org-columns-default-format "%A{+}")) (org-columns))
  172. (get-char-property (point) 'org-columns-value-modified))))
  173. (should
  174. (equal
  175. "3.0"
  176. (org-test-with-temp-text
  177. "* H
  178. ** S1
  179. :PROPERTIES:
  180. :A: 1
  181. :END:
  182. ** S1
  183. :PROPERTIES:
  184. :A: 2
  185. :END:"
  186. (let ((org-columns-default-format "%A{+;%.1f}")) (org-columns))
  187. (get-char-property (point) 'org-columns-value-modified))))
  188. ;; {$} is a shortcut for {+;%.2f}.
  189. (should
  190. (equal
  191. "3.60"
  192. (org-test-with-temp-text
  193. "* H
  194. ** S1
  195. :PROPERTIES:
  196. :A: 1.50
  197. :END:
  198. ** S1
  199. :PROPERTIES:
  200. :A: 2.10
  201. :END:"
  202. (let ((org-columns-default-format "%A{$}")) (org-columns))
  203. (get-char-property (point) 'org-columns-value-modified))))
  204. ;; Obey to format string even in leaf values.
  205. (should
  206. (equal
  207. "1.0"
  208. (org-test-with-temp-text
  209. "* H
  210. :PROPERTIES:
  211. :A: 1
  212. :END:"
  213. (let ((org-columns-default-format "%A{+;%.1f}")) (org-columns))
  214. (get-char-property (point) 'org-columns-value-modified))))
  215. ;; {:} sums times. Plain numbers are minutes.
  216. (should
  217. (equal
  218. "4:10"
  219. (org-test-with-temp-text
  220. "* H
  221. ** S1
  222. :PROPERTIES:
  223. :A: 1:30
  224. :END:
  225. ** S1
  226. :PROPERTIES:
  227. :A: 2:40
  228. :END:"
  229. (let ((org-columns-default-format "%A{:}")) (org-columns))
  230. (get-char-property (point) 'org-columns-value-modified))))
  231. (should
  232. (equal
  233. "1:32"
  234. (org-test-with-temp-text
  235. "* H
  236. ** S1
  237. :PROPERTIES:
  238. :A: 1:30
  239. :END:
  240. ** S1
  241. :PROPERTIES:
  242. :A: 2
  243. :END:"
  244. (let ((org-columns-default-format "%A{:}")) (org-columns))
  245. (get-char-property (point) 'org-columns-value-modified))))
  246. ;; {X}, {X/} and {X%} indicate checkbox status.
  247. (should
  248. (equal
  249. "[ ]"
  250. (org-test-with-temp-text
  251. "* H
  252. ** S1
  253. :PROPERTIES:
  254. :A: [ ]
  255. :END:
  256. ** S1
  257. :PROPERTIES:
  258. :A: [ ]
  259. :END:"
  260. (let ((org-columns-default-format "%A{X}")) (org-columns))
  261. (get-char-property (point) 'org-columns-value-modified))))
  262. (should
  263. (equal
  264. "[-]"
  265. (org-test-with-temp-text
  266. "* H
  267. ** S1
  268. :PROPERTIES:
  269. :A: [ ]
  270. :END:
  271. ** S1
  272. :PROPERTIES:
  273. :A: [X]
  274. :END:"
  275. (let ((org-columns-default-format "%A{X}")) (org-columns))
  276. (get-char-property (point) 'org-columns-value-modified))))
  277. (should
  278. (equal
  279. "[X]"
  280. (org-test-with-temp-text
  281. "* H
  282. ** S1
  283. :PROPERTIES:
  284. :A: [X]
  285. :END:
  286. ** S1
  287. :PROPERTIES:
  288. :A: [X]
  289. :END:"
  290. (let ((org-columns-default-format "%A{X}")) (org-columns))
  291. (get-char-property (point) 'org-columns-value-modified))))
  292. (should
  293. (equal
  294. "[1/2]"
  295. (org-test-with-temp-text
  296. "* H
  297. ** S1
  298. :PROPERTIES:
  299. :A: [ ]
  300. :END:
  301. ** S1
  302. :PROPERTIES:
  303. :A: [X]
  304. :END:"
  305. (let ((org-columns-default-format "%A{X/}")) (org-columns))
  306. (get-char-property (point) 'org-columns-value-modified))))
  307. (should
  308. (equal
  309. "[50%]"
  310. (org-test-with-temp-text
  311. "* H
  312. ** S1
  313. :PROPERTIES:
  314. :A: [ ]
  315. :END:
  316. ** S1
  317. :PROPERTIES:
  318. :A: [X]
  319. :END:"
  320. (let ((org-columns-default-format "%A{X%}")) (org-columns))
  321. (get-char-property (point) 'org-columns-value-modified))))
  322. ;; {X/} handles recursive summaries.
  323. (should
  324. (equal
  325. "[1/2]"
  326. (org-test-with-temp-text
  327. "* H
  328. ** S1
  329. :PROPERTIES:
  330. :A: [ ]
  331. :END:
  332. ** S2
  333. *** S21
  334. :PROPERTIES:
  335. :A: [X]
  336. :END:
  337. *** S22
  338. :PROPERTIES:
  339. :A: [X]
  340. :END:"
  341. (let ((org-columns-default-format "%A{X/}")) (org-columns))
  342. (get-char-property (point) 'org-columns-value-modified))))
  343. (should
  344. (equal
  345. "[1/2]"
  346. (org-test-with-temp-text
  347. "* H
  348. ** S1
  349. :PROPERTIES:
  350. :A: [X]
  351. :END:
  352. ** S2
  353. *** S21
  354. :PROPERTIES:
  355. :A: [ ]
  356. :END:
  357. *** S22
  358. :PROPERTIES:
  359. :A: [ ]
  360. :END:"
  361. (let ((org-columns-default-format "%A{X/}")) (org-columns))
  362. (get-char-property (point) 'org-columns-value-modified))))
  363. ;; {X%} handles recursive summaries.
  364. (should
  365. (equal
  366. "[50%]"
  367. (org-test-with-temp-text
  368. "* H
  369. ** S1
  370. :PROPERTIES:
  371. :A: [ ]
  372. :END:
  373. ** S2
  374. *** S21
  375. :PROPERTIES:
  376. :A: [X]
  377. :END:
  378. *** S22
  379. :PROPERTIES:
  380. :A: [X]
  381. :END:"
  382. (let ((org-columns-default-format "%A{X%}")) (org-columns))
  383. (get-char-property (point) 'org-columns-value-modified))))
  384. (should
  385. (equal
  386. "[50%]"
  387. (org-test-with-temp-text
  388. "* H
  389. ** S1
  390. :PROPERTIES:
  391. :A: [X]
  392. :END:
  393. ** S2
  394. *** S21
  395. :PROPERTIES:
  396. :A: [ ]
  397. :END:
  398. *** S22
  399. :PROPERTIES:
  400. :A: [ ]
  401. :END:"
  402. (let ((org-columns-default-format "%A{X%}")) (org-columns))
  403. (get-char-property (point) 'org-columns-value-modified))))
  404. ;; {min} is the smallest number in column, {max} the largest one.
  405. ;; {mean} is the arithmetic mean of numbers in column.
  406. (should
  407. (equal
  408. "42"
  409. (org-test-with-temp-text
  410. "* H
  411. ** S1
  412. :PROPERTIES:
  413. :A: 99
  414. :END:
  415. ** S1
  416. :PROPERTIES:
  417. :A: 42
  418. :END:"
  419. (let ((org-columns-default-format "%A{min}")) (org-columns))
  420. (get-char-property (point) 'org-columns-value-modified))))
  421. (should
  422. (equal
  423. "99"
  424. (org-test-with-temp-text
  425. "* H
  426. ** S1
  427. :PROPERTIES:
  428. :A: 99
  429. :END:
  430. ** S1
  431. :PROPERTIES:
  432. :A: 42
  433. :END:"
  434. (let ((org-columns-default-format "%A{max}")) (org-columns))
  435. (get-char-property (point) 'org-columns-value-modified))))
  436. (should
  437. (equal
  438. "51.0"
  439. (org-test-with-temp-text
  440. "* H
  441. ** S1
  442. :PROPERTIES:
  443. :A: 60
  444. :END:
  445. ** S1
  446. :PROPERTIES:
  447. :A: 42
  448. :END:"
  449. (let ((org-columns-default-format "%A{mean}")) (org-columns))
  450. (get-char-property (point) 'org-columns-value-modified))))
  451. ;; {:min}, {:max} and {:mean} apply to time values.
  452. (should
  453. (equal
  454. "1:20"
  455. (org-test-with-temp-text
  456. "* H
  457. ** S1
  458. :PROPERTIES:
  459. :A: 4:40
  460. :END:
  461. ** S1
  462. :PROPERTIES:
  463. :A: 1:20
  464. :END:"
  465. (let ((org-columns-default-format "%A{:min}")) (org-columns))
  466. (get-char-property (point) 'org-columns-value-modified))))
  467. (should
  468. (equal
  469. "4:40"
  470. (org-test-with-temp-text
  471. "* H
  472. ** S1
  473. :PROPERTIES:
  474. :A: 4:40
  475. :END:
  476. ** S1
  477. :PROPERTIES:
  478. :A: 1:20
  479. :END:"
  480. (let ((org-columns-default-format "%A{:max}")) (org-columns))
  481. (get-char-property (point) 'org-columns-value-modified))))
  482. (should
  483. (equal
  484. "3:00"
  485. (org-test-with-temp-text
  486. "* H
  487. ** S1
  488. :PROPERTIES:
  489. :A: 4:40
  490. :END:
  491. ** S1
  492. :PROPERTIES:
  493. :A: 1:20
  494. :END:"
  495. (let ((org-columns-default-format "%A{:mean}")) (org-columns))
  496. (get-char-property (point) 'org-columns-value-modified))))
  497. ;; {@min}, {@max} and {@mean} apply to ages.
  498. (should
  499. (equal
  500. "0min"
  501. (org-test-at-time "<2014-03-04 Tue>"
  502. (org-test-with-temp-text
  503. "* H
  504. ** S1
  505. :PROPERTIES:
  506. :A: <2012-03-29 Thu>
  507. :END:
  508. ** S1
  509. :PROPERTIES:
  510. :A: <2014-03-04 Tue>
  511. :END:"
  512. (let ((org-columns-default-format "%A{@min}")) (org-columns))
  513. (get-char-property (point) 'org-columns-value-modified)))))
  514. (should
  515. (equal
  516. "2d"
  517. (org-test-at-time "<2014-03-04 Tue>"
  518. (org-test-with-temp-text
  519. "* H
  520. ** S1
  521. :PROPERTIES:
  522. :A: <2014-03-03 Mon>
  523. :END:
  524. ** S1
  525. :PROPERTIES:
  526. :A: <2014-03-02 Sun>
  527. :END:"
  528. (let ((org-columns-default-format "%A{@max}")) (org-columns))
  529. (get-char-property (point) 'org-columns-value-modified)))))
  530. (should
  531. (equal
  532. "1d 12h"
  533. (org-test-at-time "<2014-03-04 Tue>"
  534. (org-test-with-temp-text
  535. "* H
  536. ** S1
  537. :PROPERTIES:
  538. :A: <2014-03-03 Mon>
  539. :END:
  540. ** S1
  541. :PROPERTIES:
  542. :A: <2014-03-02 Sun>
  543. :END:"
  544. (let ((org-columns-default-format "%A{@mean}")) (org-columns))
  545. (get-char-property (point) 'org-columns-value-modified)))))
  546. ;; If a time value is expressed as a duration, return a duration.
  547. ;; If any of them follows H:MM:SS pattern, use it too. Also handle
  548. ;; combinations of duration and H:MM:SS patterns.
  549. (should
  550. (equal
  551. "3d 4:20"
  552. (org-test-with-temp-text
  553. "* H
  554. ** S1
  555. :PROPERTIES:
  556. :A: 3d 3h
  557. :END:
  558. ** S1
  559. :PROPERTIES:
  560. :A: 1:20
  561. :END:"
  562. (let ((org-columns-default-format "%A{:}")
  563. (org-duration-units '(("d" . 1440) ("h" . 60)))
  564. (org-duration-format '(("d" . nil) (special . h:mm))))
  565. (org-columns))
  566. (get-char-property (point) 'org-columns-value-modified))))
  567. (should
  568. (equal
  569. "6:00:10"
  570. (org-test-with-temp-text
  571. "* H
  572. ** S1
  573. :PROPERTIES:
  574. :A: 4:40:10
  575. :END:
  576. ** S1
  577. :PROPERTIES:
  578. :A: 1:20
  579. :END:"
  580. (let ((org-columns-default-format "%A{:}")) (org-columns))
  581. (get-char-property (point) 'org-columns-value-modified))))
  582. (should
  583. (equal
  584. "3d 4:20"
  585. (org-test-with-temp-text
  586. "* H
  587. ** S1
  588. :PROPERTIES:
  589. :A: 3d 3h
  590. :END:
  591. ** S1
  592. :PROPERTIES:
  593. :A: 0d 1:20
  594. :END:"
  595. (let ((org-columns-default-format "%A{:}")
  596. (org-duration-units '(("d" . 1440) ("h" . 60)))
  597. (org-duration-format '(("d" . nil) (special . h:mm))))
  598. (org-columns))
  599. (get-char-property (point) 'org-columns-value-modified))))
  600. ;; @min, @max and @mean also accept regular duration.
  601. (should
  602. (equal
  603. "1d 10h"
  604. (org-test-with-temp-text
  605. "* H
  606. ** S1
  607. :PROPERTIES:
  608. :A: 1d 10h 0min
  609. :END:
  610. ** S1
  611. :PROPERTIES:
  612. :A: 5d 3h
  613. :END:"
  614. (let ((org-columns-default-format "%A{@min}")) (org-columns))
  615. (get-char-property (point) 'org-columns-value-modified))))
  616. ;; {est+} gives a low-high estimate using mean and standard
  617. ;; deviation.
  618. (should
  619. (equal
  620. "3-17"
  621. (org-test-with-temp-text
  622. "* H
  623. ** S1
  624. :PROPERTIES:
  625. :A: 0-10
  626. :END:
  627. ** S1
  628. :PROPERTIES:
  629. :A: 0-10
  630. :END:"
  631. (let ((org-columns-default-format "%A{est+}")) (org-columns))
  632. (get-char-property (point) 'org-columns-value-modified))))
  633. ;; When using {est+} summary, a single number is understood as
  634. ;; a degenerate range.
  635. (should
  636. (equal
  637. "4-4"
  638. (org-test-with-temp-text
  639. "* H
  640. ** S1
  641. :PROPERTIES:
  642. :A: 4
  643. :END:
  644. "
  645. (let ((org-columns-default-format "%A{est+}")) (org-columns))
  646. (get-char-property (point) 'org-columns-value-modified))))
  647. ;; Allow custom summary types.
  648. (should
  649. (equal
  650. "1|2"
  651. (org-test-with-temp-text
  652. "* H
  653. ** S1
  654. :PROPERTIES:
  655. :A: 1
  656. :END:
  657. ** S1
  658. :PROPERTIES:
  659. :A: 2
  660. :END:"
  661. (let ((org-columns-summary-types
  662. '(("custom" . (lambda (s _) (mapconcat #'identity s "|")))))
  663. (org-columns-default-format "%A{custom}")) (org-columns))
  664. (get-char-property (point) 'org-columns-value-modified))))
  665. ;; Allow custom _collect_ for summary types.
  666. (should
  667. (equal
  668. "2"
  669. (org-test-with-temp-text
  670. "* H
  671. ** S1
  672. :PROPERTIES:
  673. :A: 1
  674. :END:
  675. ** S1
  676. :PROPERTIES:
  677. :A: 2
  678. :A-OK: 1
  679. :END:"
  680. (let ((org-columns-summary-types
  681. '(("custom" org-columns--summary-sum
  682. (lambda (p)
  683. (if (equal "1" (org-entry-get nil (format "%s-OK" p)))
  684. (org-entry-get nil p)
  685. "")))))
  686. (org-columns-default-format "%A{custom}")) (org-columns))
  687. (get-char-property (point) 'org-columns-value-modified))))
  688. ;; Allow custom collect function to be used for different columns
  689. (should
  690. (equal
  691. '("2" "1")
  692. (org-test-with-temp-text
  693. "* H
  694. ** S1
  695. :PROPERTIES:
  696. :A: 1
  697. :B: 1
  698. :B-OK: 1
  699. :END:
  700. ** S1
  701. :PROPERTIES:
  702. :A: 2
  703. :B: 2
  704. :A-OK: 1
  705. :END:"
  706. (let ((org-columns-summary-types
  707. '(("custom" org-columns--summary-sum
  708. (lambda (p)
  709. (if (equal "1" (org-entry-get nil (format "%s-OK" p)))
  710. (org-entry-get nil p)
  711. "")))))
  712. (org-columns-default-format "%A{custom} %B{custom}")) (org-columns))
  713. (list (get-char-property (point) 'org-columns-value-modified)
  714. (get-char-property (1+ (point)) 'org-columns-value-modified)))))
  715. ;; Allow multiple summary types applied to the same property.
  716. (should
  717. (equal
  718. '("42" "99")
  719. (org-test-with-temp-text
  720. "* H
  721. ** S1
  722. :PROPERTIES:
  723. :A: 99
  724. :END:
  725. ** S1
  726. :PROPERTIES:
  727. :A: 42
  728. :END:"
  729. (let ((org-columns-default-format "%A{min} %A{max}")) (org-columns))
  730. (list (get-char-property (point) 'org-columns-value-modified)
  731. (get-char-property (1+ (point)) 'org-columns-value-modified)))))
  732. ;; Allow mixing both summarized and non-summarized columns for
  733. ;; a property. However, the first column takes precedence and
  734. ;; updates the value.
  735. (should
  736. (equal
  737. '("1000" "42")
  738. (org-test-with-temp-text
  739. "* H
  740. :PROPERTIES:
  741. :A: 1000
  742. :END:
  743. ** S1
  744. :PROPERTIES:
  745. :A: 99
  746. :END:
  747. ** S1
  748. :PROPERTIES:
  749. :A: 42
  750. :END:"
  751. (let ((org-columns-default-format "%A %A{min}")) (org-columns))
  752. (list (get-char-property (point) 'org-columns-value-modified)
  753. (get-char-property (1+ (point)) 'org-columns-value-modified)))))
  754. (should
  755. (equal
  756. '("42" "42")
  757. (org-test-with-temp-text
  758. "* H
  759. :PROPERTIES:
  760. :A: 1000
  761. :END:
  762. ** S1
  763. :PROPERTIES:
  764. :A: 99
  765. :END:
  766. ** S1
  767. :PROPERTIES:
  768. :A: 42
  769. :END:"
  770. (let ((org-columns-default-format "%A{min} %A")) (org-columns))
  771. (list (get-char-property (point) 'org-columns-value-modified)
  772. (get-char-property (1+ (point)) 'org-columns-value-modified))))))
  773. (ert-deftest test-org-colview/columns-new ()
  774. "Test `org-columns-new' specifications."
  775. ;; Insert new column at the left of the current one.
  776. (should
  777. (equal '("FOO" "ITEM")
  778. (org-test-with-temp-text "* H"
  779. (let ((org-columns-default-format "%ITEM")) (org-columns))
  780. (org-columns-new nil "FOO" "FOO" nil nil nil)
  781. (list (get-char-property (point) 'org-columns-key)
  782. (get-char-property (1+ (point)) 'org-columns-key)))))
  783. (should
  784. (equal '("ITEM" "FOO" "BAR")
  785. (org-test-with-temp-text "* H"
  786. (let ((org-columns-default-format "%ITEM %BAR")) (org-columns))
  787. (forward-char)
  788. (org-columns-new nil "FOO" "FOO" nil nil nil)
  789. (list (get-char-property (1- (point)) 'org-columns-key)
  790. (get-char-property (point) 'org-columns-key)
  791. (get-char-property (1+ (point)) 'org-columns-key)))))
  792. ;; Update #+COLUMNS keyword if needed.
  793. (should
  794. (equal "#+COLUMNS: %FOO %ITEM"
  795. (org-test-with-temp-text "#+COLUMNS: %ITEM\n<point>* H"
  796. (let ((org-columns-default-format "%ITEM")) (org-columns))
  797. (org-columns-new nil "FOO" "FOO" nil nil nil)
  798. (goto-char (point-min))
  799. (buffer-substring-no-properties (point) (line-end-position)))))
  800. (should
  801. (equal "#+COLUMNS: %ITEM %FOO %BAR"
  802. (org-test-with-temp-text "#+COLUMNS: %ITEM %BAR\n<point>* H"
  803. (let ((org-columns-default-format "%ITEM %BAR")) (org-columns))
  804. (forward-char)
  805. (org-columns-new nil "FOO" "FOO" nil nil nil)
  806. (goto-char (point-min))
  807. (buffer-substring-no-properties (point) (line-end-position)))))
  808. ;; Mind case when updating #+COLUMNS.
  809. (should
  810. (equal "#+COLUMNS: %ITEM %Foo %BAR"
  811. (org-test-with-temp-text "#+COLUMNS: %ITEM %BAR\n<point>* H"
  812. (let ((org-columns-default-format "%ITEM %BAR")) (org-columns))
  813. (forward-char)
  814. (org-columns-new nil "Foo" "Foo" nil nil nil)
  815. (goto-char (point-min))
  816. (buffer-substring-no-properties (point) (line-end-position)))))
  817. (should
  818. (equal "#+columns: %ITEM %Foo %BAR"
  819. (org-test-with-temp-text "#+columns: %ITEM %BAR\n<point>* H"
  820. (let ((org-columns-default-format "%ITEM %BAR")) (org-columns))
  821. (forward-char)
  822. (org-columns-new nil "Foo" "Foo" nil nil nil)
  823. (goto-char (point-min))
  824. (buffer-substring-no-properties (point) (line-end-position)))))
  825. ;; Also update :COLUMNS: properties.
  826. (should
  827. (equal "%FOO %ITEM"
  828. (org-test-with-temp-text "* H\n:PROPERTIES:\n:COLUMNS: %ITEM\n:END:"
  829. (let ((org-columns-default-format "%ITEM")) (org-columns))
  830. (org-columns-new nil "FOO" "FOO" nil nil nil)
  831. (org-entry-get nil "COLUMNS"))))
  832. ;; If no keyword nor any property is available, insert one.
  833. (should
  834. (string-match-p (regexp-quote "#+COLUMNS: %FOO %ITEM")
  835. (org-test-with-temp-text "* H"
  836. (let ((org-columns-default-format "%ITEM")) (org-columns))
  837. (org-columns-new nil "FOO" "FOO" nil nil nil)
  838. (buffer-string)))))
  839. (ert-deftest test-org-colview/columns-update ()
  840. "Test `org-columns-update' specifications."
  841. ;; Update display.
  842. (should
  843. (equal
  844. "12 |"
  845. (org-test-with-temp-text
  846. "* H
  847. :PROPERTIES:
  848. :A: 1
  849. :END:
  850. "
  851. (let ((org-columns-default-format "%5A")) (org-columns))
  852. (search-forward "1")
  853. (insert "2")
  854. (org-columns-update "A")
  855. (get-char-property (point-min) 'display))))
  856. ;; Update is case-insensitive.
  857. (should
  858. (equal
  859. "12 |"
  860. (org-test-with-temp-text
  861. "* H
  862. :PROPERTIES:
  863. :A: 1
  864. :END:
  865. "
  866. (let ((org-columns-default-format "%5A")) (org-columns))
  867. (search-forward "1")
  868. (insert "2")
  869. (org-columns-update "a")
  870. (get-char-property (point-min) 'display))))
  871. ;; Update stored values.
  872. (should
  873. (equal
  874. '("12" "12")
  875. (org-test-with-temp-text
  876. "* H
  877. :PROPERTIES:
  878. :A: 1
  879. :END:
  880. "
  881. (let ((org-columns-default-format "%5A")) (org-columns))
  882. (search-forward "1")
  883. (insert "2")
  884. (org-columns-update "A")
  885. (list (get-char-property (point-min) 'org-columns-value)
  886. (get-char-property (point-min) 'org-columns-value-modified)))))
  887. ;; When multiple columns are using the same property, value is
  888. ;; updated according to the specifications of the first one.
  889. (should
  890. (equal
  891. "2"
  892. (org-test-with-temp-text
  893. "* H
  894. :PROPERTIES:
  895. :A: 1
  896. :END:
  897. ** S
  898. :PROPERTIES:
  899. :A: 2
  900. :END:"
  901. (let ((org-columns-default-format "%A{min} %A")) (org-columns))
  902. (org-columns-update "A")
  903. (org-entry-get nil "A"))))
  904. (should
  905. (equal
  906. "1"
  907. (org-test-with-temp-text
  908. "* H
  909. :PROPERTIES:
  910. :A: 1
  911. :END:
  912. ** S
  913. :PROPERTIES:
  914. :A: 2
  915. :END:"
  916. (let ((org-columns-default-format "%A %A{min}")) (org-columns))
  917. (org-columns-update "A")
  918. (org-entry-get nil "A"))))
  919. ;; Ensure modifications propagate in upper levels even when multiple
  920. ;; summary types apply to the same property.
  921. (should
  922. (equal
  923. '("1" "22")
  924. (org-test-with-temp-text
  925. "* H
  926. ** S1
  927. :PROPERTIES:
  928. :A: 1
  929. :END:
  930. ** S2
  931. :PROPERTIES:
  932. :A: <point>2
  933. :END:"
  934. (save-excursion
  935. (goto-char (point-min))
  936. (let ((org-columns-default-format "%A{min} %A{max}")) (org-columns)))
  937. (insert "2")
  938. (org-columns-update "A")
  939. (list (get-char-property 1 'org-columns-value)
  940. (get-char-property 2 'org-columns-value-modified)))))
  941. ;; Ensure additional processing is done (e.g., ellipses, special
  942. ;; keywords fontification...).
  943. (should
  944. (equal
  945. "ve.. |"
  946. (org-test-with-temp-text
  947. "* H
  948. :PROPERTIES:
  949. :A: text
  950. :END:
  951. "
  952. (let ((org-columns-default-format "%4A")
  953. (org-columns-ellipses ".."))
  954. (org-columns))
  955. (search-forward ":A: ")
  956. (insert "very long ")
  957. (org-columns-update "A")
  958. (get-char-property (point-min) 'display))))
  959. ;; Values obtained from inline tasks are at the same level as those
  960. ;; obtained from children of the current node.
  961. (when (featurep 'org-inlinetask)
  962. (should
  963. (equal
  964. "2"
  965. (org-test-with-temp-text
  966. "* H
  967. *************** Inline task
  968. :PROPERTIES:
  969. :A: 2
  970. :END:
  971. *************** END
  972. ** Children
  973. :PROPERTIES:
  974. :A: 3
  975. :END:
  976. "
  977. (let ((org-columns-default-format "%A{min}")
  978. (org-columns-ellipses "..")
  979. (org-inlinetask-min-level 15))
  980. (org-columns))
  981. (get-char-property (point-min) 'org-columns-value)))))
  982. ;; Handle `org-columns-modify-value-for-display-function', even with
  983. ;; multiple titles for the same property.
  984. (should
  985. (equal '("foo" "bar")
  986. (org-test-with-temp-text "* H"
  987. (let ((org-columns-default-format "%ITEM %ITEM(Name)")
  988. (org-columns-modify-value-for-display-function
  989. (lambda (title value)
  990. (pcase title ("ITEM" "foo") ("Name" "bar") (_ "baz")))))
  991. (org-columns))
  992. (list (get-char-property 1 'org-columns-value-modified)
  993. (get-char-property 2 'org-columns-value-modified))))))
  994. (ert-deftest test-org-colview/columns-move-left ()
  995. "Test `org-columns-move-left' specifications."
  996. ;; Error when trying to move the left-most column.
  997. (should-error
  998. (org-test-with-temp-text "* H"
  999. (let ((org-columns-default-format "%ITEM")) (org-columns))
  1000. (org-columns-move-left)))
  1001. ;; Otherwise, move column to left and update display.
  1002. (should
  1003. (equal '("2" "1")
  1004. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  1005. (let ((org-columns-default-format "%A %B")) (org-columns))
  1006. (forward-char)
  1007. (org-columns-move-left)
  1008. (list (get-char-property (point) 'org-columns-value)
  1009. (get-char-property (1+ (point)) 'org-columns-value)))))
  1010. ;; Handle multiple columns with the same property.
  1011. (should
  1012. (equal '("2" "1")
  1013. (org-test-with-temp-text
  1014. "* H
  1015. ** S1
  1016. :PROPERTIES:
  1017. :A: 1
  1018. :END:
  1019. ** S1
  1020. :PROPERTIES:
  1021. :A: 2
  1022. :END:"
  1023. (let ((org-columns-default-format "%ITEM %A{min} %A{max}"))
  1024. (org-columns))
  1025. (forward-char 2)
  1026. (org-columns-move-left)
  1027. (list (get-char-property (point) 'org-columns-value)
  1028. (get-char-property (1+ (point)) 'org-columns-value)))))
  1029. ;; Special case: do not update values even if move entails changing
  1030. ;; them.
  1031. (should
  1032. (equal "1"
  1033. (org-test-with-temp-text
  1034. "* H
  1035. :PROPERTIES:
  1036. :A: 1
  1037. :END:
  1038. ** S1
  1039. :PROPERTIES:
  1040. :A: 99
  1041. :END:"
  1042. (let ((org-columns-default-format "%A %A{max}"))
  1043. (org-columns))
  1044. (forward-char)
  1045. (org-columns-move-left)
  1046. ;; Since the first column matching a given property
  1047. ;; determines how a value is computed, the following
  1048. ;; should return "99" instead. However, this behavior is
  1049. ;; in practice surprising so we just ignore the value
  1050. ;; change. It can be computed later.
  1051. (org-entry-get (point) "A")))))
  1052. (ert-deftest test-org-colview/columns-move-right ()
  1053. "Test `org-columns-move-right' specifications."
  1054. ;; Error when trying to move the right-most column.
  1055. (should-error
  1056. (org-test-with-temp-text "* H"
  1057. (let ((org-columns-default-format "%ITEM")) (org-columns))
  1058. (org-columns-move-right)))
  1059. ;; Otherwise, move column to left and update display.
  1060. (should
  1061. (equal '("2" "1")
  1062. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  1063. (let ((org-columns-default-format "%A %B")) (org-columns))
  1064. (org-columns-move-right)
  1065. (list (get-char-property (1- (point)) 'org-columns-value)
  1066. (get-char-property (point) 'org-columns-value)))))
  1067. ;; Handle multiple columns with the same property.
  1068. (should
  1069. (equal '("2" "1")
  1070. (org-test-with-temp-text
  1071. "* H
  1072. ** S1
  1073. :PROPERTIES:
  1074. :A: 1
  1075. :END:
  1076. ** S1
  1077. :PROPERTIES:
  1078. :A: 2
  1079. :END:"
  1080. (let ((org-columns-default-format "%ITEM %A{min} %A{max}"))
  1081. (org-columns))
  1082. (forward-char)
  1083. (org-columns-move-right)
  1084. (list (get-char-property (1- (point)) 'org-columns-value)
  1085. (get-char-property (point) 'org-columns-value)))))
  1086. ;; Special case: do not update values even if move entails changing
  1087. ;; them.
  1088. (should
  1089. (equal "1"
  1090. (org-test-with-temp-text
  1091. "* H
  1092. :PROPERTIES:
  1093. :A: 1
  1094. :END:
  1095. ** S1
  1096. :PROPERTIES:
  1097. :A: 99
  1098. :END:"
  1099. (let ((org-columns-default-format "%A %A{max}"))
  1100. (org-columns))
  1101. (org-columns-move-right)
  1102. ;; See `test-org-colview/columns-move-left' for an
  1103. ;; explanation.
  1104. (org-entry-get (point) "A")))))
  1105. (ert-deftest test-org-colview/columns-next-allowed-value ()
  1106. "Test `org-columns-next-allowed-value' specifications."
  1107. ;; Cannot shift "ITEM" property.
  1108. (should-error
  1109. (org-test-with-temp-text "* H"
  1110. (let ((org-columns-default-format "%ITEM")) (org-columns))
  1111. (org-columns-next-allowed-value)))
  1112. ;; Throw an error when allowed values are not defined.
  1113. (should-error
  1114. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  1115. (let ((org-columns-default-format "%A")) (org-columns))
  1116. (org-columns-next-allowed-value)))
  1117. ;; Throw an error when there's only one value to select.
  1118. (should-error
  1119. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A_ALL: 1\n:END:"
  1120. (let ((org-columns-default-format "%A")) (org-columns))
  1121. (org-columns-next-allowed-value)))
  1122. ;; By default select the next allowed value. Where there is no more
  1123. ;; value, start again from first possible one.
  1124. (should
  1125. (equal "2"
  1126. (org-test-with-temp-text
  1127. "* H\n:PROPERTIES:\n:A: 1\n:A_ALL: 1 2 3\n:END:"
  1128. (let ((org-columns-default-format "%A")) (org-columns))
  1129. (org-columns-next-allowed-value)
  1130. (org-entry-get (point) "A"))))
  1131. (should
  1132. (equal "3"
  1133. (org-test-with-temp-text
  1134. "* H\n:PROPERTIES:\n:A: 2\n:A_ALL: 1 2 3\n:END:"
  1135. (let ((org-columns-default-format "%A")) (org-columns))
  1136. (org-columns-next-allowed-value)
  1137. (org-entry-get (point) "A"))))
  1138. (should
  1139. (equal "1"
  1140. (org-test-with-temp-text
  1141. "* H\n:PROPERTIES:\n:A: 3\n:A_ALL: 1 2 3\n:END:"
  1142. (let ((org-columns-default-format "%A")) (org-columns))
  1143. (org-columns-next-allowed-value)
  1144. (org-entry-get (point) "A"))))
  1145. ;; PREVIOUS argument moves backward.
  1146. (should
  1147. (equal "1"
  1148. (org-test-with-temp-text
  1149. "* H\n:PROPERTIES:\n:A: 2\n:A_ALL: 1 2 3\n:END:"
  1150. (let ((org-columns-default-format "%A")) (org-columns))
  1151. (org-columns-next-allowed-value 'previous)
  1152. (org-entry-get (point) "A"))))
  1153. (should
  1154. (equal "2"
  1155. (org-test-with-temp-text
  1156. "* H\n:PROPERTIES:\n:A: 3\n:A_ALL: 1 2 3\n:END:"
  1157. (let ((org-columns-default-format "%A")) (org-columns))
  1158. (org-columns-next-allowed-value 'previous)
  1159. (org-entry-get (point) "A"))))
  1160. (should
  1161. (equal "3"
  1162. (org-test-with-temp-text
  1163. "* H\n:PROPERTIES:\n:A: 1\n:A_ALL: 1 2 3\n:END:"
  1164. (let ((org-columns-default-format "%A")) (org-columns))
  1165. (org-columns-next-allowed-value 'previous)
  1166. (org-entry-get (point) "A"))))
  1167. ;; Select Nth element with optional argument NTH.
  1168. (should
  1169. (equal "1"
  1170. (org-test-with-temp-text
  1171. "* H\n:PROPERTIES:\n:A: 2\n:A_ALL: 1 2 3\n:END:"
  1172. (let ((org-columns-default-format "%A")) (org-columns))
  1173. (org-columns-next-allowed-value nil 1)
  1174. (org-entry-get (point) "A"))))
  1175. ;; If NTH is negative, go backwards, 0 being the last one and -1 the
  1176. ;; penultimate.
  1177. (should
  1178. (equal "3"
  1179. (org-test-with-temp-text
  1180. "* H\n:PROPERTIES:\n:A: 2\n:A_ALL: 1 2 3\n:END:"
  1181. (let ((org-columns-default-format "%A")) (org-columns))
  1182. (org-columns-next-allowed-value nil 0)
  1183. (org-entry-get (point) "A"))))
  1184. (should
  1185. (equal "2"
  1186. (org-test-with-temp-text
  1187. "* H\n:PROPERTIES:\n:A: 2\n:A_ALL: 1 2 3\n:END:"
  1188. (let ((org-columns-default-format "%A")) (org-columns))
  1189. (org-columns-next-allowed-value nil -1)
  1190. (org-entry-get (point) "A"))))
  1191. ;; Throw an error if NTH is greater than the number of allowed
  1192. ;; values.
  1193. (should-error
  1194. (org-test-with-temp-text
  1195. "* H\n:PROPERTIES:\n:A: 2\n:A_ALL: 1 2 3\n:END:"
  1196. (let ((org-columns-default-format "%A")) (org-columns))
  1197. (org-columns-next-allowed-value nil 4)
  1198. (org-entry-get (point) "A")))
  1199. ;; Pathological case: when shifting the value alters the current
  1200. ;; heading, make sure all columns are still at their correct
  1201. ;; location.
  1202. (should
  1203. (equal '("H" "" "" "" "TODO")
  1204. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  1205. (org-test-with-temp-text "* H"
  1206. (let ((org-columns-default-format "%ITEM %A %B %C %TODO"))
  1207. (org-columns)
  1208. (forward-char 4)
  1209. (org-columns-next-allowed-value)
  1210. (list (get-char-property (- (point) 4) 'org-columns-value)
  1211. (get-char-property (- (point) 3) 'org-columns-value)
  1212. (get-char-property (- (point) 2) 'org-columns-value)
  1213. (get-char-property (- (point) 1) 'org-columns-value)
  1214. (get-char-property (point) 'org-columns-value)))))))
  1215. (should
  1216. (equal '("H" "VERYLONGTODO")
  1217. (let ((org-todo-keywords '((sequence "TODO" "VERYLONGTODO"))))
  1218. (org-test-with-temp-text "* TODO H"
  1219. (let ((org-columns-default-format "%ITEM %TODO"))
  1220. (org-columns)
  1221. (forward-char)
  1222. (org-columns-next-allowed-value)
  1223. (list (get-char-property (- (point) 1) 'org-columns-value)
  1224. (get-char-property (point) 'org-columns-value))))))))
  1225. ;;; Dynamic block
  1226. (ert-deftest test-org-colview/dblock ()
  1227. "Test the column view table."
  1228. (should
  1229. (equal
  1230. "#+BEGIN: columnview
  1231. | ITEM |
  1232. |------|
  1233. | H |
  1234. #+END:"
  1235. (org-test-with-temp-text
  1236. "* H\n<point>#+BEGIN: columnview\n#+END:"
  1237. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1238. (buffer-substring-no-properties (point) (point-max)))))
  1239. (should
  1240. (equal
  1241. "#+BEGIN: columnview
  1242. | ITEM | A |
  1243. |------+---|
  1244. | H | 1 |
  1245. #+END:"
  1246. (org-test-with-temp-text
  1247. "* H\n:PROPERTIES:\n:A: 1\n:END:\n<point>#+BEGIN: columnview\n#+END:"
  1248. (let ((org-columns-default-format "%ITEM %A")) (org-update-dblock))
  1249. (buffer-substring-no-properties (point) (point-max)))))
  1250. ;; Properties are case insensitive.
  1251. (should
  1252. (equal
  1253. "#+BEGIN: columnview
  1254. | a |
  1255. |---|
  1256. | 1 |
  1257. #+END:"
  1258. (org-test-with-temp-text
  1259. "* H\n:PROPERTIES:\n:A: 1\n:END:\n<point>#+BEGIN: columnview\n#+END:"
  1260. (let ((org-columns-default-format "%a")) (org-update-dblock))
  1261. (buffer-substring-no-properties (point) (point-max)))))
  1262. ;; Test titles given to columns.
  1263. (should
  1264. (equal
  1265. "#+BEGIN: columnview
  1266. | Name | Prop |
  1267. |------+------|
  1268. | H | 1 |
  1269. #+END:"
  1270. (org-test-with-temp-text
  1271. "* H\n:PROPERTIES:\n:A: 1\n:END:\n<point>#+BEGIN: columnview\n#+END:"
  1272. (let ((org-columns-default-format "%ITEM(Name) %A(Prop)"))
  1273. (org-update-dblock))
  1274. (buffer-substring-no-properties (point) (point-max)))))
  1275. ;; Test `:id' parameter
  1276. (should
  1277. (equal
  1278. "#+BEGIN: columnview :id local
  1279. | ITEM |
  1280. |------|
  1281. | H1 |
  1282. | H1.1 |
  1283. #+END:
  1284. "
  1285. (org-test-with-temp-text
  1286. "* H1\n<point>#+BEGIN: columnview :id local\n#+END:\n** H1.1\n* H2"
  1287. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1288. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1289. (should
  1290. (equal
  1291. "#+BEGIN: columnview :id global
  1292. | ITEM |
  1293. |------|
  1294. | H1 |
  1295. | H1.1 |
  1296. | H2 |
  1297. #+END:
  1298. "
  1299. (org-test-with-temp-text
  1300. "\n* H1\n<point>#+BEGIN: columnview :id global\n#+END:\n** H1.1\n* H2"
  1301. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1302. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1303. ;; Test `:hlines' parameter.
  1304. (should
  1305. (equal
  1306. "#+BEGIN: columnview :hlines t :id global
  1307. | ITEM |
  1308. |------|
  1309. | H |
  1310. |------|
  1311. | H2 |
  1312. |------|
  1313. | H2.1 |
  1314. #+END:\n"
  1315. (org-test-with-temp-text
  1316. "
  1317. * H
  1318. <point>#+BEGIN: columnview :hlines t :id global
  1319. #+END:
  1320. * H2
  1321. ** H2.1"
  1322. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1323. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1324. (should
  1325. (equal
  1326. "#+BEGIN: columnview :hlines 1 :id global
  1327. | ITEM |
  1328. |------|
  1329. | H |
  1330. |------|
  1331. | H2 |
  1332. | H2.1 |
  1333. #+END:\n"
  1334. (org-test-with-temp-text
  1335. "
  1336. * H
  1337. <point>#+BEGIN: columnview :hlines 1 :id global
  1338. #+END:
  1339. * H2
  1340. ** H2.1"
  1341. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1342. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1343. (should
  1344. (equal
  1345. "#+BEGIN: columnview :hlines 1 :id \"id\"
  1346. | ITEM |
  1347. |------|
  1348. | H2 |
  1349. | H2.1 |
  1350. #+END:
  1351. "
  1352. (org-test-with-temp-text
  1353. "
  1354. * H
  1355. <point>#+BEGIN: columnview :hlines 1 :id \"id\"
  1356. #+END:
  1357. * H2
  1358. :PROPERTIES:
  1359. :ID: id
  1360. :END:
  1361. ** H2.1"
  1362. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1363. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1364. (should
  1365. (equal
  1366. "#+BEGIN: columnview :hlines 1 :id id
  1367. | ITEM |
  1368. |------|
  1369. | H2 |
  1370. | H2.1 |
  1371. #+END:
  1372. "
  1373. (org-test-with-temp-text
  1374. "
  1375. * H
  1376. <point>#+BEGIN: columnview :hlines 1 :id id
  1377. #+END:
  1378. * H2
  1379. :PROPERTIES:
  1380. :ID: id
  1381. :END:
  1382. ** H2.1"
  1383. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1384. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1385. ;; Test `:indent' parameter.
  1386. (should
  1387. (equal
  1388. "#+BEGIN: columnview :indent t
  1389. | ITEM |
  1390. |----------|
  1391. | H1 |
  1392. | \\_ H1.1 |
  1393. #+END:
  1394. "
  1395. (org-test-with-temp-text
  1396. "* H1\n<point>#+BEGIN: columnview :indent t\n#+END:\n** H1.1"
  1397. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1398. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1399. (should
  1400. (equal
  1401. "#+BEGIN: columnview :indent t
  1402. | Prop | Name |
  1403. |------+----------|
  1404. | | H1 |
  1405. | | \\_ H1.1 |
  1406. #+END:
  1407. "
  1408. (org-test-with-temp-text
  1409. "* H1\n<point>#+BEGIN: columnview :indent t\n#+END:\n** H1.1"
  1410. (let ((org-columns-default-format "%A(Prop) %ITEM(Name)"))
  1411. (org-update-dblock))
  1412. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1413. ;; Test `:vlines' parameter.
  1414. (should
  1415. (equal
  1416. "#+BEGIN: columnview :vlines t
  1417. | | ITEM | A |
  1418. |---+------+----|
  1419. | | H | 1 |
  1420. | / | <> | <> |
  1421. #+END:"
  1422. (org-test-with-temp-text
  1423. "* H\n:PROPERTIES:\n:A: 1\n:END:\n<point>#+BEGIN: columnview :vlines t\n#+END:"
  1424. (let ((org-columns-default-format "%ITEM %A")) (org-update-dblock))
  1425. (buffer-substring-no-properties (point) (point-max)))))
  1426. ;; Test `:skip-empty-rows' parameter.
  1427. (should
  1428. (equal
  1429. "#+BEGIN: columnview :skip-empty-rows t
  1430. | ITEM | A |
  1431. |------+---|
  1432. | H1.1 | 1 |
  1433. #+END:
  1434. "
  1435. (org-test-with-temp-text
  1436. "
  1437. * H1
  1438. <point>#+BEGIN: columnview :skip-empty-rows t
  1439. #+END:
  1440. ** H1.1
  1441. :PROPERTIES:
  1442. :A: 1
  1443. :END:"
  1444. (let ((org-columns-default-format "%ITEM %A")) (org-update-dblock))
  1445. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1446. ;; Test `:exclude-tags' parameter.
  1447. (should
  1448. (equal
  1449. "#+BEGIN: columnview :exclude-tags (\"excludeme\")
  1450. | ITEM | A |
  1451. |------+---|
  1452. | H1 | |
  1453. #+END:
  1454. "
  1455. (org-test-with-temp-text
  1456. "
  1457. * H1
  1458. <point>#+BEGIN: columnview :exclude-tags (\"excludeme\")
  1459. #+END:
  1460. ** H1.1 :excludeme:
  1461. :PROPERTIES:
  1462. :A: 1
  1463. :END:"
  1464. (let ((org-columns-default-format "%ITEM %A")) (org-update-dblock))
  1465. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1466. ;; Test `:format' parameter.
  1467. (should
  1468. (equal
  1469. "#+BEGIN: columnview :format \"%ITEM(Name)\"
  1470. | Name |
  1471. |------|
  1472. | H |
  1473. #+END:"
  1474. (org-test-with-temp-text
  1475. "* H\n<point>#+BEGIN: columnview :format \"%ITEM(Name)\"\n#+END:"
  1476. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1477. (buffer-substring-no-properties (point) (point-max)))))
  1478. ;; When inserting ITEM values, make sure to clean sensitive
  1479. ;; contents, like unique targets or forbidden inline src-blocks.
  1480. (should
  1481. (equal
  1482. "#+BEGIN: columnview
  1483. | ITEM |
  1484. |------|
  1485. | H 1 |
  1486. #+END:"
  1487. (org-test-with-temp-text
  1488. "* H <<target>> 1\n<point>#+BEGIN: columnview\n#+END:"
  1489. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1490. (buffer-substring-no-properties (point) (point-max)))))
  1491. (should
  1492. (equal
  1493. "#+BEGIN: columnview
  1494. | ITEM |
  1495. |------|
  1496. | H 1 |
  1497. #+END:"
  1498. (org-test-with-temp-text
  1499. "* H src_emacs-lisp{(+ 1 1)} 1\n<point>#+BEGIN: columnview\n#+END:"
  1500. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1501. (buffer-substring-no-properties (point) (point-max))))))
  1502. (provide 'test-org-colview)
  1503. ;;; test-org-colview.el ends here