test-org-clock.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 (floor (- (org-time-string-to-seconds end)
  38. (org-time-string-to-seconds beg))))))
  39. (concat org-clock-string " " beg
  40. (when end
  41. (concat "--" end " => "
  42. (format "%2d:%02d"
  43. (/ sec-diff 3600)
  44. (/ (mod sec-diff 3600) 60))))
  45. "\n")))
  46. (defun test-org-clock-clocktable-contents-at-point (options)
  47. "Return contents of a clocktable at point.
  48. OPTIONS is a string of clocktable options. Caption is ignored in
  49. contents. The clocktable doesn't appear in the buffer."
  50. (save-excursion
  51. (insert "#+BEGIN: clocktable " options "\n")
  52. (insert "#+END:\n"))
  53. (unwind-protect
  54. (save-excursion
  55. (let ((org-time-clocksum-format
  56. '(:hours "%d" :require-hours t :minutes ":%02d"
  57. :require-minutes t)))
  58. (org-update-dblock))
  59. (forward-line)
  60. ;; Skip caption.
  61. (when (looking-at "#\\+CAPTION:") (forward-line))
  62. (buffer-substring (point)
  63. (progn (search-forward "#+END:")
  64. (match-beginning 0))))
  65. ;; Remove clocktable.
  66. (delete-region (point) (search-forward "#+END:\n"))))
  67. ;;; Clock drawer
  68. (ert-deftest test-org-clock/into-drawer ()
  69. "Test `org-clock-into-drawer' specifications."
  70. ;; When `org-clock-into-drawer' is nil, do not use a clock drawer.
  71. (should-not
  72. (org-test-with-temp-text "* H"
  73. (let ((org-clock-into-drawer nil)
  74. (org-log-into-drawer nil))
  75. (org-clock-into-drawer))))
  76. (should-not
  77. (org-test-with-temp-text "* H"
  78. (let ((org-clock-into-drawer nil)
  79. (org-log-into-drawer t))
  80. (org-clock-into-drawer))))
  81. (should-not
  82. (org-test-with-temp-text "* H"
  83. (let ((org-clock-into-drawer nil)
  84. (org-log-into-drawer "BAR"))
  85. (org-clock-into-drawer))))
  86. ;; When `org-clock-into-drawer' is a string, use it
  87. ;; unconditionally.
  88. (should
  89. (equal "FOO"
  90. (org-test-with-temp-text "* H"
  91. (let ((org-clock-into-drawer "FOO")
  92. (org-log-into-drawer nil))
  93. (org-clock-into-drawer)))))
  94. (should
  95. (equal "FOO"
  96. (org-test-with-temp-text "* H"
  97. (let ((org-clock-into-drawer "FOO")
  98. (org-log-into-drawer t))
  99. (org-clock-into-drawer)))))
  100. (should
  101. (equal "FOO"
  102. (org-test-with-temp-text "* H"
  103. (let ((org-clock-into-drawer "FOO")
  104. (org-log-into-drawer "BAR"))
  105. (org-clock-into-drawer)))))
  106. ;; When `org-clock-into-drawer' is an integer, return it.
  107. (should
  108. (= 1
  109. (org-test-with-temp-text "* H"
  110. (let ((org-clock-into-drawer 1)
  111. (org-log-into-drawer nil))
  112. (org-clock-into-drawer)))))
  113. (should
  114. (= 1
  115. (org-test-with-temp-text "* H"
  116. (let ((org-clock-into-drawer 1)
  117. (org-log-into-drawer t))
  118. (org-clock-into-drawer)))))
  119. (should
  120. (= 1
  121. (org-test-with-temp-text "* H"
  122. (let ((org-clock-into-drawer 1)
  123. (org-log-into-drawer "BAR"))
  124. (org-clock-into-drawer)))))
  125. ;; Otherwise, any non-nil value defaults to `org-log-into-drawer' or
  126. ;; "LOGBOOK" if it is nil.
  127. (should
  128. (equal "LOGBOOK"
  129. (org-test-with-temp-text "* H"
  130. (let ((org-clock-into-drawer t)
  131. (org-log-into-drawer nil))
  132. (org-clock-into-drawer)))))
  133. (should
  134. (equal "LOGBOOK"
  135. (org-test-with-temp-text "* H"
  136. (let ((org-clock-into-drawer t)
  137. (org-log-into-drawer t))
  138. (org-clock-into-drawer)))))
  139. (should
  140. (equal "FOO"
  141. (org-test-with-temp-text "* H"
  142. (let ((org-clock-into-drawer t)
  143. (org-log-into-drawer "FOO"))
  144. (org-clock-into-drawer)))))
  145. ;; A non-nil "CLOCK_INTO_DRAWER" property overrides
  146. ;; `org-clock-into-drawer' value.
  147. (should
  148. (equal "LOGBOOK"
  149. (org-test-with-temp-text
  150. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: t\n:END:"
  151. (let ((org-clock-into-drawer nil)
  152. (org-log-into-drawer nil))
  153. (org-clock-into-drawer)))))
  154. (should
  155. (equal "FOO"
  156. (org-test-with-temp-text
  157. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: FOO\n:END:"
  158. (let ((org-clock-into-drawer nil)
  159. (org-log-into-drawer nil))
  160. (org-clock-into-drawer)))))
  161. (should-not
  162. (org-test-with-temp-text
  163. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: nil\n:END:"
  164. (let ((org-clock-into-drawer t)
  165. (org-log-into-drawer nil))
  166. (org-clock-into-drawer))))
  167. ;; "CLOCK_INTO_DRAWER" can be inherited.
  168. (should
  169. (equal "LOGBOOK"
  170. (org-test-with-temp-text
  171. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: t\n:END:\n** H2<point>"
  172. (let ((org-clock-into-drawer nil)
  173. (org-log-into-drawer nil))
  174. (org-clock-into-drawer)))))
  175. (should
  176. (equal "FOO"
  177. (org-test-with-temp-text
  178. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: FOO\n:END:\n** H2<point>"
  179. (let ((org-clock-into-drawer nil)
  180. (org-log-into-drawer nil))
  181. (org-clock-into-drawer)))))
  182. (should-not
  183. (org-test-with-temp-text
  184. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: nil\n:END:\n** H2<point>"
  185. (let ((org-clock-into-drawer t)
  186. (org-log-into-drawer nil))
  187. (org-clock-into-drawer)))))
  188. (ert-deftest test-org-clock/drawer-name ()
  189. "Test `org-clock-drawer-name' specifications."
  190. ;; A nil value for `org-clock-into-drawer' means no drawer is
  191. ;; expected whatsoever.
  192. (should-not
  193. (org-test-with-temp-text "* H"
  194. (let ((org-clock-into-drawer nil)
  195. (org-log-into-drawer nil))
  196. (org-clock-drawer-name))))
  197. (should-not
  198. (org-test-with-temp-text "* H"
  199. (let ((org-clock-into-drawer nil)
  200. (org-log-into-drawer t))
  201. (org-clock-drawer-name))))
  202. (should-not
  203. (org-test-with-temp-text "* H"
  204. (let ((org-clock-into-drawer nil)
  205. (org-log-into-drawer "FOO"))
  206. (org-clock-drawer-name))))
  207. ;; A string value for `org-clock-into-drawer' means to use it
  208. ;; unconditionally.
  209. (should
  210. (equal "FOO"
  211. (org-test-with-temp-text "* H"
  212. (let ((org-clock-into-drawer "FOO")
  213. (org-log-into-drawer nil))
  214. (org-clock-drawer-name)))))
  215. (should
  216. (equal "FOO"
  217. (org-test-with-temp-text "* H"
  218. (let ((org-clock-into-drawer "FOO")
  219. (org-log-into-drawer t))
  220. (org-clock-drawer-name)))))
  221. (should
  222. (equal "FOO"
  223. (org-test-with-temp-text "* H"
  224. (let ((org-clock-into-drawer "FOO")
  225. (org-log-into-drawer "BAR"))
  226. (org-clock-drawer-name)))))
  227. ;; When the value in `org-clock-into-drawer' is a number, re-use
  228. ;; `org-log-into-drawer' or use default "LOGBOOK" value.
  229. (should
  230. (equal "FOO"
  231. (org-test-with-temp-text "* H"
  232. (let ((org-clock-into-drawer 1)
  233. (org-log-into-drawer "FOO"))
  234. (org-clock-drawer-name)))))
  235. (should
  236. (equal "LOGBOOK"
  237. (org-test-with-temp-text "* H"
  238. (let ((org-clock-into-drawer 1)
  239. (org-log-into-drawer t))
  240. (org-clock-drawer-name)))))
  241. (should
  242. (equal "LOGBOOK"
  243. (org-test-with-temp-text "* H"
  244. (let ((org-clock-into-drawer 1)
  245. (org-log-into-drawer nil))
  246. (org-clock-drawer-name))))))
  247. ;;; Clocktable
  248. (ert-deftest test-org-clock/clocktable/ranges ()
  249. "Test ranges in Clock table."
  250. ;; Relative time: Previous two days.
  251. (should
  252. (equal
  253. "| Headline | Time | |
  254. |------------------------------+--------+------|
  255. | *Total time* | *8:00* | |
  256. |------------------------------+--------+------|
  257. | Relative times in clocktable | 8:00 | |
  258. | Foo | | 8:00 |
  259. "
  260. (org-test-with-temp-text
  261. "* Relative times in clocktable\n** Foo\n<point>"
  262. (insert (org-test-clock-create-clock "-3d 8:00" "-3d 12:00"))
  263. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  264. (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
  265. (test-org-clock-clocktable-contents-at-point
  266. ":tstart \"<-2d>\" :tend \"<today>\" :indent nil"))))
  267. ;; Relative time: Yesterday until now.
  268. (should
  269. (equal
  270. "| Headline | Time | |
  271. |------------------------------+--------+------|
  272. | *Total time* | *6:00* | |
  273. |------------------------------+--------+------|
  274. | Relative times in clocktable | 6:00 | |
  275. | Foo | | 6:00 |
  276. "
  277. (org-test-with-temp-text
  278. "* Relative times in clocktable\n** Foo\n<point>"
  279. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  280. (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
  281. (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
  282. (test-org-clock-clocktable-contents-at-point
  283. ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil"))))
  284. ;; Test `untilnow' block.
  285. (should
  286. (equal
  287. "| Headline | Time | |
  288. |------------------------------+--------+------|
  289. | *Total time* | *6:00* | |
  290. |------------------------------+--------+------|
  291. | Relative times in clocktable | 6:00 | |
  292. | Foo | | 6:00 |
  293. "
  294. (org-test-with-temp-text
  295. "* Relative times in clocktable\n** Foo\n<point>"
  296. (insert (org-test-clock-create-clock "-10y 15:00" "-10y 18:00"))
  297. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  298. (test-org-clock-clocktable-contents-at-point
  299. ":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. "
  311. (org-test-with-temp-text "** H1\n\n*** H2 :tag:\n\n*** H3\n<point>"
  312. (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
  313. (goto-line 4)
  314. (insert (org-test-clock-create-clock ". 2:00" ". 4:00"))
  315. (goto-line 2)
  316. (test-org-clock-clocktable-contents-at-point
  317. ":tags \"tag\" :indent nil")))))
  318. (ert-deftest test-org-clock/clocktable/scope ()
  319. "Test \":scope\" parameter in Clock table."
  320. ;; Test `file-with-archives' scope. In particular, preserve "TBLFM"
  321. ;; line, and ignore "file" column.
  322. (should
  323. (equal
  324. "| Headline | Time | |
  325. |--------------+-------------+-----|
  326. | *Total time* | *704d 9:01* | foo |
  327. |--------------+-------------+-----|
  328. | Test | 704d 9:01 | foo |
  329. "
  330. (org-test-with-temp-text-in-file
  331. "* Test
  332. CLOCK: [2012-03-29 Thu 16:40]--[2014-03-04 Thu 00:41] => 16905:01
  333. #+BEGIN: clocktable :scope file-with-archives
  334. #+TBLFM: $3=string(\"foo\")
  335. #+END:
  336. "
  337. (search-forward "#+begin:")
  338. (beginning-of-line)
  339. (org-update-dblock)
  340. (forward-line 2)
  341. (buffer-substring-no-properties
  342. (point) (progn (goto-char (point-max))
  343. (line-beginning-position -1)))))))
  344. (ert-deftest test-org-clock/clocktable/maxlevel ()
  345. "Test \":maxlevel\" parameter in Clock table."
  346. (should
  347. (equal "| Headline | Time | |
  348. |--------------+--------+------|
  349. | *Total time* | *6:00* | |
  350. |--------------+--------+------|
  351. | Foo | 6:00 | |
  352. | \\_ Bar | | 2:00 |
  353. "
  354. (org-test-with-temp-text
  355. "
  356. * Foo
  357. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  358. ** Bar
  359. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
  360. * Report
  361. <point>#+BEGIN: clocktable :maxlevel 3
  362. #+END:"
  363. (org-update-dblock)
  364. (buffer-substring-no-properties
  365. (line-beginning-position 3)
  366. (progn (goto-char (point-max))
  367. (line-beginning-position))))))
  368. (should
  369. (equal "| Headline | Time | |
  370. |--------------+--------+------|
  371. | *Total time* | *6:00* | |
  372. |--------------+--------+------|
  373. | Foo | 6:00 | |
  374. | \\_ Bar | | 2:00 |
  375. "
  376. (org-test-with-temp-text
  377. "
  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. * Report
  383. <point>#+BEGIN: clocktable :maxlevel 2
  384. #+END:"
  385. (org-update-dblock)
  386. (buffer-substring-no-properties
  387. (line-beginning-position 3)
  388. (progn (goto-char (point-max))
  389. (line-beginning-position))))))
  390. (should
  391. (equal "| Headline | Time |
  392. |--------------+--------|
  393. | *Total time* | *6:00* |
  394. |--------------+--------|
  395. | Foo | 6:00 |
  396. "
  397. (org-test-with-temp-text
  398. "
  399. * Foo
  400. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  401. ** Bar
  402. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
  403. * Report
  404. <point>#+BEGIN: clocktable :maxlevel 1
  405. #+END:"
  406. (org-update-dblock)
  407. (buffer-substring-no-properties
  408. (line-beginning-position 3)
  409. (progn (goto-char (point-max))
  410. (line-beginning-position))))))
  411. ;; Special ":maxlevel 0" case: only report total file time.
  412. (should
  413. (equal "| Headline | Time |
  414. |--------------+--------|
  415. | *Total time* | *6:00* |
  416. |--------------+--------|
  417. "
  418. (org-test-with-temp-text
  419. "
  420. * Foo
  421. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  422. ** Bar
  423. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
  424. * Report
  425. <point>#+BEGIN: clocktable :maxlevel 0
  426. #+END:"
  427. (org-update-dblock)
  428. (buffer-substring-no-properties
  429. (line-beginning-position 3)
  430. (progn (goto-char (point-max))
  431. (line-beginning-position)))))))
  432. (ert-deftest test-org-clock/clocktable/formula ()
  433. "Test \":formula\" parameter in Clock table."
  434. ;; Test ":formula %". Handle various duration formats.
  435. (should
  436. (equal
  437. "| Headline | Time | % |
  438. |--------------+--------+-------|
  439. | *Total time* | *6:00* | 100.0 |
  440. |--------------+--------+-------|
  441. | Foo | 4:00 | 66.7 |
  442. | Bar | 2:00 | 33.3 |
  443. "
  444. (org-test-with-temp-text
  445. "* Foo
  446. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  447. * Bar
  448. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
  449. * Report
  450. <point>#+BEGIN: clocktable :maxlevel 1 :formula %
  451. #+END:
  452. "
  453. (org-update-dblock)
  454. (buffer-substring-no-properties (line-beginning-position 3)
  455. (line-beginning-position 9)))))
  456. (should
  457. (equal
  458. "| Headline | Time | % |
  459. |--------------+-----------+-------|
  460. | *Total time* | *1d 4:00* | 100.0 |
  461. |--------------+-----------+-------|
  462. | Foo | 1d 2:00 | 92.9 |
  463. | Bar | 2:00 | 7.1 |
  464. "
  465. (org-test-with-temp-text
  466. "
  467. * Foo
  468. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  469. * Bar
  470. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
  471. * Report
  472. <point>#+BEGIN: clocktable :maxlevel 1 :formula %
  473. #+END:"
  474. (org-update-dblock)
  475. (buffer-substring-no-properties (line-beginning-position 3)
  476. (line-beginning-position 9))))))
  477. (provide 'test-org-clock)
  478. ;;; test-org-clock.el end here