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/>.
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'."
28 `(letf (((symbol-function 'message
) (lambda (&rest args
) nil
)))
29 (org-test-with-temp-text ,text
30 (let (org-timer-start-time
32 org-timer-countdown-timer
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."
41 `(letf (((symbol-function 'current-time
) (lambda () ,time
)))
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
52 (org-timer-secs-to-hms 30)))
55 (org-timer-hms-to-secs (org-timer-secs-to-hms 30))))
56 ;; Minutes to HMS, and back again
59 (org-timer-secs-to-hms 130)))
62 (org-timer-hms-to-secs (org-timer-secs-to-hms 130))))
63 ;; Hours to HMS, and back again
66 (org-timer-secs-to-hms 3690)))
69 (org-timer-hms-to-secs (org-timer-secs-to-hms 3690))))
70 ;; Negative seconds to HMS, and back again
73 (org-timer-secs-to-hms -
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."
83 (org-timer-fix-incomplete "1:02:03")))
87 (org-timer-fix-incomplete "02:03")))
91 (org-timer-fix-incomplete "03"))))
93 (ert-deftest test-org-timer
/change-times
()
94 "Test changing HMS format by offset."
100 (org-test-with-temp-text "
103 (org-timer-change-times-in-region (point-min) (point-max)
111 (org-test-with-temp-text "
114 (org-timer-change-times-in-region (point-min) (point-max)
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'.
138 (test-org-timer/with-temp-text
""
139 (test-org-timer/with-current-time test-org-timer
/time0
141 (test-org-timer/with-current-time test-org-timer
/time1
143 (org-trim (buffer-string)))))
144 ;; Insert item timer string.
146 (equal "- 0:03:26 ::"
147 (test-org-timer/with-temp-text
""
148 (test-org-timer/with-current-time test-org-timer
/time0
150 (test-org-timer/with-current-time test-org-timer
/time1
152 (org-trim (buffer-string)))))
153 ;; Start with `org-timer'.
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
159 (test-org-timer/with-current-time test-org-timer
/time1
161 (org-trim (buffer-string)))))
162 ;; Restart with `org-timer'.
165 (test-org-timer/with-temp-text
""
166 (test-org-timer/with-current-time test-org-timer
/time0
168 (test-org-timer/with-current-time test-org-timer
/time1
170 (org-trim (buffer-string))))))
172 (ert-deftest test-org-timer
/set-timer
()
173 "Test setting countdown timer."
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
181 (org-trim (buffer-string)))))
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
189 (org-trim (buffer-string))))))
191 (ert-deftest test-org-timer
/pause-timer
()
192 "Test pausing relative and countdown timers."
193 ;; Pause relative timer.
196 (test-org-timer/with-temp-text
""
197 (test-org-timer/with-current-time test-org-timer
/time0
199 (test-org-timer/with-current-time test-org-timer
/time1
200 (org-timer-pause-or-continue))
202 (org-trim (buffer-string)))))
203 ;; Pause then continue relative timer.
206 (test-org-timer/with-temp-text
""
207 (test-org-timer/with-current-time test-org-timer
/time0
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
215 (org-trim (buffer-string)))))
216 ;; Pause then continue countdown timer.
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
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
236 (test-org-timer/with-current-time test-org-timer
/time1
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
243 (test-org-timer/with-current-time test-org-timer
/time1
244 (org-timer-pause-or-continue)
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
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)
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.
271 (test-org-timer/with-temp-text
""
273 (org-timer-set-timer 10))
274 :type
(list 'error
'user-error
))
275 ;; Countdown timer is running.
277 (test-org-timer/with-temp-text
""
278 (org-timer-set-timer 10)
280 :type
(list 'error
'user-error
)))
282 (ert-deftest test-org-timer
/set-timer-from-effort-prop
()
283 "Test timer setting from effort property."
286 (test-org-timer/with-temp-text
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