test-org-timer.el 9.4 KB

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