org-list: Modify M-RET on a description tag
[org-mode.git] / testing / lisp / test-org-timer.el
blob7164a5d1156f59baaf15fc4e70b6f190ae486c07
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))))))
183 (ert-deftest test-org-timer/pause-timer ()
184 "Test pausing relative and countdown timers."
185 ;; Pause relative timer.
186 (should
187 (equal "0:03:26"
188 (test-org-timer/with-temp-text ""
189 (test-org-timer/with-current-time test-org-timer/time0
190 (org-timer-start))
191 (test-org-timer/with-current-time test-org-timer/time1
192 (org-timer-pause-or-continue))
193 (org-timer)
194 (org-trim (buffer-string)))))
195 ;; Pause then continue relative timer.
196 (should
197 (equal "0:08:21"
198 (test-org-timer/with-temp-text ""
199 (test-org-timer/with-current-time test-org-timer/time0
200 (org-timer-start))
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
206 (org-timer))
207 (org-trim (buffer-string)))))
208 ;; Pause then continue countdown timer.
209 (should
210 (equal "0:01:39"
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
219 (org-timer))
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
227 (org-timer-start))
228 (test-org-timer/with-current-time test-org-timer/time1
229 (org-timer-stop))
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
234 (org-timer-start))
235 (test-org-timer/with-current-time test-org-timer/time1
236 (org-timer-pause-or-continue)
237 (org-timer-stop))
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
245 (org-timer-stop))
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)
254 (org-timer-stop))
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.
262 (should-error
263 (test-org-timer/with-temp-text ""
264 (org-timer-start)
265 (org-timer-set-timer 10))
266 :type (list 'error 'user-error))
267 ;; Countdown timer is running.
268 (should-error
269 (test-org-timer/with-temp-text ""
270 (org-timer-set-timer 10)
271 (org-timer-start))
272 :type (list 'error 'user-error)))
274 (provide 'test-org-timer)
275 ;;; test-org-timer.el end here