org.texi: Delete misplaced colon
[org-mode.git] / testing / lisp / test-org-timer.el
blob9c0aba91da7bb371d4f15c1cf9f659590b472108
1 ;;; test-org-timer.el --- Tests for org-timer.el
3 ;; Copyright (C) 2014-2015 Kyle Meyer
5 ;; Author: Kyle Meyer <kyle@kyleam.com>
7 ;; This file is not part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Code:
24 (defmacro test-org-timer/with-temp-text (text &rest body)
25 "Like `org-test-with-temp-text', but set timer-specific variables.
26 Also, mute output from `message'."
27 (declare (indent 1))
28 `(letf (((symbol-function 'message) (lambda (&rest args) nil)))
29 (org-test-with-temp-text ,text
30 (let (org-timer-start-time
31 org-timer-pause-time
32 org-timer-countdown-timer
33 org-timer-display)
34 (unwind-protect (progn ,@body)
35 (when (timerp org-timer-countdown-timer)
36 (cancel-timer org-timer-countdown-timer)))))))
38 (defmacro test-org-timer/with-current-time (time &rest body)
39 "Run BODY, setting `current-time' output to TIME."
40 (declare (indent 1))
41 `(letf (((symbol-function 'current-time) (lambda () ,time)))
42 ,@body))
45 ;;; Time conversion and formatting
47 (ert-deftest test-org-timer/secs-to-hms ()
48 "Test conversion between HMS format and seconds."
49 ;; Seconds to HMS, and back again
50 (should
51 (equal "0:00:30"
52 (org-timer-secs-to-hms 30)))
53 (should
54 (equal 30
55 (org-timer-hms-to-secs (org-timer-secs-to-hms 30))))
56 ;; Minutes to HMS, and back again
57 (should
58 (equal "0:02:10"
59 (org-timer-secs-to-hms 130)))
60 (should
61 (equal 130
62 (org-timer-hms-to-secs (org-timer-secs-to-hms 130))))
63 ;; Hours to HMS, and back again
64 (should
65 (equal "1:01:30"
66 (org-timer-secs-to-hms 3690)))
67 (should
68 (equal 3690
69 (org-timer-hms-to-secs (org-timer-secs-to-hms 3690))))
70 ;; Negative seconds to HMS, and back again
71 (should
72 (equal "-1:01:30"
73 (org-timer-secs-to-hms -3690)))
74 (should
75 (equal -3690
76 (org-timer-hms-to-secs (org-timer-secs-to-hms -3690)))))
78 (ert-deftest test-org-timer/fix-incomplete ()
79 "Test conversion to complete HMS format."
80 ;; No fix is needed.
81 (should
82 (equal "1:02:03"
83 (org-timer-fix-incomplete "1:02:03")))
84 ;; Hour is missing.
85 (should
86 (equal "0:02:03"
87 (org-timer-fix-incomplete "02:03")))
88 ;; Minute is missing.
89 (should
90 (equal "0:00:03"
91 (org-timer-fix-incomplete "03"))))
93 (ert-deftest test-org-timer/change-times ()
94 "Test changing HMS format by offset."
95 ;; Add time.
96 (should
97 (equal "
98 1:31:15
99 4:00:55"
100 (org-test-with-temp-text "
101 0:00:25
102 2:30:05"
103 (org-timer-change-times-in-region (point-min) (point-max)
104 "1:30:50")
105 (buffer-string))))
106 ;; Subtract time.
107 (should
108 (equal "
109 -1:30:25
110 0:59:15"
111 (org-test-with-temp-text "
112 0:00:25
113 2:30:05"
114 (org-timer-change-times-in-region (point-min) (point-max)
115 "-1:30:50")
116 (buffer-string)))))
119 ;;; Timers
121 ;; Dummy times for overriding `current-time'
122 (defvar test-org-timer/time0 '(21635 62793 797149 675000))
123 ;; Add 3 minutes and 26 seconds.
124 (defvar test-org-timer/time1
125 (time-add test-org-timer/time0 (seconds-to-time 206)))
126 ;; Add 2 minutes and 41 seconds (6 minutes and 7 seconds total).
127 (defvar test-org-timer/time2
128 (time-add test-org-timer/time1 (seconds-to-time 161)))
129 ;; Add 4 minutes and 55 seconds (11 minutes and 2 seconds total).
130 (defvar test-org-timer/time3
131 (time-add test-org-timer/time2 (seconds-to-time 295)))
133 (ert-deftest test-org-timer/start-relative ()
134 "Test starting relative timer."
135 ;; Insert plain timer string, starting with `org-timer-start'.
136 (should
137 (equal "0:03:26"
138 (test-org-timer/with-temp-text ""
139 (test-org-timer/with-current-time test-org-timer/time0
140 (org-timer-start))
141 (test-org-timer/with-current-time test-org-timer/time1
142 (org-timer))
143 (org-trim (buffer-string)))))
144 ;; Insert item timer string.
145 (should
146 (equal "- 0:03:26 ::"
147 (test-org-timer/with-temp-text ""
148 (test-org-timer/with-current-time test-org-timer/time0
149 (org-timer-start))
150 (test-org-timer/with-current-time test-org-timer/time1
151 (org-timer-item))
152 (org-trim (buffer-string)))))
153 ;; Start with `org-timer'.
154 (should
155 (equal "0:00:00 0:03:26"
156 (test-org-timer/with-temp-text ""
157 (test-org-timer/with-current-time test-org-timer/time0
158 (org-timer))
159 (test-org-timer/with-current-time test-org-timer/time1
160 (org-timer))
161 (org-trim (buffer-string)))))
162 ;; Restart with `org-timer'.
163 (should
164 (equal "0:00:00"
165 (test-org-timer/with-temp-text ""
166 (test-org-timer/with-current-time test-org-timer/time0
167 (org-timer-start))
168 (test-org-timer/with-current-time test-org-timer/time1
169 (org-timer '(4)))
170 (org-trim (buffer-string))))))
172 (ert-deftest test-org-timer/set-timer ()
173 "Test setting countdown timer."
174 (should
175 (equal "0:06:34"
176 (test-org-timer/with-temp-text ""
177 (test-org-timer/with-current-time test-org-timer/time0
178 (org-timer-set-timer 10))
179 (test-org-timer/with-current-time test-org-timer/time1
180 (org-timer))
181 (org-trim (buffer-string)))))
182 (should
183 (equal "0:00:04"
184 (test-org-timer/with-temp-text ""
185 (test-org-timer/with-current-time test-org-timer/time0
186 (org-timer-set-timer "3:30"))
187 (test-org-timer/with-current-time test-org-timer/time1
188 (org-timer))
189 (org-trim (buffer-string))))))
191 (ert-deftest test-org-timer/pause-timer ()
192 "Test pausing relative and countdown timers."
193 ;; Pause relative timer.
194 (should
195 (equal "0:03:26"
196 (test-org-timer/with-temp-text ""
197 (test-org-timer/with-current-time test-org-timer/time0
198 (org-timer-start))
199 (test-org-timer/with-current-time test-org-timer/time1
200 (org-timer-pause-or-continue))
201 (org-timer)
202 (org-trim (buffer-string)))))
203 ;; Pause then continue relative timer.
204 (should
205 (equal "0:08:21"
206 (test-org-timer/with-temp-text ""
207 (test-org-timer/with-current-time test-org-timer/time0
208 (org-timer-start))
209 (test-org-timer/with-current-time test-org-timer/time1
210 (org-timer-pause-or-continue))
211 (test-org-timer/with-current-time test-org-timer/time2
212 (org-timer-pause-or-continue))
213 (test-org-timer/with-current-time test-org-timer/time3
214 (org-timer))
215 (org-trim (buffer-string)))))
216 ;; Pause then continue countdown timer.
217 (should
218 (equal "0:01:39"
219 (test-org-timer/with-temp-text ""
220 (test-org-timer/with-current-time test-org-timer/time0
221 (org-timer-set-timer 10))
222 (test-org-timer/with-current-time test-org-timer/time1
223 (org-timer-pause-or-continue))
224 (test-org-timer/with-current-time test-org-timer/time2
225 (org-timer-pause-or-continue))
226 (test-org-timer/with-current-time test-org-timer/time3
227 (org-timer))
228 (org-trim (buffer-string))))))
230 (ert-deftest test-org-timer/stop ()
231 "Test stopping relative and countdown timers."
232 ;; Stop running relative timer.
233 (test-org-timer/with-temp-text ""
234 (test-org-timer/with-current-time test-org-timer/time0
235 (org-timer-start))
236 (test-org-timer/with-current-time test-org-timer/time1
237 (org-timer-stop))
238 (should-not org-timer-start-time))
239 ;; Stop paused relative timer.
240 (test-org-timer/with-temp-text ""
241 (test-org-timer/with-current-time test-org-timer/time0
242 (org-timer-start))
243 (test-org-timer/with-current-time test-org-timer/time1
244 (org-timer-pause-or-continue)
245 (org-timer-stop))
246 (should-not org-timer-start-time)
247 (should-not org-timer-pause-time))
248 ;; Stop running countdown timer.
249 (test-org-timer/with-temp-text ""
250 (test-org-timer/with-current-time test-org-timer/time0
251 (org-timer-set-timer 10))
252 (test-org-timer/with-current-time test-org-timer/time1
253 (org-timer-stop))
254 (should-not org-timer-start-time)
255 (should-not org-timer-countdown-timer))
256 ;; Stop paused countdown timer.
257 (test-org-timer/with-temp-text ""
258 (test-org-timer/with-current-time test-org-timer/time0
259 (org-timer-set-timer 10))
260 (test-org-timer/with-current-time test-org-timer/time1
261 (org-timer-pause-or-continue)
262 (org-timer-stop))
263 (should-not org-timer-start-time)
264 (should-not org-timer-pause-time)
265 (should-not org-timer-countdown-timer)))
267 (ert-deftest test-org-timer/other-timer-error ()
268 "Test for error when other timer running."
269 ;; Relative timer is running.
270 (should-error
271 (test-org-timer/with-temp-text ""
272 (org-timer-start)
273 (org-timer-set-timer 10))
274 :type (list 'error 'user-error))
275 ;; Countdown timer is running.
276 (should-error
277 (test-org-timer/with-temp-text ""
278 (org-timer-set-timer 10)
279 (org-timer-start))
280 :type (list 'error 'user-error)))
282 (ert-deftest test-org-timer/set-timer-from-effort-prop ()
283 "Test timer setting from effort property."
284 (should
285 (< (* 60 9) ; 9m
286 (test-org-timer/with-temp-text
287 "* foo
288 :PROPERTIES:
289 :Effort: 10
290 :END:"
291 (org-mode)
292 (org-timer-set-timer)
293 (org-timer-hms-to-secs (org-timer nil t)))
294 (1+ (* 60 10)) ; 10m 1s
298 (provide 'test-org-timer)
299 ;;; test-org-timer.el end here