test-org-clock.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 ()
  249. "Test clocktable specifications."
  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. (provide 'test-org-clock)
  301. ;;; test-org-clock.el end here