test-org-clock.el 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. ;;; test-org-clock.el --- Tests for org-clock.el
  2. ;; Copyright (C) 2012, 2014, 2015 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Released under the GNU General Public License version 3
  5. ;; see: http://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/ranges ()
  253. "Test ranges in Clock table."
  254. ;; Relative time: Previous two days.
  255. (should
  256. (equal
  257. "| Headline | Time | |
  258. |------------------------------+--------+------|
  259. | *Total time* | *8:00* | |
  260. |------------------------------+--------+------|
  261. | Relative times in clocktable | 8:00 | |
  262. | Foo | | 8:00 |"
  263. (org-test-with-temp-text
  264. "* Relative times in clocktable\n** Foo\n<point>"
  265. (insert (org-test-clock-create-clock "-3d 8:00" "-3d 12:00"))
  266. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  267. (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
  268. (test-org-clock-clocktable-contents
  269. ":tstart \"<-2d>\" :tend \"<today>\" :indent nil"))))
  270. ;; Relative time: Yesterday until now.
  271. (should
  272. (equal
  273. "| Headline | Time | |
  274. |------------------------------+--------+------|
  275. | *Total time* | *6:00* | |
  276. |------------------------------+--------+------|
  277. | Relative times in clocktable | 6:00 | |
  278. | Foo | | 6:00 |"
  279. (org-test-with-temp-text
  280. "* Relative times in clocktable\n** Foo\n<point>"
  281. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  282. (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
  283. (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
  284. (test-org-clock-clocktable-contents
  285. ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil"))))
  286. ;; Test `untilnow' block.
  287. (should
  288. (equal
  289. "| Headline | Time | |
  290. |------------------------------+--------+------|
  291. | *Total time* | *6:00* | |
  292. |------------------------------+--------+------|
  293. | Relative times in clocktable | 6:00 | |
  294. | Foo | | 6:00 |"
  295. (org-test-with-temp-text
  296. "* Relative times in clocktable\n** Foo\n<point>"
  297. (insert (org-test-clock-create-clock "-10y 15:00" "-10y 18:00"))
  298. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  299. (test-org-clock-clocktable-contents ":block untilnow :indent nil")))))
  300. (ert-deftest test-org-clock/clocktable/tags ()
  301. "Test \":tags\" parameter in Clock table."
  302. ;; Test tag filtering.
  303. (should
  304. (equal
  305. "| Headline | Time | |
  306. |--------------+--------+------|
  307. | *Total time* | *2:00* | |
  308. |--------------+--------+------|
  309. | H1 | | 2:00 |"
  310. (org-test-with-temp-text "** H1\n\n*** H2 :tag:\n\n*** H3\n<point>"
  311. (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
  312. (goto-line 4)
  313. (insert (org-test-clock-create-clock ". 2:00" ". 4:00"))
  314. (test-org-clock-clocktable-contents ":tags \"tag\" :indent nil")))))
  315. (ert-deftest test-org-clock/clocktable/scope ()
  316. "Test \":scope\" parameter in Clock table."
  317. ;; Test `file-with-archives' scope. In particular, preserve "TBLFM"
  318. ;; line, and ignore "file" column.
  319. (should
  320. (equal
  321. "| Headline | Time | |
  322. |--------------+--------+-----|
  323. | *Total time* | *8:40* | foo |
  324. |--------------+--------+-----|
  325. | Test | 8:40 | foo |
  326. #+TBLFM: $3=string(\"foo\")"
  327. (org-test-with-temp-text-in-file
  328. "* Test
  329. CLOCK: [2012-03-29 Thu 8:00]--[2012-03-29 Thu 16:40] => 8:40"
  330. (test-org-clock-clocktable-contents ":scope file-with-archives"
  331. "#+TBLFM: $3=string(\"foo\")"))))
  332. ;; Test "function" scope.
  333. (should
  334. (string-match-p
  335. (regexp-quote "| ALL *Total time* | *1:00* |")
  336. (org-test-with-temp-text-in-file
  337. "* Test
  338. CLOCK: [2012-03-29 Thu 16:00]--[2012-03-29 Thu 17:00] => 1:00"
  339. (let ((the-file (buffer-file-name)))
  340. (org-test-with-temp-text-in-file ""
  341. (test-org-clock-clocktable-contents
  342. (format ":scope (lambda () (list %S))" the-file))))))))
  343. (ert-deftest test-org-clock/clocktable/maxlevel ()
  344. "Test \":maxlevel\" parameter in Clock table."
  345. (should
  346. (equal "| Headline | Time | |
  347. |--------------+--------+------|
  348. | *Total time* | *6:00* | |
  349. |--------------+--------+------|
  350. | Foo | 6:00 | |
  351. | \\_ Bar | | 2:00 |"
  352. (org-test-with-temp-text
  353. "* Foo
  354. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  355. ** Bar
  356. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  357. (test-org-clock-clocktable-contents ":maxlevel 3"))))
  358. (should
  359. (equal "| Headline | Time | |
  360. |--------------+--------+------|
  361. | *Total time* | *6:00* | |
  362. |--------------+--------+------|
  363. | Foo | 6:00 | |
  364. | \\_ Bar | | 2:00 |"
  365. (org-test-with-temp-text
  366. "* Foo
  367. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  368. ** Bar
  369. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  370. (test-org-clock-clocktable-contents ":maxlevel 2"))))
  371. (should
  372. (equal "| Headline | Time |
  373. |--------------+--------|
  374. | *Total time* | *6:00* |
  375. |--------------+--------|
  376. | Foo | 6:00 |"
  377. (org-test-with-temp-text
  378. "* Foo
  379. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  380. ** Bar
  381. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  382. (test-org-clock-clocktable-contents ":maxlevel 1"))))
  383. ;; Special ":maxlevel 0" case: only report total file time.
  384. (should
  385. (equal "| Headline | Time |
  386. |--------------+--------|
  387. | *Total time* | *6:00* |
  388. |--------------+--------|"
  389. (org-test-with-temp-text
  390. "* Foo
  391. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  392. ** Bar
  393. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  394. (test-org-clock-clocktable-contents ":maxlevel 0")))))
  395. (ert-deftest test-org-clock/clocktable/formula ()
  396. "Test \":formula\" parameter in Clock table."
  397. ;; Test ":formula %". Handle various duration formats.
  398. (should
  399. (equal
  400. "| Headline | Time | % |
  401. |--------------+--------+-------|
  402. | *Total time* | *6:00* | 100.0 |
  403. |--------------+--------+-------|
  404. | Foo | 4:00 | 66.7 |
  405. | Bar | 2:00 | 33.3 |"
  406. (org-test-with-temp-text
  407. "* Foo
  408. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  409. * Bar
  410. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  411. (test-org-clock-clocktable-contents ":maxlevel 1 :formula %"))))
  412. (should
  413. (equal
  414. "| Headline | Time | % |
  415. |--------------+---------+-------|
  416. | *Total time* | *28:00* | 100.0 |
  417. |--------------+---------+-------|
  418. | Foo | 26:00 | 92.9 |
  419. | Bar | 2:00 | 7.1 |"
  420. (org-test-with-temp-text
  421. "* Foo
  422. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  423. * Bar
  424. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  425. (test-org-clock-clocktable-contents ":maxlevel 1 :formula %"))))
  426. ;; Properly align column with different depths.
  427. (should
  428. (equal "| Headline | Time | | | % |
  429. |---------------+--------+------+------+-------|
  430. | *Total time* | *1:00* | | | 100.0 |
  431. |---------------+--------+------+------+-------|
  432. | foo | 1:00 | | | 100.0 |
  433. | \\_ sub | | 0:15 | | 25.0 |
  434. | \\_ sub2 | | 0:15 | | 25.0 |
  435. | \\_ sub3 | | 0:30 | | 50.0 |
  436. | \\_ subsub1 | | | 0:15 | 25.0 |
  437. | \\_ subsub1 | | | 0:15 | 25.0 |"
  438. (org-test-with-temp-text
  439. "* foo
  440. ** sub
  441. :LOGBOOK:
  442. CLOCK: [2017-03-18 Sat 15:00]--[2017-03-18 Sat 15:15] => 0:15
  443. :END:
  444. ** sub2
  445. :LOGBOOK:
  446. CLOCK: [2017-03-18 Sat 15:15]--[2017-03-18 Sat 15:30] => 0:15
  447. :END:
  448. ** sub3
  449. *** subsub1
  450. :LOGBOOK:
  451. CLOCK: [2017-03-18 Sat 13:00]--[2017-03-18 Sat 13:15] => 0:15
  452. :END:
  453. *** subsub1
  454. :LOGBOOK:
  455. CLOCK: [2017-03-18 Sat 14:00]--[2017-03-18 Sat 14:15] => 0:15
  456. :END:"
  457. (test-org-clock-clocktable-contents ":maxlevel 3 :formula %")))))
  458. (ert-deftest test-org-clock/clocktable/lang ()
  459. "Test \":lang\" parameter in Clock table."
  460. ;; Test foreign translation
  461. (should
  462. (equal
  463. "| Headline | Time |
  464. |--------------+---------|
  465. | *Total time* | *26:00* |
  466. |--------------+---------|
  467. | Foo | 26:00 |"
  468. (org-test-with-temp-text
  469. "* Foo
  470. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  471. (test-org-clock-clocktable-contents ":maxlevel 1 :lang en"))))
  472. (should
  473. (equal
  474. "| En-tête | Durée |
  475. |----------------+---------|
  476. | *Durée totale* | *26:00* |
  477. |----------------+---------|
  478. | Foo | 26:00 |"
  479. (org-test-with-temp-text
  480. "* Foo
  481. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  482. (test-org-clock-clocktable-contents ":maxlevel 1 :lang fr"))))
  483. ;; No :lang parameter is equivalent to "en".
  484. (should
  485. (equal
  486. (org-test-with-temp-text
  487. "* Foo
  488. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  489. (test-org-clock-clocktable-contents ":maxlevel 1 :lang en"))
  490. (org-test-with-temp-text
  491. "* Foo
  492. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  493. (test-org-clock-clocktable-contents ":maxlevel 1"))))
  494. ;; Unknown translation fall backs to "en".
  495. (should
  496. (equal
  497. "| Headline | Time |
  498. |--------------+---------|
  499. | *Total time* | *26:00* |
  500. |--------------+---------|
  501. | Foo | 26:00 |"
  502. (org-test-with-temp-text
  503. "* Foo
  504. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  505. (test-org-clock-clocktable-contents ":maxlevel 1 :lang foo")))))
  506. (ert-deftest test-org-clock/clocktable/link ()
  507. "Test \":link\" parameter in Clock table."
  508. ;; If there is no file attached to the document, link directly to
  509. ;; the headline.
  510. (should
  511. (equal
  512. "| Headline | Time |
  513. |--------------+---------|
  514. | *Total time* | *26:00* |
  515. |--------------+---------|
  516. | [[Foo][Foo]] | 26:00 |"
  517. (org-test-with-temp-text
  518. "* Foo
  519. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  520. (test-org-clock-clocktable-contents ":link t"))))
  521. ;; Otherwise, link to the headline in the current file.
  522. (should
  523. (equal
  524. "| Headline | Time |
  525. |--------------+---------|
  526. | *Total time* | *26:00* |
  527. |--------------+---------|
  528. | [[file:filename::Foo][Foo]] | 26:00 |"
  529. (org-test-with-temp-text-in-file
  530. "* Foo
  531. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  532. (let ((file (buffer-file-name)))
  533. (replace-regexp-in-string
  534. (regexp-quote file) "filename"
  535. (test-org-clock-clocktable-contents ":link t"))))))
  536. ;; Ignore TODO keyword, priority cookie, COMMENT and tags in
  537. ;; headline.
  538. (should
  539. (equal
  540. "| Headline | Time |
  541. |--------------+---------|
  542. | *Total time* | *26:00* |
  543. |--------------+---------|
  544. | [[Foo][Foo]] | 26:00 |"
  545. (org-test-with-temp-text
  546. "* TODO Foo
  547. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  548. (test-org-clock-clocktable-contents ":link t"))))
  549. (should
  550. (equal
  551. "| Headline | Time |
  552. |--------------+---------|
  553. | *Total time* | *26:00* |
  554. |--------------+---------|
  555. | [[Foo][Foo]] | 26:00 |"
  556. (org-test-with-temp-text
  557. "* [#A] Foo
  558. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  559. (test-org-clock-clocktable-contents ":link t"))))
  560. (should
  561. (equal
  562. "| Headline | Time |
  563. |--------------+---------|
  564. | *Total time* | *26:00* |
  565. |--------------+---------|
  566. | [[Foo][Foo]] | 26:00 |"
  567. (org-test-with-temp-text
  568. "* COMMENT Foo
  569. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  570. (test-org-clock-clocktable-contents ":link t"))))
  571. (should
  572. (equal
  573. "| Headline | Time |
  574. |--------------+---------|
  575. | *Total time* | *26:00* |
  576. |--------------+---------|
  577. | [[Foo][Foo]] | 26:00 |"
  578. (org-test-with-temp-text
  579. "* Foo :tag:
  580. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  581. (test-org-clock-clocktable-contents ":link t"))))
  582. ;; Remove statistics cookie from headline description.
  583. (should
  584. (equal
  585. "| Headline | Time |
  586. |--------------+---------|
  587. | *Total time* | *26:00* |
  588. |--------------+---------|
  589. | [[Foo][Foo]] | 26:00 |"
  590. (org-test-with-temp-text
  591. "* Foo [50%]
  592. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  593. (test-org-clock-clocktable-contents ":link t"))))
  594. (should
  595. (equal
  596. "| Headline | Time |
  597. |--------------+---------|
  598. | *Total time* | *26:00* |
  599. |--------------+---------|
  600. | [[Foo][Foo]] | 26:00 |"
  601. (org-test-with-temp-text
  602. "* Foo [1/2]
  603. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  604. (test-org-clock-clocktable-contents ":link t"))))
  605. ;; Replace links with their description, or turn them into plain
  606. ;; links if there is no description.
  607. (should
  608. (equal
  609. "| Headline | Time |
  610. |--------------+---------|
  611. | *Total time* | *26:00* |
  612. |--------------+---------|
  613. | [[Foo %5B%5Bhttp://orgmode.org%5D%5BOrg mode%5D%5D][Foo Org mode]] | 26:00 |"
  614. (org-test-with-temp-text
  615. "* Foo [[http://orgmode.org][Org mode]]
  616. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  617. (test-org-clock-clocktable-contents ":link t"))))
  618. (should
  619. (equal
  620. "| Headline | Time |
  621. |------------------------+---------|
  622. | *Total time* | *26:00* |
  623. |------------------------+---------|
  624. | [[Foo %5B%5Bhttp://orgmode.org%5D%5D][Foo http://orgmode.org]] | 26:00 |"
  625. (org-test-with-temp-text
  626. "* Foo [[http://orgmode.org]]
  627. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  628. (test-org-clock-clocktable-contents ":link t")))))
  629. (ert-deftest test-org-clock/clocktable/compact ()
  630. "Test \":compact\" parameter in Clock table."
  631. ;; With :compact, all headlines are in the same column.
  632. (should
  633. (equal
  634. "| Headline | Time |
  635. |--------------+---------|
  636. | *Total time* | *26:00* |
  637. |--------------+---------|
  638. | Foo | 26:00 |"
  639. (org-test-with-temp-text
  640. "* Foo
  641. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  642. (test-org-clock-clocktable-contents ":compact t"))))
  643. (should
  644. (equal
  645. "| Headline | Time |
  646. |--------------+---------|
  647. | *Total time* | *52:00* |
  648. |--------------+---------|
  649. | Foo | 52:00 |
  650. | \\_ Bar | 26:00 |"
  651. (org-test-with-temp-text
  652. "* Foo
  653. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  654. ** Bar
  655. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  656. (test-org-clock-clocktable-contents ":compact t"))))
  657. ;; :maxlevel does not affect :compact parameter.
  658. (should
  659. (equal
  660. "| Headline | Time |
  661. |--------------+---------|
  662. | *Total time* | *52:00* |
  663. |--------------+---------|
  664. | Foo | 52:00 |
  665. | \\_ Bar | 26:00 |"
  666. (org-test-with-temp-text
  667. "* Foo
  668. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  669. ** Bar
  670. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  671. (test-org-clock-clocktable-contents ":compact t :maxlevel 2"))))
  672. ;; :compact implies a non-nil :indent parameter.
  673. (should
  674. (equal
  675. "| Headline | Time |
  676. |--------------+---------|
  677. | *Total time* | *52:00* |
  678. |--------------+---------|
  679. | Foo | 52:00 |
  680. | \\_ Bar | 26:00 |"
  681. (org-test-with-temp-text
  682. "* Foo
  683. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  684. ** Bar
  685. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  686. (test-org-clock-clocktable-contents ":compact t :indent nil"))))
  687. ;; :compact implies a nil :level parameter.
  688. (should
  689. (equal
  690. "| Headline | Time |
  691. |--------------+---------|
  692. | *Total time* | *52:00* |
  693. |--------------+---------|
  694. | Foo | 52:00 |
  695. | \\_ Bar | 26:00 |"
  696. (org-test-with-temp-text
  697. "* Foo
  698. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  699. ** Bar
  700. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  701. (test-org-clock-clocktable-contents ":compact t :level t")))))
  702. (ert-deftest test-org-clock/clocktable/properties ()
  703. "Test \":properties\" parameter in Clock table."
  704. ;; Include a new column with list properties.
  705. (should
  706. (equal
  707. "| A | Headline | Time |
  708. |---+--------------+---------|
  709. | | *Total time* | *26:00* |
  710. |---+--------------+---------|
  711. | 1 | Foo | 26:00 |"
  712. (org-test-with-temp-text
  713. "* Foo
  714. :PROPERTIES:
  715. :A: 1
  716. :END:
  717. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  718. (test-org-clock-clocktable-contents ":properties (\"A\")"))))
  719. (should
  720. (equal
  721. "| A | Headline | Time | |
  722. |---+--------------+---------+-------|
  723. | | *Total time* | *52:00* | |
  724. |---+--------------+---------+-------|
  725. | | Foo | 52:00 | |
  726. | 1 | \\_ Bar | | 26:00 |"
  727. (org-test-with-temp-text
  728. "* Foo
  729. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  730. ** Bar
  731. :PROPERTIES:
  732. :A: 1
  733. :END:
  734. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  735. (test-org-clock-clocktable-contents ":properties (\"A\")"))))
  736. ;; Handle missing properties.
  737. (should
  738. (equal
  739. "| A | Headline | Time |
  740. |---+--------------+---------|
  741. | | *Total time* | *26:00* |
  742. |---+--------------+---------|
  743. | 1 | Foo | 26:00 |"
  744. (org-test-with-temp-text
  745. "* Foo
  746. :PROPERTIES:
  747. :A: 1
  748. :END:
  749. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  750. (test-org-clock-clocktable-contents ":properties (\"A\")")))))
  751. (ert-deftest test-org-clock/clocktable/tcolumns ()
  752. "Test \":tcolumns\" parameter in Clock table."
  753. ;; When :tcolumns is smaller than the deepest headline level, lump
  754. ;; lower levels in the last column.
  755. (should
  756. (equal
  757. "| Headline | Time |
  758. |--------------+---------|
  759. | *Total time* | *52:00* |
  760. |--------------+---------|
  761. | Foo | 52:00 |
  762. | \\_ Bar | 26:00 |"
  763. (org-test-with-temp-text
  764. "* Foo
  765. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  766. ** Bar
  767. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  768. (test-org-clock-clocktable-contents ":tcolumns 1"))))
  769. ;; :tcolumns cannot create more columns than the deepest headline
  770. ;; level.
  771. (should
  772. (equal
  773. "| Headline | Time | |
  774. |--------------+---------+-------|
  775. | *Total time* | *52:00* | |
  776. |--------------+---------+-------|
  777. | Foo | 52:00 | |
  778. | \\_ Bar | | 26:00 |"
  779. (org-test-with-temp-text
  780. "* Foo
  781. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  782. ** Bar
  783. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  784. (test-org-clock-clocktable-contents ":tcolumns 3"))))
  785. ;; Pathological case: when no headline contributes to the total
  786. ;; time, there is only one time column.
  787. (should
  788. (equal "| Headline | Time |
  789. |--------------+--------|
  790. | *Total time* | *0:00* |"
  791. (org-test-with-temp-text
  792. "* Foo
  793. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 11:09] => 0:00
  794. ** Bar
  795. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 13:09] => 0:00"
  796. (test-org-clock-clocktable-contents ":tcolumns 2")))))
  797. (ert-deftest test-org-clock/clocktable/step ()
  798. "Test \":step\" parameter in Clock table."
  799. ;; Regression test: week crossing month boundary before :wstart
  800. ;; day-of-week.
  801. (should
  802. (equal "
  803. Weekly report starting on: [2017-09-25 Mon]
  804. | Headline | Time |
  805. |--------------+--------|
  806. | *Total time* | *1:00* |
  807. |--------------+--------|
  808. | Foo | 1:00 |"
  809. (org-test-with-temp-text
  810. "* Foo
  811. CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
  812. CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00
  813. CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] => 3:00"
  814. (let ((system-time-locale "en_US"))
  815. (test-org-clock-clocktable-contents
  816. ":step week :block 2017-09 :stepskip0 t")))))
  817. (should
  818. (equal "
  819. Weekly report starting on: [2017-10-01 Sun]
  820. | Headline | Time |
  821. |--------------+--------|
  822. | *Total time* | *2:00* |
  823. |--------------+--------|
  824. | Foo | 2:00 |
  825. Weekly report starting on: [2017-10-02 Mon]
  826. | Headline | Time |
  827. |--------------+--------|
  828. | *Total time* | *7:00* |
  829. |--------------+--------|
  830. | Foo | 7:00 |
  831. Weekly report starting on: [2017-10-09 Mon]
  832. | Headline | Time |
  833. |--------------+--------|
  834. | *Total time* | *5:00* |
  835. |--------------+--------|
  836. | Foo | 5:00 |
  837. "
  838. (org-test-with-temp-text
  839. "* Foo
  840. CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
  841. CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00
  842. CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] => 3:00
  843. CLOCK: [2017-10-08 Sun 09:00]--[2017-10-08 Sun 13:00] => 4:00
  844. CLOCK: [2017-10-09 Mon 09:00]--[2017-10-09 Mon 14:00] => 5:00"
  845. (let ((system-time-locale "en_US"))
  846. (test-org-clock-clocktable-contents
  847. ":step week :block 2017-10 :stepskip0 t")))))
  848. ;; :step day
  849. (should
  850. (equal "
  851. Daily report: [2017-10-02 Mon]
  852. | Headline | Time |
  853. |--------------+--------|
  854. | *Total time* | *3:00* |
  855. |--------------+--------|
  856. | Foo | 3:00 |
  857. Daily report: [2017-10-03 Tue]
  858. | Headline | Time |
  859. |--------------+--------|
  860. | *Total time* | *0:00* |
  861. Daily report: [2017-10-04 Wed]
  862. | Headline | Time |
  863. |--------------+--------|
  864. | *Total time* | *0:00* |
  865. Daily report: [2017-10-05 Thu]
  866. | Headline | Time |
  867. |--------------+--------|
  868. | *Total time* | *0:00* |
  869. Daily report: [2017-10-06 Fri]
  870. | Headline | Time |
  871. |--------------+--------|
  872. | *Total time* | *0:00* |
  873. Daily report: [2017-10-07 Sat]
  874. | Headline | Time |
  875. |--------------+--------|
  876. | *Total time* | *0:00* |
  877. Daily report: [2017-10-08 Sun]
  878. | Headline | Time |
  879. |--------------+--------|
  880. | *Total time* | *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. (equal "
  896. Weekly report starting on: [2017-12-25 Mon]
  897. | Headline | Time |
  898. |--------------+--------|
  899. | *Total time* | *8:00* |
  900. |--------------+--------|
  901. | Foo | 8:00 |"
  902. (org-test-with-temp-text
  903. "* Foo
  904. CLOCK: [2017-12-27 Wed 08:00]--[2017-12-27 Wed 16:00] => 8:00"
  905. (let ((system-time-locale "en_US"))
  906. (test-org-clock-clocktable-contents
  907. (concat ":step week :tstart \"<2017-12-25 Mon>\" "
  908. ":tend \"<2017-12-27 Wed 23:59>\""))))))
  909. (should
  910. (equal "
  911. Daily report: [2017-12-27 Wed]
  912. | Headline | Time |
  913. |--------------+--------|
  914. | *Total time* | *8:00* |
  915. |--------------+--------|
  916. | Foo | 8:00 |"
  917. (org-test-with-temp-text
  918. "* Foo
  919. CLOCK: [2017-12-27 Wed 08:00]--[2017-12-27 Wed 16:00] => 8:00"
  920. (let ((system-time-locale "en_US"))
  921. (test-org-clock-clocktable-contents
  922. (concat ":step day :tstart \"<2017-12-25 Mon>\" "
  923. ":tend \"<2017-12-27 Wed 23:59>\" :stepskip0 t")))))))
  924. (provide 'test-org-clock)
  925. ;;; test-org-clock.el end here