Merge branch 'maint'
[org-mode.git] / testing / lisp / test-org-clock.el
blob335b7a69b20e2dc6288857bf91f517c1fe8920f2
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"))))
85 ;;; Clocktable
87 (ert-deftest test-org-clock/clocktable ()
88 "Test clocktable specifications."
89 ;; Relative time: Previous two days.
90 (should
91 (equal
92 "| Headline | Time | |
93 |------------------------------+--------+------|
94 | *Total time* | *8:00* | |
95 |------------------------------+--------+------|
96 | Relative times in clocktable | 8:00 | |
97 | Foo | | 8:00 |
99 (org-test-with-temp-text
100 "* Relative times in clocktable\n** Foo\n<point>"
101 (insert (org-test-clock-create-clock "-3d 8:00" "-3d 12:00"))
102 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
103 (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
104 (test-org-clock-clocktable-contents-at-point
105 ":tstart \"<-2d>\" :tend \"<today>\" :indent nil"))))
106 ;; Relative time: Yesterday until now.
107 (should
108 (equal
109 "| Headline | Time | |
110 |------------------------------+--------+------|
111 | *Total time* | *6:00* | |
112 |------------------------------+--------+------|
113 | Relative times in clocktable | 6:00 | |
114 | Foo | | 6:00 |
116 (org-test-with-temp-text
117 "* Relative times in clocktable\n** Foo\n<point>"
118 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
119 (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
120 (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
121 (test-org-clock-clocktable-contents-at-point
122 ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil"))))
123 ;; Test `untilnow' block.
124 (should
125 (equal
126 "| Headline | Time | |
127 |------------------------------+--------+------|
128 | *Total time* | *6:00* | |
129 |------------------------------+--------+------|
130 | Relative times in clocktable | 6:00 | |
131 | Foo | | 6:00 |
133 (org-test-with-temp-text
134 "* Relative times in clocktable\n** Foo\n<point>"
135 (insert (org-test-clock-create-clock "-10y 15:00" "-10y 18:00"))
136 (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
137 (test-org-clock-clocktable-contents-at-point
138 ":block untilnow :indent nil")))))
140 (provide 'test-org-clock)
141 ;;; test-org-clock.el end here