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"))))
87 (ert-deftest test-org-clock
/clocktable
()
88 "Test clocktable specifications."
89 ;; Relative time: Previous two days.
92 "| Headline | Time | |
93 |------------------------------+--------+------|
94 | *Total time* | *8:00* | |
95 |------------------------------+--------+------|
96 | Relative times in clocktable | 8:00 | |
99 (org-test-with-temp-text
100 "* Relative times in clocktable\n** Foo\n<point>"
101 (insert (org-test-clock-create-clock "-3d 8:00" "-3d 12:00"))
102 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
103 (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
104 (test-org-clock-clocktable-contents-at-point
105 ":tstart \"<-2d>\" :tend \"<today>\" :indent nil"))))
106 ;; Relative time: Yesterday until now.
109 "| Headline | Time | |
110 |------------------------------+--------+------|
111 | *Total time* | *6:00* | |
112 |------------------------------+--------+------|
113 | Relative times in clocktable | 6:00 | |
116 (org-test-with-temp-text
117 "* Relative times in clocktable\n** Foo\n<point>"
118 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
119 (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
120 (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
121 (test-org-clock-clocktable-contents-at-point
122 ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil"))))
123 ;; Test `untilnow' block.
126 "| Headline | Time | |
127 |------------------------------+--------+------|
128 | *Total time* | *6:00* | |
129 |------------------------------+--------+------|
130 | Relative times in clocktable | 6:00 | |
133 (org-test-with-temp-text
134 "* Relative times in clocktable\n** Foo\n<point>"
135 (insert (org-test-clock-create-clock "-10y 15:00" "-10y 18:00"))
136 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
137 (test-org-clock-clocktable-contents-at-point
138 ":block untilnow :indent nil")))))
140 (provide 'test-org-clock
)
141 ;;; test-org-clock.el end here