1 ;;; test-org-clock.el --- Tests for org-clock.el
3 ;; Copyright (C) 2012 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")
67 (insert "#+END: clocktable\n"))
73 (when (looking-at "#\\+CAPTION:") (forward-line))
74 (buffer-substring (point)
75 (progn (search-forward "#+END: clocktable")
76 (match-beginning 0))))
78 (delete-region (point)
79 (progn (search-forward "#+END: clocktable")
87 (ert-deftest test-org-clock
/clocktable
()
88 "Test clocktable specifications."
89 ;; Relative time: Previous two days.
92 "| Headline | Time | |
93 |------------------------------+---------+-------|
94 | *Total time* | *16:00* | |
95 |------------------------------+---------+-------|
96 | Relative times in clocktable | 16:00 | |
100 (org-test-with-temp-text "* Relative times in clocktable\n** Foo\n** Bar\n"
102 ;; Install Clock lines in "Foo".
103 (search-forward "** Foo")
105 (insert (org-test-clock-create-clock "-2d 8:00" "-2d 13:00"))
106 (insert (org-test-clock-create-clock ". 8:00" "13:00"))
107 ;; Install Clock lines in "Bar".
108 (search-forward "** Bar")
110 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
111 (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
112 (insert (org-test-clock-create-clock "-1d 15:00" "-1d 18:00"))
113 (insert (org-test-clock-create-clock ". 15:00"))
114 ;; Previous two days.
115 (goto-char (point-min))
117 (test-org-clock-clocktable-contents-at-point
118 ":tstart \"<today-2>\" :tend \"<today>\" :indent nil")))))
119 ;; Relative time: Yesterday until now.
122 "| Headline | Time | |
123 |------------------------------+---------+------|
124 | *Total time* | *13:00* | |
125 |------------------------------+---------+------|
126 | Relative times in clocktable | 13:00 | |
130 (org-test-with-temp-text "* Relative times in clocktable\n** Foo\n** Bar\n"
132 ;; Install Clock lines in "Foo".
133 (search-forward "** Foo")
135 (insert (org-test-clock-create-clock "-2d 8:00" "-2d 13:00"))
136 (insert (org-test-clock-create-clock ". 8:00" "13:00"))
137 ;; Install Clock lines in "Bar".
138 (search-forward "** Bar")
140 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
141 (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
142 (insert (org-test-clock-create-clock "-1d 15:00" "-1d 18:00"))
143 (insert (org-test-clock-create-clock ". 15:00"))
144 ;; Previous two days.
145 (goto-char (point-min))
147 (test-org-clock-clocktable-contents-at-point
148 ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil"))))))
151 (provide 'test-org-clock
)
152 ;;; test-org-clock.el end here