lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / testing / lisp / test-org-timer.el
blob27156dfa979089cd459981ff30df78604540a8e2
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/>.
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 `(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
51 (should
52 (equal "0:00:30"
53 (org-timer-secs-to-hms 30)))
54 (should
55 (equal 30
56 (org-timer-hms-to-secs (org-timer-secs-to-hms 30))))
57 ;; Minutes to HMS, and back again
58 (should
59 (equal "0:02:10"
60 (org-timer-secs-to-hms 130)))
61 (should
62 (equal 130
63 (org-timer-hms-to-secs (org-timer-secs-to-hms 130))))
64 ;; Hours to HMS, and back again
65 (should
66 (equal "1:01:30"
67 (org-timer-secs-to-hms 3690)))
68 (should
69 (equal 3690
70 (org-timer-hms-to-secs (org-timer-secs-to-hms 3690))))
71 ;; Negative seconds to HMS, and back again
72 (should
73 (equal "-1:01:30"
74 (org-timer-secs-to-hms -3690)))
75 (should
76 (equal -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."
81 ;; No fix is needed.
82 (should
83 (equal "1:02:03"
84 (org-timer-fix-incomplete "1:02:03")))
85 ;; Hour is missing.
86 (should
87 (equal "0:02:03"
88 (org-timer-fix-incomplete "02:03")))
89 ;; Minute is missing.
90 (should
91 (equal "0:00:03"
92 (org-timer-fix-incomplete "03"))))
94 (ert-deftest test-org-timer/change-times ()
95 "Test changing HMS format by offset."
96 ;; Add time.
97 (should
98 (equal "
99 1:31:15
100 4:00:55"
101 (org-test-with-temp-text "
102 0:00:25
103 2:30:05"
104 (org-timer-change-times-in-region (point-min) (point-max)
105 "1:30:50")
106 (buffer-string))))
107 ;; Subtract time.
108 (should
109 (equal "
110 -1:30:25
111 0:59:15"
112 (org-test-with-temp-text "
113 0:00:25
114 2:30:05"
115 (org-timer-change-times-in-region (point-min) (point-max)
116 "-1:30:50")
117 (buffer-string)))))
120 ;;; Timers
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'.
137 (should
138 (equal "0:03:26"
139 (test-org-timer/with-temp-text ""
140 (test-org-timer/with-current-time test-org-timer/time0
141 (org-timer-start))
142 (test-org-timer/with-current-time test-org-timer/time1
143 (org-timer))
144 (org-trim (buffer-string)))))
145 ;; Insert item timer string.
146 (should
147 (equal "- 0:03:26 ::"
148 (test-org-timer/with-temp-text ""
149 (test-org-timer/with-current-time test-org-timer/time0
150 (org-timer-start))
151 (test-org-timer/with-current-time test-org-timer/time1
152 (org-timer-item))
153 (org-trim (buffer-string)))))
154 ;; Start with `org-timer'.
155 (should
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
159 (org-timer))
160 (test-org-timer/with-current-time test-org-timer/time1
161 (org-timer))
162 (org-trim (buffer-string)))))
163 ;; Restart with `org-timer'.
164 (should
165 (equal "0:00:00"
166 (test-org-timer/with-temp-text ""
167 (test-org-timer/with-current-time test-org-timer/time0
168 (org-timer-start))
169 (test-org-timer/with-current-time test-org-timer/time1
170 (org-timer '(4)))
171 (org-trim (buffer-string))))))
173 (ert-deftest test-org-timer/set-timer ()
174 "Test setting countdown timer."
175 (should
176 (equal "0:06:34"
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
181 (org-timer))
182 (org-trim (buffer-string)))))
183 (should
184 (equal "0:00:04"
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
189 (org-timer))
190 (org-trim (buffer-string))))))
192 (ert-deftest test-org-timer/pause-timer ()
193 "Test pausing relative and countdown timers."
194 ;; Pause relative timer.
195 (should
196 (equal "0:03:26"
197 (test-org-timer/with-temp-text ""
198 (test-org-timer/with-current-time test-org-timer/time0
199 (org-timer-start))
200 (test-org-timer/with-current-time test-org-timer/time1
201 (org-timer-pause-or-continue))
202 (org-timer)
203 (org-trim (buffer-string)))))
204 ;; Pause then continue relative timer.
205 (should
206 (equal "0:08:21"
207 (test-org-timer/with-temp-text ""
208 (test-org-timer/with-current-time test-org-timer/time0
209 (org-timer-start))
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
215 (org-timer))
216 (org-trim (buffer-string)))))
217 ;; Pause then continue countdown timer.
218 (should
219 (equal "0:01:39"
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
228 (org-timer))
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
236 (org-timer-start))
237 (test-org-timer/with-current-time test-org-timer/time1
238 (org-timer-stop))
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
243 (org-timer-start))
244 (test-org-timer/with-current-time test-org-timer/time1
245 (org-timer-pause-or-continue)
246 (org-timer-stop))
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
254 (org-timer-stop))
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)
263 (org-timer-stop))
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.
271 (should-error
272 (test-org-timer/with-temp-text ""
273 (org-timer-start)
274 (org-timer-set-timer 10))
275 :type (list 'error 'user-error))
276 ;; Countdown timer is running.
277 (should-error
278 (test-org-timer/with-temp-text ""
279 (org-timer-set-timer 10)
280 (org-timer-start))
281 :type (list 'error 'user-error)))
283 (ert-deftest test-org-timer/set-timer-from-effort-prop ()
284 "Test timer setting from effort property."
285 (should
286 (< (* 60 9) ; 9m
287 (test-org-timer/with-temp-text
288 "* foo
289 :PROPERTIES:
290 :Effort: 10
291 :END:"
292 (org-mode)
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