org-clock: Fix clock percent
[org-mode/org-tableheadings.git] / testing / lisp / test-org-clock.el
blobe0f23ead0b1aeb3ce0dbe5f2f197531e41d04175
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-time-clocksum-format
71 '(:hours "%d" :require-hours t :minutes ":%02d"
72 :require-minutes t)))
73 (org-update-dblock))
74 (forward-line)
75 ;; Skip caption.
76 (when (looking-at "#\\+CAPTION:") (forward-line))
77 (buffer-substring (point)
78 (progn (search-forward "#+END:")
79 (match-beginning 0))))
80 ;; Remove clocktable.
81 (delete-region (point) (search-forward "#+END:\n"))))
84 ;;; Clock drawer
86 (ert-deftest test-org-clock/into-drawer ()
87 "Test `org-clock-into-drawer' specifications."
88 ;; When `org-clock-into-drawer' is nil, do not use a clock drawer.
89 (should-not
90 (org-test-with-temp-text "* H"
91 (let ((org-clock-into-drawer nil)
92 (org-log-into-drawer nil))
93 (org-clock-into-drawer))))
94 (should-not
95 (org-test-with-temp-text "* H"
96 (let ((org-clock-into-drawer nil)
97 (org-log-into-drawer t))
98 (org-clock-into-drawer))))
99 (should-not
100 (org-test-with-temp-text "* H"
101 (let ((org-clock-into-drawer nil)
102 (org-log-into-drawer "BAR"))
103 (org-clock-into-drawer))))
104 ;; When `org-clock-into-drawer' is a string, use it
105 ;; unconditionally.
106 (should
107 (equal "FOO"
108 (org-test-with-temp-text "* H"
109 (let ((org-clock-into-drawer "FOO")
110 (org-log-into-drawer nil))
111 (org-clock-into-drawer)))))
112 (should
113 (equal "FOO"
114 (org-test-with-temp-text "* H"
115 (let ((org-clock-into-drawer "FOO")
116 (org-log-into-drawer t))
117 (org-clock-into-drawer)))))
118 (should
119 (equal "FOO"
120 (org-test-with-temp-text "* H"
121 (let ((org-clock-into-drawer "FOO")
122 (org-log-into-drawer "BAR"))
123 (org-clock-into-drawer)))))
124 ;; When `org-clock-into-drawer' is an integer, return it.
125 (should
126 (= 1
127 (org-test-with-temp-text "* H"
128 (let ((org-clock-into-drawer 1)
129 (org-log-into-drawer nil))
130 (org-clock-into-drawer)))))
131 (should
132 (= 1
133 (org-test-with-temp-text "* H"
134 (let ((org-clock-into-drawer 1)
135 (org-log-into-drawer t))
136 (org-clock-into-drawer)))))
137 (should
138 (= 1
139 (org-test-with-temp-text "* H"
140 (let ((org-clock-into-drawer 1)
141 (org-log-into-drawer "BAR"))
142 (org-clock-into-drawer)))))
143 ;; Otherwise, any non-nil value defaults to `org-log-into-drawer' or
144 ;; "LOGBOOK" if it is nil.
145 (should
146 (equal "LOGBOOK"
147 (org-test-with-temp-text "* H"
148 (let ((org-clock-into-drawer t)
149 (org-log-into-drawer nil))
150 (org-clock-into-drawer)))))
151 (should
152 (equal "LOGBOOK"
153 (org-test-with-temp-text "* H"
154 (let ((org-clock-into-drawer t)
155 (org-log-into-drawer t))
156 (org-clock-into-drawer)))))
157 (should
158 (equal "FOO"
159 (org-test-with-temp-text "* H"
160 (let ((org-clock-into-drawer t)
161 (org-log-into-drawer "FOO"))
162 (org-clock-into-drawer)))))
163 ;; A non-nil "CLOCK_INTO_DRAWER" property overrides
164 ;; `org-clock-into-drawer' value.
165 (should
166 (equal "LOGBOOK"
167 (org-test-with-temp-text
168 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: t\n:END:"
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:"
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:"
182 (let ((org-clock-into-drawer t)
183 (org-log-into-drawer nil))
184 (org-clock-into-drawer))))
185 ;; "CLOCK_INTO_DRAWER" can be inherited.
186 (should
187 (equal "LOGBOOK"
188 (org-test-with-temp-text
189 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: t\n:END:\n** H2<point>"
190 (let ((org-clock-into-drawer nil)
191 (org-log-into-drawer nil))
192 (org-clock-into-drawer)))))
193 (should
194 (equal "FOO"
195 (org-test-with-temp-text
196 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: FOO\n:END:\n** H2<point>"
197 (let ((org-clock-into-drawer nil)
198 (org-log-into-drawer nil))
199 (org-clock-into-drawer)))))
200 (should-not
201 (org-test-with-temp-text
202 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: nil\n:END:\n** H2<point>"
203 (let ((org-clock-into-drawer t)
204 (org-log-into-drawer nil))
205 (org-clock-into-drawer)))))
207 (ert-deftest test-org-clock/drawer-name ()
208 "Test `org-clock-drawer-name' specifications."
209 ;; A nil value for `org-clock-into-drawer' means no drawer is
210 ;; expected whatsoever.
211 (should-not
212 (org-test-with-temp-text "* H"
213 (let ((org-clock-into-drawer nil)
214 (org-log-into-drawer nil))
215 (org-clock-drawer-name))))
216 (should-not
217 (org-test-with-temp-text "* H"
218 (let ((org-clock-into-drawer nil)
219 (org-log-into-drawer t))
220 (org-clock-drawer-name))))
221 (should-not
222 (org-test-with-temp-text "* H"
223 (let ((org-clock-into-drawer nil)
224 (org-log-into-drawer "FOO"))
225 (org-clock-drawer-name))))
226 ;; A string value for `org-clock-into-drawer' means to use it
227 ;; unconditionally.
228 (should
229 (equal "FOO"
230 (org-test-with-temp-text "* H"
231 (let ((org-clock-into-drawer "FOO")
232 (org-log-into-drawer nil))
233 (org-clock-drawer-name)))))
234 (should
235 (equal "FOO"
236 (org-test-with-temp-text "* H"
237 (let ((org-clock-into-drawer "FOO")
238 (org-log-into-drawer t))
239 (org-clock-drawer-name)))))
240 (should
241 (equal "FOO"
242 (org-test-with-temp-text "* H"
243 (let ((org-clock-into-drawer "FOO")
244 (org-log-into-drawer "BAR"))
245 (org-clock-drawer-name)))))
246 ;; When the value in `org-clock-into-drawer' is a number, re-use
247 ;; `org-log-into-drawer' or use default "LOGBOOK" value.
248 (should
249 (equal "FOO"
250 (org-test-with-temp-text "* H"
251 (let ((org-clock-into-drawer 1)
252 (org-log-into-drawer "FOO"))
253 (org-clock-drawer-name)))))
254 (should
255 (equal "LOGBOOK"
256 (org-test-with-temp-text "* H"
257 (let ((org-clock-into-drawer 1)
258 (org-log-into-drawer t))
259 (org-clock-drawer-name)))))
260 (should
261 (equal "LOGBOOK"
262 (org-test-with-temp-text "* H"
263 (let ((org-clock-into-drawer 1)
264 (org-log-into-drawer nil))
265 (org-clock-drawer-name))))))
268 ;;; Clocktable
270 (ert-deftest test-org-clock/clocktable ()
271 "Test clocktable specifications."
272 ;; Relative time: Previous two days.
273 (should
274 (equal
275 "| Headline | Time | |
276 |------------------------------+--------+------|
277 | *Total time* | *8:00* | |
278 |------------------------------+--------+------|
279 | Relative times in clocktable | 8:00 | |
280 | Foo | | 8:00 |
282 (org-test-with-temp-text
283 "* Relative times in clocktable\n** Foo\n<point>"
284 (insert (org-test-clock-create-clock "-3d 8:00" "-3d 12:00"))
285 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
286 (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
287 (test-org-clock-clocktable-contents-at-point
288 ":tstart \"<-2d>\" :tend \"<today>\" :indent nil"))))
289 ;; Relative time: Yesterday until now.
290 (should
291 (equal
292 "| Headline | Time | |
293 |------------------------------+--------+------|
294 | *Total time* | *6:00* | |
295 |------------------------------+--------+------|
296 | Relative times in clocktable | 6:00 | |
297 | Foo | | 6:00 |
299 (org-test-with-temp-text
300 "* Relative times in clocktable\n** Foo\n<point>"
301 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
302 (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
303 (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
304 (test-org-clock-clocktable-contents-at-point
305 ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil"))))
306 ;; Test `untilnow' block.
307 (should
308 (equal
309 "| Headline | Time | |
310 |------------------------------+--------+------|
311 | *Total time* | *6:00* | |
312 |------------------------------+--------+------|
313 | Relative times in clocktable | 6:00 | |
314 | Foo | | 6:00 |
316 (org-test-with-temp-text
317 "* Relative times in clocktable\n** Foo\n<point>"
318 (insert (org-test-clock-create-clock "-10y 15:00" "-10y 18:00"))
319 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
320 (test-org-clock-clocktable-contents-at-point
321 ":block untilnow :indent nil"))))
322 ;; Test tag filtering.
323 (should
324 (equal
325 "| Headline | Time | |
326 |--------------+--------+------|
327 | *Total time* | *2:00* | |
328 |--------------+--------+------|
329 | H1 | | 2:00 |
331 (org-test-with-temp-text "** H1\n\n*** H2 :tag:\n\n*** H3\n<point>"
332 (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
333 (goto-line 4)
334 (insert (org-test-clock-create-clock ". 2:00" ". 4:00"))
335 (goto-line 2)
336 (test-org-clock-clocktable-contents-at-point
337 ":tags \"tag\" :indent nil"))))
338 ;; Test `file-with-archives' scope. In particular, preserve "TBLFM"
339 ;; line, and ignore "file" column.
340 (should
341 (equal
342 "| Headline | Time | |
343 |--------------+-------------+-----|
344 | *Total time* | *704d 9:01* | foo |
345 |--------------+-------------+-----|
346 | Test | 704d 9:01 | foo |
348 (org-test-with-temp-text-in-file
349 "* Test
350 CLOCK: [2012-03-29 Thu 16:40]--[2014-03-04 Thu 00:41] => 16905:01
352 #+BEGIN: clocktable :scope file-with-archives
353 #+TBLFM: $3=string(\"foo\")
354 #+END:
356 (search-forward "#+begin:")
357 (beginning-of-line)
358 (org-update-dblock)
359 (forward-line 2)
360 (buffer-substring-no-properties
361 (point) (progn (goto-char (point-max))
362 (line-beginning-position -1))))))
363 ;; Test ":formula %". Handle various duration formats.
364 (should
365 (equal
366 "| Headline | Time | % |
367 |--------------+--------+-------|
368 | *Total time* | *6:00* | 100.0 |
369 |--------------+--------+-------|
370 | Foo | 4:00 | 66.7 |
371 | Bar | 2:00 | 33.3 |
373 (org-test-with-temp-text
374 "* Foo
375 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
376 * Bar
377 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
379 * Report
380 <point>#+BEGIN: clocktable :maxlevel 1 :formula %
381 #+END:
383 (org-update-dblock)
384 (buffer-substring-no-properties (line-beginning-position 3)
385 (line-beginning-position 9)))))
386 (should
387 (equal
388 "| Headline | Time | % |
389 |--------------+-----------+-------|
390 | *Total time* | *1d 4:00* | 100.0 |
391 |--------------+-----------+-------|
392 | Foo | 1d 2:00 | 92.9 |
393 | Bar | 2:00 | 7.1 |
395 (org-test-with-temp-text
397 * Foo
398 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
399 * Bar
400 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
401 * Report
402 <point>#+BEGIN: clocktable :maxlevel 1 :formula %
403 #+END:"
404 (org-update-dblock)
405 (buffer-substring-no-properties (line-beginning-position 3)
406 (line-beginning-position 9))))))
408 (provide 'test-org-clock)
409 ;;; test-org-clock.el end here