test-org-duration.el 6.2 KB

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