org-clock: Re-activate tests
[org-mode/org-tableheadings.git] / testing / lisp / test-org-clock.el
blobe85325c1482f5ec5f7d35e6dcd4181073e460621
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
52 (floor (- (org-time-string-to-seconds end)
53 (org-time-string-to-seconds beg))))))
54 (concat org-clock-string " " beg
55 (when end
56 (concat "--" end " => "
57 (format "%2d:%02d"
58 (/ sec-diff 3600)
59 (/ (mod sec-diff 3600) 60))))
60 "\n")))
62 (defun test-org-clock-clocktable-contents (options &optional initial)
63 "Return contents of a Clock table for current buffer
65 OPTIONS is a string of Clock table options. Optional argument
66 INITIAL is a string specifying initial contents within the Clock
67 table.
69 Caption is ignored in contents. The clocktable doesn't appear in
70 the buffer."
71 (declare (indent 2))
72 (goto-char (point-min))
73 (save-excursion
74 (insert "#+BEGIN: clocktable " options "\n")
75 (when initial (insert initial))
76 (unless (string-suffix-p "\n" initial) (insert "\n"))
77 (insert "#+END:\n"))
78 (unwind-protect
79 (save-excursion
80 (let ((org-duration-format 'h:mm)) (org-update-dblock))
81 (forward-line)
82 ;; Skip caption.
83 (when (looking-at "#\\+CAPTION:") (forward-line))
84 (buffer-substring-no-properties
85 (point) (progn (search-forward "#+END:") (line-end-position 0))))
86 ;; Remove clocktable.
87 (delete-region (point) (search-forward "#+END:\n"))))
90 ;;; Clock drawer
92 (ert-deftest test-org-clock/into-drawer ()
93 "Test `org-clock-into-drawer' specifications."
94 ;; When `org-clock-into-drawer' is nil, do not use a clock drawer.
95 (should-not
96 (org-test-with-temp-text "* H"
97 (let ((org-clock-into-drawer nil)
98 (org-log-into-drawer nil))
99 (org-clock-into-drawer))))
100 (should-not
101 (org-test-with-temp-text "* H"
102 (let ((org-clock-into-drawer nil)
103 (org-log-into-drawer t))
104 (org-clock-into-drawer))))
105 (should-not
106 (org-test-with-temp-text "* H"
107 (let ((org-clock-into-drawer nil)
108 (org-log-into-drawer "BAR"))
109 (org-clock-into-drawer))))
110 ;; When `org-clock-into-drawer' is a string, use it
111 ;; unconditionally.
112 (should
113 (equal "FOO"
114 (org-test-with-temp-text "* H"
115 (let ((org-clock-into-drawer "FOO")
116 (org-log-into-drawer nil))
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 t))
123 (org-clock-into-drawer)))))
124 (should
125 (equal "FOO"
126 (org-test-with-temp-text "* H"
127 (let ((org-clock-into-drawer "FOO")
128 (org-log-into-drawer "BAR"))
129 (org-clock-into-drawer)))))
130 ;; When `org-clock-into-drawer' is an integer, return it.
131 (should
132 (= 1
133 (org-test-with-temp-text "* H"
134 (let ((org-clock-into-drawer 1)
135 (org-log-into-drawer nil))
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 t))
142 (org-clock-into-drawer)))))
143 (should
144 (= 1
145 (org-test-with-temp-text "* H"
146 (let ((org-clock-into-drawer 1)
147 (org-log-into-drawer "BAR"))
148 (org-clock-into-drawer)))))
149 ;; Otherwise, any non-nil value defaults to `org-log-into-drawer' or
150 ;; "LOGBOOK" if it is nil.
151 (should
152 (equal "LOGBOOK"
153 (org-test-with-temp-text "* H"
154 (let ((org-clock-into-drawer t)
155 (org-log-into-drawer nil))
156 (org-clock-into-drawer)))))
157 (should
158 (equal "LOGBOOK"
159 (org-test-with-temp-text "* H"
160 (let ((org-clock-into-drawer t)
161 (org-log-into-drawer t))
162 (org-clock-into-drawer)))))
163 (should
164 (equal "FOO"
165 (org-test-with-temp-text "* H"
166 (let ((org-clock-into-drawer t)
167 (org-log-into-drawer "FOO"))
168 (org-clock-into-drawer)))))
169 ;; A non-nil "CLOCK_INTO_DRAWER" property overrides
170 ;; `org-clock-into-drawer' value.
171 (should
172 (equal "LOGBOOK"
173 (org-test-with-temp-text
174 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: t\n:END:"
175 (let ((org-clock-into-drawer nil)
176 (org-log-into-drawer nil))
177 (org-clock-into-drawer)))))
178 (should
179 (equal "FOO"
180 (org-test-with-temp-text
181 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: FOO\n:END:"
182 (let ((org-clock-into-drawer nil)
183 (org-log-into-drawer nil))
184 (org-clock-into-drawer)))))
185 (should-not
186 (org-test-with-temp-text
187 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: nil\n:END:"
188 (let ((org-clock-into-drawer t)
189 (org-log-into-drawer nil))
190 (org-clock-into-drawer))))
191 ;; "CLOCK_INTO_DRAWER" can be inherited.
192 (should
193 (equal "LOGBOOK"
194 (org-test-with-temp-text
195 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: t\n:END:\n** H2<point>"
196 (let ((org-clock-into-drawer nil)
197 (org-log-into-drawer nil))
198 (org-clock-into-drawer)))))
199 (should
200 (equal "FOO"
201 (org-test-with-temp-text
202 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: FOO\n:END:\n** H2<point>"
203 (let ((org-clock-into-drawer nil)
204 (org-log-into-drawer nil))
205 (org-clock-into-drawer)))))
206 (should-not
207 (org-test-with-temp-text
208 "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: nil\n:END:\n** H2<point>"
209 (let ((org-clock-into-drawer t)
210 (org-log-into-drawer nil))
211 (org-clock-into-drawer)))))
213 (ert-deftest test-org-clock/drawer-name ()
214 "Test `org-clock-drawer-name' specifications."
215 ;; A nil value for `org-clock-into-drawer' means no drawer is
216 ;; expected whatsoever.
217 (should-not
218 (org-test-with-temp-text "* H"
219 (let ((org-clock-into-drawer nil)
220 (org-log-into-drawer nil))
221 (org-clock-drawer-name))))
222 (should-not
223 (org-test-with-temp-text "* H"
224 (let ((org-clock-into-drawer nil)
225 (org-log-into-drawer t))
226 (org-clock-drawer-name))))
227 (should-not
228 (org-test-with-temp-text "* H"
229 (let ((org-clock-into-drawer nil)
230 (org-log-into-drawer "FOO"))
231 (org-clock-drawer-name))))
232 ;; A string value for `org-clock-into-drawer' means to use it
233 ;; unconditionally.
234 (should
235 (equal "FOO"
236 (org-test-with-temp-text "* H"
237 (let ((org-clock-into-drawer "FOO")
238 (org-log-into-drawer nil))
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 t))
245 (org-clock-drawer-name)))))
246 (should
247 (equal "FOO"
248 (org-test-with-temp-text "* H"
249 (let ((org-clock-into-drawer "FOO")
250 (org-log-into-drawer "BAR"))
251 (org-clock-drawer-name)))))
252 ;; When the value in `org-clock-into-drawer' is a number, re-use
253 ;; `org-log-into-drawer' or use default "LOGBOOK" value.
254 (should
255 (equal "FOO"
256 (org-test-with-temp-text "* H"
257 (let ((org-clock-into-drawer 1)
258 (org-log-into-drawer "FOO"))
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 t))
265 (org-clock-drawer-name)))))
266 (should
267 (equal "LOGBOOK"
268 (org-test-with-temp-text "* H"
269 (let ((org-clock-into-drawer 1)
270 (org-log-into-drawer nil))
271 (org-clock-drawer-name))))))
274 ;;; Clocktable
276 (ert-deftest test-org-clock/clocktable/ranges ()
277 "Test ranges in Clock table."
278 ;; Relative time: Previous two days.
279 (should
280 (equal
281 "| Headline | Time | |
282 |------------------------------+--------+------|
283 | *Total time* | *8:00* | |
284 |------------------------------+--------+------|
285 | Relative times in clocktable | 8:00 | |
286 | Foo | | 8:00 |"
287 (org-test-with-temp-text
288 "* Relative times in clocktable\n** Foo\n<point>"
289 (insert (org-test-clock-create-clock "-3d 8:00" "-3d 12:00"))
290 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
291 (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
292 (test-org-clock-clocktable-contents
293 ":tstart \"<-2d>\" :tend \"<today>\" :indent nil"))))
294 ;; Relative time: Yesterday until now.
295 (should
296 (equal
297 "| Headline | Time | |
298 |------------------------------+--------+------|
299 | *Total time* | *6:00* | |
300 |------------------------------+--------+------|
301 | Relative times in clocktable | 6:00 | |
302 | Foo | | 6:00 |"
303 (org-test-with-temp-text
304 "* Relative times in clocktable\n** Foo\n<point>"
305 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
306 (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
307 (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
308 (test-org-clock-clocktable-contents
309 ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil"))))
310 ;; Test `untilnow' block.
311 (should
312 (equal
313 "| Headline | Time | |
314 |------------------------------+--------+------|
315 | *Total time* | *6:00* | |
316 |------------------------------+--------+------|
317 | Relative times in clocktable | 6:00 | |
318 | Foo | | 6:00 |"
319 (org-test-with-temp-text
320 "* Relative times in clocktable\n** Foo\n<point>"
321 (insert (org-test-clock-create-clock "-10y 15:00" "-10y 18:00"))
322 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
323 (test-org-clock-clocktable-contents ":block untilnow :indent nil")))))
325 (ert-deftest test-org-clock/clocktable/match ()
326 "Test \":match\" parameter in Clock table."
327 ;; Test match filtering.
328 (should
329 (equal
330 "| Headline | Time | |
331 |--------------+--------+------|
332 | *Total time* | *2:00* | |
333 |--------------+--------+------|
334 | H1 | | 2:00 |"
335 (org-test-with-temp-text "** H1\n\n*** H2 :tag:\n\n*** H3\n<point>"
336 (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
337 (goto-line 4)
338 (insert (org-test-clock-create-clock ". 2:00" ". 4:00"))
339 (test-org-clock-clocktable-contents ":match \"tag\" :indent nil")))))
341 (ert-deftest test-org-clock/clocktable/tags ()
342 "Test \":tags\" parameter in Clock table."
343 ;; Test tags column.
344 (should
345 (equal
346 "| Tags | Headline | Time | |
347 |------+--------------+--------+------|
348 | | *Total time* | *1:00* | |
349 |------+--------------+--------+------|
350 | tag | H1 | | 1:00 |"
351 (org-test-with-temp-text "** H1 :tag:\n\n*** H2 \n<point>"
352 (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
353 (goto-line 4)
354 (test-org-clock-clocktable-contents ":tags t :indent nil")))))
356 (ert-deftest test-org-clock/clocktable/scope ()
357 "Test \":scope\" parameter in Clock table."
358 ;; Test `file-with-archives' scope. In particular, preserve "TBLFM"
359 ;; line, and ignore "file" column.
360 (should
361 (equal
362 "| Headline | Time | |
363 |--------------+--------+-----|
364 | *Total time* | *8:40* | foo |
365 |--------------+--------+-----|
366 | Test | 8:40 | foo |
367 #+TBLFM: $3=string(\"foo\")"
368 (org-test-with-temp-text-in-file
369 "* Test
370 CLOCK: [2012-03-29 Thu 8:00]--[2012-03-29 Thu 16:40] => 8:40"
371 (test-org-clock-clocktable-contents ":scope file-with-archives"
372 "#+TBLFM: $3=string(\"foo\")"))))
373 ;; Test "function" scope.
374 (should
375 (string-match-p
376 (regexp-quote "| ALL *Total time* | *1:00* |")
377 (org-test-with-temp-text-in-file
378 "* Test
379 CLOCK: [2012-03-29 Thu 16:00]--[2012-03-29 Thu 17:00] => 1:00"
380 (let ((the-file (buffer-file-name)))
381 (org-test-with-temp-text-in-file ""
382 (test-org-clock-clocktable-contents
383 (format ":scope (lambda () (list %S))" the-file))))))))
385 (ert-deftest test-org-clock/clocktable/maxlevel ()
386 "Test \":maxlevel\" parameter in Clock table."
387 (should
388 (equal "| Headline | Time | |
389 |--------------+--------+------|
390 | *Total time* | *6:00* | |
391 |--------------+--------+------|
392 | Foo | 6:00 | |
393 | \\_ Bar | | 2:00 |"
394 (org-test-with-temp-text
395 "* Foo
396 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
397 ** Bar
398 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
399 (test-org-clock-clocktable-contents ":maxlevel 3"))))
400 (should
401 (equal "| Headline | Time | |
402 |--------------+--------+------|
403 | *Total time* | *6:00* | |
404 |--------------+--------+------|
405 | Foo | 6:00 | |
406 | \\_ Bar | | 2:00 |"
407 (org-test-with-temp-text
408 "* Foo
409 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
410 ** Bar
411 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
412 (test-org-clock-clocktable-contents ":maxlevel 2"))))
413 (should
414 (equal "| Headline | Time |
415 |--------------+--------|
416 | *Total time* | *6:00* |
417 |--------------+--------|
418 | Foo | 6:00 |"
419 (org-test-with-temp-text
420 "* Foo
421 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
422 ** Bar
423 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
424 (test-org-clock-clocktable-contents ":maxlevel 1"))))
425 ;; Special ":maxlevel 0" case: only report total file time.
426 (should
427 (equal "| Headline | Time |
428 |--------------+--------|
429 | *Total time* | *6:00* |
430 |--------------+--------|"
431 (org-test-with-temp-text
432 "* Foo
433 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
434 ** Bar
435 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
436 (test-org-clock-clocktable-contents ":maxlevel 0")))))
438 (ert-deftest test-org-clock/clocktable/formula ()
439 "Test \":formula\" parameter in Clock table."
440 ;; Test ":formula %". Handle various duration formats.
441 (should
442 (equal
443 "| Headline | Time | % |
444 |--------------+--------+-------|
445 | *Total time* | *6:00* | 100.0 |
446 |--------------+--------+-------|
447 | Foo | 4:00 | 66.7 |
448 | Bar | 2:00 | 33.3 |"
449 (org-test-with-temp-text
450 "* Foo
451 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
452 * Bar
453 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
454 (test-org-clock-clocktable-contents ":maxlevel 1 :formula %"))))
455 (should
456 (equal
457 "| Headline | Time | % |
458 |--------------+---------+-------|
459 | *Total time* | *28:00* | 100.0 |
460 |--------------+---------+-------|
461 | Foo | 26:00 | 92.9 |
462 | Bar | 2:00 | 7.1 |"
463 (org-test-with-temp-text
464 "* Foo
465 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
466 * Bar
467 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
468 (test-org-clock-clocktable-contents ":maxlevel 1 :formula %"))))
469 ;; Properly align column with different depths.
470 (should
471 (equal "| Headline | Time | | | % |
472 |---------------+--------+------+------+-------|
473 | *Total time* | *1:00* | | | 100.0 |
474 |---------------+--------+------+------+-------|
475 | foo | 1:00 | | | 100.0 |
476 | \\_ sub | | 0:15 | | 25.0 |
477 | \\_ sub2 | | 0:15 | | 25.0 |
478 | \\_ sub3 | | 0:30 | | 50.0 |
479 | \\_ subsub1 | | | 0:15 | 25.0 |
480 | \\_ subsub1 | | | 0:15 | 25.0 |"
481 (org-test-with-temp-text
482 "* foo
483 ** sub
484 :LOGBOOK:
485 CLOCK: [2017-03-18 Sat 15:00]--[2017-03-18 Sat 15:15] => 0:15
486 :END:
487 ** sub2
488 :LOGBOOK:
489 CLOCK: [2017-03-18 Sat 15:15]--[2017-03-18 Sat 15:30] => 0:15
490 :END:
491 ** sub3
492 *** subsub1
493 :LOGBOOK:
494 CLOCK: [2017-03-18 Sat 13:00]--[2017-03-18 Sat 13:15] => 0:15
495 :END:
496 *** subsub1
497 :LOGBOOK:
498 CLOCK: [2017-03-18 Sat 14:00]--[2017-03-18 Sat 14:15] => 0:15
499 :END:"
500 (test-org-clock-clocktable-contents ":maxlevel 3 :formula %")))))
502 (ert-deftest test-org-clock/clocktable/lang ()
503 "Test \":lang\" parameter in Clock table."
504 ;; Test foreign translation
505 (should
506 (equal
507 "| Headline | Time |
508 |--------------+---------|
509 | *Total time* | *26:00* |
510 |--------------+---------|
511 | Foo | 26:00 |"
512 (org-test-with-temp-text
513 "* Foo
514 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
515 (test-org-clock-clocktable-contents ":maxlevel 1 :lang en"))))
516 (should
517 (equal
518 "| En-tête | Durée |
519 |----------------+---------|
520 | *Durée totale* | *26:00* |
521 |----------------+---------|
522 | Foo | 26:00 |"
523 (org-test-with-temp-text
524 "* Foo
525 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
526 (test-org-clock-clocktable-contents ":maxlevel 1 :lang fr"))))
527 ;; No :lang parameter is equivalent to "en".
528 (should
529 (equal
530 (org-test-with-temp-text
531 "* Foo
532 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
533 (test-org-clock-clocktable-contents ":maxlevel 1 :lang en"))
534 (org-test-with-temp-text
535 "* Foo
536 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
537 (test-org-clock-clocktable-contents ":maxlevel 1"))))
538 ;; Unknown translation fall backs to "en".
539 (should
540 (equal
541 "| Headline | Time |
542 |--------------+---------|
543 | *Total time* | *26:00* |
544 |--------------+---------|
545 | Foo | 26:00 |"
546 (org-test-with-temp-text
547 "* Foo
548 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
549 (test-org-clock-clocktable-contents ":maxlevel 1 :lang foo")))))
551 (ert-deftest test-org-clock/clocktable/link ()
552 "Test \":link\" parameter in Clock table."
553 ;; If there is no file attached to the document, link directly to
554 ;; the headline.
555 (should
556 (equal
557 "| Headline | Time |
558 |--------------+---------|
559 | *Total time* | *26:00* |
560 |--------------+---------|
561 | [[Foo][Foo]] | 26:00 |"
562 (org-test-with-temp-text
563 "* Foo
564 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
565 (test-org-clock-clocktable-contents ":link t"))))
566 ;; Otherwise, link to the headline in the current file.
567 (should
568 (equal
569 "| Headline | Time |
570 |--------------+---------|
571 | *Total time* | *26:00* |
572 |--------------+---------|
573 | [[file:filename::Foo][Foo]] | 26:00 |"
574 (org-test-with-temp-text-in-file
575 "* Foo
576 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
577 (let ((file (buffer-file-name)))
578 (replace-regexp-in-string
579 (regexp-quote file) "filename"
580 (test-org-clock-clocktable-contents ":link t"))))))
581 ;; Ignore TODO keyword, priority cookie, COMMENT and tags in
582 ;; headline.
583 (should
584 (equal
585 "| Headline | Time |
586 |--------------+---------|
587 | *Total time* | *26:00* |
588 |--------------+---------|
589 | [[Foo][Foo]] | 26:00 |"
590 (org-test-with-temp-text
591 "* TODO Foo
592 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
593 (test-org-clock-clocktable-contents ":link t"))))
594 (should
595 (equal
596 "| Headline | Time |
597 |--------------+---------|
598 | *Total time* | *26:00* |
599 |--------------+---------|
600 | [[Foo][Foo]] | 26:00 |"
601 (org-test-with-temp-text
602 "* [#A] Foo
603 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
604 (test-org-clock-clocktable-contents ":link t"))))
605 (should
606 (equal
607 "| Headline | Time |
608 |--------------+---------|
609 | *Total time* | *26:00* |
610 |--------------+---------|
611 | [[Foo][Foo]] | 26:00 |"
612 (org-test-with-temp-text
613 "* COMMENT Foo
614 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
615 (test-org-clock-clocktable-contents ":link t"))))
616 (should
617 (equal
618 "| Headline | Time |
619 |--------------+---------|
620 | *Total time* | *26:00* |
621 |--------------+---------|
622 | [[Foo][Foo]] | 26:00 |"
623 (org-test-with-temp-text
624 "* Foo :tag:
625 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
626 (test-org-clock-clocktable-contents ":link t"))))
627 ;; Remove statistics cookie from headline description.
628 (should
629 (equal
630 "| Headline | Time |
631 |--------------+---------|
632 | *Total time* | *26:00* |
633 |--------------+---------|
634 | [[Foo][Foo]] | 26:00 |"
635 (org-test-with-temp-text
636 "* Foo [50%]
637 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
638 (test-org-clock-clocktable-contents ":link t"))))
639 (should
640 (equal
641 "| Headline | Time |
642 |--------------+---------|
643 | *Total time* | *26:00* |
644 |--------------+---------|
645 | [[Foo][Foo]] | 26:00 |"
646 (org-test-with-temp-text
647 "* Foo [1/2]
648 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
649 (test-org-clock-clocktable-contents ":link t"))))
650 ;; Replace links with their description, or turn them into plain
651 ;; links if there is no description.
652 (should
653 (equal
654 "| Headline | Time |
655 |--------------+---------|
656 | *Total time* | *26:00* |
657 |--------------+---------|
658 | [[Foo %5B%5Bhttps://orgmode.org%5D%5BOrg mode%5D%5D][Foo Org mode]] | 26:00 |"
659 (org-test-with-temp-text
660 "* Foo [[https://orgmode.org][Org mode]]
661 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
662 (test-org-clock-clocktable-contents ":link t"))))
663 (should
664 (equal
665 "| Headline | Time |
666 |-------------------------+---------|
667 | *Total time* | *26:00* |
668 |-------------------------+---------|
669 | [[Foo %5B%5Bhttps://orgmode.org%5D%5D][Foo https://orgmode.org]] | 26:00 |"
670 (org-test-with-temp-text
671 "* Foo [[https://orgmode.org]]
672 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
673 (test-org-clock-clocktable-contents ":link t")))))
675 (ert-deftest test-org-clock/clocktable/compact ()
676 "Test \":compact\" parameter in Clock table."
677 ;; With :compact, all headlines are in the same column.
678 (should
679 (equal
680 "| Headline | Time |
681 |--------------+---------|
682 | *Total time* | *26:00* |
683 |--------------+---------|
684 | Foo | 26:00 |"
685 (org-test-with-temp-text
686 "* Foo
687 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
688 (test-org-clock-clocktable-contents ":compact t"))))
689 (should
690 (equal
691 "| Headline | Time |
692 |--------------+---------|
693 | *Total time* | *52:00* |
694 |--------------+---------|
695 | Foo | 52:00 |
696 | \\_ Bar | 26:00 |"
697 (org-test-with-temp-text
698 "* Foo
699 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
700 ** Bar
701 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
702 (test-org-clock-clocktable-contents ":compact t"))))
703 ;; :maxlevel does not affect :compact parameter.
704 (should
705 (equal
706 "| Headline | Time |
707 |--------------+---------|
708 | *Total time* | *52:00* |
709 |--------------+---------|
710 | Foo | 52:00 |
711 | \\_ Bar | 26:00 |"
712 (org-test-with-temp-text
713 "* Foo
714 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
715 ** Bar
716 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
717 (test-org-clock-clocktable-contents ":compact t :maxlevel 2"))))
718 ;; :compact implies a non-nil :indent parameter.
719 (should
720 (equal
721 "| Headline | Time |
722 |--------------+---------|
723 | *Total time* | *52:00* |
724 |--------------+---------|
725 | Foo | 52:00 |
726 | \\_ Bar | 26:00 |"
727 (org-test-with-temp-text
728 "* Foo
729 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
730 ** Bar
731 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
732 (test-org-clock-clocktable-contents ":compact t :indent nil"))))
733 ;; :compact implies a nil :level parameter.
734 (should
735 (equal
736 "| Headline | Time |
737 |--------------+---------|
738 | *Total time* | *52:00* |
739 |--------------+---------|
740 | Foo | 52:00 |
741 | \\_ Bar | 26:00 |"
742 (org-test-with-temp-text
743 "* Foo
744 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
745 ** Bar
746 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
747 (test-org-clock-clocktable-contents ":compact t :level t")))))
749 (ert-deftest test-org-clock/clocktable/properties ()
750 "Test \":properties\" parameter in Clock table."
751 ;; Include a new column with list properties.
752 (should
753 (equal
754 "| A | Headline | Time |
755 |---+--------------+---------|
756 | | *Total time* | *26:00* |
757 |---+--------------+---------|
758 | 1 | Foo | 26:00 |"
759 (org-test-with-temp-text
760 "* Foo
761 :PROPERTIES:
762 :A: 1
763 :END:
764 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
765 (test-org-clock-clocktable-contents ":properties (\"A\")"))))
766 (should
767 (equal
768 "| A | Headline | Time | |
769 |---+--------------+---------+-------|
770 | | *Total time* | *52:00* | |
771 |---+--------------+---------+-------|
772 | | Foo | 52:00 | |
773 | 1 | \\_ Bar | | 26:00 |"
774 (org-test-with-temp-text
775 "* Foo
776 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
777 ** Bar
778 :PROPERTIES:
779 :A: 1
780 :END:
781 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
782 (test-org-clock-clocktable-contents ":properties (\"A\")"))))
783 ;; Handle missing properties.
784 (should
785 (equal
786 "| A | Headline | Time |
787 |---+--------------+---------|
788 | | *Total time* | *26:00* |
789 |---+--------------+---------|
790 | 1 | Foo | 26:00 |"
791 (org-test-with-temp-text
792 "* Foo
793 :PROPERTIES:
794 :A: 1
795 :END:
796 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
797 (test-org-clock-clocktable-contents ":properties (\"A\")")))))
799 (ert-deftest test-org-clock/clocktable/tcolumns ()
800 "Test \":tcolumns\" parameter in Clock table."
801 ;; When :tcolumns is smaller than the deepest headline level, lump
802 ;; lower levels in the last column.
803 (should
804 (equal
805 "| Headline | Time |
806 |--------------+---------|
807 | *Total time* | *52:00* |
808 |--------------+---------|
809 | Foo | 52:00 |
810 | \\_ Bar | 26:00 |"
811 (org-test-with-temp-text
812 "* Foo
813 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
814 ** Bar
815 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
816 (test-org-clock-clocktable-contents ":tcolumns 1"))))
817 ;; :tcolumns cannot create more columns than the deepest headline
818 ;; level.
819 (should
820 (equal
821 "| Headline | Time | |
822 |--------------+---------+-------|
823 | *Total time* | *52:00* | |
824 |--------------+---------+-------|
825 | Foo | 52:00 | |
826 | \\_ Bar | | 26:00 |"
827 (org-test-with-temp-text
828 "* Foo
829 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
830 ** Bar
831 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
832 (test-org-clock-clocktable-contents ":tcolumns 3"))))
833 ;; Pathological case: when no headline contributes to the total
834 ;; time, there is only one time column.
835 (should
836 (equal "| Headline | Time |
837 |--------------+--------|
838 | *Total time* | *0:00* |"
839 (org-test-with-temp-text
840 "* Foo
841 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 11:09] => 0:00
842 ** Bar
843 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 13:09] => 0:00"
844 (test-org-clock-clocktable-contents ":tcolumns 2")))))
846 (ert-deftest test-org-clock/clocktable/step ()
847 "Test \":step\" parameter in Clock table."
848 ;; Regression test: week crossing month boundary before :wstart
849 ;; day-of-week.
850 (should
851 (equal "
852 Weekly report starting on: [2017-09-25 Mon]
853 | Headline | Time |
854 |--------------+--------|
855 | *Total time* | *1:00* |
856 |--------------+--------|
857 | Foo | 1:00 |"
858 (org-test-with-temp-text
859 "* Foo
860 CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
861 CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00
862 CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] => 3:00"
863 (let ((system-time-locale "en_US"))
864 (test-org-clock-clocktable-contents
865 ":step week :block 2017-09 :stepskip0 t")))))
866 (should
867 (equal "
868 Weekly report starting on: [2017-10-01 Sun]
869 | Headline | Time |
870 |--------------+--------|
871 | *Total time* | *2:00* |
872 |--------------+--------|
873 | Foo | 2:00 |
875 Weekly report starting on: [2017-10-02 Mon]
876 | Headline | Time |
877 |--------------+--------|
878 | *Total time* | *7:00* |
879 |--------------+--------|
880 | Foo | 7:00 |
882 Weekly report starting on: [2017-10-09 Mon]
883 | Headline | Time |
884 |--------------+--------|
885 | *Total time* | *5:00* |
886 |--------------+--------|
887 | Foo | 5:00 |
889 (org-test-with-temp-text
890 "* Foo
891 CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
892 CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00
893 CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] => 3:00
894 CLOCK: [2017-10-08 Sun 09:00]--[2017-10-08 Sun 13:00] => 4:00
895 CLOCK: [2017-10-09 Mon 09:00]--[2017-10-09 Mon 14:00] => 5:00"
896 (let ((system-time-locale "en_US"))
897 (test-org-clock-clocktable-contents
898 ":step week :block 2017-10 :stepskip0 t")))))
899 ;; :step day
900 (should
901 (equal "
902 Daily report: [2017-10-02 Mon]
903 | Headline | Time |
904 |--------------+--------|
905 | *Total time* | *3:00* |
906 |--------------+--------|
907 | Foo | 3:00 |
909 Daily report: [2017-10-03 Tue]
910 | Headline | Time |
911 |--------------+--------|
912 | *Total time* | *0:00* |
914 Daily report: [2017-10-04 Wed]
915 | Headline | Time |
916 |--------------+--------|
917 | *Total time* | *0:00* |
919 Daily report: [2017-10-05 Thu]
920 | Headline | Time |
921 |--------------+--------|
922 | *Total time* | *0:00* |
924 Daily report: [2017-10-06 Fri]
925 | Headline | Time |
926 |--------------+--------|
927 | *Total time* | *0:00* |
929 Daily report: [2017-10-07 Sat]
930 | Headline | Time |
931 |--------------+--------|
932 | *Total time* | *0:00* |
934 Daily report: [2017-10-08 Sun]
935 | Headline | Time |
936 |--------------+--------|
937 | *Total time* | *4:00* |
938 |--------------+--------|
939 | Foo | 4:00 |"
940 (org-test-with-temp-text
941 "* Foo
942 CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
943 CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00
944 CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] => 3:00
945 CLOCK: [2017-10-08 Sun 09:00]--[2017-10-08 Sun 13:00] => 4:00
946 CLOCK: [2017-10-09 Mon 09:00]--[2017-10-09 Mon 14:00] => 5:00"
947 (let ((system-time-locale "en_US"))
948 (test-org-clock-clocktable-contents
949 ":step day :block 2017-W40")))))
950 ;; Regression test: take :tstart and :tend hours into consideration.
951 (should
952 (equal "
953 Weekly report starting on: [2017-12-25 Mon]
954 | Headline | Time |
955 |--------------+--------|
956 | *Total time* | *8:00* |
957 |--------------+--------|
958 | Foo | 8:00 |"
959 (org-test-with-temp-text
960 "* Foo
961 CLOCK: [2017-12-27 Wed 08:00]--[2017-12-27 Wed 16:00] => 8:00"
962 (let ((system-time-locale "en_US"))
963 (test-org-clock-clocktable-contents
964 (concat ":step week :tstart \"<2017-12-25 Mon>\" "
965 ":tend \"<2017-12-27 Wed 23:59>\""))))))
966 (should
967 (equal "
968 Daily report: [2017-12-27 Wed]
969 | Headline | Time |
970 |--------------+--------|
971 | *Total time* | *8:00* |
972 |--------------+--------|
973 | Foo | 8:00 |"
974 (org-test-with-temp-text
975 "* Foo
976 CLOCK: [2017-12-27 Wed 08:00]--[2017-12-27 Wed 16:00] => 8:00"
977 (let ((system-time-locale "en_US"))
978 (test-org-clock-clocktable-contents
979 (concat ":step day :tstart \"<2017-12-25 Mon>\" "
980 ":tend \"<2017-12-27 Wed 23:59>\" :stepskip0 t")))))))
982 (provide 'test-org-clock)
983 ;;; test-org-clock.el end here