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
/ranges
()
268 "Test ranges in Clock table."
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")))))
320 (ert-deftest test-org-clock
/clocktable
/tags
()
321 "Test \":tags\" parameter in Clock table."
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")))))
339 (ert-deftest test-org-clock
/clocktable
/scope
()
340 "Test \":scope\" parameter in Clock table."
341 ;; Test `file-with-archives' scope. In particular, preserve "TBLFM"
342 ;; line, and ignore "file" column.
345 "| Headline | Time | |
346 |--------------+-------------+-----|
347 | *Total time* | *704d 9:01* | foo |
348 |--------------+-------------+-----|
349 | Test | 704d 9:01 | foo |
351 (org-test-with-temp-text-in-file
353 CLOCK: [2012-03-29 Thu 16:40]--[2014-03-04 Thu 00:41] => 16905:01
355 #+BEGIN: clocktable :scope file-with-archives
356 #+TBLFM: $3=string(\"foo\")
359 (search-forward "#+begin:")
363 (buffer-substring-no-properties
364 (point) (progn (goto-char (point-max))
365 (line-beginning-position -
1)))))))
367 (ert-deftest test-org-clock
/clocktable
/maxlevel
()
368 "Test \":maxlevel\" parameter in Clock table."
370 (equal "| Headline | Time | | |
371 |--------------+--------+------+---|
372 | *Total time* | *6:00* | | |
373 |--------------+--------+------+---|
375 | \\_ Bar | | 2:00 | |
377 (org-test-with-temp-text
380 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
382 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
385 <point>#+BEGIN: clocktable :maxlevel 3
388 (buffer-substring-no-properties
389 (line-beginning-position 3)
390 (progn (goto-char (point-max))
391 (line-beginning-position))))))
393 (equal "| Headline | Time | |
394 |--------------+--------+------|
395 | *Total time* | *6:00* | |
396 |--------------+--------+------|
400 (org-test-with-temp-text
403 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
405 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
408 <point>#+BEGIN: clocktable :maxlevel 2
411 (buffer-substring-no-properties
412 (line-beginning-position 3)
413 (progn (goto-char (point-max))
414 (line-beginning-position))))))
416 (equal "| Headline | Time |
417 |--------------+--------|
418 | *Total time* | *6:00* |
419 |--------------+--------|
422 (org-test-with-temp-text
425 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
427 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
430 <point>#+BEGIN: clocktable :maxlevel 1
433 (buffer-substring-no-properties
434 (line-beginning-position 3)
435 (progn (goto-char (point-max))
436 (line-beginning-position))))))
437 ;; Special ":maxlevel 0" case: only report total file time.
439 (equal "| Headline | Time |
440 |--------------+--------|
441 | *Total time* | *6:00* |
442 |--------------+--------|
444 (org-test-with-temp-text
447 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
449 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
452 <point>#+BEGIN: clocktable :maxlevel 0
455 (buffer-substring-no-properties
456 (line-beginning-position 3)
457 (progn (goto-char (point-max))
458 (line-beginning-position)))))))
460 (ert-deftest test-org-clock
/clocktable
/formula
()
461 "Test \":formula\" parameter in Clock table."
462 ;; Test ":formula %". Handle various duration formats.
465 "| Headline | Time | % |
466 |--------------+--------+-------|
467 | *Total time* | *6:00* | 100.0 |
468 |--------------+--------+-------|
469 | Foo | 4:00 | 66.7 |
470 | Bar | 2:00 | 33.3 |
472 (org-test-with-temp-text
474 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
476 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
479 <point>#+BEGIN: clocktable :maxlevel 1 :formula %
483 (buffer-substring-no-properties (line-beginning-position 3)
484 (line-beginning-position 9)))))
487 "| Headline | Time | % |
488 |--------------+-----------+-------|
489 | *Total time* | *1d 4:00* | 100.0 |
490 |--------------+-----------+-------|
491 | Foo | 1d 2:00 | 92.9 |
494 (org-test-with-temp-text
497 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
499 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00
501 <point>#+BEGIN: clocktable :maxlevel 1 :formula %
504 (buffer-substring-no-properties (line-beginning-position 3)
505 (line-beginning-position 9))))))
507 (ert-deftest test-org-clock
/clocktable
/lang
()
508 "Test \":lang\" parameter in Clock table."
509 ;; Test foreign translation
513 |--------------+-----------|
514 | *Total time* | *1d 2:00* |
515 |--------------+-----------|
518 (org-test-with-temp-text
520 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
523 <point>#+BEGIN: clocktable :maxlevel 1 :lang en
526 (buffer-substring-no-properties (line-beginning-position 3)
527 (line-beginning-position 8)))))
531 |----------------+-----------|
532 | *Durée totale* | *1d 2:00* |
533 |----------------+-----------|
536 (org-test-with-temp-text
538 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
541 <point>#+BEGIN: clocktable :maxlevel 1 :lang fr
544 (buffer-substring-no-properties (line-beginning-position 3)
545 (line-beginning-position 8)))))
546 ;; No :lang parameter is equivalent to "en".
549 (org-test-with-temp-text
551 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
554 <point>#+BEGIN: clocktable :maxlevel 1 :lang en
557 (buffer-substring-no-properties (line-beginning-position 3)
558 (line-beginning-position 8)))
559 (org-test-with-temp-text
561 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
564 <point>#+BEGIN: clocktable :maxlevel 1
566 (let ((org-export-default-language nil
)) (org-update-dblock))
567 (buffer-substring-no-properties (line-beginning-position 3)
568 (line-beginning-position 8)))))
569 ;; Unknown translation fall backs to "en".
573 |--------------+-----------|
574 | *Total time* | *1d 2:00* |
575 |--------------+-----------|
578 (org-test-with-temp-text
580 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
583 <point>#+BEGIN: clocktable :maxlevel 1 :lang foo
586 (buffer-substring-no-properties (line-beginning-position 3)
587 (line-beginning-position 8))))))
589 (ert-deftest test-org-clock
/clocktable
/compact
()
590 "Test \":compact\" parameter in Clock table."
591 ;; With :compact, all headlines are in the same column.
595 |--------------+-----------|
596 | *Total time* | *1d 2:00* |
597 |--------------+-----------|
600 (org-test-with-temp-text
602 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
605 <point>#+BEGIN: clocktable :compact t
608 (buffer-substring-no-properties (line-beginning-position 3)
609 (line-beginning-position 8)))))
613 |--------------+-----------|
614 | *Total time* | *2d 4:00* |
615 |--------------+-----------|
617 | \\_ Bar | 1d 2:00 |
619 (org-test-with-temp-text
621 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
623 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
626 <point>#+BEGIN: clocktable :compact t
629 (buffer-substring-no-properties (line-beginning-position 3)
630 (line-beginning-position 9)))))
631 ;; :maxlevel does not affect :compact parameter.
635 |--------------+-----------|
636 | *Total time* | *2d 4:00* |
637 |--------------+-----------|
639 | \\_ Bar | 1d 2:00 |
641 (org-test-with-temp-text
643 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
645 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
648 <point>#+BEGIN: clocktable :compact t :maxlevel 2
651 (buffer-substring-no-properties (line-beginning-position 3)
652 (line-beginning-position 9)))))
653 ;; :compact implies a non-nil :indent parameter.
657 |--------------+-----------|
658 | *Total time* | *2d 4:00* |
659 |--------------+-----------|
661 | \\_ Bar | 1d 2:00 |
663 (org-test-with-temp-text
665 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
667 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
670 <point>#+BEGIN: clocktable :compact t :indent nil
673 (buffer-substring-no-properties (line-beginning-position 3)
674 (line-beginning-position 9)))))
675 ;; :compact implies a nil :level parameter.
679 |--------------+-----------|
680 | *Total time* | *2d 4:00* |
681 |--------------+-----------|
683 | \\_ Bar | 1d 2:00 |
685 (org-test-with-temp-text
687 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
689 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
692 <point>#+BEGIN: clocktable :compact t :level t
695 (buffer-substring-no-properties (line-beginning-position 3)
696 (line-beginning-position 9))))))
699 (provide 'test-org-clock
)
700 ;;; test-org-clock.el end here