test-org-duration.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. ;;; test-org-duration.el --- Tests for org-duration.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017 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 <http://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (ert-deftest test-org-duration/to-minutes ()
  16. "Test `org-duration-to-minutes' specifications."
  17. ;; Raise an error for unknown duration format.
  18. (should-error (org-duration-to-minutes "1:2"))
  19. ;; Return number of minutes, as a float.
  20. (should (= (org-duration-to-minutes "1:01") 61))
  21. (should (floatp (org-duration-to-minutes "1:01")))
  22. ;; Handle various duration formats.
  23. (should (= (org-duration-to-minutes "1:20:30") 80.5))
  24. (should (= (org-duration-to-minutes "2h 10min") 130))
  25. (should (= (org-duration-to-minutes "1d 1:02") 1502))
  26. (should (= (org-duration-to-minutes "2.5h") 150))
  27. ;; Special case: a bare number is treated as minutes.
  28. (should (= (org-duration-to-minutes "2") 2))
  29. (should (= (org-duration-to-minutes "2.5") 2.5))
  30. (should (= (org-duration-to-minutes 1) 1))
  31. ;; Special case: the empty string is 0.0.
  32. (should (= (org-duration-to-minutes "") 0.0))
  33. ;; Support custom units.
  34. (should (= 4
  35. (let ((org-duration-units '(("longmin" . 2)))
  36. org-duration--unit-re
  37. org-duration--full-re
  38. org-duration--mixed-re)
  39. (org-duration-set-regexps)
  40. (org-duration-to-minutes "2longmin"))))
  41. (should (= 61
  42. (let ((org-duration-units '(("h" . 61)))
  43. org-duration--unit-re
  44. org-duration--full-re
  45. org-duration--mixed-re)
  46. (org-duration-set-regexps)
  47. (org-duration-to-minutes "1h"))))
  48. ;; When CANONICAL is non-nil, ignore custom units and only recognize
  49. ;; units defined in `org-duration-canonical-units'.
  50. (should (= 60
  51. (let ((org-duration-units '(("h" . 61)))
  52. org-duration--unit-re
  53. org-duration--full-re
  54. org-duration--mixed-re)
  55. (org-duration-set-regexps)
  56. (org-duration-to-minutes "1h" t))))
  57. (should-error (let ((org-duration-units '(("longmin" . 2)))
  58. org-duration--unit-re
  59. org-duration--full-re
  60. org-duration--mixed-re)
  61. (org-duration-set-regexps)
  62. (org-duration-to-minutes "2longmin" t))))
  63. (ert-deftest test-org-duration/from-minutes ()
  64. "Test `org-duration-from-minutes' specifications."
  65. ;; Format number of minutes according to `org-duration-format'.
  66. (should (equal "1:00"
  67. (let ((org-duration-format 'h:mm))
  68. (org-duration-from-minutes 60))))
  69. (should (equal "1:01:30"
  70. (let ((org-duration-format 'h:mm:ss))
  71. (org-duration-from-minutes 61.5))))
  72. (should (equal "1:01"
  73. (let ((org-duration-format 'h:mm))
  74. (org-duration-from-minutes 61.5))))
  75. ;; Handle required parameter in advanced format specifications.
  76. (should (equal "1h"
  77. (let ((org-duration-format '(("h" . nil) ("min" . nil))))
  78. (org-duration-from-minutes 60))))
  79. (should (equal "1h 0min"
  80. (let ((org-duration-format '(("h" . nil) ("min" . t))))
  81. (org-duration-from-minutes 60))))
  82. (should (equal "50min"
  83. (let ((org-duration-format '(("h" . nil) ("min" . nil))))
  84. (org-duration-from-minutes 50))))
  85. (should (equal "0h 50min"
  86. (let ((org-duration-format '(("h" . t) ("min" . t))))
  87. (org-duration-from-minutes 50))))
  88. ;; Handle mixed mode.
  89. (should (equal "1d 0:10"
  90. (let ((org-duration-format '(("d" . nil) (special . h:mm))))
  91. (org-duration-from-minutes (+ (* 24 60) 10)))))
  92. (should (equal "1d 0:12:30"
  93. (let ((org-duration-format '(("d" . nil) (special . h:mm:ss))))
  94. (org-duration-from-minutes (+ (* 24 60) 12.5)))))
  95. ;; Handle fractional duration. Parameter is the precision.
  96. (should (equal "1.5h"
  97. (let ((org-duration-format '(("h" . nil) (special . 1))))
  98. (org-duration-from-minutes 90))))
  99. (should (equal "1.50h"
  100. (let ((org-duration-format '(("h" . nil) (special . 2))))
  101. (org-duration-from-minutes 90))))
  102. ;; When using fractional duration, use first required unit or the
  103. ;; first with a non-zero integer part. If none is found, refer to
  104. ;; smallest unit specified in format.
  105. (should (equal "0.7h"
  106. (let ((org-duration-format
  107. '(("h" . t) ("min" . nil) (special . 1))))
  108. (org-duration-from-minutes 40))))
  109. (should (equal "40.0min"
  110. (let ((org-duration-format
  111. '(("h" . nil) ("min" . nil) (special . 1))))
  112. (org-duration-from-minutes 40))))
  113. (should (equal "0.5min"
  114. (let ((org-duration-format
  115. '(("h" . nil) ("min" . nil) (special . 1))))
  116. (org-duration-from-minutes 0.5)))))
  117. (ert-deftest test-org-duration/p ()
  118. "Test `org-duration-p' specifications."
  119. ;; Test all duration formats.
  120. (should (org-duration-p "3:12"))
  121. (should (org-duration-p "123:12"))
  122. (should (org-duration-p "1:23:45"))
  123. (should (org-duration-p "3d 3h 4min"))
  124. (should (org-duration-p "3d 13:35"))
  125. (should (org-duration-p "2.35h"))
  126. ;; Handle custom units, but return nil for unknown units.
  127. (should-not (org-duration-p "1minute"))
  128. (should (let ((org-duration-units '(("minute" . 1)))
  129. org-duration--unit-re
  130. org-duration--full-re
  131. org-duration--mixed-re)
  132. (org-duration-set-regexps)
  133. (org-duration-p "2minute")))
  134. ;; Tolerate white space between the number and the unit.
  135. (should (org-duration-p "2 h"))
  136. ;; Return nil for ill-formed H:MM:SS strings.
  137. (should-not (org-duration-p "3::12"))
  138. (should-not (org-duration-p "3:2"))
  139. (should-not (org-duration-p "3:12:4"))
  140. ;; Return nil in mixed mode if H:MM:SS part is not last.
  141. (should-not (org-duration-p "3d 13:35 13h")))
  142. (ert-deftest test-org-duration/h:mm-only-p ()
  143. "Test `org-duration-h:mm-only-p' specifications."
  144. (should (org-duration-h:mm-only-p '("123:31" "1:00")))
  145. (should-not (org-duration-h:mm-only-p '("123:32" "1h")))
  146. (should (eq 'h:mm:ss (org-duration-h:mm-only-p '("3:33" "1:23:45")))))
  147. (provide 'test-org-duration)
  148. ;;; test-org-duration.el ends here