test-org-clock.el 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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. (apply #'encode-time
  19. (mapcar (lambda (el) (or el 0))
  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 semimonth".
  1009. (should
  1010. (string-match-p
  1011. "
  1012. .*?\\[2014-03-01 .*
  1013. .*
  1014. .*
  1015. |.*?| \\*8:00\\* |
  1016. .*
  1017. | Foo +| 8:00 +|
  1018. .*?\\[2014-03-16 .*
  1019. .*
  1020. .*
  1021. |.*?| \\*2:00\\* |
  1022. .*
  1023. | Foo +| 2:00 +|
  1024. .*?\\[2014-04-01 .*
  1025. .*
  1026. .*
  1027. |.*?| \\*7:00\\* |
  1028. .*
  1029. | Foo +| 7: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-03-24 Mon 08:00]--[2014-03-24 Mon 10:00] => 2:00
  1035. CLOCK: [2014-04-03 Thu 08:00]--[2014-04-03 Thu 15:00] => 7:00"
  1036. (let ((system-time-locale "en_US"))
  1037. (test-org-clock-clocktable-contents
  1038. ":step semimonth :block 2014 :stepskip0 t")))))
  1039. ;; Test ":step year".
  1040. (should
  1041. (string-match-p
  1042. "
  1043. .*?\\[2012-01-01 .*
  1044. .*
  1045. .*
  1046. |.*?| \\*8:00\\* |
  1047. .*
  1048. | Foo +| 8:00 +|
  1049. .*?\\[2014-01-01 .*
  1050. .*
  1051. .*
  1052. |.*?| \\*8:00\\* |
  1053. .*
  1054. | Foo +| 8:00 +|
  1055. "
  1056. (org-test-with-temp-text
  1057. "* Foo
  1058. CLOCK: [2012-03-29 Thu 08:00]--[2012-03-29 Thu 16:00] => 8:00
  1059. CLOCK: [2014-03-04 Tue 08:00]--[2014-03-04 Tue 16:00] => 8:00"
  1060. (let ((system-time-locale "en_US"))
  1061. (test-org-clock-clocktable-contents
  1062. ":step year :block untilnow :stepskip0 t")))))
  1063. ;; Regression test: Respect DST
  1064. (should
  1065. (string-match-p
  1066. "
  1067. .*?\\[2018-10-29 .*
  1068. .*
  1069. .*
  1070. |.*?| \\*8:00\\* |
  1071. .*
  1072. | Foo +| 8:00 +|
  1073. "
  1074. (org-test-with-temp-text
  1075. "* Foo
  1076. CLOCK: [2018-10-29 Mon 08:00]--[2018-10-29 Mon 16:00] => 8:00"
  1077. (let ((system-time-locale "en_US"))
  1078. (test-org-clock-clocktable-contents
  1079. (concat ":step day "
  1080. ":stepskip0 t "
  1081. ":tstart \"2018-10-01\" "
  1082. ":tend \"2018-11-01\"")))))))
  1083. (ert-deftest test-org-clock/clocktable/extend-today-until ()
  1084. "Test assignment of clock time to days in presence of \"org-extend-today-until\"."
  1085. ;; Basic test of :block with org-extend-today-until - the report for
  1086. ;; 2017-09-30 should include the time clocked on 2017-10-01 before
  1087. ;; 04:00.
  1088. (should
  1089. (equal "| Headline | Time |
  1090. |--------------+--------|
  1091. | *Total time* | *2:00* |
  1092. |--------------+--------|
  1093. | Foo | 2:00 |"
  1094. (org-test-with-temp-text
  1095. "* Foo
  1096. CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
  1097. CLOCK: [2017-10-01 Sun 02:00]--[2017-10-01 Sun 03:00] => 1:00
  1098. CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00"
  1099. (setq-local org-extend-today-until 4)
  1100. (let ((system-time-locale "en_US"))
  1101. (test-org-clock-clocktable-contents
  1102. ":block 2017-09-30")))))
  1103. ;; Week-length block - time on Monday before 04:00 should be
  1104. ;; assigned to previous week.
  1105. (should
  1106. (string-match-p "
  1107. .*? \\[2017-10-01 .*
  1108. .*
  1109. .*
  1110. |.*?| \\*2:00\\* |
  1111. .*
  1112. | Foo +| 2:00 |
  1113. .*? \\[2017-10-02 .*
  1114. .*
  1115. .*
  1116. |.*?| \\*2:00\\* |
  1117. .*
  1118. | Foo +| 2:00 |
  1119. "
  1120. (org-test-with-temp-text
  1121. "* Foo
  1122. CLOCK: [2017-10-01 Sun 12:00]--[2017-10-01 Sun 13:00] => 1:00
  1123. CLOCK: [2017-10-02 Mon 02:00]--[2017-10-02 Mon 03:00] => 1:00
  1124. CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 13:00] => 2:00"
  1125. (setq-local org-extend-today-until 4)
  1126. (let ((system-time-locale "en_US"))
  1127. (test-org-clock-clocktable-contents
  1128. ":step week :block 2017-10 :stepskip0 t"))))))
  1129. (ert-deftest test-org-clock/clocktable/hidefiles ()
  1130. "Test \":hidefiles\" parameter in Clock table."
  1131. ;; Test that hidefiles removes the file column.
  1132. (should
  1133. (equal
  1134. "| Headline | Time |
  1135. |--------------+--------|
  1136. | *Total time* | *1:00* |
  1137. |--------------+--------|
  1138. | Test | 1:00 |"
  1139. (org-test-with-temp-text-in-file
  1140. "* Test
  1141. CLOCK: [2012-03-29 Thu 16:00]--[2012-03-29 Thu 17:00] => 1:00"
  1142. (let ((the-file (buffer-file-name)))
  1143. (org-test-with-temp-text-in-file ""
  1144. (test-org-clock-clocktable-contents
  1145. (format ":hidefiles t :scope (lambda () (list %S))" the-file))))))))
  1146. (provide 'test-org-clock)
  1147. ;;; test-org-clock.el end here