Fix opening custom ID links with percent escaped syntax
[org-mode/org-tableheadings.git] / testing / lisp / test-org-timer.el
blobd66621009f02eb387af3534948cfa5aa6825a981
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 (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'."
29 (declare (indent 1))
30 `(cl-letf (((symbol-function 'message) (lambda (&rest args) nil)))
31 (org-test-with-temp-text ,text
32 (let (org-timer-start-time
33 org-timer-pause-time
34 org-timer-countdown-timer
35 org-timer-display)
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."
42 (declare (indent 1))
43 `(cl-letf (((symbol-function 'current-time) (lambda () ,time)))
44 ,@body))
47 ;;; Time conversion and formatting
49 (ert-deftest test-org-timer/secs-to-hms ()
50 "Test conversion between HMS format and seconds."
51 ;; Seconds to HMS, and back again
52 (should
53 (equal "0:00:30"
54 (org-timer-secs-to-hms 30)))
55 (should
56 (equal 30
57 (org-timer-hms-to-secs (org-timer-secs-to-hms 30))))
58 ;; Minutes to HMS, and back again
59 (should
60 (equal "0:02:10"
61 (org-timer-secs-to-hms 130)))
62 (should
63 (equal 130
64 (org-timer-hms-to-secs (org-timer-secs-to-hms 130))))
65 ;; Hours to HMS, and back again
66 (should
67 (equal "1:01:30"
68 (org-timer-secs-to-hms 3690)))
69 (should
70 (equal 3690
71 (org-timer-hms-to-secs (org-timer-secs-to-hms 3690))))
72 ;; Negative seconds to HMS, and back again
73 (should
74 (equal "-1:01:30"
75 (org-timer-secs-to-hms -3690)))
76 (should
77 (equal -3690
78 (org-timer-hms-to-secs (org-timer-secs-to-hms -3690)))))
80 (ert-deftest test-org-timer/fix-incomplete ()
81 "Test conversion to complete HMS format."
82 ;; No fix is needed.
83 (should
84 (equal "1:02:03"
85 (org-timer-fix-incomplete "1:02:03")))
86 ;; Hour is missing.
87 (should
88 (equal "0:02:03"
89 (org-timer-fix-incomplete "02:03")))
90 ;; Minute is missing.
91 (should
92 (equal "0:00:03"
93 (org-timer-fix-incomplete "03"))))
95 (ert-deftest test-org-timer/change-times ()
96 "Test changing HMS format by offset."
97 ;; Add time.
98 (should
99 (equal "
100 1:31:15
101 4:00:55"
102 (org-test-with-temp-text "
103 0:00:25
104 2:30:05"
105 (org-timer-change-times-in-region (point-min) (point-max)
106 "1:30:50")
107 (buffer-string))))
108 ;; Subtract time.
109 (should
110 (equal "
111 -1:30:25
112 0:59:15"
113 (org-test-with-temp-text "
114 0:00:25
115 2:30:05"
116 (org-timer-change-times-in-region (point-min) (point-max)
117 "-1:30:50")
118 (buffer-string)))))
121 ;;; Timers
123 ;; Dummy times for overriding `current-time'
124 (defvar test-org-timer/time0 '(21635 62793 797149 675000))
125 ;; Add 3 minutes and 26 seconds.
126 (defvar test-org-timer/time1
127 (time-add test-org-timer/time0 (seconds-to-time 206)))
128 ;; Add 2 minutes and 41 seconds (6 minutes and 7 seconds total).
129 (defvar test-org-timer/time2
130 (time-add test-org-timer/time1 (seconds-to-time 161)))
131 ;; Add 4 minutes and 55 seconds (11 minutes and 2 seconds total).
132 (defvar test-org-timer/time3
133 (time-add test-org-timer/time2 (seconds-to-time 295)))
135 (ert-deftest test-org-timer/start-relative ()
136 "Test starting relative timer."
137 ;; Insert plain timer string, starting with `org-timer-start'.
138 (should
139 (equal "0:03:26"
140 (test-org-timer/with-temp-text ""
141 (test-org-timer/with-current-time test-org-timer/time0
142 (org-timer-start))
143 (test-org-timer/with-current-time test-org-timer/time1
144 (org-timer))
145 (org-trim (buffer-string)))))
146 ;; Insert item timer string.
147 (should
148 (equal "- 0:03:26 ::"
149 (test-org-timer/with-temp-text ""
150 (test-org-timer/with-current-time test-org-timer/time0
151 (org-timer-start))
152 (test-org-timer/with-current-time test-org-timer/time1
153 (org-timer-item))
154 (org-trim (buffer-string)))))
155 ;; Start with `org-timer'.
156 (should
157 (equal "0:00:00 0:03:26"
158 (test-org-timer/with-temp-text ""
159 (test-org-timer/with-current-time test-org-timer/time0
160 (org-timer))
161 (test-org-timer/with-current-time test-org-timer/time1
162 (org-timer))
163 (org-trim (buffer-string)))))
164 ;; Restart with `org-timer'.
165 (should
166 (equal "0:00:00"
167 (test-org-timer/with-temp-text ""
168 (test-org-timer/with-current-time test-org-timer/time0
169 (org-timer-start))
170 (test-org-timer/with-current-time test-org-timer/time1
171 (org-timer '(4)))
172 (org-trim (buffer-string))))))
174 (ert-deftest test-org-timer/set-timer ()
175 "Test setting countdown timer."
176 (should
177 (equal "0:06:34"
178 (test-org-timer/with-temp-text ""
179 (test-org-timer/with-current-time test-org-timer/time0
180 (org-timer-set-timer 10))
181 (test-org-timer/with-current-time test-org-timer/time1
182 (org-timer))
183 (org-trim (buffer-string)))))
184 (should
185 (equal "0:00:04"
186 (test-org-timer/with-temp-text ""
187 (test-org-timer/with-current-time test-org-timer/time0
188 (org-timer-set-timer "3:30"))
189 (test-org-timer/with-current-time test-org-timer/time1
190 (org-timer))
191 (org-trim (buffer-string))))))
193 (ert-deftest test-org-timer/pause-timer ()
194 "Test pausing relative and countdown timers."
195 ;; Pause relative timer.
196 (should
197 (equal "0:03:26"
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 (org-timer)
204 (org-trim (buffer-string)))))
205 ;; Pause then continue relative timer.
206 (should
207 (equal "0:08:21"
208 (test-org-timer/with-temp-text ""
209 (test-org-timer/with-current-time test-org-timer/time0
210 (org-timer-start))
211 (test-org-timer/with-current-time test-org-timer/time1
212 (org-timer-pause-or-continue))
213 (test-org-timer/with-current-time test-org-timer/time2
214 (org-timer-pause-or-continue))
215 (test-org-timer/with-current-time test-org-timer/time3
216 (org-timer))
217 (org-trim (buffer-string)))))
218 ;; Pause then continue countdown timer.
219 (should
220 (equal "0:01:39"
221 (test-org-timer/with-temp-text ""
222 (test-org-timer/with-current-time test-org-timer/time0
223 (org-timer-set-timer 10))
224 (test-org-timer/with-current-time test-org-timer/time1
225 (org-timer-pause-or-continue))
226 (test-org-timer/with-current-time test-org-timer/time2
227 (org-timer-pause-or-continue))
228 (test-org-timer/with-current-time test-org-timer/time3
229 (org-timer))
230 (org-trim (buffer-string))))))
232 (ert-deftest test-org-timer/stop ()
233 "Test stopping relative and countdown timers."
234 ;; Stop running relative timer.
235 (test-org-timer/with-temp-text ""
236 (test-org-timer/with-current-time test-org-timer/time0
237 (org-timer-start))
238 (test-org-timer/with-current-time test-org-timer/time1
239 (org-timer-stop))
240 (should-not org-timer-start-time))
241 ;; Stop paused relative timer.
242 (test-org-timer/with-temp-text ""
243 (test-org-timer/with-current-time test-org-timer/time0
244 (org-timer-start))
245 (test-org-timer/with-current-time test-org-timer/time1
246 (org-timer-pause-or-continue)
247 (org-timer-stop))
248 (should-not org-timer-start-time)
249 (should-not org-timer-pause-time))
250 ;; Stop running countdown timer.
251 (test-org-timer/with-temp-text ""
252 (test-org-timer/with-current-time test-org-timer/time0
253 (org-timer-set-timer 10))
254 (test-org-timer/with-current-time test-org-timer/time1
255 (org-timer-stop))
256 (should-not org-timer-start-time)
257 (should-not org-timer-countdown-timer))
258 ;; Stop paused countdown timer.
259 (test-org-timer/with-temp-text ""
260 (test-org-timer/with-current-time test-org-timer/time0
261 (org-timer-set-timer 10))
262 (test-org-timer/with-current-time test-org-timer/time1
263 (org-timer-pause-or-continue)
264 (org-timer-stop))
265 (should-not org-timer-start-time)
266 (should-not org-timer-pause-time)
267 (should-not org-timer-countdown-timer)))
269 (ert-deftest test-org-timer/other-timer-error ()
270 "Test for error when other timer running."
271 ;; Relative timer is running.
272 (should-error
273 (test-org-timer/with-temp-text ""
274 (org-timer-start)
275 (org-timer-set-timer 10))
276 :type (list 'error 'user-error))
277 ;; Countdown timer is running.
278 (should-error
279 (test-org-timer/with-temp-text ""
280 (org-timer-set-timer 10)
281 (org-timer-start))
282 :type (list 'error 'user-error)))
284 (ert-deftest test-org-timer/set-timer-from-effort-prop ()
285 "Test timer setting from effort property."
286 (should
287 (< (* 60 9) ; 9m
288 (test-org-timer/with-temp-text
289 "* foo
290 :PROPERTIES:
291 :Effort: 10
292 :END:"
293 (org-mode)
294 (org-timer-set-timer)
295 (org-timer-hms-to-secs (org-timer nil t)))
296 (1+ (* 60 10)) ; 10m 1s
300 (provide 'test-org-timer)
301 ;;; test-org-timer.el end here