test-org-clocktable: fix batch testing
[org-mode.git] / testing / lisp / test-org-clock.el
blob7af004691b502c06cc65253f5a2af418a37b25ad
1 ;;; test-org-clock.el --- Tests for org-clock.el
3 ;; Copyright (C) 2012, 2014 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: clocktable\n"))
68 (unwind-protect
69 (save-excursion
70 (org-update-dblock)
71 (forward-line)
72 ;; Skip caption.
73 (when (looking-at "#\\+CAPTION:") (forward-line))
74 (buffer-substring (point)
75 (progn (search-forward "#+END: clocktable")
76 (match-beginning 0))))
77 ;; Remove clocktable.
78 (delete-region (point)
79 (progn (search-forward "#+END: clocktable")
80 (forward-line)
81 (point)))))
85 ;;; Clocktable
87 (defun test-org-clock/clocktable (string)
88 (let ((org-clock-total-time-cell-format "*%s*"))
89 ;; Install Clock lines in "Foo".
90 (search-forward "** Foo")
91 (forward-line)
92 (insert (org-test-clock-create-clock "-2d 8:00" "-2d 13:00"))
93 (insert (org-test-clock-create-clock ". 8:00" "13:00"))
94 ;; Install Clock lines in "Bar".
95 (search-forward "** Bar")
96 (forward-line)
97 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
98 (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
99 (insert (org-test-clock-create-clock "-1d 15:00" "-1d 18:00"))
100 (insert (org-test-clock-create-clock ". 15:00"))
101 ;; Previous two days.
102 (goto-char (point-min))
103 (forward-line)
104 (test-org-clock-clocktable-contents-at-point string)))
105 (ert-deftest test-org-clock/clocktable1 ()
106 "Test clocktable specifications."
107 ;; Relative time: Previous two days.
108 (should
109 (equal
110 "| Headline | Time | |
111 |------------------------------+---------+-------|
112 | *Total time* | *16:00* | |
113 |------------------------------+---------+-------|
114 | Relative times in clocktable | 16:00 | |
115 | Foo | | 5:00 |
116 | Bar | | 11:00 |
118 (org-test-with-temp-text
119 "* Relative times in clocktable\n** Foo\n** Bar\n"
120 (test-org-clock/clocktable ":tstart \"<today-2>\" :tend \"<today>\" :indent nil")))))
121 (ert-deftest test-org-clock/clocktable2 ()
122 "Test clocktable specifications."
123 ;; Relative time: Yesterday until now.
124 (should
125 (equal
126 "| Headline | Time | |
127 |------------------------------+---------+------|
128 | *Total time* | *13:00* | |
129 |------------------------------+---------+------|
130 | Relative times in clocktable | 13:00 | |
131 | Foo | | 5:00 |
132 | Bar | | 8:00 |
134 (org-test-with-temp-text
135 "* Relative times in clocktable\n** Foo\n** Bar\n"
136 (test-org-clock/clocktable ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil")))))
138 (provide 'test-org-clock)
139 ;;; test-org-clock.el end here