@@ -49,7 +49,7 @@ func TestParseString(t *testing.T) {
4949 }
5050}
5151
52- //TestParseDuration test if string returned by a duration is equal to string returned with the package
52+ // TestParseDuration test if string returned by a duration is equal to string returned with the package
5353func TestParseDuration (t * testing.T ) {
5454
5555 for i , duration := range []time.Duration {
@@ -89,3 +89,63 @@ func TestParseDuration(t *testing.T) {
8989 }
9090 }
9191}
92+
93+ func TestString (t * testing.T ) {
94+
95+ for i , tt := range []struct {
96+ dur time.Duration
97+ expected string
98+ }{
99+ //This times are returned with time.Duration string
100+ {time .Duration (0 ), "0s" },
101+ {time .Duration (time .Hour ), "1h" },
102+ {time .Duration (time .Minute ), "1m" },
103+ {time .Duration (time .Second ), "1s" },
104+ {time .Duration (time .Millisecond ), "1ms" },
105+ {time .Duration (time .Microsecond ), "1us" },
106+ {time .Duration (time .Nanosecond ), "1ns" },
107+ {time .Duration (4 * time .Second + time .Nanosecond ), "4s1ns" },
108+ {time .Duration (time .Hour + 4 * time .Second + time .Nanosecond ), "1h4s1ns" },
109+ {time .Duration (61 * time .Minute + 10 * time .Millisecond ), "1h1m10ms" },
110+ {time .Duration (61 * time .Minute + 123456789 * time .Nanosecond ), "1h1m123ms456us789ns" },
111+ {time .Duration (time .Millisecond + 20 * time .Nanosecond ), "1ms20ns" },
112+ {time .Duration (time .Second + 20 * time .Nanosecond ), "1s20ns" },
113+ {time .Duration (693 * time .Nanosecond ), "693ns" },
114+ {time .Duration (10 * time .Second + time .Microsecond + 693 * time .Nanosecond ), "10s1us693ns" },
115+ {time .Duration (time .Millisecond + 1 * time .Nanosecond ), "1ms1ns" },
116+ {time .Duration (time .Second + 20 * time .Nanosecond ), "1s20ns" },
117+ {time .Duration (60 * time .Hour + 8 * time .Millisecond ), "2d12h8ms" },
118+ {time .Duration (96 * time .Hour + 63 * time .Second ), "4d1m3s" },
119+ {time .Duration (48 * time .Hour + 3 * time .Second + 96 * time .Nanosecond ), "2d3s96ns" },
120+ {time .Duration (168 * time .Hour + 48 * time .Hour + 3 * time .Second + 96 * time .Nanosecond ), "1w2d3s96ns" },
121+ {time .Duration (168 * time .Hour + 48 * time .Hour + 3 * time .Second + 3 * time .Microsecond + 96 * time .Nanosecond ), "1w2d3s3us96ns" },
122+
123+ // this is the maximum supported by golang std: 2540400h10m10.000000000s
124+ {time .Duration (2540400 * time .Hour + 10 * time .Minute + 10 * time .Second ), "15121w3d10m10s" },
125+
126+ // this is max int64, the max duration supported to convert
127+ {time .Duration (9_223_372_036_854_775_807 * time .Nanosecond ), "15250w1d23h47m16s854ms775us807ns" },
128+
129+ // this is max int64 in negative, the max negative duration supported to convert
130+ {time .Duration (- 9_223_372_036_854_775_807 * time .Nanosecond ), "-15250w1d23h47m16s854ms775us807ns" },
131+
132+ // negatives
133+ {time .Duration (- time .Hour ), "-1h" },
134+ {time .Duration (- time .Minute ), "-1m" },
135+ {time .Duration (- time .Second ), "-1s" },
136+ {time .Duration (- time .Millisecond ), "-1ms" },
137+ {time .Duration (- time .Microsecond ), "-1us" },
138+ {time .Duration (- time .Nanosecond ), "-1ns" },
139+ {time .Duration (- 4 * time .Second - time .Nanosecond ), "-4s1ns" },
140+ } {
141+ stringDuration := String (tt .dur )
142+ if tt .expected != stringDuration {
143+ t .Errorf ("index %d -> in: %s returned: %s\t not equal to %s" , i , tt .dur , stringDuration , tt .expected )
144+ }
145+
146+ durationParsed , _ := ParseDuration (stringDuration )
147+ if durationParsed != tt .dur {
148+ t .Errorf ("error converting string to duration: index %d -> in: %s returned: %s" , i , tt .dur , durationParsed )
149+ }
150+ }
151+ }
0 commit comments