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