Fix typo.
[org-mode.git] / testing / lisp / test-org-clock.el
blob2dc2150dd003e63f96a4bf9a83bde75b44b60a6b
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
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 (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* | *16:00* | |
95 |------------------------------+---------+-------|
96 | Relative times in clocktable | 16:00 | |
97 | Foo | | 5:00 |
98 | Bar | | 11:00 |
100 (org-test-with-temp-text "* Relative times in clocktable\n** Foo\n** Bar\n"
101 (progn
102 ;; Install Clock lines in "Foo".
103 (search-forward "** Foo")
104 (forward-line)
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")
109 (forward-line)
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))
116 (forward-line)
117 (test-org-clock-clocktable-contents-at-point
118 ":tstart \"<today-2>\" :tend \"<today>\" :indent nil")))))
119 ;; Relative time: Yesterday until now.
120 (should
121 (equal
122 "| Headline | Time | |
123 |------------------------------+---------+------|
124 | *Total time* | *13:00* | |
125 |------------------------------+---------+------|
126 | Relative times in clocktable | 13:00 | |
127 | Foo | | 5:00 |
128 | Bar | | 8:00 |
130 (org-test-with-temp-text "* Relative times in clocktable\n** Foo\n** Bar\n"
131 (progn
132 ;; Install Clock lines in "Foo".
133 (search-forward "** Foo")
134 (forward-line)
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")
139 (forward-line)
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))
146 (forward-line)
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