emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob 930e548a635bc1c86654d54836ceba576f593c5d 9612 bytes (raw)
name: testing/lisp/test-org-timer.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
 
;;; test-org-timer.el --- Tests for org-timer.el  -*- lexical-binding: t; -*-

;; Copyright (C) 2014-2015, 2019  Kyle Meyer

;; Author: Kyle Meyer <kyle@kyleam.com>

;; This file is not part of GNU Emacs.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(eval-and-compile (require 'cl-lib))

(defmacro test-org-timer/with-temp-text (text &rest body)
  "Like `org-test-with-temp-text', but set timer-specific variables.
Also, mute output from `message'."
  (declare (indent 1))
  `(cl-letf (((symbol-function 'message) (lambda (&rest args) nil)))
     (org-test-with-temp-text ,text
       (let (org-timer-start-time
	     org-timer-pause-time
	     org-timer-countdown-timer
	     org-timer-display)
	 (unwind-protect (progn ,@body)
	   (when (timerp org-timer-countdown-timer)
	     (cancel-timer org-timer-countdown-timer)))))))

(defmacro test-org-timer/with-current-time (time &rest body)
  "Run BODY, setting `current-time' output to TIME."
  (declare (indent 1))
  `(org-test-at-time ,time ,@body))

\f
;;; Time conversion and formatting

(ert-deftest test-org-timer/secs-to-hms ()
  "Test conversion between HMS format and seconds."
  ;; Seconds to HMS, and back again
  (should
   (equal "0:00:30"
	  (org-timer-secs-to-hms 30)))
  (should
   (equal 30
	  (org-timer-hms-to-secs (org-timer-secs-to-hms 30))))
  ;; Minutes to HMS, and back again
  (should
   (equal "0:02:10"
	  (org-timer-secs-to-hms 130)))
  (should
   (equal 130
	  (org-timer-hms-to-secs (org-timer-secs-to-hms 130))))
  ;; Hours to HMS, and back again
  (should
   (equal "1:01:30"
	  (org-timer-secs-to-hms 3690)))
  (should
   (equal 3690
	  (org-timer-hms-to-secs (org-timer-secs-to-hms 3690))))
  ;; Negative seconds to HMS, and back again
  (should
   (equal "-1:01:30"
	  (org-timer-secs-to-hms -3690)))
  (should
   (equal -3690
	  (org-timer-hms-to-secs (org-timer-secs-to-hms -3690)))))

(ert-deftest test-org-timer/fix-incomplete ()
  "Test conversion to complete HMS format."
  ;; No fix is needed.
  (should
   (equal "1:02:03"
	  (org-timer-fix-incomplete "1:02:03")))
  ;; Hour is missing.
  (should
   (equal "0:02:03"
	  (org-timer-fix-incomplete "02:03")))
  ;; Minute is missing.
  (should
   (equal "0:00:03"
	  (org-timer-fix-incomplete "03"))))

(ert-deftest test-org-timer/change-times ()
  "Test changing HMS format by offset."
  ;; Add time.
  (should
   (equal "
1:31:15
4:00:55"
	  (org-test-with-temp-text "
0:00:25
2:30:05"
	    (org-timer-change-times-in-region (point-min) (point-max)
					      "1:30:50")
	    (buffer-string))))
  ;; Subtract time.
  (should
   (equal "
-1:30:25
0:59:15"
	  (org-test-with-temp-text "
0:00:25
2:30:05"
	    (org-timer-change-times-in-region (point-min) (point-max)
					      "-1:30:50")
	    (buffer-string)))))

\f
;;; Timers

;; Dummy times for overriding `current-time'
(defvar test-org-timer/time0 '(21635 62793 797149 675000))
;; Add 3 minutes and 26 seconds.
(defvar test-org-timer/time1
  (time-add test-org-timer/time0 (seconds-to-time 206)))
;; Add 2 minutes and 41 seconds (6 minutes and 7 seconds total).
(defvar test-org-timer/time2
  (time-add test-org-timer/time1 (seconds-to-time 161)))
;; Add 4 minutes and 55 seconds (11 minutes and 2 seconds total).
(defvar test-org-timer/time3
  (time-add test-org-timer/time2 (seconds-to-time 295)))

(ert-deftest test-org-timer/start-relative ()
  "Test starting relative timer."
  ;; Insert plain timer string, starting with `org-timer-start'.
  (should
   (equal "0:03:26"
	  (test-org-timer/with-temp-text ""
	    (test-org-timer/with-current-time test-org-timer/time0
	      (org-timer-start))
	    (test-org-timer/with-current-time test-org-timer/time1
	      (org-timer))
	    (org-trim (buffer-string)))))
  ;; Insert item timer string.
  (should
   (equal "- 0:03:26 ::"
	  (test-org-timer/with-temp-text ""
	    (test-org-timer/with-current-time test-org-timer/time0
	      (org-timer-start))
	    (test-org-timer/with-current-time test-org-timer/time1
	      (org-timer-item))
	    (org-trim (buffer-string)))))
  ;; Start with `org-timer'.
  (should
   (equal "0:00:00 0:03:26"
	  (test-org-timer/with-temp-text ""
	    (test-org-timer/with-current-time test-org-timer/time0
	      (org-timer))
	    (test-org-timer/with-current-time test-org-timer/time1
	      (org-timer))
	    (org-trim (buffer-string)))))
  ;; Restart with `org-timer'.
  (should
   (equal "0:00:00"
	  (test-org-timer/with-temp-text ""
	    (test-org-timer/with-current-time test-org-timer/time0
	      (org-timer-start))
	    (test-org-timer/with-current-time test-org-timer/time1
	      (org-timer '(4)))
	    (org-trim (buffer-string))))))

(ert-deftest test-org-timer/set-timer ()
  "Test setting countdown timer."
  (should
   (equal "0:06:34"
	  (test-org-timer/with-temp-text ""
	    (test-org-timer/with-current-time test-org-timer/time0
	      (org-timer-set-timer 10))
	    (test-org-timer/with-current-time test-org-timer/time1
	      (org-timer))
	    (org-trim (buffer-string)))))
  (should
   (equal "0:00:04"
	  (test-org-timer/with-temp-text ""
	    (test-org-timer/with-current-time test-org-timer/time0
	      (org-timer-set-timer "3:30"))
	    (test-org-timer/with-current-time test-org-timer/time1
	      (org-timer))
	    (org-trim (buffer-string))))))

(ert-deftest test-org-timer/pause-timer ()
  "Test pausing relative and countdown timers."
  ;; Pause relative timer.
  (should
   (equal "0:03:26"
	  (test-org-timer/with-temp-text ""
	    (test-org-timer/with-current-time test-org-timer/time0
	      (org-timer-start))
	    (test-org-timer/with-current-time test-org-timer/time1
	      (org-timer-pause-or-continue))
	    (org-timer)
	    (org-trim (buffer-string)))))
  ;; Pause then continue relative timer.
  (should
   (equal "0:08:21"
	  (test-org-timer/with-temp-text ""
	    (test-org-timer/with-current-time test-org-timer/time0
	      (org-timer-start))
	    (test-org-timer/with-current-time test-org-timer/time1
	      (org-timer-pause-or-continue))
	    (test-org-timer/with-current-time test-org-timer/time2
	      (org-timer-pause-or-continue))
	    (test-org-timer/with-current-time test-org-timer/time3
	      (org-timer))
	    (org-trim (buffer-string)))))
  ;; Pause then continue countdown timer.
  (should
   (equal "0:01:39"
	  (test-org-timer/with-temp-text ""
	    (test-org-timer/with-current-time test-org-timer/time0
	      (org-timer-set-timer 10))
	    (test-org-timer/with-current-time test-org-timer/time1
	      (org-timer-pause-or-continue))
	    (test-org-timer/with-current-time test-org-timer/time2
	      (org-timer-pause-or-continue))
	    (test-org-timer/with-current-time test-org-timer/time3
	      (org-timer))
	    (org-trim (buffer-string))))))

(ert-deftest test-org-timer/stop ()
  "Test stopping relative and countdown timers."
  ;; Stop running relative timer.
  (test-org-timer/with-temp-text ""
    (test-org-timer/with-current-time test-org-timer/time0
      (org-timer-start))
    (test-org-timer/with-current-time test-org-timer/time1
      (org-timer-stop))
    (should-not org-timer-start-time))
  ;; Stop paused relative timer.
  (test-org-timer/with-temp-text ""
    (test-org-timer/with-current-time test-org-timer/time0
      (org-timer-start))
    (test-org-timer/with-current-time test-org-timer/time1
      (org-timer-pause-or-continue)
      (org-timer-stop))
    (should-not org-timer-start-time)
    (should-not org-timer-pause-time))
  ;; Stop running countdown timer.
  (test-org-timer/with-temp-text ""
    (test-org-timer/with-current-time test-org-timer/time0
      (org-timer-set-timer 10))
    (test-org-timer/with-current-time test-org-timer/time1
      (org-timer-stop))
    (should-not org-timer-start-time)
    (should-not org-timer-countdown-timer))
  ;; Stop paused countdown timer.
  (test-org-timer/with-temp-text ""
    (test-org-timer/with-current-time test-org-timer/time0
      (org-timer-set-timer 10))
    (test-org-timer/with-current-time test-org-timer/time1
      (org-timer-pause-or-continue)
      (org-timer-stop))
    (should-not org-timer-start-time)
    (should-not org-timer-pause-time)
    (should-not org-timer-countdown-timer)))

(ert-deftest test-org-timer/other-timer-error ()
  "Test for error when other timer running."
  ;; Relative timer is running.
  (should-error
   (test-org-timer/with-temp-text ""
     (org-timer-start)
     (org-timer-set-timer 10))
   :type (list 'error 'user-error))
  ;; Countdown timer is running.
  (should-error
   (test-org-timer/with-temp-text ""
     (org-timer-set-timer 10)
     (org-timer-start))
   :type (list 'error 'user-error)))

(ert-deftest test-org-timer/set-timer-from-effort-prop ()
  "Test timer setting from effort property."
  (should
   (< (* 60 9) 				; 9m
      (test-org-timer/with-temp-text
       "* foo
:PROPERTIES:
:Effort:   10
:END:"
       (org-mode)
       (org-timer-set-timer)
       (org-timer-hms-to-secs (org-timer nil t)))
      (1+ (* 60 10))			; 10m 1s
      )))

\f
(provide 'test-org-timer)
;;; test-org-timer.el end here

debug log:

solving 930e548a63 ...
found 930e548a63 in https://list.orgmode.org/orgmode/jwvfsgw6syx.fsf-monnier+emacs@gnu.org/
found 5d938f22ec in https://git.savannah.gnu.org/cgit/emacs/org-mode.git
preparing index
index prepared:
100644 5d938f22ec3250a91aff65dc52ae25e95a8694fb	testing/lisp/test-org-timer.el

applying [1/1] https://list.orgmode.org/orgmode/jwvfsgw6syx.fsf-monnier+emacs@gnu.org/
diff --git a/testing/lisp/test-org-timer.el b/testing/lisp/test-org-timer.el
index 5d938f22ec..930e548a63 100644

Checking patch testing/lisp/test-org-timer.el...
Applied patch testing/lisp/test-org-timer.el cleanly.

index at:
100644 930e548a635bc1c86654d54836ceba576f593c5d	testing/lisp/test-org-timer.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).