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