Use Org duration library
[org-mode.git] / testing / lisp / test-org-clock.el
blob60b32ebf84ecf9724658b2ca94e5d7b31717b84a
1 ;;; test-org-clock.el --- Tests for org-clock.el
3 ;; Copyright (C) 2012, 2014, 2015 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
7 ;; Released under the GNU General Public License version 3
8 ;; see: http://www.gnu.org/licenses/gpl-3.0.html
10 ;;;; Comments
14 ;;; Code:
16 (defun org-test-clock-create-timestamp (input &optional inactive with-time)
17 "Create a timestamp out of a date/time prompt string.
19 INPUT is a string as expected in a date/time prompt, i.e \"+2d\"
20 or \"2/5\".
22 When optional argument INACTIVE is non-nil, return an inactive
23 timestamp. When optional argument WITH-TIME is non-nil, also
24 insert hours and minutes.
26 Return the timestamp as a string."
27 (org-element-interpret-data
28 (let ((time (decode-time
29 (apply #'encode-time
30 (mapcar (lambda (el) (or el 0))
31 (org-read-date-analyze
32 input nil (decode-time (current-time))))))))
33 (list 'timestamp
34 (list :type (if inactive 'inactive 'active)
35 :minute-start (and with-time (nth 1 time))
36 :hour-start (and with-time (nth 2 time))
37 :day-start (nth 3 time)
38 :month-start (nth 4 time)
39 :year-start (nth 5 time))))))
41 (defun org-test-clock-create-clock (input1 &optional input2)
42 "Create a clock line out of two date/time prompts.
44 INPUT1 and INPUT2 are strings as expected in a date/time prompt,
45 i.e \"+2d\" or \"2/5\". They respectively refer to start and end
46 range. INPUT2 can be omitted if clock hasn't finished yet.
48 Return the clock line as a string."
49 (let* ((beg (org-test-clock-create-timestamp input1 t t))
50 (end (and input2 (org-test-clock-create-timestamp input2 t t)))
51 (sec-diff (and input2 (floor (- (org-time-string-to-seconds end)
52 (org-time-string-to-seconds beg))))))
53 (concat org-clock-string " " beg
54 (when end
55 (concat "--" end " => "
56 (format "%2d:%02d"
57 (/ sec-diff 3600)
58 (/ (mod sec-diff 3600) 60))))
59 "\n")))
61 (defun test-org-clock-clocktable-contents-at-point (options)
62 "Return contents of a clocktable at point.
63 OPTIONS is a string of clocktable options. Caption is ignored in
64 contents. The clocktable doesn't appear in the buffer."
65 (save-excursion
66 (insert "#+BEGIN: clocktable " options "\n")
67 (insert "#+END:\n"))
68 (unwind-protect
69 (save-excursion
70 (let ((org-duration-format 'h:mm)) (org-update-dblock))
71 (forward-line)
72 ;; Skip caption.
73 (when (looking-at "#\\+CAPTION:") (forward-line))
74 (buffer-substring (point)
75 (progn (search-forward "#+END:")
76 (match-beginning 0))))
77 ;; Remove clocktable.
78 (delete-region (point) (search-forward "#+END:\n"))))
81 ;;; Clock drawer
83 (ert-deftest test-org-clock/into-drawer ()
84 "Test `org-clock-into-drawer' specifications."
85 ;; When `org-clock-into-drawer' is nil, do not use a clock drawer.
86 (should-not
87 (org-test-with-temp-text "* H"
88 (let ((org-clock-into-drawer nil)
89 (org-log-into-drawer nil))
90 (org-clock-into-drawer))))
91 (should-not
92 (org-test-with-temp-text "* H"
93 (let ((org-clock-into-drawer nil)
94 (org-log-into-drawer t))
95 (org-clock-into-drawer))))
96 (should-not
97 (org-test-with-temp-text "* H"
98 (let ((org-clock-into-drawer nil)
99 (org-log-into-drawer "BAR"))
100 (org-clock-into-drawer))))
101 ;; When `org-clock-into-drawer' is a string, use it
102 ;; unconditionally.
103 (should
104 (equal "FOO"
105 (org-test-with-temp-text "* H"
106 (let ((org-clock-into-drawer "FOO")
107 (org-log-into-drawer nil))
108 (org-clock-into-drawer)))))
109 (should
110 (equal "FOO"
111 (org-test-with-temp-text "* H"
112 (let ((org-clock-into-drawer "FOO")
113 (org-log-into-drawer t))
114 (org-clock-into-drawer)))))
115 (should
116 (equal "FOO"
117 (org-test-with-temp-text "* H"
118 (let ((org-clock-into-drawer "FOO")
119 (org-log-into-drawer "BAR"))
120 (org-clock-into-drawer)))))
121 ;; When `org-clock-into-drawer' is an integer, return it.
122 (should
123 (= 1
124 (org-test-with-temp-text "* H"
125 (let ((org-clock-into-drawer 1)
126 (org-log-into-drawer nil))
127 (org-clock-into-drawer)))))
128 (should
129 (= 1
130 (org-test-with-temp-text "* H"
131 (let ((org-clock-into-drawer 1)
132 (org-log-into-drawer t))
133 (org-clock-into-drawer)))))
134 (should
135 (= 1
136 (org-test-with-temp-text "* H"
137 (let ((org-clock-into-drawer 1)
138 (org-log-into-drawer "BAR"))
139 (org-clock-into-drawer)))))
140 ;; Otherwise, any non-nil value defaults to `org-log-into-drawer' or
141 ;; "LOGBOOK" if it is nil.
142 (should
143 (equal "LOGBOOK"
144 (org-test-with-temp-text "* H"
145 (let ((org-clock-into-drawer t)
146 (org-log-into-drawer nil))
147 (org-clock-into-drawer)))))
148 (should
149 (equal "LOGBOOK"
150 (org-test-with-temp-text "* H"
151 (let ((org-clock-into-drawer t)
152 (org-log-into-drawer t))
153 (org-clock-into-drawer)))))
154 (should
155 (equal "FOO"
156 (org-test-with-temp-text "* H"
157 (let ((org-clock-into-drawer t)
158 (org-log-into-drawer "FOO"))
159 (org-clock-into-drawer)))))
160 ;; A non-nil "CLOCK_INTO_DRAWER" property overrides
161 ;; `org-clock-into-drawer' value.
162 (should
163 (equal "LOGBOOK"
164 (org-test-with-temp-text
165 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: t\n:END:"
166 (let ((org-clock-into-drawer nil)
167 (org-log-into-drawer nil))
168 (org-clock-into-drawer)))))
169 (should
170 (equal "FOO"
171 (org-test-with-temp-text
172 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: FOO\n:END:"
173 (let ((org-clock-into-drawer nil)
174 (org-log-into-drawer nil))
175 (org-clock-into-drawer)))))
176 (should-not
177 (org-test-with-temp-text
178 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: nil\n:END:"
179 (let ((org-clock-into-drawer t)
180 (org-log-into-drawer nil))
181 (org-clock-into-drawer))))
182 ;; "CLOCK_INTO_DRAWER" can be inherited.
183 (should
184 (equal "LOGBOOK"
185 (org-test-with-temp-text
186 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: t\n:END:\n** H2<point>"
187 (let ((org-clock-into-drawer nil)
188 (org-log-into-drawer nil))
189 (org-clock-into-drawer)))))
190 (should
191 (equal "FOO"
192 (org-test-with-temp-text
193 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: FOO\n:END:\n** H2<point>"
194 (let ((org-clock-into-drawer nil)
195 (org-log-into-drawer nil))
196 (org-clock-into-drawer)))))
197 (should-not
198 (org-test-with-temp-text
199 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: nil\n:END:\n** H2<point>"
200 (let ((org-clock-into-drawer t)
201 (org-log-into-drawer nil))
202 (org-clock-into-drawer)))))
204 (ert-deftest test-org-clock/drawer-name ()
205 "Test `org-clock-drawer-name' specifications."
206 ;; A nil value for `org-clock-into-drawer' means no drawer is
207 ;; expected whatsoever.
208 (should-not
209 (org-test-with-temp-text "* H"
210 (let ((org-clock-into-drawer nil)
211 (org-log-into-drawer nil))
212 (org-clock-drawer-name))))
213 (should-not
214 (org-test-with-temp-text "* H"
215 (let ((org-clock-into-drawer nil)
216 (org-log-into-drawer t))
217 (org-clock-drawer-name))))
218 (should-not
219 (org-test-with-temp-text "* H"
220 (let ((org-clock-into-drawer nil)
221 (org-log-into-drawer "FOO"))
222 (org-clock-drawer-name))))
223 ;; A string value for `org-clock-into-drawer' means to use it
224 ;; unconditionally.
225 (should
226 (equal "FOO"
227 (org-test-with-temp-text "* H"
228 (let ((org-clock-into-drawer "FOO")
229 (org-log-into-drawer nil))
230 (org-clock-drawer-name)))))
231 (should
232 (equal "FOO"
233 (org-test-with-temp-text "* H"
234 (let ((org-clock-into-drawer "FOO")
235 (org-log-into-drawer t))
236 (org-clock-drawer-name)))))
237 (should
238 (equal "FOO"
239 (org-test-with-temp-text "* H"
240 (let ((org-clock-into-drawer "FOO")
241 (org-log-into-drawer "BAR"))
242 (org-clock-drawer-name)))))
243 ;; When the value in `org-clock-into-drawer' is a number, re-use
244 ;; `org-log-into-drawer' or use default "LOGBOOK" value.
245 (should
246 (equal "FOO"
247 (org-test-with-temp-text "* H"
248 (let ((org-clock-into-drawer 1)
249 (org-log-into-drawer "FOO"))
250 (org-clock-drawer-name)))))
251 (should
252 (equal "LOGBOOK"
253 (org-test-with-temp-text "* H"
254 (let ((org-clock-into-drawer 1)
255 (org-log-into-drawer t))
256 (org-clock-drawer-name)))))
257 (should
258 (equal "LOGBOOK"
259 (org-test-with-temp-text "* H"
260 (let ((org-clock-into-drawer 1)
261 (org-log-into-drawer nil))
262 (org-clock-drawer-name))))))
265 ;;; Clocktable
267 (ert-deftest test-org-clock/clocktable ()
268 "Test clocktable specifications."
269 ;; Relative time: Previous two days.
270 (should
271 (equal
272 "| Headline | Time | |
273 |------------------------------+--------+------|
274 | *Total time* | *8:00* | |
275 |------------------------------+--------+------|
276 | Relative times in clocktable | 8:00 | |
277 | Foo | | 8:00 |
279 (org-test-with-temp-text
280 "* Relative times in clocktable\n** Foo\n<point>"
281 (insert (org-test-clock-create-clock "-3d 8:00" "-3d 12:00"))
282 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
283 (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
284 (test-org-clock-clocktable-contents-at-point
285 ":tstart \"<-2d>\" :tend \"<today>\" :indent nil"))))
286 ;; Relative time: Yesterday until now.
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 |
296 (org-test-with-temp-text
297 "* Relative times in clocktable\n** Foo\n<point>"
298 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
299 (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
300 (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
301 (test-org-clock-clocktable-contents-at-point
302 ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil"))))
303 ;; Test `untilnow' block.
304 (should
305 (equal
306 "| Headline | Time | |
307 |------------------------------+--------+------|
308 | *Total time* | *6:00* | |
309 |------------------------------+--------+------|
310 | Relative times in clocktable | 6:00 | |
311 | Foo | | 6:00 |
313 (org-test-with-temp-text
314 "* Relative times in clocktable\n** Foo\n<point>"
315 (insert (org-test-clock-create-clock "-10y 15:00" "-10y 18:00"))
316 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
317 (test-org-clock-clocktable-contents-at-point
318 ":block untilnow :indent nil"))))
319 ;; Test tag filtering.
320 (should
321 (equal
322 "| Headline | Time | |
323 |--------------+--------+------|
324 | *Total time* | *2:00* | |
325 |--------------+--------+------|
326 | H1 | | 2:00 |
328 (org-test-with-temp-text "** H1\n\n*** H2 :tag:\n\n*** H3\n<point>"
329 (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
330 (goto-line 4)
331 (insert (org-test-clock-create-clock ". 2:00" ". 4:00"))
332 (goto-line 2)
333 (test-org-clock-clocktable-contents-at-point
334 ":tags \"tag\" :indent nil"))))
335 ;; Test `file-with-archives' scope. In particular, preserve "TBLFM"
336 ;; line, and ignore "file" column.
337 (should
338 (equal
339 "| Headline | Time | |
340 |--------------+-------------+-----|
341 | *Total time* | *704d 9:01* | foo |
342 |--------------+-------------+-----|
343 | Test | 704d 9:01 | foo |
345 (org-test-with-temp-text-in-file
346 "* Test
347 CLOCK: [2012-03-29 Thu 16:40]--[2014-03-04 Thu 00:41] => 16905:01
349 #+BEGIN: clocktable :scope file-with-archives
350 #+TBLFM: $3=string(\"foo\")
351 #+END:
353 (search-forward "#+begin:")
354 (beginning-of-line)
355 (org-update-dblock)
356 (forward-line 2)
357 (buffer-substring-no-properties
358 (point) (progn (goto-char (point-max))
359 (line-beginning-position -1))))))
360 ;; Test ":formula %". Handle various duration formats.
361 (should
362 (equal
363 "| Headline | Time | % |
364 |--------------+--------+-------|
365 | *Total time* | *6:00* | 100.0 |
366 |--------------+--------+-------|
367 | Foo | 4:00 | 66.7 |
368 | Bar | 2:00 | 33.3 |
370 (org-test-with-temp-text
371 "* Foo
372 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
373 * Bar
374 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
376 * Report
377 <point>#+BEGIN: clocktable :maxlevel 1 :formula %
378 #+END:
380 (org-update-dblock)
381 (buffer-substring-no-properties (line-beginning-position 3)
382 (line-beginning-position 9)))))
383 (should
384 (equal
385 "| Headline | Time | % |
386 |--------------+-----------+-------|
387 | *Total time* | *1d 4:00* | 100.0 |
388 |--------------+-----------+-------|
389 | Foo | 1d 2:00 | 92.9 |
390 | Bar | 2:00 | 7.1 |
392 (org-test-with-temp-text
394 * Foo
395 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
396 * Bar
397 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
398 * Report
399 <point>#+BEGIN: clocktable :maxlevel 1 :formula %
400 #+END:"
401 (org-update-dblock)
402 (buffer-substring-no-properties (line-beginning-position 3)
403 (line-beginning-position 9))))))
405 (provide 'test-org-clock)
406 ;;; test-org-clock.el end here