1 ;;; test-org-timer.el --- Tests for org-timer.el
3 ;; Copyright (C) 2014-2015, 2019 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 (eval-and-compile (require 'cl-lib
))
26 (defmacro test-org-timer
/with-temp-text
(text &rest body
)
27 "Like `org-test-with-temp-text', but set timer-specific variables.
28 Also, mute output from `message'."
30 `(cl-letf (((symbol-function 'message
) (lambda (&rest args
) nil
)))
31 (org-test-with-temp-text ,text
32 (let (org-timer-start-time
34 org-timer-countdown-timer
36 (unwind-protect (progn ,@body
)
37 (when (timerp org-timer-countdown-timer
)
38 (cancel-timer org-timer-countdown-timer
)))))))
40 (defmacro test-org-timer
/with-current-time
(time &rest body
)
41 "Run BODY, setting `current-time' output to TIME."
43 `(org-test-at-time ,time
,@body
))
46 ;;; Time conversion and formatting
48 (ert-deftest test-org-timer
/secs-to-hms
()
49 "Test conversion between HMS format and seconds."
50 ;; Seconds to HMS, and back again
53 (org-timer-secs-to-hms 30)))
56 (org-timer-hms-to-secs (org-timer-secs-to-hms 30))))
57 ;; Minutes to HMS, and back again
60 (org-timer-secs-to-hms 130)))
63 (org-timer-hms-to-secs (org-timer-secs-to-hms 130))))
64 ;; Hours to HMS, and back again
67 (org-timer-secs-to-hms 3690)))
70 (org-timer-hms-to-secs (org-timer-secs-to-hms 3690))))
71 ;; Negative seconds to HMS, and back again
74 (org-timer-secs-to-hms -
3690)))
77 (org-timer-hms-to-secs (org-timer-secs-to-hms -
3690)))))
79 (ert-deftest test-org-timer
/fix-incomplete
()
80 "Test conversion to complete HMS format."
84 (org-timer-fix-incomplete "1:02:03")))
88 (org-timer-fix-incomplete "02:03")))
92 (org-timer-fix-incomplete "03"))))
94 (ert-deftest test-org-timer
/change-times
()
95 "Test changing HMS format by offset."
101 (org-test-with-temp-text "
104 (org-timer-change-times-in-region (point-min) (point-max)
112 (org-test-with-temp-text "
115 (org-timer-change-times-in-region (point-min) (point-max)
122 ;; Dummy times for overriding `current-time'
123 (defvar test-org-timer
/time0
'(21635 62793 797149 675000))
124 ;; Add 3 minutes and 26 seconds.
125 (defvar test-org-timer
/time1
126 (time-add test-org-timer
/time0
(seconds-to-time 206)))
127 ;; Add 2 minutes and 41 seconds (6 minutes and 7 seconds total).
128 (defvar test-org-timer
/time2
129 (time-add test-org-timer
/time1
(seconds-to-time 161)))
130 ;; Add 4 minutes and 55 seconds (11 minutes and 2 seconds total).
131 (defvar test-org-timer
/time3
132 (time-add test-org-timer
/time2
(seconds-to-time 295)))
134 (ert-deftest test-org-timer
/start-relative
()
135 "Test starting relative timer."
136 ;; Insert plain timer string, starting with `org-timer-start'.
139 (test-org-timer/with-temp-text
""
140 (test-org-timer/with-current-time test-org-timer
/time0
142 (test-org-timer/with-current-time test-org-timer
/time1
144 (org-trim (buffer-string)))))
145 ;; Insert item timer string.
147 (equal "- 0:03:26 ::"
148 (test-org-timer/with-temp-text
""
149 (test-org-timer/with-current-time test-org-timer
/time0
151 (test-org-timer/with-current-time test-org-timer
/time1
153 (org-trim (buffer-string)))))
154 ;; Start with `org-timer'.
156 (equal "0:00:00 0:03:26"
157 (test-org-timer/with-temp-text
""
158 (test-org-timer/with-current-time test-org-timer
/time0
160 (test-org-timer/with-current-time test-org-timer
/time1
162 (org-trim (buffer-string)))))
163 ;; Restart with `org-timer'.
166 (test-org-timer/with-temp-text
""
167 (test-org-timer/with-current-time test-org-timer
/time0
169 (test-org-timer/with-current-time test-org-timer
/time1
171 (org-trim (buffer-string))))))
173 (ert-deftest test-org-timer
/set-timer
()
174 "Test setting countdown timer."
177 (test-org-timer/with-temp-text
""
178 (test-org-timer/with-current-time test-org-timer
/time0
179 (org-timer-set-timer 10))
180 (test-org-timer/with-current-time test-org-timer
/time1
182 (org-trim (buffer-string)))))
185 (test-org-timer/with-temp-text
""
186 (test-org-timer/with-current-time test-org-timer
/time0
187 (org-timer-set-timer "3:30"))
188 (test-org-timer/with-current-time test-org-timer
/time1
190 (org-trim (buffer-string))))))
192 (ert-deftest test-org-timer
/pause-timer
()
193 "Test pausing relative and countdown timers."
194 ;; Pause relative timer.
197 (test-org-timer/with-temp-text
""
198 (test-org-timer/with-current-time test-org-timer
/time0
200 (test-org-timer/with-current-time test-org-timer
/time1
201 (org-timer-pause-or-continue))
203 (org-trim (buffer-string)))))
204 ;; Pause then continue relative timer.
207 (test-org-timer/with-temp-text
""
208 (test-org-timer/with-current-time test-org-timer
/time0
210 (test-org-timer/with-current-time test-org-timer
/time1
211 (org-timer-pause-or-continue))
212 (test-org-timer/with-current-time test-org-timer
/time2
213 (org-timer-pause-or-continue))
214 (test-org-timer/with-current-time test-org-timer
/time3
216 (org-trim (buffer-string)))))
217 ;; Pause then continue countdown timer.
220 (test-org-timer/with-temp-text
""
221 (test-org-timer/with-current-time test-org-timer
/time0
222 (org-timer-set-timer 10))
223 (test-org-timer/with-current-time test-org-timer
/time1
224 (org-timer-pause-or-continue))
225 (test-org-timer/with-current-time test-org-timer
/time2
226 (org-timer-pause-or-continue))
227 (test-org-timer/with-current-time test-org-timer
/time3
229 (org-trim (buffer-string))))))
231 (ert-deftest test-org-timer
/stop
()
232 "Test stopping relative and countdown timers."
233 ;; Stop running relative timer.
234 (test-org-timer/with-temp-text
""
235 (test-org-timer/with-current-time test-org-timer
/time0
237 (test-org-timer/with-current-time test-org-timer
/time1
239 (should-not org-timer-start-time
))
240 ;; Stop paused relative timer.
241 (test-org-timer/with-temp-text
""
242 (test-org-timer/with-current-time test-org-timer
/time0
244 (test-org-timer/with-current-time test-org-timer
/time1
245 (org-timer-pause-or-continue)
247 (should-not org-timer-start-time
)
248 (should-not org-timer-pause-time
))
249 ;; Stop running countdown timer.
250 (test-org-timer/with-temp-text
""
251 (test-org-timer/with-current-time test-org-timer
/time0
252 (org-timer-set-timer 10))
253 (test-org-timer/with-current-time test-org-timer
/time1
255 (should-not org-timer-start-time
)
256 (should-not org-timer-countdown-timer
))
257 ;; Stop paused countdown timer.
258 (test-org-timer/with-temp-text
""
259 (test-org-timer/with-current-time test-org-timer
/time0
260 (org-timer-set-timer 10))
261 (test-org-timer/with-current-time test-org-timer
/time1
262 (org-timer-pause-or-continue)
264 (should-not org-timer-start-time
)
265 (should-not org-timer-pause-time
)
266 (should-not org-timer-countdown-timer
)))
268 (ert-deftest test-org-timer
/other-timer-error
()
269 "Test for error when other timer running."
270 ;; Relative timer is running.
272 (test-org-timer/with-temp-text
""
274 (org-timer-set-timer 10))
275 :type
(list 'error
'user-error
))
276 ;; Countdown timer is running.
278 (test-org-timer/with-temp-text
""
279 (org-timer-set-timer 10)
281 :type
(list 'error
'user-error
)))
283 (ert-deftest test-org-timer
/set-timer-from-effort-prop
()
284 "Test timer setting from effort property."
287 (test-org-timer/with-temp-text
293 (org-timer-set-timer)
294 (org-timer-hms-to-secs (org-timer nil t
)))
295 (1+ (* 60 10)) ; 10m 1s
299 (provide 'test-org-timer
)
300 ;;; test-org-timer.el end here