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
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\"
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
30 (mapcar (lambda (el) (or el
0))
31 (org-read-date-analyze
32 input nil
(decode-time (current-time))))))))
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
55 (concat "--" end
" => "
58 (/ (mod sec-diff
3600) 60))))
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."
66 (insert "#+BEGIN: clocktable " options
"\n")
70 (let ((org-time-clocksum-format
71 '(:hours
"%d" :require-hours t
:minutes
":%02d"
76 (when (looking-at "#\\+CAPTION:") (forward-line))
77 (buffer-substring (point)
78 (progn (search-forward "#+END:")
79 (match-beginning 0))))
81 (delete-region (point) (search-forward "#+END:\n"))))
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.
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))))
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))))
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
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)))))
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)))))
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.
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)))))
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)))))
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.
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)))))
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)))))
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.
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)))))
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)))))
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.
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)))))
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)))))
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.
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))))
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))))
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
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)))))
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)))))
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.
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)))))
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)))))
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))))))
270 (ert-deftest test-org-clock
/clocktable
()
271 "Test clocktable specifications."
272 ;; Relative time: Previous two days.
275 "| Headline | Time | |
276 |------------------------------+--------+------|
277 | *Total time* | *8:00* | |
278 |------------------------------+--------+------|
279 | Relative times in clocktable | 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.
292 "| Headline | Time | |
293 |------------------------------+--------+------|
294 | *Total time* | *6:00* | |
295 |------------------------------+--------+------|
296 | Relative times in clocktable | 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.
309 "| Headline | Time | |
310 |------------------------------+--------+------|
311 | *Total time* | *6:00* | |
312 |------------------------------+--------+------|
313 | Relative times in clocktable | 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.
325 "| Headline | Time | |
326 |--------------+--------+------|
327 | *Total time* | *2:00* | |
328 |--------------+--------+------|
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"))
334 (insert (org-test-clock-create-clock ". 2:00" ". 4:00"))
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.
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
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\")
356 (search-forward "#+begin:")
360 (buffer-substring-no-properties
361 (point) (progn (goto-char (point-max))
362 (line-beginning-position -
1))))))
363 ;; Test ":formula %". Handle various duration formats.
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
375 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
377 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
380 <point>#+BEGIN: clocktable :maxlevel 1 :formula %
384 (buffer-substring-no-properties (line-beginning-position 3)
385 (line-beginning-position 9)))))
388 "| Headline | Time | % |
389 |--------------+-----------+-------|
390 | *Total time* | *1d 4:00* | 100.0 |
391 |--------------+-----------+-------|
392 | Foo | 1d 2:00 | 92.9 |
395 (org-test-with-temp-text
398 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
400 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
402 <point>#+BEGIN: clocktable :maxlevel 1 :formula %
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