Merge branch 'maint'
[org-mode/org-tableheadings.git] / testing / lisp / test-org-clock.el
blob29e7de8d322310ef0689ca29714b38ff585d5566
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")))))
323 (provide 'test-org-clock)
324 ;;; test-org-clock.el end here