test-org-clock.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 ()
  246. "Test clocktable specifications."
  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. ;; Test tag filtering.
  298. (should
  299. (equal
  300. "| Headline | Time | |
  301. |--------------+--------+------|
  302. | *Total time* | *2:00* | |
  303. |--------------+--------+------|
  304. | H1 | | 2:00 |
  305. "
  306. (org-test-with-temp-text "** H1\n\n*** H2 :tag:\n\n*** H3\n<point>"
  307. (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
  308. (goto-line 4)
  309. (insert (org-test-clock-create-clock ". 2:00" ". 4:00"))
  310. (goto-line 2)
  311. (test-org-clock-clocktable-contents-at-point
  312. ":tags \"tag\" :indent nil"))))
  313. ;; Test `file-with-archives' scope. In particular, preserve "TBLFM"
  314. ;; line, and ignore "file" column.
  315. (should
  316. (equal
  317. "| Headline | Time | |
  318. |--------------+-------------+-----|
  319. | *Total time* | *704d 9:01* | foo |
  320. |--------------+-------------+-----|
  321. | Test | 704d 9:01 | foo |
  322. "
  323. (org-test-with-temp-text-in-file
  324. "* Test
  325. CLOCK: [2012-03-29 Thu 16:40]--[2014-03-04 Thu 00:41] => 16905:01
  326. #+BEGIN: clocktable :scope file-with-archives
  327. #+TBLFM: $3=string(\"foo\")
  328. #+END:
  329. "
  330. (search-forward "#+begin:")
  331. (beginning-of-line)
  332. (org-update-dblock)
  333. (forward-line 2)
  334. (buffer-substring-no-properties
  335. (point) (progn (goto-char (point-max))
  336. (line-beginning-position -1))))))
  337. ;; Test ":formula %". Handle various duration formats.
  338. (should
  339. (equal
  340. "| Headline | Time | % |
  341. |--------------+--------+-------|
  342. | *Total time* | *6:00* | 100.0 |
  343. |--------------+--------+-------|
  344. | Foo | 4:00 | 66.7 |
  345. | Bar | 2:00 | 33.3 |
  346. "
  347. (org-test-with-temp-text
  348. "* Foo
  349. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  350. * Bar
  351. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
  352. * Report
  353. <point>#+BEGIN: clocktable :maxlevel 1 :formula %
  354. #+END:
  355. "
  356. (org-update-dblock)
  357. (buffer-substring-no-properties (line-beginning-position 3)
  358. (line-beginning-position 9)))))
  359. (should
  360. (equal
  361. "| Headline | Time | % |
  362. |--------------+-----------+-------|
  363. | *Total time* | *1d 4:00* | 100.0 |
  364. |--------------+-----------+-------|
  365. | Foo | 1d 2:00 | 92.9 |
  366. | Bar | 2:00 | 7.1 |
  367. "
  368. (org-test-with-temp-text
  369. "
  370. * Foo
  371. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  372. * Bar
  373. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
  374. * Report
  375. <point>#+BEGIN: clocktable :maxlevel 1 :formula %
  376. #+END:"
  377. (org-update-dblock)
  378. (buffer-substring-no-properties (line-beginning-position 3)
  379. (line-beginning-position 9))))))
  380. (provide 'test-org-clock)
  381. ;;; test-org-clock.el end here