test-org-duration.el 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ;;; test-org-duration.el --- Tests for org-duration.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017, 2019 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (require 'org-duration)
  16. (ert-deftest test-org-duration/to-minutes ()
  17. "Test `org-duration-to-minutes' specifications."
  18. ;; Raise an error for unknown duration format.
  19. (should-error (org-duration-to-minutes "1:2"))
  20. ;; Return number of minutes, as a float.
  21. (should (= (org-duration-to-minutes "1:01") 61))
  22. (should (floatp (org-duration-to-minutes "1:01")))
  23. ;; Handle various duration formats.
  24. (should (= (org-duration-to-minutes "1:20:30") 80.5))
  25. (should (= (org-duration-to-minutes "2h 10min") 130))
  26. (should (= (org-duration-to-minutes "1d 1:02") 1502))
  27. (should (= (org-duration-to-minutes "2.5h") 150))
  28. ;; Special case: a bare number is treated as minutes.
  29. (should (= (org-duration-to-minutes "2") 2))
  30. (should (= (org-duration-to-minutes "2.5") 2.5))
  31. (should (= (org-duration-to-minutes 1) 1))
  32. ;; Special case: the empty string is 0.0.
  33. (should (= (org-duration-to-minutes "") 0.0))
  34. ;; Support custom units.
  35. (should (= 4
  36. (let ((org-duration-units '(("longmin" . 2)))
  37. org-duration--unit-re
  38. org-duration--full-re
  39. org-duration--mixed-re)
  40. (org-duration-set-regexps)
  41. (org-duration-to-minutes "2longmin"))))
  42. (should (= 61
  43. (let ((org-duration-units '(("h" . 61)))
  44. org-duration--unit-re
  45. org-duration--full-re
  46. org-duration--mixed-re)
  47. (org-duration-set-regexps)
  48. (org-duration-to-minutes "1h"))))
  49. ;; When CANONICAL is non-nil, ignore custom units and only recognize
  50. ;; units defined in `org-duration-canonical-units'.
  51. (should (= 60
  52. (let ((org-duration-units '(("h" . 61)))
  53. org-duration--unit-re
  54. org-duration--full-re
  55. org-duration--mixed-re)
  56. (org-duration-set-regexps)
  57. (org-duration-to-minutes "1h" t))))
  58. (should-error (let ((org-duration-units '(("longmin" . 2)))
  59. org-duration--unit-re
  60. org-duration--full-re
  61. org-duration--mixed-re)
  62. (org-duration-set-regexps)
  63. (org-duration-to-minutes "2longmin" t))))
  64. (ert-deftest test-org-duration/from-minutes ()
  65. "Test `org-duration-from-minutes' specifications."
  66. ;; Format number of minutes according to `org-duration-format'.
  67. (should (equal "1:00"
  68. (let ((org-duration-format 'h:mm))
  69. (org-duration-from-minutes 60))))
  70. (should (equal "1:01:30"
  71. (let ((org-duration-format 'h:mm:ss))
  72. (org-duration-from-minutes 61.5))))
  73. (should (equal "1:01"
  74. (let ((org-duration-format 'h:mm))
  75. (org-duration-from-minutes 61.5))))
  76. ;; Handle required parameter in advanced format specifications.
  77. (should (equal "1h"
  78. (let ((org-duration-format '(("h" . nil) ("min" . nil))))
  79. (org-duration-from-minutes 60))))
  80. (should (equal "1h 0min"
  81. (let ((org-duration-format '(("h" . nil) ("min" . t))))
  82. (org-duration-from-minutes 60))))
  83. (should (equal "50min"
  84. (let ((org-duration-format '(("h" . nil) ("min" . nil))))
  85. (org-duration-from-minutes 50))))
  86. (should (equal "0h 50min"
  87. (let ((org-duration-format '(("h" . t) ("min" . t))))
  88. (org-duration-from-minutes 50))))
  89. ;; Handle mixed mode.
  90. (should (equal "1d 0:10"
  91. (let ((org-duration-format '(("d" . nil) (special . h:mm))))
  92. (org-duration-from-minutes (+ (* 24 60) 10)))))
  93. (should (equal "1d 0:12:30"
  94. (let ((org-duration-format '(("d" . nil) (special . h:mm:ss))))
  95. (org-duration-from-minutes (+ (* 24 60) 12.5)))))
  96. ;; Handle fractional duration. Parameter is the precision.
  97. (should (equal "1.5h"
  98. (let ((org-duration-format '(("h" . nil) (special . 1))))
  99. (org-duration-from-minutes 90))))
  100. (should (equal "1.50h"
  101. (let ((org-duration-format '(("h" . nil) (special . 2))))
  102. (org-duration-from-minutes 90))))
  103. ;; When using fractional duration, use first required unit or the
  104. ;; first with a non-zero integer part. If none is found, refer to
  105. ;; smallest unit specified in format.
  106. (should (equal "0.7h"
  107. (let ((org-duration-format
  108. '(("h" . t) ("min" . nil) (special . 1))))
  109. (org-duration-from-minutes 40))))
  110. (should (equal "40.0min"
  111. (let ((org-duration-format
  112. '(("h" . nil) ("min" . nil) (special . 1))))
  113. (org-duration-from-minutes 40))))
  114. (should (equal "0.5min"
  115. (let ((org-duration-format
  116. '(("h" . nil) ("min" . nil) (special . 1))))
  117. (org-duration-from-minutes 0.5))))
  118. ;; Handle compact form.
  119. (should (equal "0h50min"
  120. (let ((org-duration-format '(("h" . t) ("min" . t) compact)))
  121. (org-duration-from-minutes 50))))
  122. (should (equal "1d0:10"
  123. (let ((org-duration-format
  124. '(("d" . nil) (special . h:mm) compact)))
  125. (org-duration-from-minutes (+ (* 24 60) 10))))))
  126. (ert-deftest test-org-duration/p ()
  127. "Test `org-duration-p' specifications."
  128. ;; Test all duration formats.
  129. (should (org-duration-p "3:12"))
  130. (should (org-duration-p "123:12"))
  131. (should (org-duration-p "1:23:45"))
  132. (should (org-duration-p "3d 3h 4min"))
  133. (should (org-duration-p "3d3h4min"))
  134. (should (org-duration-p "3d 13:35"))
  135. (should (org-duration-p "3d13:35"))
  136. (should (org-duration-p "2.35h"))
  137. ;; Handle custom units, but return nil for unknown units.
  138. (should-not (org-duration-p "1minute"))
  139. (should (let ((org-duration-units '(("minute" . 1)))
  140. org-duration--unit-re
  141. org-duration--full-re
  142. org-duration--mixed-re)
  143. (org-duration-set-regexps)
  144. (org-duration-p "2minute")))
  145. ;; Tolerate white space between the number and the unit.
  146. (should (org-duration-p "2 h"))
  147. ;; Return nil for ill-formed H:MM:SS strings.
  148. (should-not (org-duration-p "3::12"))
  149. (should-not (org-duration-p "3:2"))
  150. (should-not (org-duration-p "3:12:4"))
  151. ;; Return nil in mixed mode if H:MM:SS part is not the last one.
  152. (should-not (org-duration-p "3d 13:35 13h")))
  153. (ert-deftest test-org-duration/h:mm-only-p ()
  154. "Test `org-duration-h:mm-only-p' specifications."
  155. (should (org-duration-h:mm-only-p '("123:31" "1:00")))
  156. (should-not (org-duration-h:mm-only-p '("123:32" "1h")))
  157. (should (eq 'h:mm:ss (org-duration-h:mm-only-p '("3:33" "1:23:45")))))
  158. (provide 'test-org-duration)
  159. ;;; test-org-duration.el ends here