test-org-timer.el 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. (ert-deftest test-org-timer/pause-timer ()
  164. "Test pausing relative and countdown timers."
  165. ;; Pause relative timer.
  166. (should
  167. (equal "0:03:26"
  168. (test-org-timer/with-temp-text ""
  169. (test-org-timer/with-current-time test-org-timer/time0
  170. (org-timer-start))
  171. (test-org-timer/with-current-time test-org-timer/time1
  172. (org-timer-pause-or-continue))
  173. (org-timer)
  174. (org-trim (buffer-string)))))
  175. ;; Pause then continue relative timer.
  176. (should
  177. (equal "0:08:21"
  178. (test-org-timer/with-temp-text ""
  179. (test-org-timer/with-current-time test-org-timer/time0
  180. (org-timer-start))
  181. (test-org-timer/with-current-time test-org-timer/time1
  182. (org-timer-pause-or-continue))
  183. (test-org-timer/with-current-time test-org-timer/time2
  184. (org-timer-pause-or-continue))
  185. (test-org-timer/with-current-time test-org-timer/time3
  186. (org-timer))
  187. (org-trim (buffer-string)))))
  188. ;; Pause then continue countdown timer.
  189. (should
  190. (equal "0:01:39"
  191. (test-org-timer/with-temp-text ""
  192. (test-org-timer/with-current-time test-org-timer/time0
  193. (org-timer-set-timer 10))
  194. (test-org-timer/with-current-time test-org-timer/time1
  195. (org-timer-pause-or-continue))
  196. (test-org-timer/with-current-time test-org-timer/time2
  197. (org-timer-pause-or-continue))
  198. (test-org-timer/with-current-time test-org-timer/time3
  199. (org-timer))
  200. (org-trim (buffer-string))))))
  201. (ert-deftest test-org-timer/stop ()
  202. "Test stopping relative and countdown timers."
  203. ;; Stop running relative timer.
  204. (test-org-timer/with-temp-text ""
  205. (test-org-timer/with-current-time test-org-timer/time0
  206. (org-timer-start))
  207. (test-org-timer/with-current-time test-org-timer/time1
  208. (org-timer-stop))
  209. (should-not org-timer-start-time))
  210. ;; Stop paused relative timer.
  211. (test-org-timer/with-temp-text ""
  212. (test-org-timer/with-current-time test-org-timer/time0
  213. (org-timer-start))
  214. (test-org-timer/with-current-time test-org-timer/time1
  215. (org-timer-pause-or-continue)
  216. (org-timer-stop))
  217. (should-not org-timer-start-time)
  218. (should-not org-timer-pause-time))
  219. ;; Stop running countdown timer.
  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-stop))
  225. (should-not org-timer-start-time)
  226. (should-not org-timer-countdown-timer))
  227. ;; Stop paused 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-pause-or-continue)
  233. (org-timer-stop))
  234. (should-not org-timer-start-time)
  235. (should-not org-timer-pause-time)
  236. (should-not org-timer-countdown-timer)))
  237. (ert-deftest test-org-timer/other-timer-error ()
  238. "Test for error when other timer running."
  239. ;; Relative timer is running.
  240. (should-error
  241. (test-org-timer/with-temp-text ""
  242. (org-timer-start)
  243. (org-timer-set-timer 10))
  244. :type (list 'error 'user-error))
  245. ;; Countdown timer is running.
  246. (should-error
  247. (test-org-timer/with-temp-text ""
  248. (org-timer-set-timer 10)
  249. (org-timer-start))
  250. :type (list 'error 'user-error)))
  251. (provide 'test-org-timer)
  252. ;;; test-org-timer.el end here