test-org-timer.el 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. ;;; test-org-timer.el --- Tests for org-timer.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2014-2015, 2019 Kyle Meyer
  3. ;; Author: Kyle Meyer <kyle@kyleam.com>
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (eval-and-compile (require 'cl-lib))
  17. (require 'org-timer)
  18. (defmacro test-org-timer/with-temp-text (text &rest body)
  19. "Like `org-test-with-temp-text', but set timer-specific variables.
  20. Also, mute output from `message'."
  21. (declare (indent 1))
  22. `(cl-letf (((symbol-function 'message) (lambda (&rest _args) nil)))
  23. (org-test-with-temp-text ,text
  24. (let (org-timer-start-time
  25. org-timer-pause-time
  26. org-timer-countdown-timer
  27. org-timer-display)
  28. (unwind-protect (progn ,@body)
  29. (when (timerp org-timer-countdown-timer)
  30. (cancel-timer org-timer-countdown-timer)))))))
  31. (defmacro test-org-timer/with-current-time (time &rest body)
  32. "Run BODY, setting `current-time' output to TIME."
  33. (declare (indent 1))
  34. `(org-test-at-time ,time ,@body))
  35. ;;; Time conversion and formatting
  36. (ert-deftest test-org-timer/secs-to-hms ()
  37. "Test conversion between HMS format and seconds."
  38. ;; Seconds to HMS, and back again
  39. (should
  40. (equal "0:00:30"
  41. (org-timer-secs-to-hms 30)))
  42. (should
  43. (equal 30
  44. (org-timer-hms-to-secs (org-timer-secs-to-hms 30))))
  45. ;; Minutes to HMS, and back again
  46. (should
  47. (equal "0:02:10"
  48. (org-timer-secs-to-hms 130)))
  49. (should
  50. (equal 130
  51. (org-timer-hms-to-secs (org-timer-secs-to-hms 130))))
  52. ;; Hours to HMS, and back again
  53. (should
  54. (equal "1:01:30"
  55. (org-timer-secs-to-hms 3690)))
  56. (should
  57. (equal 3690
  58. (org-timer-hms-to-secs (org-timer-secs-to-hms 3690))))
  59. ;; Negative seconds to HMS, and back again
  60. (should
  61. (equal "-1:01:30"
  62. (org-timer-secs-to-hms -3690)))
  63. (should
  64. (equal -3690
  65. (org-timer-hms-to-secs (org-timer-secs-to-hms -3690)))))
  66. (ert-deftest test-org-timer/fix-incomplete ()
  67. "Test conversion to complete HMS format."
  68. ;; No fix is needed.
  69. (should
  70. (equal "1:02:03"
  71. (org-timer-fix-incomplete "1:02:03")))
  72. ;; Hour is missing.
  73. (should
  74. (equal "0:02:03"
  75. (org-timer-fix-incomplete "02:03")))
  76. ;; Minute is missing.
  77. (should
  78. (equal "0:00:03"
  79. (org-timer-fix-incomplete "03"))))
  80. (ert-deftest test-org-timer/change-times ()
  81. "Test changing HMS format by offset."
  82. ;; Add time.
  83. (should
  84. (equal "
  85. 1:31:15
  86. 4:00:55"
  87. (org-test-with-temp-text "
  88. 0:00:25
  89. 2:30:05"
  90. (org-timer-change-times-in-region (point-min) (point-max)
  91. "1:30:50")
  92. (buffer-string))))
  93. ;; Subtract time.
  94. (should
  95. (equal "
  96. -1:30:25
  97. 0:59:15"
  98. (org-test-with-temp-text "
  99. 0:00:25
  100. 2:30:05"
  101. (org-timer-change-times-in-region (point-min) (point-max)
  102. "-1:30:50")
  103. (buffer-string)))))
  104. ;;; Timers
  105. ;; Dummy times for overriding `current-time'
  106. (defvar test-org-timer/time0 '(21635 62793 797149 675000))
  107. ;; Add 3 minutes and 26 seconds.
  108. (defvar test-org-timer/time1
  109. (time-add test-org-timer/time0 (seconds-to-time 206)))
  110. ;; Add 2 minutes and 41 seconds (6 minutes and 7 seconds total).
  111. (defvar test-org-timer/time2
  112. (time-add test-org-timer/time1 (seconds-to-time 161)))
  113. ;; Add 4 minutes and 55 seconds (11 minutes and 2 seconds total).
  114. (defvar test-org-timer/time3
  115. (time-add test-org-timer/time2 (seconds-to-time 295)))
  116. (ert-deftest test-org-timer/start-relative ()
  117. "Test starting relative timer."
  118. ;; Insert plain timer string, starting with `org-timer-start'.
  119. (should
  120. (equal "0:03:26"
  121. (test-org-timer/with-temp-text ""
  122. (test-org-timer/with-current-time test-org-timer/time0
  123. (org-timer-start))
  124. (test-org-timer/with-current-time test-org-timer/time1
  125. (org-timer))
  126. (org-trim (buffer-string)))))
  127. ;; Insert item timer string.
  128. (should
  129. (equal "- 0:03:26 ::"
  130. (test-org-timer/with-temp-text ""
  131. (test-org-timer/with-current-time test-org-timer/time0
  132. (org-timer-start))
  133. (test-org-timer/with-current-time test-org-timer/time1
  134. (org-timer-item))
  135. (org-trim (buffer-string)))))
  136. ;; Start with `org-timer'.
  137. (should
  138. (equal "0:00:00 0:03:26"
  139. (test-org-timer/with-temp-text ""
  140. (test-org-timer/with-current-time test-org-timer/time0
  141. (org-timer))
  142. (test-org-timer/with-current-time test-org-timer/time1
  143. (org-timer))
  144. (org-trim (buffer-string)))))
  145. ;; Restart with `org-timer'.
  146. (should
  147. (equal "0:00:00"
  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 '(4)))
  153. (org-trim (buffer-string))))))
  154. (ert-deftest test-org-timer/set-timer ()
  155. "Test setting countdown timer."
  156. (should
  157. (equal "0:06:34"
  158. (test-org-timer/with-temp-text ""
  159. (test-org-timer/with-current-time test-org-timer/time0
  160. (org-timer-set-timer 10))
  161. (test-org-timer/with-current-time test-org-timer/time1
  162. (org-timer))
  163. (org-trim (buffer-string)))))
  164. (should
  165. (equal "0:00:04"
  166. (test-org-timer/with-temp-text ""
  167. (test-org-timer/with-current-time test-org-timer/time0
  168. (org-timer-set-timer "3:30"))
  169. (test-org-timer/with-current-time test-org-timer/time1
  170. (org-timer))
  171. (org-trim (buffer-string))))))
  172. (ert-deftest test-org-timer/pause-timer ()
  173. "Test pausing relative and countdown timers."
  174. ;; Pause relative timer.
  175. (should
  176. (equal "0:03:26"
  177. (test-org-timer/with-temp-text ""
  178. (test-org-timer/with-current-time test-org-timer/time0
  179. (org-timer-start))
  180. (test-org-timer/with-current-time test-org-timer/time1
  181. (org-timer-pause-or-continue))
  182. (org-timer)
  183. (org-trim (buffer-string)))))
  184. ;; Pause then continue relative timer.
  185. (should
  186. (equal "0:08:21"
  187. (test-org-timer/with-temp-text ""
  188. (test-org-timer/with-current-time test-org-timer/time0
  189. (org-timer-start))
  190. (test-org-timer/with-current-time test-org-timer/time1
  191. (org-timer-pause-or-continue))
  192. (test-org-timer/with-current-time test-org-timer/time2
  193. (org-timer-pause-or-continue))
  194. (test-org-timer/with-current-time test-org-timer/time3
  195. (org-timer))
  196. (org-trim (buffer-string)))))
  197. ;; Pause then continue countdown timer.
  198. (should
  199. (equal "0:01:39"
  200. (test-org-timer/with-temp-text ""
  201. (test-org-timer/with-current-time test-org-timer/time0
  202. (org-timer-set-timer 10))
  203. (test-org-timer/with-current-time test-org-timer/time1
  204. (org-timer-pause-or-continue))
  205. (test-org-timer/with-current-time test-org-timer/time2
  206. (org-timer-pause-or-continue))
  207. (test-org-timer/with-current-time test-org-timer/time3
  208. (org-timer))
  209. (org-trim (buffer-string))))))
  210. (ert-deftest test-org-timer/stop ()
  211. "Test stopping relative and countdown timers."
  212. ;; Stop running relative timer.
  213. (test-org-timer/with-temp-text ""
  214. (test-org-timer/with-current-time test-org-timer/time0
  215. (org-timer-start))
  216. (test-org-timer/with-current-time test-org-timer/time1
  217. (org-timer-stop))
  218. (should-not org-timer-start-time))
  219. ;; Stop paused relative timer.
  220. (test-org-timer/with-temp-text ""
  221. (test-org-timer/with-current-time test-org-timer/time0
  222. (org-timer-start))
  223. (test-org-timer/with-current-time test-org-timer/time1
  224. (org-timer-pause-or-continue)
  225. (org-timer-stop))
  226. (should-not org-timer-start-time)
  227. (should-not org-timer-pause-time))
  228. ;; Stop running countdown timer.
  229. (test-org-timer/with-temp-text ""
  230. (test-org-timer/with-current-time test-org-timer/time0
  231. (org-timer-set-timer 10))
  232. (test-org-timer/with-current-time test-org-timer/time1
  233. (org-timer-stop))
  234. (should-not org-timer-start-time)
  235. (should-not org-timer-countdown-timer))
  236. ;; Stop paused countdown timer.
  237. (test-org-timer/with-temp-text ""
  238. (test-org-timer/with-current-time test-org-timer/time0
  239. (org-timer-set-timer 10))
  240. (test-org-timer/with-current-time test-org-timer/time1
  241. (org-timer-pause-or-continue)
  242. (org-timer-stop))
  243. (should-not org-timer-start-time)
  244. (should-not org-timer-pause-time)
  245. (should-not org-timer-countdown-timer)))
  246. (ert-deftest test-org-timer/other-timer-error ()
  247. "Test for error when other timer running."
  248. ;; Relative timer is running.
  249. (should-error
  250. (test-org-timer/with-temp-text ""
  251. (org-timer-start)
  252. (org-timer-set-timer 10))
  253. :type (list 'error 'user-error))
  254. ;; Countdown timer is running.
  255. (should-error
  256. (test-org-timer/with-temp-text ""
  257. (org-timer-set-timer 10)
  258. (org-timer-start))
  259. :type (list 'error 'user-error)))
  260. (ert-deftest test-org-timer/set-timer-from-effort-prop ()
  261. "Test timer setting from effort property."
  262. (should
  263. (< (* 60 9) ; 9m
  264. (test-org-timer/with-temp-text
  265. "* foo
  266. :PROPERTIES:
  267. :Effort: 10
  268. :END:"
  269. (org-mode)
  270. (org-timer-set-timer)
  271. (org-timer-hms-to-secs (org-timer nil t)))
  272. (1+ (* 60 10)) ; 10m 1s
  273. )))
  274. (provide 'test-org-timer)
  275. ;;; test-org-timer.el end here