Merge branch 'maint'
[org-mode.git] / testing / lisp / test-org-clock.el
blob4f73dbcf720949ae864ce336d28ba7a22aa3c740
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/tags ()
326 "Test \":tags\" parameter in Clock table."
327 ;; Test tag 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 ":tags \"tag\" :indent nil")))))
341 (ert-deftest test-org-clock/clocktable/scope ()
342 "Test \":scope\" parameter in Clock table."
343 ;; Test `file-with-archives' scope. In particular, preserve "TBLFM"
344 ;; line, and ignore "file" column.
345 (should
346 (equal
347 "| Headline | Time | |
348 |--------------+--------+-----|
349 | *Total time* | *8:40* | foo |
350 |--------------+--------+-----|
351 | Test | 8:40 | foo |
352 #+TBLFM: $3=string(\"foo\")"
353 (org-test-with-temp-text-in-file
354 "* Test
355 CLOCK: [2012-03-29 Thu 8:00]--[2012-03-29 Thu 16:40] => 8:40"
356 (test-org-clock-clocktable-contents ":scope file-with-archives"
357 "#+TBLFM: $3=string(\"foo\")"))))
358 ;; Test "function" scope.
359 (should
360 (string-match-p
361 (regexp-quote "| ALL *Total time* | *1:00* |")
362 (org-test-with-temp-text-in-file
363 "* Test
364 CLOCK: [2012-03-29 Thu 16:00]--[2012-03-29 Thu 17:00] => 1:00"
365 (let ((the-file (buffer-file-name)))
366 (org-test-with-temp-text-in-file ""
367 (test-org-clock-clocktable-contents
368 (format ":scope (lambda () (list %S))" the-file))))))))
370 (ert-deftest test-org-clock/clocktable/maxlevel ()
371 "Test \":maxlevel\" parameter in Clock table."
372 (should
373 (equal "| Headline | Time | |
374 |--------------+--------+------|
375 | *Total time* | *6:00* | |
376 |--------------+--------+------|
377 | Foo | 6:00 | |
378 | \\_ Bar | | 2:00 |"
379 (org-test-with-temp-text
380 "* Foo
381 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
382 ** Bar
383 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
384 (test-org-clock-clocktable-contents ":maxlevel 3"))))
385 (should
386 (equal "| Headline | Time | |
387 |--------------+--------+------|
388 | *Total time* | *6:00* | |
389 |--------------+--------+------|
390 | Foo | 6:00 | |
391 | \\_ Bar | | 2:00 |"
392 (org-test-with-temp-text
393 "* Foo
394 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
395 ** Bar
396 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
397 (test-org-clock-clocktable-contents ":maxlevel 2"))))
398 (should
399 (equal "| Headline | Time |
400 |--------------+--------|
401 | *Total time* | *6:00* |
402 |--------------+--------|
403 | Foo | 6:00 |"
404 (org-test-with-temp-text
405 "* Foo
406 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
407 ** Bar
408 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
409 (test-org-clock-clocktable-contents ":maxlevel 1"))))
410 ;; Special ":maxlevel 0" case: only report total file time.
411 (should
412 (equal "| Headline | Time |
413 |--------------+--------|
414 | *Total time* | *6:00* |
415 |--------------+--------|"
416 (org-test-with-temp-text
417 "* Foo
418 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
419 ** Bar
420 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
421 (test-org-clock-clocktable-contents ":maxlevel 0")))))
423 (ert-deftest test-org-clock/clocktable/formula ()
424 "Test \":formula\" parameter in Clock table."
425 ;; Test ":formula %". Handle various duration formats.
426 (should
427 (equal
428 "| Headline | Time | % |
429 |--------------+--------+-------|
430 | *Total time* | *6:00* | 100.0 |
431 |--------------+--------+-------|
432 | Foo | 4:00 | 66.7 |
433 | Bar | 2:00 | 33.3 |"
434 (org-test-with-temp-text
435 "* Foo
436 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
437 * Bar
438 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
439 (test-org-clock-clocktable-contents ":maxlevel 1 :formula %"))))
440 (should
441 (equal
442 "| Headline | Time | % |
443 |--------------+---------+-------|
444 | *Total time* | *28:00* | 100.0 |
445 |--------------+---------+-------|
446 | Foo | 26:00 | 92.9 |
447 | Bar | 2:00 | 7.1 |"
448 (org-test-with-temp-text
449 "* Foo
450 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
451 * Bar
452 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
453 (test-org-clock-clocktable-contents ":maxlevel 1 :formula %"))))
454 ;; Properly align column with different depths.
455 (should
456 (equal "| Headline | Time | | | % |
457 |---------------+--------+------+------+-------|
458 | *Total time* | *1:00* | | | 100.0 |
459 |---------------+--------+------+------+-------|
460 | foo | 1:00 | | | 100.0 |
461 | \\_ sub | | 0:15 | | 25.0 |
462 | \\_ sub2 | | 0:15 | | 25.0 |
463 | \\_ sub3 | | 0:30 | | 50.0 |
464 | \\_ subsub1 | | | 0:15 | 25.0 |
465 | \\_ subsub1 | | | 0:15 | 25.0 |"
466 (org-test-with-temp-text
467 "* foo
468 ** sub
469 :LOGBOOK:
470 CLOCK: [2017-03-18 Sat 15:00]--[2017-03-18 Sat 15:15] => 0:15
471 :END:
472 ** sub2
473 :LOGBOOK:
474 CLOCK: [2017-03-18 Sat 15:15]--[2017-03-18 Sat 15:30] => 0:15
475 :END:
476 ** sub3
477 *** subsub1
478 :LOGBOOK:
479 CLOCK: [2017-03-18 Sat 13:00]--[2017-03-18 Sat 13:15] => 0:15
480 :END:
481 *** subsub1
482 :LOGBOOK:
483 CLOCK: [2017-03-18 Sat 14:00]--[2017-03-18 Sat 14:15] => 0:15
484 :END:"
485 (test-org-clock-clocktable-contents ":maxlevel 3 :formula %")))))
487 (ert-deftest test-org-clock/clocktable/lang ()
488 "Test \":lang\" parameter in Clock table."
489 ;; Test foreign translation
490 (should
491 (equal
492 "| Headline | Time |
493 |--------------+---------|
494 | *Total time* | *26:00* |
495 |--------------+---------|
496 | Foo | 26:00 |"
497 (org-test-with-temp-text
498 "* Foo
499 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
500 (test-org-clock-clocktable-contents ":maxlevel 1 :lang en"))))
501 (should
502 (equal
503 "| En-tête | Durée |
504 |----------------+---------|
505 | *Durée totale* | *26:00* |
506 |----------------+---------|
507 | Foo | 26:00 |"
508 (org-test-with-temp-text
509 "* Foo
510 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
511 (test-org-clock-clocktable-contents ":maxlevel 1 :lang fr"))))
512 ;; No :lang parameter is equivalent to "en".
513 (should
514 (equal
515 (org-test-with-temp-text
516 "* Foo
517 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
518 (test-org-clock-clocktable-contents ":maxlevel 1 :lang en"))
519 (org-test-with-temp-text
520 "* Foo
521 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
522 (test-org-clock-clocktable-contents ":maxlevel 1"))))
523 ;; Unknown translation fall backs to "en".
524 (should
525 (equal
526 "| Headline | Time |
527 |--------------+---------|
528 | *Total time* | *26:00* |
529 |--------------+---------|
530 | Foo | 26:00 |"
531 (org-test-with-temp-text
532 "* Foo
533 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
534 (test-org-clock-clocktable-contents ":maxlevel 1 :lang foo")))))
536 (ert-deftest test-org-clock/clocktable/link ()
537 "Test \":link\" parameter in Clock table."
538 ;; If there is no file attached to the document, link directly to
539 ;; the headline.
540 (should
541 (equal
542 "| Headline | Time |
543 |--------------+---------|
544 | *Total time* | *26:00* |
545 |--------------+---------|
546 | [[Foo][Foo]] | 26:00 |"
547 (org-test-with-temp-text
548 "* Foo
549 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
550 (test-org-clock-clocktable-contents ":link t"))))
551 ;; Otherwise, link to the headline in the current file.
552 (should
553 (equal
554 "| Headline | Time |
555 |--------------+---------|
556 | *Total time* | *26:00* |
557 |--------------+---------|
558 | [[file:filename::Foo][Foo]] | 26:00 |"
559 (org-test-with-temp-text-in-file
560 "* Foo
561 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
562 (let ((file (buffer-file-name)))
563 (replace-regexp-in-string
564 (regexp-quote file) "filename"
565 (test-org-clock-clocktable-contents ":link t"))))))
566 ;; Ignore TODO keyword, priority cookie, COMMENT and tags in
567 ;; headline.
568 (should
569 (equal
570 "| Headline | Time |
571 |--------------+---------|
572 | *Total time* | *26:00* |
573 |--------------+---------|
574 | [[Foo][Foo]] | 26:00 |"
575 (org-test-with-temp-text
576 "* TODO Foo
577 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
578 (test-org-clock-clocktable-contents ":link t"))))
579 (should
580 (equal
581 "| Headline | Time |
582 |--------------+---------|
583 | *Total time* | *26:00* |
584 |--------------+---------|
585 | [[Foo][Foo]] | 26:00 |"
586 (org-test-with-temp-text
587 "* [#A] Foo
588 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
589 (test-org-clock-clocktable-contents ":link t"))))
590 (should
591 (equal
592 "| Headline | Time |
593 |--------------+---------|
594 | *Total time* | *26:00* |
595 |--------------+---------|
596 | [[Foo][Foo]] | 26:00 |"
597 (org-test-with-temp-text
598 "* COMMENT Foo
599 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
600 (test-org-clock-clocktable-contents ":link t"))))
601 (should
602 (equal
603 "| Headline | Time |
604 |--------------+---------|
605 | *Total time* | *26:00* |
606 |--------------+---------|
607 | [[Foo][Foo]] | 26:00 |"
608 (org-test-with-temp-text
609 "* Foo :tag:
610 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
611 (test-org-clock-clocktable-contents ":link t"))))
612 ;; Remove statistics cookie from headline description.
613 (should
614 (equal
615 "| Headline | Time |
616 |--------------+---------|
617 | *Total time* | *26:00* |
618 |--------------+---------|
619 | [[Foo][Foo]] | 26:00 |"
620 (org-test-with-temp-text
621 "* Foo [50%]
622 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
623 (test-org-clock-clocktable-contents ":link t"))))
624 (should
625 (equal
626 "| Headline | Time |
627 |--------------+---------|
628 | *Total time* | *26:00* |
629 |--------------+---------|
630 | [[Foo][Foo]] | 26:00 |"
631 (org-test-with-temp-text
632 "* Foo [1/2]
633 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
634 (test-org-clock-clocktable-contents ":link t"))))
635 ;; Replace links with their description, or turn them into plain
636 ;; links if there is no description.
637 (should
638 (equal
639 "| Headline | Time |
640 |--------------+---------|
641 | *Total time* | *26:00* |
642 |--------------+---------|
643 | [[Foo %5B%5Bhttp://orgmode.org%5D%5BOrg mode%5D%5D][Foo Org mode]] | 26:00 |"
644 (org-test-with-temp-text
645 "* Foo [[http://orgmode.org][Org mode]]
646 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
647 (test-org-clock-clocktable-contents ":link t"))))
648 (should
649 (equal
650 "| Headline | Time |
651 |------------------------+---------|
652 | *Total time* | *26:00* |
653 |------------------------+---------|
654 | [[Foo %5B%5Bhttp://orgmode.org%5D%5D][Foo http://orgmode.org]] | 26:00 |"
655 (org-test-with-temp-text
656 "* Foo [[http://orgmode.org]]
657 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
658 (test-org-clock-clocktable-contents ":link t")))))
660 (ert-deftest test-org-clock/clocktable/compact ()
661 "Test \":compact\" parameter in Clock table."
662 ;; With :compact, all headlines are in the same column.
663 (should
664 (equal
665 "| Headline | Time |
666 |--------------+---------|
667 | *Total time* | *26:00* |
668 |--------------+---------|
669 | Foo | 26:00 |"
670 (org-test-with-temp-text
671 "* Foo
672 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
673 (test-org-clock-clocktable-contents ":compact t"))))
674 (should
675 (equal
676 "| Headline | Time |
677 |--------------+---------|
678 | *Total time* | *52:00* |
679 |--------------+---------|
680 | Foo | 52:00 |
681 | \\_ Bar | 26:00 |"
682 (org-test-with-temp-text
683 "* Foo
684 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
685 ** Bar
686 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
687 (test-org-clock-clocktable-contents ":compact t"))))
688 ;; :maxlevel does not affect :compact parameter.
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 :maxlevel 2"))))
703 ;; :compact implies a non-nil :indent 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 :indent nil"))))
718 ;; :compact implies a nil :level 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 :level t")))))
734 (ert-deftest test-org-clock/clocktable/properties ()
735 "Test \":properties\" parameter in Clock table."
736 ;; Include a new column with list properties.
737 (should
738 (equal
739 "| A | Headline | Time |
740 |---+--------------+---------|
741 | | *Total time* | *26:00* |
742 |---+--------------+---------|
743 | 1 | Foo | 26:00 |"
744 (org-test-with-temp-text
745 "* Foo
746 :PROPERTIES:
747 :A: 1
748 :END:
749 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
750 (test-org-clock-clocktable-contents ":properties (\"A\")"))))
751 (should
752 (equal
753 "| A | Headline | Time | |
754 |---+--------------+---------+-------|
755 | | *Total time* | *52:00* | |
756 |---+--------------+---------+-------|
757 | | Foo | 52:00 | |
758 | 1 | \\_ Bar | | 26:00 |"
759 (org-test-with-temp-text
760 "* Foo
761 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
762 ** Bar
763 :PROPERTIES:
764 :A: 1
765 :END:
766 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
767 (test-org-clock-clocktable-contents ":properties (\"A\")"))))
768 ;; Handle missing properties.
769 (should
770 (equal
771 "| A | Headline | Time |
772 |---+--------------+---------|
773 | | *Total time* | *26:00* |
774 |---+--------------+---------|
775 | 1 | Foo | 26:00 |"
776 (org-test-with-temp-text
777 "* Foo
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\")")))))
784 (ert-deftest test-org-clock/clocktable/tcolumns ()
785 "Test \":tcolumns\" parameter in Clock table."
786 ;; When :tcolumns is smaller than the deepest headline level, lump
787 ;; lower levels in the last column.
788 (should
789 (equal
790 "| Headline | Time |
791 |--------------+---------|
792 | *Total time* | *52:00* |
793 |--------------+---------|
794 | Foo | 52:00 |
795 | \\_ Bar | 26:00 |"
796 (org-test-with-temp-text
797 "* Foo
798 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
799 ** Bar
800 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
801 (test-org-clock-clocktable-contents ":tcolumns 1"))))
802 ;; :tcolumns cannot create more columns than the deepest headline
803 ;; level.
804 (should
805 (equal
806 "| Headline | Time | |
807 |--------------+---------+-------|
808 | *Total time* | *52:00* | |
809 |--------------+---------+-------|
810 | Foo | 52:00 | |
811 | \\_ Bar | | 26:00 |"
812 (org-test-with-temp-text
813 "* Foo
814 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
815 ** Bar
816 CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
817 (test-org-clock-clocktable-contents ":tcolumns 3"))))
818 ;; Pathological case: when no headline contributes to the total
819 ;; time, there is only one time column.
820 (should
821 (equal "| Headline | Time |
822 |--------------+--------|
823 | *Total time* | *0:00* |"
824 (org-test-with-temp-text
825 "* Foo
826 CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 11:09] => 0:00
827 ** Bar
828 CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 13:09] => 0:00"
829 (test-org-clock-clocktable-contents ":tcolumns 2")))))
831 (ert-deftest test-org-clock/clocktable/step ()
832 "Test \":step\" parameter in Clock table."
833 ;; Regression test: week crossing month boundary before :wstart
834 ;; day-of-week.
835 (should
836 (equal "
837 Weekly report starting on: [2017-09-25 Mon]
838 | Headline | Time |
839 |--------------+--------|
840 | *Total time* | *1:00* |
841 |--------------+--------|
842 | Foo | 1:00 |"
843 (org-test-with-temp-text
844 "* Foo
845 CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
846 CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00
847 CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] => 3:00"
848 (let ((system-time-locale "en_US"))
849 (test-org-clock-clocktable-contents
850 ":step week :block 2017-09 :stepskip0 t")))))
851 (should
852 (equal "
853 Weekly report starting on: [2017-10-01 Sun]
854 | Headline | Time |
855 |--------------+--------|
856 | *Total time* | *2:00* |
857 |--------------+--------|
858 | Foo | 2:00 |
860 Weekly report starting on: [2017-10-02 Mon]
861 | Headline | Time |
862 |--------------+--------|
863 | *Total time* | *7:00* |
864 |--------------+--------|
865 | Foo | 7:00 |
867 Weekly report starting on: [2017-10-09 Mon]
868 | Headline | Time |
869 |--------------+--------|
870 | *Total time* | *5:00* |
871 |--------------+--------|
872 | Foo | 5:00 |
874 (org-test-with-temp-text
875 "* Foo
876 CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
877 CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00
878 CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] => 3:00
879 CLOCK: [2017-10-08 Sun 09:00]--[2017-10-08 Sun 13:00] => 4:00
880 CLOCK: [2017-10-09 Mon 09:00]--[2017-10-09 Mon 14:00] => 5:00"
881 (let ((system-time-locale "en_US"))
882 (test-org-clock-clocktable-contents
883 ":step week :block 2017-10 :stepskip0 t")))))
884 ;; :step day
885 (should
886 (equal "
887 Daily report: [2017-10-02 Mon]
888 | Headline | Time |
889 |--------------+--------|
890 | *Total time* | *3:00* |
891 |--------------+--------|
892 | Foo | 3:00 |
894 Daily report: [2017-10-03 Tue]
895 | Headline | Time |
896 |--------------+--------|
897 | *Total time* | *0:00* |
899 Daily report: [2017-10-04 Wed]
900 | Headline | Time |
901 |--------------+--------|
902 | *Total time* | *0:00* |
904 Daily report: [2017-10-05 Thu]
905 | Headline | Time |
906 |--------------+--------|
907 | *Total time* | *0:00* |
909 Daily report: [2017-10-06 Fri]
910 | Headline | Time |
911 |--------------+--------|
912 | *Total time* | *0:00* |
914 Daily report: [2017-10-07 Sat]
915 | Headline | Time |
916 |--------------+--------|
917 | *Total time* | *0:00* |
919 Daily report: [2017-10-08 Sun]
920 | Headline | Time |
921 |--------------+--------|
922 | *Total time* | *4:00* |
923 |--------------+--------|
924 | Foo | 4:00 |"
925 (org-test-with-temp-text
926 "* Foo
927 CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
928 CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00
929 CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] => 3:00
930 CLOCK: [2017-10-08 Sun 09:00]--[2017-10-08 Sun 13:00] => 4:00
931 CLOCK: [2017-10-09 Mon 09:00]--[2017-10-09 Mon 14:00] => 5:00"
932 (let ((system-time-locale "en_US"))
933 (test-org-clock-clocktable-contents
934 ":step day :block 2017-W40"))))))
936 (provide 'test-org-clock)
937 ;;; test-org-clock.el end here