test-org-duration.el 6.1 KB

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