test-org-clock.el 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. ;;; test-org-clock.el --- Tests for org-clock.el
  2. ;; Copyright (C) 2012, 2014, 2015, 2019 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Released under the GNU General Public License version 3
  5. ;; see: https://www.gnu.org/licenses/gpl-3.0.html
  6. ;;;; Comments
  7. ;;; Code:
  8. (defun org-test-clock-create-timestamp (input &optional inactive with-time)
  9. "Create a timestamp out of a date/time prompt string.
  10. INPUT is a string as expected in a date/time prompt, i.e \"+2d\"
  11. or \"2/5\".
  12. When optional argument INACTIVE is non-nil, return an inactive
  13. timestamp. When optional argument WITH-TIME is non-nil, also
  14. insert hours and minutes.
  15. Return the timestamp as a string."
  16. (org-element-interpret-data
  17. (let ((time (decode-time
  18. (org-encode-time
  19. (org-fix-decoded-time
  20. (org-read-date-analyze
  21. input nil (decode-time (current-time))))))))
  22. (list 'timestamp
  23. (list :type (if inactive 'inactive 'active)
  24. :minute-start (and with-time (nth 1 time))
  25. :hour-start (and with-time (nth 2 time))
  26. :day-start (nth 3 time)
  27. :month-start (nth 4 time)
  28. :year-start (nth 5 time))))))
  29. (defun org-test-clock-create-clock (input1 &optional input2)
  30. "Create a clock line out of two date/time prompts.
  31. INPUT1 and INPUT2 are strings as expected in a date/time prompt,
  32. i.e \"+2d\" or \"2/5\". They respectively refer to start and end
  33. range. INPUT2 can be omitted if clock hasn't finished yet.
  34. Return the clock line as a string."
  35. (let* ((beg (org-test-clock-create-timestamp input1 t t))
  36. (end (and input2 (org-test-clock-create-timestamp input2 t t)))
  37. (sec-diff (and input2
  38. (floor (- (org-time-string-to-seconds end)
  39. (org-time-string-to-seconds beg))))))
  40. (concat org-clock-string " " beg
  41. (when end
  42. (concat "--" end " => "
  43. (format "%2d:%02d"
  44. (/ sec-diff 3600)
  45. (/ (mod sec-diff 3600) 60))))
  46. "\n")))
  47. (defun test-org-clock-clocktable-contents (options &optional initial)
  48. "Return contents of a Clock table for current buffer
  49. OPTIONS is a string of Clock table options. Optional argument
  50. INITIAL is a string specifying initial contents within the Clock
  51. table.
  52. Caption is ignored in contents. The clocktable doesn't appear in
  53. the buffer."
  54. (declare (indent 2))
  55. (goto-char (point-min))
  56. (save-excursion
  57. (insert "#+BEGIN: clocktable " options "\n")
  58. (when initial (insert initial))
  59. (unless (string-suffix-p "\n" initial) (insert "\n"))
  60. (insert "#+END:\n"))
  61. (unwind-protect
  62. (save-excursion
  63. (let ((org-duration-format 'h:mm)) (org-update-dblock))
  64. (forward-line)
  65. ;; Skip caption.
  66. (when (looking-at "#\\+CAPTION:") (forward-line))
  67. (buffer-substring-no-properties
  68. (point) (progn (search-forward "#+END:") (line-end-position 0))))
  69. ;; Remove clocktable.
  70. (delete-region (point) (search-forward "#+END:\n"))))
  71. ;;; Clock drawer
  72. (ert-deftest test-org-clock/into-drawer ()
  73. "Test `org-clock-into-drawer' specifications."
  74. ;; When `org-clock-into-drawer' is nil, do not use a clock drawer.
  75. (should-not
  76. (org-test-with-temp-text "* H"
  77. (let ((org-clock-into-drawer nil)
  78. (org-log-into-drawer nil))
  79. (org-clock-into-drawer))))
  80. (should-not
  81. (org-test-with-temp-text "* H"
  82. (let ((org-clock-into-drawer nil)
  83. (org-log-into-drawer t))
  84. (org-clock-into-drawer))))
  85. (should-not
  86. (org-test-with-temp-text "* H"
  87. (let ((org-clock-into-drawer nil)
  88. (org-log-into-drawer "BAR"))
  89. (org-clock-into-drawer))))
  90. ;; When `org-clock-into-drawer' is a string, use it
  91. ;; unconditionally.
  92. (should
  93. (equal "FOO"
  94. (org-test-with-temp-text "* H"
  95. (let ((org-clock-into-drawer "FOO")
  96. (org-log-into-drawer nil))
  97. (org-clock-into-drawer)))))
  98. (should
  99. (equal "FOO"
  100. (org-test-with-temp-text "* H"
  101. (let ((org-clock-into-drawer "FOO")
  102. (org-log-into-drawer t))
  103. (org-clock-into-drawer)))))
  104. (should
  105. (equal "FOO"
  106. (org-test-with-temp-text "* H"
  107. (let ((org-clock-into-drawer "FOO")
  108. (org-log-into-drawer "BAR"))
  109. (org-clock-into-drawer)))))
  110. ;; When `org-clock-into-drawer' is an integer, return it.
  111. (should
  112. (= 1
  113. (org-test-with-temp-text "* H"
  114. (let ((org-clock-into-drawer 1)
  115. (org-log-into-drawer nil))
  116. (org-clock-into-drawer)))))
  117. (should
  118. (= 1
  119. (org-test-with-temp-text "* H"
  120. (let ((org-clock-into-drawer 1)
  121. (org-log-into-drawer t))
  122. (org-clock-into-drawer)))))
  123. (should
  124. (= 1
  125. (org-test-with-temp-text "* H"
  126. (let ((org-clock-into-drawer 1)
  127. (org-log-into-drawer "BAR"))
  128. (org-clock-into-drawer)))))
  129. ;; Otherwise, any non-nil value defaults to `org-log-into-drawer' or
  130. ;; "LOGBOOK" if it is nil.
  131. (should
  132. (equal "LOGBOOK"
  133. (org-test-with-temp-text "* H"
  134. (let ((org-clock-into-drawer t)
  135. (org-log-into-drawer nil))
  136. (org-clock-into-drawer)))))
  137. (should
  138. (equal "LOGBOOK"
  139. (org-test-with-temp-text "* H"
  140. (let ((org-clock-into-drawer t)
  141. (org-log-into-drawer t))
  142. (org-clock-into-drawer)))))
  143. (should
  144. (equal "FOO"
  145. (org-test-with-temp-text "* H"
  146. (let ((org-clock-into-drawer t)
  147. (org-log-into-drawer "FOO"))
  148. (org-clock-into-drawer)))))
  149. ;; A non-nil "CLOCK_INTO_DRAWER" property overrides
  150. ;; `org-clock-into-drawer' value.
  151. (should
  152. (equal "LOGBOOK"
  153. (org-test-with-temp-text
  154. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: t\n:END:"
  155. (let ((org-clock-into-drawer nil)
  156. (org-log-into-drawer nil))
  157. (org-clock-into-drawer)))))
  158. (should
  159. (equal "FOO"
  160. (org-test-with-temp-text
  161. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: FOO\n:END:"
  162. (let ((org-clock-into-drawer nil)
  163. (org-log-into-drawer nil))
  164. (org-clock-into-drawer)))))
  165. (should-not
  166. (org-test-with-temp-text
  167. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: nil\n:END:"
  168. (let ((org-clock-into-drawer t)
  169. (org-log-into-drawer nil))
  170. (org-clock-into-drawer))))
  171. ;; "CLOCK_INTO_DRAWER" can be inherited.
  172. (should
  173. (equal "LOGBOOK"
  174. (org-test-with-temp-text
  175. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: t\n:END:\n** H2<point>"
  176. (let ((org-clock-into-drawer nil)
  177. (org-log-into-drawer nil))
  178. (org-clock-into-drawer)))))
  179. (should
  180. (equal "FOO"
  181. (org-test-with-temp-text
  182. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: FOO\n:END:\n** H2<point>"
  183. (let ((org-clock-into-drawer nil)
  184. (org-log-into-drawer nil))
  185. (org-clock-into-drawer)))))
  186. (should-not
  187. (org-test-with-temp-text
  188. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: nil\n:END:\n** H2<point>"
  189. (let ((org-clock-into-drawer t)
  190. (org-log-into-drawer nil))
  191. (org-clock-into-drawer)))))
  192. (ert-deftest test-org-clock/drawer-name ()
  193. "Test `org-clock-drawer-name' specifications."
  194. ;; A nil value for `org-clock-into-drawer' means no drawer is
  195. ;; expected whatsoever.
  196. (should-not
  197. (org-test-with-temp-text "* H"
  198. (let ((org-clock-into-drawer nil)
  199. (org-log-into-drawer nil))
  200. (org-clock-drawer-name))))
  201. (should-not
  202. (org-test-with-temp-text "* H"
  203. (let ((org-clock-into-drawer nil)
  204. (org-log-into-drawer t))
  205. (org-clock-drawer-name))))
  206. (should-not
  207. (org-test-with-temp-text "* H"
  208. (let ((org-clock-into-drawer nil)
  209. (org-log-into-drawer "FOO"))
  210. (org-clock-drawer-name))))
  211. ;; A string value for `org-clock-into-drawer' means to use it
  212. ;; unconditionally.
  213. (should
  214. (equal "FOO"
  215. (org-test-with-temp-text "* H"
  216. (let ((org-clock-into-drawer "FOO")
  217. (org-log-into-drawer nil))
  218. (org-clock-drawer-name)))))
  219. (should
  220. (equal "FOO"
  221. (org-test-with-temp-text "* H"
  222. (let ((org-clock-into-drawer "FOO")
  223. (org-log-into-drawer t))
  224. (org-clock-drawer-name)))))
  225. (should
  226. (equal "FOO"
  227. (org-test-with-temp-text "* H"
  228. (let ((org-clock-into-drawer "FOO")
  229. (org-log-into-drawer "BAR"))
  230. (org-clock-drawer-name)))))
  231. ;; When the value in `org-clock-into-drawer' is a number, re-use
  232. ;; `org-log-into-drawer' or use default "LOGBOOK" value.
  233. (should
  234. (equal "FOO"
  235. (org-test-with-temp-text "* H"
  236. (let ((org-clock-into-drawer 1)
  237. (org-log-into-drawer "FOO"))
  238. (org-clock-drawer-name)))))
  239. (should
  240. (equal "LOGBOOK"
  241. (org-test-with-temp-text "* H"
  242. (let ((org-clock-into-drawer 1)
  243. (org-log-into-drawer t))
  244. (org-clock-drawer-name)))))
  245. (should
  246. (equal "LOGBOOK"
  247. (org-test-with-temp-text "* H"
  248. (let ((org-clock-into-drawer 1)
  249. (org-log-into-drawer nil))
  250. (org-clock-drawer-name))))))
  251. ;;; Clocktable
  252. (ert-deftest test-org-clock/clocktable/insert ()
  253. "Test insert clocktable dynamic block with `org-dynamic-block-insert-dblock'."
  254. (should
  255. (equal
  256. "| Headline | Time |
  257. |--------------+--------|
  258. | *Total time* | *1:00* |
  259. |--------------+--------|
  260. | H1 | 1:00 |"
  261. (org-test-with-temp-text "* H1\n<point>"
  262. (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
  263. (goto-line 2)
  264. (require 'org-clock)
  265. (org-dynamic-block-insert-dblock "clocktable")
  266. (goto-line 1)
  267. (unwind-protect
  268. (save-excursion
  269. (when (search-forward "#+CAPTION:") (forward-line))
  270. (buffer-substring-no-properties
  271. (point) (progn (search-forward "#+END:") (line-end-position 0))))
  272. (delete-region (point) (search-forward "#+END:\n")))))))
  273. (ert-deftest test-org-clock/clocktable/ranges ()
  274. "Test ranges in Clock table."
  275. ;; Relative time: Previous two days.
  276. (should
  277. (equal
  278. "| Headline | Time | |
  279. |------------------------------+--------+------|
  280. | *Total time* | *8:00* | |
  281. |------------------------------+--------+------|
  282. | Relative times in clocktable | 8:00 | |
  283. | Foo | | 8:00 |"
  284. (org-test-with-temp-text
  285. "* Relative times in clocktable\n** Foo\n<point>"
  286. (insert (org-test-clock-create-clock "-3d 8:00" "-3d 12:00"))
  287. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  288. (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
  289. (test-org-clock-clocktable-contents
  290. ":tstart \"<-2d>\" :tend \"<today>\" :indent nil"))))
  291. ;; Relative time: Yesterday until now.
  292. (should
  293. (equal
  294. "| Headline | Time | |
  295. |------------------------------+--------+------|
  296. | *Total time* | *6:00* | |
  297. |------------------------------+--------+------|
  298. | Relative times in clocktable | 6:00 | |
  299. | Foo | | 6:00 |"
  300. (org-test-with-temp-text
  301. "* Relative times in clocktable\n** Foo\n<point>"
  302. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  303. (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
  304. (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
  305. (test-org-clock-clocktable-contents
  306. ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil"))))
  307. ;; Test `untilnow' block.
  308. (should
  309. (equal
  310. "| Headline | Time | |
  311. |------------------------------+--------+------|
  312. | *Total time* | *6:00* | |
  313. |------------------------------+--------+------|
  314. | Relative times in clocktable | 6:00 | |
  315. | Foo | | 6:00 |"
  316. (org-test-with-temp-text
  317. "* Relative times in clocktable\n** Foo\n<point>"
  318. (insert (org-test-clock-create-clock "-10y 15:00" "-10y 18:00"))
  319. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  320. (test-org-clock-clocktable-contents ":block untilnow :indent nil")))))
  321. (ert-deftest test-org-clock/clocktable/match ()
  322. "Test \":match\" parameter in Clock table."
  323. ;; Test match filtering.
  324. (should
  325. (equal
  326. "| Headline | Time | |
  327. |--------------+--------+------|
  328. | *Total time* | *2:00* | |
  329. |--------------+--------+------|
  330. | H1 | | 2:00 |"
  331. (org-test-with-temp-text "** H1\n\n*** H2 :tag:\n\n*** H3\n<point>"
  332. (insert (org-test-clock-create-clock ". 8:00" ". 9:00"))
  333. (goto-line 4)
  334. (insert (org-test-clock-create-clock ". 9:00" ". 11:00"))
  335. (test-org-clock-clocktable-contents ":match \"tag\" :indent nil")))))
  336. (ert-deftest test-org-clock/clocktable/tags ()
  337. "Test \":tags\" parameter in Clock table."
  338. ;; Test tags column.
  339. (should
  340. (equal
  341. "| Tags | Headline | Time | |
  342. |------+--------------+--------+------|
  343. | | *Total time* | *1:00* | |
  344. |------+--------------+--------+------|
  345. | tag | H1 | | 1:00 |"
  346. (org-test-with-temp-text "** H1 :tag:\n\n*** H2 \n<point>"
  347. (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
  348. (goto-line 4)
  349. (test-org-clock-clocktable-contents ":tags t :indent nil")))))
  350. (ert-deftest test-org-clock/clocktable/scope ()
  351. "Test \":scope\" parameter in Clock table."
  352. ;; Test `file-with-archives' scope. In particular, preserve "TBLFM"
  353. ;; line, and ignore "file" column.
  354. (should
  355. (equal
  356. "| Headline | Time | |
  357. |--------------+--------+-----|
  358. | *Total time* | *8:40* | foo |
  359. |--------------+--------+-----|
  360. | Test | 8:40 | foo |
  361. #+TBLFM: $3=string(\"foo\")"
  362. (org-test-with-temp-text-in-file
  363. "* Test
  364. CLOCK: [2012-03-29 Thu 8:00]--[2012-03-29 Thu 16:40] => 8:40"
  365. (test-org-clock-clocktable-contents ":scope file-with-archives"
  366. "#+TBLFM: $3=string(\"foo\")"))))
  367. ;; Test "function" scope.
  368. (should
  369. (string-match-p
  370. (regexp-quote "| ALL *Total time* | *1:00* |")
  371. (org-test-with-temp-text-in-file
  372. "* Test
  373. CLOCK: [2012-03-29 Thu 16:00]--[2012-03-29 Thu 17:00] => 1:00"
  374. (let ((the-file (buffer-file-name)))
  375. (org-test-with-temp-text-in-file ""
  376. (test-org-clock-clocktable-contents
  377. (format ":scope (lambda () (list %S))" the-file))))))))
  378. (ert-deftest test-org-clock/clocktable/maxlevel ()
  379. "Test \":maxlevel\" parameter in Clock table."
  380. (should
  381. (equal "| Headline | Time | |
  382. |--------------+--------+------|
  383. | *Total time* | *6:00* | |
  384. |--------------+--------+------|
  385. | Foo | 6:00 | |
  386. | \\_ Bar | | 2:00 |"
  387. (org-test-with-temp-text
  388. "* Foo
  389. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  390. ** Bar
  391. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  392. (test-org-clock-clocktable-contents ":maxlevel 3"))))
  393. (should
  394. (equal "| Headline | Time | |
  395. |--------------+--------+------|
  396. | *Total time* | *6:00* | |
  397. |--------------+--------+------|
  398. | Foo | 6:00 | |
  399. | \\_ Bar | | 2:00 |"
  400. (org-test-with-temp-text
  401. "* Foo
  402. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  403. ** Bar
  404. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  405. (test-org-clock-clocktable-contents ":maxlevel 2"))))
  406. (should
  407. (equal "| Headline | Time |
  408. |--------------+--------|
  409. | *Total time* | *6:00* |
  410. |--------------+--------|
  411. | Foo | 6:00 |"
  412. (org-test-with-temp-text
  413. "* Foo
  414. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  415. ** Bar
  416. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  417. (test-org-clock-clocktable-contents ":maxlevel 1"))))
  418. ;; Special ":maxlevel 0" case: only report total file time.
  419. (should
  420. (equal "| Headline | Time |
  421. |--------------+--------|
  422. | *Total time* | *6:00* |
  423. |--------------+--------|"
  424. (org-test-with-temp-text
  425. "* Foo
  426. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  427. ** Bar
  428. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  429. (test-org-clock-clocktable-contents ":maxlevel 0")))))
  430. (ert-deftest test-org-clock/clocktable/formula ()
  431. "Test \":formula\" parameter in Clock table."
  432. ;; Test ":formula %". Handle various duration formats.
  433. (should
  434. (equal
  435. "| Headline | Time | % |
  436. |--------------+--------+-------|
  437. | *Total time* | *6:00* | 100.0 |
  438. |--------------+--------+-------|
  439. | Foo | 4:00 | 66.7 |
  440. | Bar | 2:00 | 33.3 |"
  441. (org-test-with-temp-text
  442. "* Foo
  443. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  444. * Bar
  445. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  446. (test-org-clock-clocktable-contents ":maxlevel 1 :formula %"))))
  447. (should
  448. (equal
  449. "| Headline | Time | % |
  450. |--------------+---------+-------|
  451. | *Total time* | *28:00* | 100.0 |
  452. |--------------+---------+-------|
  453. | Foo | 26:00 | 92.9 |
  454. | Bar | 2:00 | 7.1 |"
  455. (org-test-with-temp-text
  456. "* Foo
  457. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  458. * Bar
  459. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  460. (test-org-clock-clocktable-contents ":maxlevel 1 :formula %"))))
  461. ;; Properly align column with different depths.
  462. (should
  463. (equal "| Headline | Time | | | % |
  464. |---------------+--------+------+------+-------|
  465. | *Total time* | *1:00* | | | 100.0 |
  466. |---------------+--------+------+------+-------|
  467. | foo | 1:00 | | | 100.0 |
  468. | \\_ sub | | 0:15 | | 25.0 |
  469. | \\_ sub2 | | 0:15 | | 25.0 |
  470. | \\_ sub3 | | 0:30 | | 50.0 |
  471. | \\_ subsub1 | | | 0:15 | 25.0 |
  472. | \\_ subsub1 | | | 0:15 | 25.0 |"
  473. (org-test-with-temp-text
  474. "* foo
  475. ** sub
  476. :LOGBOOK:
  477. CLOCK: [2017-03-18 Sat 15:00]--[2017-03-18 Sat 15:15] => 0:15
  478. :END:
  479. ** sub2
  480. :LOGBOOK:
  481. CLOCK: [2017-03-18 Sat 15:15]--[2017-03-18 Sat 15:30] => 0:15
  482. :END:
  483. ** sub3
  484. *** subsub1
  485. :LOGBOOK:
  486. CLOCK: [2017-03-18 Sat 13:00]--[2017-03-18 Sat 13:15] => 0:15
  487. :END:
  488. *** subsub1
  489. :LOGBOOK:
  490. CLOCK: [2017-03-18 Sat 14:00]--[2017-03-18 Sat 14:15] => 0:15
  491. :END:"
  492. (test-org-clock-clocktable-contents ":maxlevel 3 :formula %")))))
  493. (ert-deftest test-org-clock/clocktable/lang ()
  494. "Test \":lang\" parameter in Clock table."
  495. ;; Test foreign translation
  496. (should
  497. (equal
  498. "| Headline | Time |
  499. |--------------+---------|
  500. | *Total time* | *26:00* |
  501. |--------------+---------|
  502. | Foo | 26:00 |"
  503. (org-test-with-temp-text
  504. "* Foo
  505. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  506. (test-org-clock-clocktable-contents ":maxlevel 1 :lang en"))))
  507. (should
  508. (equal
  509. "| En-tête | Durée |
  510. |----------------+---------|
  511. | *Durée totale* | *26:00* |
  512. |----------------+---------|
  513. | Foo | 26:00 |"
  514. (org-test-with-temp-text
  515. "* Foo
  516. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  517. (test-org-clock-clocktable-contents ":maxlevel 1 :lang fr"))))
  518. ;; No :lang parameter is equivalent to "en".
  519. (should
  520. (equal
  521. (org-test-with-temp-text
  522. "* Foo
  523. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  524. (test-org-clock-clocktable-contents ":maxlevel 1 :lang en"))
  525. (org-test-with-temp-text
  526. "* Foo
  527. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  528. (test-org-clock-clocktable-contents ":maxlevel 1"))))
  529. ;; Unknown translation fall backs to "en".
  530. (should
  531. (equal
  532. "| Headline | Time |
  533. |--------------+---------|
  534. | *Total time* | *26:00* |
  535. |--------------+---------|
  536. | Foo | 26:00 |"
  537. (org-test-with-temp-text
  538. "* Foo
  539. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  540. (test-org-clock-clocktable-contents ":maxlevel 1 :lang foo")))))
  541. (ert-deftest test-org-clock/clocktable/link ()
  542. "Test \":link\" parameter in Clock table."
  543. ;; If there is no file attached to the document, link directly to
  544. ;; the headline.
  545. (should
  546. (string-match-p "| +\\[\\[\\*Foo]\\[Foo]] +| 26:00 +|"
  547. (org-test-with-temp-text
  548. "* Foo
  549. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  550. (test-org-clock-clocktable-contents ":link t"))))
  551. ;; Otherwise, link to the headline in the current file.
  552. (should
  553. (string-match-p
  554. "| \\[\\[file:filename::\\*Foo]\\[Foo]] +| 26:00 +|"
  555. (org-test-with-temp-text
  556. (org-test-with-temp-text-in-file
  557. "* Foo
  558. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  559. (let ((file (buffer-file-name)))
  560. (replace-regexp-in-string
  561. (regexp-quote file) "filename"
  562. (test-org-clock-clocktable-contents ":link t :lang en"))))
  563. (org-table-align)
  564. (buffer-substring-no-properties (point-min) (point-max)))))
  565. ;; Ignore TODO keyword, priority cookie, COMMENT and tags in
  566. ;; headline.
  567. (should
  568. (string-match-p
  569. "| \\[\\[\\*Foo]\\[Foo]] +| 26:00 +|"
  570. (org-test-with-temp-text
  571. "* TODO Foo
  572. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  573. (test-org-clock-clocktable-contents ":link t :lang en"))))
  574. (should
  575. (string-match-p
  576. "| \\[\\[\\*Foo]\\[Foo]] +| 26:00 +|"
  577. (org-test-with-temp-text
  578. "* [#A] Foo
  579. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  580. (test-org-clock-clocktable-contents ":link t :lang en"))))
  581. (should
  582. (string-match-p
  583. "| \\[\\[\\*Foo]\\[Foo]] +| 26:00 +|"
  584. (org-test-with-temp-text
  585. "* COMMENT Foo
  586. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  587. (test-org-clock-clocktable-contents ":link t"))))
  588. (should
  589. (string-match-p
  590. "| \\[\\[\\*Foo]\\[Foo]] +| 26:00 +|"
  591. (org-test-with-temp-text
  592. "* Foo :tag:
  593. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  594. (test-org-clock-clocktable-contents ":link t :lang en"))))
  595. ;; Remove statistics cookie from headline description.
  596. (should
  597. (string-match-p
  598. "| \\[\\[\\*Foo]\\[Foo]] +| 26:00 +|"
  599. (org-test-with-temp-text
  600. "* Foo [50%]
  601. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  602. (test-org-clock-clocktable-contents ":link t :lang en"))))
  603. (should
  604. (string-match-p
  605. "| \\[\\[\\*Foo]\\[Foo]] +| 26:00 +|"
  606. (org-test-with-temp-text
  607. "* Foo [1/2]
  608. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  609. (test-org-clock-clocktable-contents ":link t :lang en"))))
  610. ;; Replace links with their description, or turn them into plain
  611. ;; links if there is no description.
  612. (should
  613. (string-match-p
  614. "| \\[\\[\\*Foo \\\\\\[\\\\\\[https://orgmode\\.org\\\\]\\\\\\[Org mode\\\\]\\\\]]\\[Foo Org mode]] +| 26:00 +|"
  615. (org-test-with-temp-text
  616. "* Foo [[https://orgmode.org][Org mode]]
  617. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  618. (test-org-clock-clocktable-contents ":link t :lang en"))))
  619. (should
  620. (string-match-p
  621. "| \\[\\[\\*Foo \\\\\\[\\\\\\[https://orgmode\\.org\\\\]\\\\]]\\[Foo https://orgmode\\.org]] +| 26:00 +|"
  622. (org-test-with-temp-text
  623. "* Foo [[https://orgmode.org]]
  624. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  625. (test-org-clock-clocktable-contents ":link t :lang en")))))
  626. (ert-deftest test-org-clock/clocktable/compact ()
  627. "Test \":compact\" parameter in Clock table."
  628. ;; With :compact, all headlines are in the same column.
  629. (should
  630. (equal
  631. "| Headline | Time |
  632. |--------------+---------|
  633. | *Total time* | *26:00* |
  634. |--------------+---------|
  635. | Foo | 26:00 |"
  636. (org-test-with-temp-text
  637. "* Foo
  638. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  639. (test-org-clock-clocktable-contents ":compact t"))))
  640. (should
  641. (equal
  642. "| Headline | Time |
  643. |--------------+---------|
  644. | *Total time* | *52:00* |
  645. |--------------+---------|
  646. | Foo | 52:00 |
  647. | \\_ Bar | 26:00 |"
  648. (org-test-with-temp-text
  649. "* Foo
  650. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  651. ** Bar
  652. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  653. (test-org-clock-clocktable-contents ":compact t"))))
  654. ;; :maxlevel does not affect :compact parameter.
  655. (should
  656. (equal
  657. "| Headline | Time |
  658. |--------------+---------|
  659. | *Total time* | *52:00* |
  660. |--------------+---------|
  661. | Foo | 52:00 |
  662. | \\_ Bar | 26:00 |"
  663. (org-test-with-temp-text
  664. "* Foo
  665. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  666. ** Bar
  667. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  668. (test-org-clock-clocktable-contents ":compact t :maxlevel 2"))))
  669. ;; :compact implies a non-nil :indent parameter.
  670. (should
  671. (equal
  672. "| Headline | Time |
  673. |--------------+---------|
  674. | *Total time* | *52:00* |
  675. |--------------+---------|
  676. | Foo | 52:00 |
  677. | \\_ Bar | 26:00 |"
  678. (org-test-with-temp-text
  679. "* Foo
  680. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  681. ** Bar
  682. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  683. (test-org-clock-clocktable-contents ":compact t :indent nil"))))
  684. ;; :compact implies a nil :level parameter.
  685. (should
  686. (equal
  687. "| Headline | Time |
  688. |--------------+---------|
  689. | *Total time* | *52:00* |
  690. |--------------+---------|
  691. | Foo | 52:00 |
  692. | \\_ Bar | 26:00 |"
  693. (org-test-with-temp-text
  694. "* Foo
  695. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  696. ** Bar
  697. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  698. (test-org-clock-clocktable-contents ":compact t :level t")))))
  699. (ert-deftest test-org-clock/clocktable/properties ()
  700. "Test \":properties\" parameter in Clock table."
  701. ;; Include a new column with list properties.
  702. (should
  703. (equal
  704. "| A | Headline | Time |
  705. |---+--------------+---------|
  706. | | *Total time* | *26:00* |
  707. |---+--------------+---------|
  708. | 1 | Foo | 26:00 |"
  709. (org-test-with-temp-text
  710. "* Foo
  711. :PROPERTIES:
  712. :A: 1
  713. :END:
  714. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  715. (test-org-clock-clocktable-contents ":properties (\"A\")"))))
  716. (should
  717. (equal
  718. "| A | Headline | Time | |
  719. |---+--------------+---------+-------|
  720. | | *Total time* | *52:00* | |
  721. |---+--------------+---------+-------|
  722. | | Foo | 52:00 | |
  723. | 1 | \\_ Bar | | 26:00 |"
  724. (org-test-with-temp-text
  725. "* Foo
  726. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  727. ** Bar
  728. :PROPERTIES:
  729. :A: 1
  730. :END:
  731. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  732. (test-org-clock-clocktable-contents ":properties (\"A\")"))))
  733. ;; Handle missing properties.
  734. (should
  735. (equal
  736. "| A | Headline | Time |
  737. |---+--------------+---------|
  738. | | *Total time* | *26:00* |
  739. |---+--------------+---------|
  740. | 1 | Foo | 26:00 |"
  741. (org-test-with-temp-text
  742. "* Foo
  743. :PROPERTIES:
  744. :A: 1
  745. :END:
  746. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  747. (test-org-clock-clocktable-contents ":properties (\"A\")")))))
  748. (ert-deftest test-org-clock/clocktable/tcolumns ()
  749. "Test \":tcolumns\" parameter in Clock table."
  750. ;; When :tcolumns is smaller than the deepest headline level, lump
  751. ;; lower levels in the last column.
  752. (should
  753. (equal
  754. "| Headline | Time |
  755. |--------------+---------|
  756. | *Total time* | *52:00* |
  757. |--------------+---------|
  758. | Foo | 52:00 |
  759. | \\_ Bar | 26:00 |"
  760. (org-test-with-temp-text
  761. "* Foo
  762. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  763. ** Bar
  764. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  765. (test-org-clock-clocktable-contents ":tcolumns 1"))))
  766. ;; :tcolumns cannot create more columns than the deepest headline
  767. ;; level.
  768. (should
  769. (equal
  770. "| Headline | Time | |
  771. |--------------+---------+-------|
  772. | *Total time* | *52:00* | |
  773. |--------------+---------+-------|
  774. | Foo | 52:00 | |
  775. | \\_ Bar | | 26:00 |"
  776. (org-test-with-temp-text
  777. "* Foo
  778. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  779. ** Bar
  780. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  781. (test-org-clock-clocktable-contents ":tcolumns 3"))))
  782. ;; Pathological case: when no headline contributes to the total
  783. ;; time, there is only one time column.
  784. (should
  785. (equal "| Headline | Time |
  786. |--------------+--------|
  787. | *Total time* | *0:00* |"
  788. (org-test-with-temp-text
  789. "* Foo
  790. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 11:09] => 0:00
  791. ** Bar
  792. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 13:09] => 0:00"
  793. (test-org-clock-clocktable-contents ":tcolumns 2")))))
  794. (ert-deftest test-org-clock/clocktable/step ()
  795. "Test \":step\" parameter in Clock table."
  796. ;; Regression test: week crossing month boundary before :wstart
  797. ;; day-of-week.
  798. (should
  799. (string-match-p
  800. "
  801. .*?\\[2017-09-25 .*
  802. .*
  803. .*
  804. |.*?| \\*1:00\\* |
  805. .*
  806. | Foo +| 1:00 +|"
  807. (org-test-with-temp-text
  808. "* Foo
  809. CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
  810. CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00
  811. CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] => 3:00"
  812. (let ((system-time-locale "en_US"))
  813. (test-org-clock-clocktable-contents
  814. ":step week :block 2017-09 :stepskip0 t")))))
  815. (should
  816. (string-match-p
  817. "
  818. .*?\\[2017-10-01 .*
  819. .*
  820. .*
  821. |.*?| \\*2:00\\* |
  822. .*
  823. | Foo +| 2:00 |
  824. .*?\\[2017-10-02 .*
  825. .*
  826. .*
  827. |.*?| \\*7:00\\* |
  828. .*
  829. | Foo +| 7:00 +|
  830. .*?\\[2017-10-09 .*
  831. .*
  832. .*
  833. |.*?| \\*5:00\\* |
  834. .*
  835. | Foo +| 5:00 +|
  836. "
  837. (org-test-with-temp-text
  838. "* Foo
  839. CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
  840. CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00
  841. CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] => 3:00
  842. CLOCK: [2017-10-08 Sun 09:00]--[2017-10-08 Sun 13:00] => 4:00
  843. CLOCK: [2017-10-09 Mon 09:00]--[2017-10-09 Mon 14:00] => 5:00"
  844. (let ((system-time-locale "en_US"))
  845. (test-org-clock-clocktable-contents
  846. ":step week :block 2017-10 :stepskip0 t")))))
  847. ;; :step day
  848. (should
  849. (string-match-p
  850. "
  851. .*?\\[2017-10-02 .*
  852. .*
  853. .*
  854. |.*?| \\*3:00\\* |
  855. .*
  856. | Foo +| 3:00 +|
  857. .*?\\[2017-10-03 .*
  858. .*
  859. .*
  860. |.*?| \\*0:00\\* |
  861. .*?\\[2017-10-04 .*
  862. .*
  863. .*
  864. |.*?| \\*0:00\\* |
  865. .*?\\[2017-10-05 .*
  866. .*
  867. .*
  868. |.*?| \\*0:00\\* |
  869. .*?\\[2017-10-06 .*
  870. .*
  871. .*
  872. |.*?| \\*0:00\\* |
  873. .*?\\[2017-10-07 .*
  874. .*
  875. .*
  876. |.*?| \\*0:00\\* |
  877. .*?\\[2017-10-08 .*
  878. .*
  879. .*
  880. |.*?| \\*4:00\\* |
  881. .*
  882. | Foo +| 4:00 +|"
  883. (org-test-with-temp-text
  884. "* Foo
  885. CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
  886. CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00
  887. CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] => 3:00
  888. CLOCK: [2017-10-08 Sun 09:00]--[2017-10-08 Sun 13:00] => 4:00
  889. CLOCK: [2017-10-09 Mon 09:00]--[2017-10-09 Mon 14:00] => 5:00"
  890. (let ((system-time-locale "en_US"))
  891. (test-org-clock-clocktable-contents
  892. ":step day :block 2017-W40")))))
  893. ;; Regression test: take :tstart and :tend hours into consideration.
  894. (should
  895. (string-match-p
  896. "
  897. .*?\\[2017-12-25 .*
  898. .*
  899. .*
  900. |.*?| \\*8:00\\* |
  901. .*
  902. | Foo +| 8:00 +|"
  903. (org-test-with-temp-text
  904. "* Foo
  905. CLOCK: [2017-12-27 Wed 08:00]--[2017-12-27 Wed 16:00] => 8:00"
  906. (let ((system-time-locale "en_US"))
  907. (test-org-clock-clocktable-contents
  908. (concat ":step week :tstart \"<2017-12-25 Mon>\" "
  909. ":tend \"<2017-12-27 Wed 23:59>\""))))))
  910. (should
  911. (string-match-p
  912. "
  913. .*?\\[2017-12-27 .*
  914. .*
  915. .*
  916. |.*?| \\*8:00\\* |
  917. .*
  918. | Foo +| 8:00 +|"
  919. (org-test-with-temp-text
  920. "* Foo
  921. CLOCK: [2017-12-27 Wed 08:00]--[2017-12-27 Wed 16:00] => 8:00"
  922. (let ((system-time-locale "en_US"))
  923. (test-org-clock-clocktable-contents
  924. (concat ":step day :tstart \"<2017-12-25 Mon>\" "
  925. ":tend \"<2017-12-27 Wed 23:59>\" :stepskip0 t"))))))
  926. ;; Test :step week", without or with ":wstart" parameter.
  927. (should
  928. (string-match-p
  929. "
  930. .*?\\[2012-03-26 .*
  931. .*
  932. .*
  933. |.*?| \\*8:00\\* |
  934. .*
  935. | Foo +| 8:00 +|
  936. .*?\\[2012-04-02 .*
  937. .*
  938. .*
  939. |.*?| \\*8:00\\* |
  940. .*
  941. | Foo +| 8:00 +|
  942. "
  943. (org-test-with-temp-text
  944. "* Foo
  945. CLOCK: [2012-03-29 Thu 08:00]--[2012-03-29 Thu 16:00] => 8:00
  946. CLOCK: [2012-04-03 Thu 08:00]--[2012-04-03 Thu 16:00] => 8:00"
  947. (let ((system-time-locale "en_US"))
  948. (test-org-clock-clocktable-contents
  949. ":step week :block 2012 :stepskip0 t")))))
  950. (should
  951. (string-match-p
  952. "
  953. .*?\\[2012-03-29 .*
  954. .*
  955. .*
  956. |.*?| \\*16:00\\* |
  957. .*
  958. | Foo +| 16:00 +|
  959. "
  960. (org-test-with-temp-text
  961. "* Foo
  962. CLOCK: [2012-03-29 Thu 08:00]--[2012-03-29 Thu 16:00] => 8:00
  963. CLOCK: [2012-04-03 Thu 08:00]--[2012-04-03 Thu 16:00] => 8:00"
  964. (let ((system-time-locale "en_US"))
  965. (test-org-clock-clocktable-contents
  966. ":step week :wstart 4 :block 2012 :stepskip0 t")))))
  967. ;; Test ":step month" without and with ":mstart".
  968. (should
  969. (string-match-p
  970. "
  971. .*?\\[2014-03-01 .*
  972. .*
  973. .*
  974. |.*?| \\*8:00\\* |
  975. .*
  976. | Foo +| 8:00 +|
  977. .*?\\[2014-04-01 .*
  978. .*
  979. .*
  980. |.*?| \\*8:00\\* |
  981. .*
  982. | Foo +| 8:00 +|
  983. "
  984. (org-test-with-temp-text
  985. "* Foo
  986. CLOCK: [2014-03-04 Tue 08:00]--[2014-03-04 Tue 16:00] => 8:00
  987. CLOCK: [2014-04-03 Thu 08:00]--[2014-04-03 Thu 16:00] => 8:00"
  988. (let ((system-time-locale "en_US"))
  989. (test-org-clock-clocktable-contents
  990. ":step month :block 2014 :stepskip0 t")))))
  991. (should
  992. (string-match-p
  993. "
  994. .*?\\[2014-03-04 .*
  995. .*
  996. .*
  997. |.*?| \\*16:00\\* |
  998. .*
  999. | Foo +| 16:00 +|
  1000. "
  1001. (org-test-with-temp-text
  1002. "* Foo
  1003. CLOCK: [2014-03-04 Tue 08:00]--[2014-03-04 Tue 16:00] => 8:00
  1004. CLOCK: [2014-04-03 Thu 08:00]--[2014-04-03 Thu 16:00] => 8:00"
  1005. (let ((system-time-locale "en_US"))
  1006. (test-org-clock-clocktable-contents
  1007. ":step month :mstart 4 :block 2014 :stepskip0 t")))))
  1008. ;; Test ":step quarter".
  1009. (should
  1010. (string-match-p
  1011. "
  1012. Quarterly report starting on:.*?\\[2014-01-01 .*
  1013. .*
  1014. .*
  1015. |.*?| \\*8:00\\* |
  1016. .*
  1017. | Foo +| 8:00 +|
  1018. .*?\\[2014-04-01 .*
  1019. .*
  1020. .*
  1021. |.*?| \\*16:00\\* |
  1022. .*
  1023. | Foo +| 16:00 +|
  1024. .*?\\[2014-07-01 .*
  1025. .*
  1026. .*
  1027. |.*?| \\*8:00\\* |
  1028. .*
  1029. | Foo +| 8:00 +|
  1030. "
  1031. (org-test-with-temp-text
  1032. "* Foo
  1033. CLOCK: [2014-03-04 Tue 08:00]--[2014-03-04 Tue 16:00] => 8:00
  1034. CLOCK: [2014-04-03 Thu 08:00]--[2014-04-03 Thu 16:00] => 8:00
  1035. CLOCK: [2014-06-04 Wed 08:00]--[2014-06-04 Wed 16:00] => 8:00
  1036. CLOCK: [2014-07-03 Thu 08:00]--[2014-07-03 Thu 16:00] => 8:00"
  1037. (let ((system-time-locale "en_US"))
  1038. (test-org-clock-clocktable-contents
  1039. ":step quarter :block 2014 :stepskip0 t")))))
  1040. ;; Test ":step semimonth".
  1041. (should
  1042. (string-match-p
  1043. "
  1044. .*?\\[2014-03-01 .*
  1045. .*
  1046. .*
  1047. |.*?| \\*8:00\\* |
  1048. .*
  1049. | Foo +| 8:00 +|
  1050. .*?\\[2014-03-16 .*
  1051. .*
  1052. .*
  1053. |.*?| \\*2:00\\* |
  1054. .*
  1055. | Foo +| 2:00 +|
  1056. .*?\\[2014-04-01 .*
  1057. .*
  1058. .*
  1059. |.*?| \\*7:00\\* |
  1060. .*
  1061. | Foo +| 7:00 +|
  1062. "
  1063. (org-test-with-temp-text
  1064. "* Foo
  1065. CLOCK: [2014-03-04 Tue 08:00]--[2014-03-04 Tue 16:00] => 8:00
  1066. CLOCK: [2014-03-24 Mon 08:00]--[2014-03-24 Mon 10:00] => 2:00
  1067. CLOCK: [2014-04-03 Thu 08:00]--[2014-04-03 Thu 15:00] => 7:00"
  1068. (let ((system-time-locale "en_US"))
  1069. (test-org-clock-clocktable-contents
  1070. ":step semimonth :block 2014 :stepskip0 t")))))
  1071. ;; Test ":step year".
  1072. (should
  1073. (string-match-p
  1074. "
  1075. .*?\\[2012-01-01 .*
  1076. .*
  1077. .*
  1078. |.*?| \\*8:00\\* |
  1079. .*
  1080. | Foo +| 8:00 +|
  1081. .*?\\[2014-01-01 .*
  1082. .*
  1083. .*
  1084. |.*?| \\*8:00\\* |
  1085. .*
  1086. | Foo +| 8:00 +|
  1087. "
  1088. (org-test-with-temp-text
  1089. "* Foo
  1090. CLOCK: [2012-03-29 Thu 08:00]--[2012-03-29 Thu 16:00] => 8:00
  1091. CLOCK: [2014-03-04 Tue 08:00]--[2014-03-04 Tue 16:00] => 8:00"
  1092. (let ((system-time-locale "en_US"))
  1093. (test-org-clock-clocktable-contents
  1094. ":step year :block untilnow :stepskip0 t")))))
  1095. ;; Regression test: Respect DST
  1096. (should
  1097. (string-match-p
  1098. "
  1099. .*?\\[2018-10-29 .*
  1100. .*
  1101. .*
  1102. |.*?| \\*8:00\\* |
  1103. .*
  1104. | Foo +| 8:00 +|
  1105. "
  1106. (org-test-with-temp-text
  1107. "* Foo
  1108. CLOCK: [2018-10-29 Mon 08:00]--[2018-10-29 Mon 16:00] => 8:00"
  1109. (let ((system-time-locale "en_US"))
  1110. (test-org-clock-clocktable-contents
  1111. (concat ":step day "
  1112. ":stepskip0 t "
  1113. ":tstart \"2018-10-01\" "
  1114. ":tend \"2018-11-01\"")))))))
  1115. (ert-deftest test-org-clock/clocktable/extend-today-until ()
  1116. "Test assignment of clock time to days in presence of \"org-extend-today-until\"."
  1117. ;; Basic test of :block with org-extend-today-until - the report for
  1118. ;; 2017-09-30 should include the time clocked on 2017-10-01 before
  1119. ;; 04:00.
  1120. (should
  1121. (equal "| Headline | Time |
  1122. |--------------+--------|
  1123. | *Total time* | *2:00* |
  1124. |--------------+--------|
  1125. | Foo | 2:00 |"
  1126. (org-test-with-temp-text
  1127. "* Foo
  1128. CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
  1129. CLOCK: [2017-10-01 Sun 02:00]--[2017-10-01 Sun 03:00] => 1:00
  1130. CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00"
  1131. (setq-local org-extend-today-until 4)
  1132. (let ((system-time-locale "en_US"))
  1133. (test-org-clock-clocktable-contents
  1134. ":block 2017-09-30")))))
  1135. ;; Week-length block - time on Monday before 04:00 should be
  1136. ;; assigned to previous week.
  1137. (should
  1138. (string-match-p "
  1139. .*? \\[2017-10-01 .*
  1140. .*
  1141. .*
  1142. |.*?| \\*2:00\\* |
  1143. .*
  1144. | Foo +| 2:00 |
  1145. .*? \\[2017-10-02 .*
  1146. .*
  1147. .*
  1148. |.*?| \\*2:00\\* |
  1149. .*
  1150. | Foo +| 2:00 |
  1151. "
  1152. (org-test-with-temp-text
  1153. "* Foo
  1154. CLOCK: [2017-10-01 Sun 12:00]--[2017-10-01 Sun 13:00] => 1:00
  1155. CLOCK: [2017-10-02 Mon 02:00]--[2017-10-02 Mon 03:00] => 1:00
  1156. CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 13:00] => 2:00"
  1157. (setq-local org-extend-today-until 4)
  1158. (let ((system-time-locale "en_US"))
  1159. (test-org-clock-clocktable-contents
  1160. ":step week :block 2017-10 :stepskip0 t"))))))
  1161. (ert-deftest test-org-clock/clocktable/hidefiles ()
  1162. "Test \":hidefiles\" parameter in Clock table."
  1163. ;; Test that hidefiles removes the file column.
  1164. (should
  1165. (equal
  1166. "| Headline | Time |
  1167. |--------------+--------|
  1168. | *Total time* | *1:00* |
  1169. |--------------+--------|
  1170. | Test | 1:00 |"
  1171. (org-test-with-temp-text-in-file
  1172. "* Test
  1173. CLOCK: [2012-03-29 Thu 16:00]--[2012-03-29 Thu 17:00] => 1:00"
  1174. (let ((the-file (buffer-file-name)))
  1175. (org-test-with-temp-text-in-file ""
  1176. (test-org-clock-clocktable-contents
  1177. (format ":hidefiles t :scope (lambda () (list %S))" the-file))))))))
  1178. (provide 'test-org-clock)
  1179. ;;; test-org-clock.el end here