1 ;;; test-org-timer.el --- Tests for org-timer.el
3 ;; Copyright (C) 2014 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))))))
183 (ert-deftest test-org-timer
/pause-timer
()
184 "Test pausing relative and countdown timers."
185 ;; Pause relative timer.
188 (test-org-timer/with-temp-text
""
189 (test-org-timer/with-current-time test-org-timer
/time0
191 (test-org-timer/with-current-time test-org-timer
/time1
192 (org-timer-pause-or-continue))
194 (org-trim (buffer-string)))))
195 ;; Pause then continue relative timer.
198 (test-org-timer/with-temp-text
""
199 (test-org-timer/with-current-time test-org-timer
/time0
201 (test-org-timer/with-current-time test-org-timer
/time1
202 (org-timer-pause-or-continue))
203 (test-org-timer/with-current-time test-org-timer
/time2
204 (org-timer-pause-or-continue))
205 (test-org-timer/with-current-time test-org-timer
/time3
207 (org-trim (buffer-string)))))
208 ;; Pause then continue countdown timer.
211 (test-org-timer/with-temp-text
""
212 (test-org-timer/with-current-time test-org-timer
/time0
213 (org-timer-set-timer 10))
214 (test-org-timer/with-current-time test-org-timer
/time1
215 (org-timer-pause-or-continue))
216 (test-org-timer/with-current-time test-org-timer
/time2
217 (org-timer-pause-or-continue))
218 (test-org-timer/with-current-time test-org-timer
/time3
220 (org-trim (buffer-string))))))
222 (ert-deftest test-org-timer
/stop
()
223 "Test stopping relative and countdown timers."
224 ;; Stop running relative timer.
225 (test-org-timer/with-temp-text
""
226 (test-org-timer/with-current-time test-org-timer
/time0
228 (test-org-timer/with-current-time test-org-timer
/time1
230 (should-not org-timer-start-time
))
231 ;; Stop paused relative timer.
232 (test-org-timer/with-temp-text
""
233 (test-org-timer/with-current-time test-org-timer
/time0
235 (test-org-timer/with-current-time test-org-timer
/time1
236 (org-timer-pause-or-continue)
238 (should-not org-timer-start-time
)
239 (should-not org-timer-pause-time
))
240 ;; Stop running countdown timer.
241 (test-org-timer/with-temp-text
""
242 (test-org-timer/with-current-time test-org-timer
/time0
243 (org-timer-set-timer 10))
244 (test-org-timer/with-current-time test-org-timer
/time1
246 (should-not org-timer-start-time
)
247 (should-not org-timer-countdown-timer
))
248 ;; Stop paused 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-pause-or-continue)
255 (should-not org-timer-start-time
)
256 (should-not org-timer-pause-time
)
257 (should-not org-timer-countdown-timer
)))
259 (ert-deftest test-org-timer
/other-timer-error
()
260 "Test for error when other timer running."
261 ;; Relative timer is running.
263 (test-org-timer/with-temp-text
""
265 (org-timer-set-timer 10))
266 :type
(list 'error
'user-error
))
267 ;; Countdown timer is running.
269 (test-org-timer/with-temp-text
""
270 (org-timer-set-timer 10)
272 :type
(list 'error
'user-error
)))
274 (provide 'test-org-timer
)
275 ;;; test-org-timer.el end here