test-org-clock.el 17 KB

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