test-org-colview.el 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. ;;; test-org-colview.el --- Tests for org-colview.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2016 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. '("H1" "H2" "H3" "H4")
  98. (org-test-with-temp-text "Top\n* H1\n** <point>H2\n*** H3\n* H4"
  99. (let ((org-columns-default-format "%ITEM")) (org-columns t))
  100. (org-map-entries
  101. (lambda () (get-char-property (point) 'org-columns-value)))))))
  102. (ert-deftest test-org-colview/columns-width ()
  103. "Test `org-columns' column widths."
  104. ;; When a width is specified in the format, use it.
  105. (should
  106. (= 9
  107. (org-test-with-temp-text "* H"
  108. (let ((org-columns-default-format "%9ITEM")) (org-columns))
  109. (aref org-columns-current-maxwidths 0))))
  110. ;; Otherwise, use the width of the largest value in the column.
  111. (should
  112. (= 2
  113. (org-test-with-temp-text
  114. "* H\n:PROPERTIES:\n:P: X\n:END:\n** H2\n:PROPERTIES:\n:P: XX\n:END:"
  115. (let ((org-columns-default-format "%P")) (org-columns))
  116. (aref org-columns-current-maxwidths 0))))
  117. ;; If the title is wider than the widest value, use title width
  118. ;; instead.
  119. (should
  120. (= 4
  121. (org-test-with-temp-text "* H"
  122. (let ((org-columns-default-format "%ITEM")) (org-columns))
  123. (aref org-columns-current-maxwidths 0))))
  124. ;; Special case: stars do count for ITEM.
  125. (should
  126. (= 6
  127. (org-test-with-temp-text "* Head"
  128. (let ((org-columns-default-format "%ITEM")) (org-columns))
  129. (aref org-columns-current-maxwidths 0))))
  130. ;; Special case: width takes into account link narrowing in ITEM.
  131. (should
  132. (equal
  133. '("* [123]" . 7)
  134. (org-test-with-temp-text "* [[http://orgmode.org][123]]"
  135. (let ((org-columns-default-format "%ITEM")) (org-columns))
  136. (cons (get-char-property (point) 'org-columns-value-modified)
  137. (aref org-columns-current-maxwidths 0)))))
  138. ;; When a value is too wide for the current column, add ellipses.
  139. ;; Take into consideration length of `org-columns-ellipses'.
  140. (should
  141. (equal "123.. |"
  142. (org-test-with-temp-text "* H\n:PROPERTIES:\n:P: 123456\n:END:"
  143. (let ((org-columns-default-format "%5P")
  144. (org-columns-ellipses ".."))
  145. (org-columns))
  146. (org-trim (get-char-property (point) 'display)))))
  147. (should
  148. (equal "1234… |"
  149. (org-test-with-temp-text "* H\n:PROPERTIES:\n:P: 123456\n:END:"
  150. (let ((org-columns-default-format "%5P")
  151. (org-columns-ellipses "…"))
  152. (org-columns))
  153. (org-trim (get-char-property (point) 'display))))))
  154. (ert-deftest test-org-colview/columns-summary ()
  155. "Test `org-columns' summary types."
  156. ;; {+} and {+;format} add numbers.
  157. (should
  158. (equal
  159. "3"
  160. (org-test-with-temp-text
  161. "* H
  162. ** S1
  163. :PROPERTIES:
  164. :A: 1
  165. :END:
  166. ** S1
  167. :PROPERTIES:
  168. :A: 2
  169. :END:"
  170. (let ((org-columns-default-format "%A{+}")) (org-columns))
  171. (get-char-property (point) 'org-columns-value-modified))))
  172. (should
  173. (equal
  174. "3.0"
  175. (org-test-with-temp-text
  176. "* H
  177. ** S1
  178. :PROPERTIES:
  179. :A: 1
  180. :END:
  181. ** S1
  182. :PROPERTIES:
  183. :A: 2
  184. :END:"
  185. (let ((org-columns-default-format "%A{+;%.1f}")) (org-columns))
  186. (get-char-property (point) 'org-columns-value-modified))))
  187. ;; {$} is a shortcut for {+;%.2f}.
  188. (should
  189. (equal
  190. "3.60"
  191. (org-test-with-temp-text
  192. "* H
  193. ** S1
  194. :PROPERTIES:
  195. :A: 1.50
  196. :END:
  197. ** S1
  198. :PROPERTIES:
  199. :A: 2.10
  200. :END:"
  201. (let ((org-columns-default-format "%A{$}")) (org-columns))
  202. (get-char-property (point) 'org-columns-value-modified))))
  203. ;; {:} sums times. Plain numbers are hours.
  204. (should
  205. (equal
  206. "4:10"
  207. (org-test-with-temp-text
  208. "* H
  209. ** S1
  210. :PROPERTIES:
  211. :A: 1:30
  212. :END:
  213. ** S1
  214. :PROPERTIES:
  215. :A: 2:40
  216. :END:"
  217. (let ((org-columns-default-format "%A{:}")) (org-columns))
  218. (get-char-property (point) 'org-columns-value-modified))))
  219. (should
  220. (equal
  221. "3:30"
  222. (org-test-with-temp-text
  223. "* H
  224. ** S1
  225. :PROPERTIES:
  226. :A: 1:30
  227. :END:
  228. ** S1
  229. :PROPERTIES:
  230. :A: 2
  231. :END:"
  232. (let ((org-columns-default-format "%A{:}")) (org-columns))
  233. (get-char-property (point) 'org-columns-value-modified))))
  234. ;; {X}, {X/} and {X%} indicate checkbox status.
  235. (should
  236. (equal
  237. "[ ]"
  238. (org-test-with-temp-text
  239. "* H
  240. ** S1
  241. :PROPERTIES:
  242. :A: [ ]
  243. :END:
  244. ** S1
  245. :PROPERTIES:
  246. :A: [ ]
  247. :END:"
  248. (let ((org-columns-default-format "%A{X}")) (org-columns))
  249. (get-char-property (point) 'org-columns-value-modified))))
  250. (should
  251. (equal
  252. "[-]"
  253. (org-test-with-temp-text
  254. "* H
  255. ** S1
  256. :PROPERTIES:
  257. :A: [ ]
  258. :END:
  259. ** S1
  260. :PROPERTIES:
  261. :A: [X]
  262. :END:"
  263. (let ((org-columns-default-format "%A{X}")) (org-columns))
  264. (get-char-property (point) 'org-columns-value-modified))))
  265. (should
  266. (equal
  267. "[X]"
  268. (org-test-with-temp-text
  269. "* H
  270. ** S1
  271. :PROPERTIES:
  272. :A: [X]
  273. :END:
  274. ** S1
  275. :PROPERTIES:
  276. :A: [X]
  277. :END:"
  278. (let ((org-columns-default-format "%A{X}")) (org-columns))
  279. (get-char-property (point) 'org-columns-value-modified))))
  280. (should
  281. (equal
  282. "[1/2]"
  283. (org-test-with-temp-text
  284. "* H
  285. ** S1
  286. :PROPERTIES:
  287. :A: [ ]
  288. :END:
  289. ** S1
  290. :PROPERTIES:
  291. :A: [X]
  292. :END:"
  293. (let ((org-columns-default-format "%A{X/}")) (org-columns))
  294. (get-char-property (point) 'org-columns-value-modified))))
  295. (should
  296. (equal
  297. "[50%]"
  298. (org-test-with-temp-text
  299. "* H
  300. ** S1
  301. :PROPERTIES:
  302. :A: [ ]
  303. :END:
  304. ** S1
  305. :PROPERTIES:
  306. :A: [X]
  307. :END:"
  308. (let ((org-columns-default-format "%A{X%}")) (org-columns))
  309. (get-char-property (point) 'org-columns-value-modified))))
  310. ;; {min} is the smallest number in column, {max} the largest one.
  311. ;; {mean} is the arithmetic mean of numbers in column.
  312. (should
  313. (equal
  314. "42"
  315. (org-test-with-temp-text
  316. "* H
  317. ** S1
  318. :PROPERTIES:
  319. :A: 99
  320. :END:
  321. ** S1
  322. :PROPERTIES:
  323. :A: 42
  324. :END:"
  325. (let ((org-columns-default-format "%A{min}")) (org-columns))
  326. (get-char-property (point) 'org-columns-value-modified))))
  327. (should
  328. (equal
  329. "99"
  330. (org-test-with-temp-text
  331. "* H
  332. ** S1
  333. :PROPERTIES:
  334. :A: 99
  335. :END:
  336. ** S1
  337. :PROPERTIES:
  338. :A: 42
  339. :END:"
  340. (let ((org-columns-default-format "%A{max}")) (org-columns))
  341. (get-char-property (point) 'org-columns-value-modified))))
  342. (should
  343. (equal
  344. "51.0"
  345. (org-test-with-temp-text
  346. "* H
  347. ** S1
  348. :PROPERTIES:
  349. :A: 60
  350. :END:
  351. ** S1
  352. :PROPERTIES:
  353. :A: 42
  354. :END:"
  355. (let ((org-columns-default-format "%A{mean}")) (org-columns))
  356. (get-char-property (point) 'org-columns-value-modified))))
  357. ;; {:min}, {:max} and {:mean} apply to time values.
  358. (should
  359. (equal
  360. "1:20"
  361. (org-test-with-temp-text
  362. "* H
  363. ** S1
  364. :PROPERTIES:
  365. :A: 4:40
  366. :END:
  367. ** S1
  368. :PROPERTIES:
  369. :A: 1:20
  370. :END:"
  371. (let ((org-columns-default-format "%A{:min}")) (org-columns))
  372. (get-char-property (point) 'org-columns-value-modified))))
  373. (should
  374. (equal
  375. "4:40"
  376. (org-test-with-temp-text
  377. "* H
  378. ** S1
  379. :PROPERTIES:
  380. :A: 4:40
  381. :END:
  382. ** S1
  383. :PROPERTIES:
  384. :A: 1:20
  385. :END:"
  386. (let ((org-columns-default-format "%A{:max}")) (org-columns))
  387. (get-char-property (point) 'org-columns-value-modified))))
  388. (should
  389. (equal
  390. "3:00"
  391. (org-test-with-temp-text
  392. "* H
  393. ** S1
  394. :PROPERTIES:
  395. :A: 4:40
  396. :END:
  397. ** S1
  398. :PROPERTIES:
  399. :A: 1:20
  400. :END:"
  401. (let ((org-columns-default-format "%A{:mean}")) (org-columns))
  402. (get-char-property (point) 'org-columns-value-modified))))
  403. ;; {@min}, {@max} and {@mean} apply to ages.
  404. (should
  405. (equal
  406. (let ((org-columns--time (float-time (current-time))))
  407. (org-columns--summary-min-age (list "<2014-03-04 Tue>") nil))
  408. (org-test-with-temp-text
  409. "* H
  410. ** S1
  411. :PROPERTIES:
  412. :A: <2012-03-29 Thu>
  413. :END:
  414. ** S1
  415. :PROPERTIES:
  416. :A: <2014-03-04 Tue>
  417. :END:"
  418. (let ((org-columns-default-format "%A{@min}")) (org-columns))
  419. (get-char-property (point) 'org-columns-value-modified))))
  420. (should
  421. (equal
  422. (let ((org-columns--time (float-time (current-time))))
  423. (org-columns--summary-max-age (list "<2012-03-29 Thu>") nil))
  424. (org-test-with-temp-text
  425. "* H
  426. ** S1
  427. :PROPERTIES:
  428. :A: <2012-03-29 Thu>
  429. :END:
  430. ** S1
  431. :PROPERTIES:
  432. :A: <2014-03-04 Tue>
  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. (let ((org-columns--time (float-time (current-time))))
  439. (org-columns--summary-mean-age
  440. (list "<2012-03-29 Thu>" "<2014-03-04 Tue>") nil))
  441. (org-test-with-temp-text
  442. "* H
  443. ** S1
  444. :PROPERTIES:
  445. :A: <2012-03-29 Thu>
  446. :END:
  447. ** S1
  448. :PROPERTIES:
  449. :A: <2014-03-04 Tue>
  450. :END:"
  451. (let ((org-columns-default-format "%A{@mean}")) (org-columns))
  452. (get-char-property (point) 'org-columns-value-modified))))
  453. ;; If a time value is expressed as a duration, return a duration.
  454. ;; If any of them follows H:MM:SS pattern, use it too.
  455. (should
  456. (equal
  457. "1d 4:20"
  458. (org-test-with-temp-text
  459. "* H
  460. ** S1
  461. :PROPERTIES:
  462. :A: 3d 3h
  463. :END:
  464. ** S1
  465. :PROPERTIES:
  466. :A: 1:20
  467. :END:"
  468. (let ((org-columns-default-format "%A{:}")
  469. (org-time-clocksum-use-fractional nil)
  470. (org-time-clocksum-format
  471. '(:days "%dd " :hours "%d" :minutes ":%02d")))
  472. (org-columns))
  473. (get-char-property (point) 'org-columns-value-modified))))
  474. (should
  475. (equal
  476. "6:00:10"
  477. (org-test-with-temp-text
  478. "* H
  479. ** S1
  480. :PROPERTIES:
  481. :A: 4:40:10
  482. :END:
  483. ** S1
  484. :PROPERTIES:
  485. :A: 1:20
  486. :END:"
  487. (let ((org-columns-default-format "%A{:}")) (org-columns))
  488. (get-char-property (point) 'org-columns-value-modified))))
  489. ;; @min, @max and @mean also accept regular duration in
  490. ;; a "?d ?h ?m ?s" format.
  491. (should
  492. (equal
  493. "1d 10h 0m 0s"
  494. (org-test-with-temp-text
  495. "* H
  496. ** S1
  497. :PROPERTIES:
  498. :A: 1d 10h 0m 0s
  499. :END:
  500. ** S1
  501. :PROPERTIES:
  502. :A: 5d 3h 0m 0s
  503. :END:"
  504. (let ((org-columns-default-format "%A{@min}")) (org-columns))
  505. (get-char-property (point) 'org-columns-value-modified))))
  506. ;; {est+} gives a low-high estimate using mean and standard
  507. ;; deviation.
  508. (should
  509. (equal
  510. "3-17"
  511. (org-test-with-temp-text
  512. "* H
  513. ** S1
  514. :PROPERTIES:
  515. :A: 0-10
  516. :END:
  517. ** S1
  518. :PROPERTIES:
  519. :A: 0-10
  520. :END:"
  521. (let ((org-columns-default-format "%A{est+}")) (org-columns))
  522. (get-char-property (point) 'org-columns-value-modified))))
  523. ;; When using {est+} summary, a single number is understood as
  524. ;; a degenerate range.
  525. (should
  526. (equal
  527. "4-4"
  528. (org-test-with-temp-text
  529. "* H
  530. ** S1
  531. :PROPERTIES:
  532. :A: 4
  533. :END:
  534. "
  535. (let ((org-columns-default-format "%A{est+}")) (org-columns))
  536. (get-char-property (point) 'org-columns-value-modified))))
  537. ;; Allow custom summary types.
  538. (should
  539. (equal
  540. "1|2"
  541. (org-test-with-temp-text
  542. "* H
  543. ** S1
  544. :PROPERTIES:
  545. :A: 1
  546. :END:
  547. ** S1
  548. :PROPERTIES:
  549. :A: 2
  550. :END:"
  551. (let ((org-columns-summary-types
  552. '(("custom" . (lambda (s _) (mapconcat #'identity s "|")))))
  553. (org-columns-default-format "%A{custom}")) (org-columns))
  554. (get-char-property (point) 'org-columns-value-modified))))
  555. ;; Allow multiple summary types applied to the same property.
  556. (should
  557. (equal
  558. '("42" "99")
  559. (org-test-with-temp-text
  560. "* H
  561. ** S1
  562. :PROPERTIES:
  563. :A: 99
  564. :END:
  565. ** S1
  566. :PROPERTIES:
  567. :A: 42
  568. :END:"
  569. (let ((org-columns-default-format "%A{min} %A{max}")) (org-columns))
  570. (list (get-char-property (point) 'org-columns-value-modified)
  571. (get-char-property (1+ (point)) 'org-columns-value-modified)))))
  572. ;; Allow mixing both summarized and non-summarized columns for
  573. ;; a property. However, the first column takes precedence and
  574. ;; updates the value.
  575. (should
  576. (equal
  577. '("1000" "42")
  578. (org-test-with-temp-text
  579. "* H
  580. :PROPERTIES:
  581. :A: 1000
  582. :END:
  583. ** S1
  584. :PROPERTIES:
  585. :A: 99
  586. :END:
  587. ** S1
  588. :PROPERTIES:
  589. :A: 42
  590. :END:"
  591. (let ((org-columns-default-format "%A %A{min}")) (org-columns))
  592. (list (get-char-property (point) 'org-columns-value-modified)
  593. (get-char-property (1+ (point)) 'org-columns-value-modified)))))
  594. (should
  595. (equal
  596. '("42" "42")
  597. (org-test-with-temp-text
  598. "* H
  599. :PROPERTIES:
  600. :A: 1000
  601. :END:
  602. ** S1
  603. :PROPERTIES:
  604. :A: 99
  605. :END:
  606. ** S1
  607. :PROPERTIES:
  608. :A: 42
  609. :END:"
  610. (let ((org-columns-default-format "%A{min} %A")) (org-columns))
  611. (list (get-char-property (point) 'org-columns-value-modified)
  612. (get-char-property (1+ (point)) 'org-columns-value-modified))))))
  613. (ert-deftest test-org-colview/columns-new ()
  614. "Test `org-columns-new' specifications."
  615. ;; Insert new column at the left of the current one.
  616. (should
  617. (equal '("FOO" "ITEM")
  618. (org-test-with-temp-text "* H"
  619. (let ((org-columns-default-format "%ITEM")) (org-columns))
  620. (org-columns-new nil "FOO" "FOO" nil nil nil)
  621. (list (get-char-property (point) 'org-columns-key)
  622. (get-char-property (1+ (point)) 'org-columns-key)))))
  623. (should
  624. (equal '("ITEM" "FOO" "BAR")
  625. (org-test-with-temp-text "* H"
  626. (let ((org-columns-default-format "%ITEM %BAR")) (org-columns))
  627. (forward-char)
  628. (org-columns-new nil "FOO" "FOO" nil nil nil)
  629. (list (get-char-property (1- (point)) 'org-columns-key)
  630. (get-char-property (point) 'org-columns-key)
  631. (get-char-property (1+ (point)) 'org-columns-key)))))
  632. ;; Update #+COLUMNS keyword if needed.
  633. (should
  634. (equal "#+COLUMNS: %FOO %ITEM"
  635. (org-test-with-temp-text "#+COLUMNS: %ITEM\n<point>* H"
  636. (let ((org-columns-default-format "%ITEM")) (org-columns))
  637. (org-columns-new nil "FOO" "FOO" nil nil nil)
  638. (goto-char (point-min))
  639. (buffer-substring-no-properties (point) (line-end-position)))))
  640. (should
  641. (equal "#+COLUMNS: %ITEM %FOO %BAR"
  642. (org-test-with-temp-text "#+COLUMNS: %ITEM %BAR\n<point>* H"
  643. (let ((org-columns-default-format "%ITEM %BAR")) (org-columns))
  644. (forward-char)
  645. (org-columns-new nil "FOO" "FOO" nil nil nil)
  646. (goto-char (point-min))
  647. (buffer-substring-no-properties (point) (line-end-position)))))
  648. ;; Mind case when updating #+COLUMNS.
  649. (should
  650. (equal "#+COLUMNS: %ITEM %Foo %BAR"
  651. (org-test-with-temp-text "#+COLUMNS: %ITEM %BAR\n<point>* H"
  652. (let ((org-columns-default-format "%ITEM %BAR")) (org-columns))
  653. (forward-char)
  654. (org-columns-new nil "Foo" "Foo" nil nil nil)
  655. (goto-char (point-min))
  656. (buffer-substring-no-properties (point) (line-end-position)))))
  657. (should
  658. (equal "#+columns: %ITEM %Foo %BAR"
  659. (org-test-with-temp-text "#+columns: %ITEM %BAR\n<point>* H"
  660. (let ((org-columns-default-format "%ITEM %BAR")) (org-columns))
  661. (forward-char)
  662. (org-columns-new nil "Foo" "Foo" nil nil nil)
  663. (goto-char (point-min))
  664. (buffer-substring-no-properties (point) (line-end-position)))))
  665. ;; Also update :COLUMNS: properties.
  666. (should
  667. (equal "%FOO %ITEM"
  668. (org-test-with-temp-text "* H\n:PROPERTIES:\n:COLUMNS: %ITEM\n:END:"
  669. (let ((org-columns-default-format "%ITEM")) (org-columns))
  670. (org-columns-new nil "FOO" "FOO" nil nil nil)
  671. (org-entry-get nil "COLUMNS"))))
  672. ;; If no keyword nor any property is available, insert one.
  673. (should
  674. (string-match-p (regexp-quote "#+COLUMNS: %FOO %ITEM")
  675. (org-test-with-temp-text "* H"
  676. (let ((org-columns-default-format "%ITEM")) (org-columns))
  677. (org-columns-new nil "FOO" "FOO" nil nil nil)
  678. (buffer-string)))))
  679. (ert-deftest test-org-colview/columns-update ()
  680. "Test `org-columns-update' specifications."
  681. ;; Update display.
  682. (should
  683. (equal
  684. "12 |"
  685. (org-test-with-temp-text
  686. "* H
  687. :PROPERTIES:
  688. :A: 1
  689. :END:
  690. "
  691. (let ((org-columns-default-format "%5A")) (org-columns))
  692. (search-forward "1")
  693. (insert "2")
  694. (org-columns-update "A")
  695. (get-char-property (point-min) 'display))))
  696. ;; Update is case-insensitive.
  697. (should
  698. (equal
  699. "12 |"
  700. (org-test-with-temp-text
  701. "* H
  702. :PROPERTIES:
  703. :A: 1
  704. :END:
  705. "
  706. (let ((org-columns-default-format "%5A")) (org-columns))
  707. (search-forward "1")
  708. (insert "2")
  709. (org-columns-update "a")
  710. (get-char-property (point-min) 'display))))
  711. ;; Update stored values.
  712. (should
  713. (equal
  714. '("12" "12")
  715. (org-test-with-temp-text
  716. "* H
  717. :PROPERTIES:
  718. :A: 1
  719. :END:
  720. "
  721. (let ((org-columns-default-format "%5A")) (org-columns))
  722. (search-forward "1")
  723. (insert "2")
  724. (org-columns-update "A")
  725. (list (get-char-property (point-min) 'org-columns-value)
  726. (get-char-property (point-min) 'org-columns-value-modified)))))
  727. ;; When multiple columns are using the same property, value is
  728. ;; updated according to the specifications of the first one.
  729. (should
  730. (equal
  731. "2"
  732. (org-test-with-temp-text
  733. "* H
  734. :PROPERTIES:
  735. :A: 1
  736. :END:
  737. ** S
  738. :PROPERTIES:
  739. :A: 2
  740. :END:"
  741. (let ((org-columns-default-format "%A{min} %A")) (org-columns))
  742. (org-columns-update "A")
  743. (org-entry-get nil "A"))))
  744. (should
  745. (equal
  746. "1"
  747. (org-test-with-temp-text
  748. "* H
  749. :PROPERTIES:
  750. :A: 1
  751. :END:
  752. ** S
  753. :PROPERTIES:
  754. :A: 2
  755. :END:"
  756. (let ((org-columns-default-format "%A %A{min}")) (org-columns))
  757. (org-columns-update "A")
  758. (org-entry-get nil "A"))))
  759. ;; Ensure modifications propagate in upper levels even when multiple
  760. ;; summary types apply to the same property.
  761. (should
  762. (equal
  763. '("1" "22")
  764. (org-test-with-temp-text
  765. "* H
  766. ** S1
  767. :PROPERTIES:
  768. :A: 1
  769. :END:
  770. ** S2
  771. :PROPERTIES:
  772. :A: <point>2
  773. :END:"
  774. (save-excursion
  775. (goto-char (point-min))
  776. (let ((org-columns-default-format "%A{min} %A{max}")) (org-columns)))
  777. (insert "2")
  778. (org-columns-update "A")
  779. (list (get-char-property 1 'org-columns-value)
  780. (get-char-property 2 'org-columns-value-modified)))))
  781. ;; Ensure additional processing is done (e.g., ellipses, special
  782. ;; keywords fontification...).
  783. (should
  784. (equal
  785. "ve.. |"
  786. (org-test-with-temp-text
  787. "* H
  788. :PROPERTIES:
  789. :A: text
  790. :END:
  791. "
  792. (let ((org-columns-default-format "%4A")
  793. (org-columns-ellipses ".."))
  794. (org-columns))
  795. (search-forward ":A: ")
  796. (insert "very long ")
  797. (org-columns-update "A")
  798. (get-char-property (point-min) 'display))))
  799. ;; Values obtained from inline tasks are at the same level as those
  800. ;; obtained from children of the current node.
  801. (when (featurep 'org-inlinetask)
  802. (should
  803. (equal
  804. "2"
  805. (org-test-with-temp-text
  806. "* H
  807. *************** Inline task
  808. :PROPERTIES:
  809. :A: 2
  810. :END:
  811. *************** END
  812. ** Children
  813. :PROPERTIES:
  814. :A: 3
  815. :END:
  816. "
  817. (let ((org-columns-default-format "%A{min}")
  818. (org-columns-ellipses "..")
  819. (org-inlinetask-min-level 15))
  820. (org-columns))
  821. (get-char-property (point-min) 'org-columns-value)))))
  822. ;; Handle `org-columns-modify-value-for-display-function', even with
  823. ;; multiple titles for the same property.
  824. (should
  825. (equal '("foo" "bar")
  826. (org-test-with-temp-text "* H"
  827. (let ((org-columns-default-format "%ITEM %ITEM(Name)")
  828. (org-columns-modify-value-for-display-function
  829. (lambda (title value)
  830. (pcase title ("ITEM" "foo") ("Name" "bar") (_ "baz")))))
  831. (org-columns))
  832. (list (get-char-property 1 'org-columns-value-modified)
  833. (get-char-property 2 'org-columns-value-modified))))))
  834. (ert-deftest test-org-colview/columns-move-left ()
  835. "Test `org-columns-move-left' specifications."
  836. ;; Error when trying to move the left-most column.
  837. (should-error
  838. (org-test-with-temp-text "* H"
  839. (let ((org-columns-default-format "%ITEM")) (org-columns))
  840. (org-columns-move-left)))
  841. ;; Otherwise, move column to left and update display.
  842. (should
  843. (equal '("2" "1")
  844. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  845. (let ((org-columns-default-format "%A %B")) (org-columns))
  846. (forward-char)
  847. (org-columns-move-left)
  848. (list (get-char-property (point) 'org-columns-value)
  849. (get-char-property (1+ (point)) 'org-columns-value)))))
  850. ;; Handle multiple columns with the same property.
  851. (should
  852. (equal '("2" "1")
  853. (org-test-with-temp-text
  854. "* H
  855. ** S1
  856. :PROPERTIES:
  857. :A: 1
  858. :END:
  859. ** S1
  860. :PROPERTIES:
  861. :A: 2
  862. :END:"
  863. (let ((org-columns-default-format "%ITEM %A{min} %A{max}"))
  864. (org-columns))
  865. (forward-char 2)
  866. (org-columns-move-left)
  867. (list (get-char-property (point) 'org-columns-value)
  868. (get-char-property (1+ (point)) 'org-columns-value)))))
  869. ;; Special case: do not update values even if move entails changing
  870. ;; them.
  871. (should
  872. (equal "1"
  873. (org-test-with-temp-text
  874. "* H
  875. :PROPERTIES:
  876. :A: 1
  877. :END:
  878. ** S1
  879. :PROPERTIES:
  880. :A: 99
  881. :END:"
  882. (let ((org-columns-default-format "%A %A{max}"))
  883. (org-columns))
  884. (forward-char)
  885. (org-columns-move-left)
  886. ;; Since the first column matching a given property
  887. ;; determines how a value is computed, the following
  888. ;; should return "99" instead. However, this behavior is
  889. ;; in practice surprising so we just ignore the value
  890. ;; change. It can be computed later.
  891. (org-entry-get (point) "A")))))
  892. (ert-deftest test-org-colview/columns-move-right ()
  893. "Test `org-columns-move-right' specifications."
  894. ;; Error when trying to move the right-most column.
  895. (should-error
  896. (org-test-with-temp-text "* H"
  897. (let ((org-columns-default-format "%ITEM")) (org-columns))
  898. (org-columns-move-right)))
  899. ;; Otherwise, move column to left and update display.
  900. (should
  901. (equal '("2" "1")
  902. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  903. (let ((org-columns-default-format "%A %B")) (org-columns))
  904. (org-columns-move-right)
  905. (list (get-char-property (1- (point)) 'org-columns-value)
  906. (get-char-property (point) 'org-columns-value)))))
  907. ;; Handle multiple columns with the same property.
  908. (should
  909. (equal '("2" "1")
  910. (org-test-with-temp-text
  911. "* H
  912. ** S1
  913. :PROPERTIES:
  914. :A: 1
  915. :END:
  916. ** S1
  917. :PROPERTIES:
  918. :A: 2
  919. :END:"
  920. (let ((org-columns-default-format "%ITEM %A{min} %A{max}"))
  921. (org-columns))
  922. (forward-char)
  923. (org-columns-move-right)
  924. (list (get-char-property (1- (point)) 'org-columns-value)
  925. (get-char-property (point) 'org-columns-value)))))
  926. ;; Special case: do not update values even if move entails changing
  927. ;; them.
  928. (should
  929. (equal "1"
  930. (org-test-with-temp-text
  931. "* H
  932. :PROPERTIES:
  933. :A: 1
  934. :END:
  935. ** S1
  936. :PROPERTIES:
  937. :A: 99
  938. :END:"
  939. (let ((org-columns-default-format "%A %A{max}"))
  940. (org-columns))
  941. (org-columns-move-right)
  942. ;; See `test-org-colview/columns-move-left' for an
  943. ;; explanation.
  944. (org-entry-get (point) "A")))))
  945. ;;; Dynamic block
  946. (ert-deftest test-org-colview/dblock ()
  947. "Test the column view table."
  948. (should
  949. (equal
  950. "#+BEGIN: columnview
  951. | ITEM |
  952. |------|
  953. | H |
  954. #+END:"
  955. (org-test-with-temp-text
  956. "* H\n<point>#+BEGIN: columnview\n#+END:"
  957. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  958. (buffer-substring-no-properties (point) (point-max)))))
  959. (should
  960. (equal
  961. "#+BEGIN: columnview
  962. | ITEM | A |
  963. |------+---|
  964. | H | 1 |
  965. #+END:"
  966. (org-test-with-temp-text
  967. "* H\n:PROPERTIES:\n:A: 1\n:END:\n<point>#+BEGIN: columnview\n#+END:"
  968. (let ((org-columns-default-format "%ITEM %A")) (org-update-dblock))
  969. (buffer-substring-no-properties (point) (point-max)))))
  970. ;; Properties are case insensitive.
  971. (should
  972. (equal
  973. "#+BEGIN: columnview
  974. | a |
  975. |---|
  976. | 1 |
  977. #+END:"
  978. (org-test-with-temp-text
  979. "* H\n:PROPERTIES:\n:A: 1\n:END:\n<point>#+BEGIN: columnview\n#+END:"
  980. (let ((org-columns-default-format "%a")) (org-update-dblock))
  981. (buffer-substring-no-properties (point) (point-max)))))
  982. ;; Test titles given to columns.
  983. (should
  984. (equal
  985. "#+BEGIN: columnview
  986. | Name | Prop |
  987. |------+------|
  988. | H | 1 |
  989. #+END:"
  990. (org-test-with-temp-text
  991. "* H\n:PROPERTIES:\n:A: 1\n:END:\n<point>#+BEGIN: columnview\n#+END:"
  992. (let ((org-columns-default-format "%ITEM(Name) %A(Prop)"))
  993. (org-update-dblock))
  994. (buffer-substring-no-properties (point) (point-max)))))
  995. ;; Test `:id' parameter
  996. (should
  997. (equal
  998. "#+BEGIN: columnview :id local
  999. | ITEM |
  1000. |------|
  1001. | H1 |
  1002. | H1.1 |
  1003. #+END:
  1004. "
  1005. (org-test-with-temp-text
  1006. "* H1\n<point>#+BEGIN: columnview :id local\n#+END:\n** H1.1\n* H2"
  1007. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1008. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1009. (should
  1010. (equal
  1011. "#+BEGIN: columnview :id global
  1012. | ITEM |
  1013. |------|
  1014. | H1 |
  1015. | H1.1 |
  1016. | H2 |
  1017. #+END:
  1018. "
  1019. (org-test-with-temp-text
  1020. "\n* H1\n<point>#+BEGIN: columnview :id global\n#+END:\n** H1.1\n* H2"
  1021. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1022. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1023. ;; Test `:hlines' parameter.
  1024. (should
  1025. (equal
  1026. "#+BEGIN: columnview :hlines t :id global
  1027. | ITEM |
  1028. |------|
  1029. | H |
  1030. |------|
  1031. | H2 |
  1032. |------|
  1033. | H2.1 |
  1034. #+END:\n"
  1035. (org-test-with-temp-text
  1036. "
  1037. * H
  1038. <point>#+BEGIN: columnview :hlines t :id global
  1039. #+END:
  1040. * H2
  1041. ** H2.1"
  1042. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1043. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1044. (should
  1045. (equal
  1046. "#+BEGIN: columnview :hlines 1 :id global
  1047. | ITEM |
  1048. |------|
  1049. | H |
  1050. |------|
  1051. | H2 |
  1052. | H2.1 |
  1053. #+END:\n"
  1054. (org-test-with-temp-text
  1055. "
  1056. * H
  1057. <point>#+BEGIN: columnview :hlines 1 :id global
  1058. #+END:
  1059. * H2
  1060. ** H2.1"
  1061. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1062. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1063. ;; Test `:indent' parameter.
  1064. (should
  1065. (equal
  1066. "#+BEGIN: columnview :indent t
  1067. | ITEM |
  1068. |----------|
  1069. | H1 |
  1070. | \\_ H1.1 |
  1071. #+END:
  1072. "
  1073. (org-test-with-temp-text
  1074. "* H1\n<point>#+BEGIN: columnview :indent t\n#+END:\n** H1.1"
  1075. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1076. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1077. (should
  1078. (equal
  1079. "#+BEGIN: columnview :indent t
  1080. | Prop | Name |
  1081. |------+----------|
  1082. | | H1 |
  1083. | | \\_ H1.1 |
  1084. #+END:
  1085. "
  1086. (org-test-with-temp-text
  1087. "* H1\n<point>#+BEGIN: columnview :indent t\n#+END:\n** H1.1"
  1088. (let ((org-columns-default-format "%A(Prop) %ITEM(Name)"))
  1089. (org-update-dblock))
  1090. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1091. ;; Test `:vlines' parameter.
  1092. (should
  1093. (equal
  1094. "#+BEGIN: columnview :vlines t
  1095. | | ITEM | A |
  1096. |---+------+----|
  1097. | | H | 1 |
  1098. | / | <> | <> |
  1099. #+END:"
  1100. (org-test-with-temp-text
  1101. "* H\n:PROPERTIES:\n:A: 1\n:END:\n<point>#+BEGIN: columnview :vlines t\n#+END:"
  1102. (let ((org-columns-default-format "%ITEM %A")) (org-update-dblock))
  1103. (buffer-substring-no-properties (point) (point-max)))))
  1104. ;; Test `:skip-empty-rows' parameter.
  1105. (should
  1106. (equal
  1107. "#+BEGIN: columnview :skip-empty-rows t
  1108. | ITEM | A |
  1109. |------+---|
  1110. | H1.1 | 1 |
  1111. #+END:
  1112. "
  1113. (org-test-with-temp-text
  1114. "
  1115. * H1
  1116. <point>#+BEGIN: columnview :skip-empty-rows t
  1117. #+END:
  1118. ** H1.1
  1119. :PROPERTIES:
  1120. :A: 1
  1121. :END:"
  1122. (let ((org-columns-default-format "%ITEM %A")) (org-update-dblock))
  1123. (buffer-substring-no-properties (point) (outline-next-heading)))))
  1124. ;; Test `:format' parameter.
  1125. (should
  1126. (equal
  1127. "#+BEGIN: columnview :format \"%ITEM(Name)\"
  1128. | Name |
  1129. |------|
  1130. | H |
  1131. #+END:"
  1132. (org-test-with-temp-text
  1133. "* H\n<point>#+BEGIN: columnview :format \"%ITEM(Name)\"\n#+END:"
  1134. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1135. (buffer-substring-no-properties (point) (point-max)))))
  1136. ;; Test `:width' parameter
  1137. (should
  1138. (equal
  1139. "#+BEGIN: columnview :width t
  1140. | ITEM | A |
  1141. |------------+---|
  1142. | H | |
  1143. | <10> | |
  1144. #+END:"
  1145. (org-test-with-temp-text
  1146. "* H\n<point>#+BEGIN: columnview :width t\n#+END:"
  1147. (let ((org-columns-default-format "%10ITEM %A")) (org-update-dblock))
  1148. (buffer-substring-no-properties (point) (point-max)))))
  1149. ;; When inserting ITEM values, make sure to clean sensitive
  1150. ;; contents, like unique targets or forbidden inline src-blocks.
  1151. (should
  1152. (equal
  1153. "#+BEGIN: columnview
  1154. | ITEM |
  1155. |------|
  1156. | H 1 |
  1157. #+END:"
  1158. (org-test-with-temp-text
  1159. "* H <<target>> 1\n<point>#+BEGIN: columnview\n#+END:"
  1160. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1161. (buffer-substring-no-properties (point) (point-max)))))
  1162. (should
  1163. (equal
  1164. "#+BEGIN: columnview
  1165. | ITEM |
  1166. |------|
  1167. | H 1 |
  1168. #+END:"
  1169. (org-test-with-temp-text
  1170. "* H src_emacs-lisp{(+ 1 1)} 1\n<point>#+BEGIN: columnview\n#+END:"
  1171. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  1172. (buffer-substring-no-properties (point) (point-max))))))
  1173. (provide 'test-org-colview)
  1174. ;;; test-org-colview.el ends here