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-duration-format 'h
:mm
)) (org-update-dblock))
73 (when (looking-at "#\\+CAPTION:") (forward-line))
74 (buffer-substring (point)
75 (progn (search-forward "#+END:")
76 (match-beginning 0))))
78 (delete-region (point) (search-forward "#+END:\n"))))
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.
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))))
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))))
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
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)))))
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)))))
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.
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)))))
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)))))
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.
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)))))
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)))))
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.
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)))))
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)))))
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.
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)))))
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)))))
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.
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))))
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))))
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
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)))))
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)))))
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.
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)))))
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)))))
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))))))
267 (ert-deftest test-org-clock
/clocktable
()
268 "Test clocktable specifications."
269 ;; Relative time: Previous two days.
272 "| Headline | Time | |
273 |------------------------------+--------+------|
274 | *Total time* | *8:00* | |
275 |------------------------------+--------+------|
276 | Relative times in clocktable | 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.
289 "| Headline | Time | |
290 |------------------------------+--------+------|
291 | *Total time* | *6:00* | |
292 |------------------------------+--------+------|
293 | Relative times in clocktable | 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.
306 "| Headline | Time | |
307 |------------------------------+--------+------|
308 | *Total time* | *6:00* | |
309 |------------------------------+--------+------|
310 | Relative times in clocktable | 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.
322 "| Headline | Time | |
323 |--------------+--------+------|
324 | *Total time* | *2:00* | |
325 |--------------+--------+------|
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"))
331 (insert (org-test-clock-create-clock ". 2:00" ". 4:00"))
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.
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
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\")
353 (search-forward "#+begin:")
357 (buffer-substring-no-properties
358 (point) (progn (goto-char (point-max))
359 (line-beginning-position -
1))))))
360 ;; Test ":formula %". Handle various duration formats.
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
372 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
374 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
377 <point>#+BEGIN: clocktable :maxlevel 1 :formula %
381 (buffer-substring-no-properties (line-beginning-position 3)
382 (line-beginning-position 9)))))
385 "| Headline | Time | % |
386 |--------------+-----------+-------|
387 | *Total time* | *1d 4:00* | 100.0 |
388 |--------------+-----------+-------|
389 | Foo | 1d 2:00 | 92.9 |
392 (org-test-with-temp-text
395 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
397 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
399 <point>#+BEGIN: clocktable :maxlevel 1 :formula %
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