1414package templates
1515
1616import (
17+ "math"
1718 "testing"
1819
1920 "github.com/stretchr/testify/require"
2021)
2122
22- func TestHumanizeDurationSecondsFloat64 (t * testing.T ) {
23+ func TestHumanizeDuration (t * testing.T ) {
2324 tc := []struct {
2425 name string
25- input float64
26+ input interface {}
2627 expected string
2728 }{
29+ // Integers
2830 {name : "zero" , input : 0 , expected : "0s" },
2931 {name : "one second" , input : 1 , expected : "1s" },
3032 {name : "one minute" , input : 60 , expected : "1m 0s" },
3133 {name : "one hour" , input : 3600 , expected : "1h 0m 0s" },
3234 {name : "one day" , input : 86400 , expected : "1d 0h 0m 0s" },
3335 {name : "one day and one hour" , input : 86400 + 3600 , expected : "1d 1h 0m 0s" },
3436 {name : "negative duration" , input : - (86400 * 2 + 3600 * 3 + 60 * 4 + 5 ), expected : "-2d 3h 4m 5s" },
37+ // Float64 with fractions
3538 {name : "using a float" , input : 899.99 , expected : "14m 59s" },
36- }
37-
38- for _ , tt := range tc {
39- t .Run (tt .name , func (t * testing.T ) {
40- result , err := HumanizeDuration (tt .input )
41- require .NoError (t , err )
42- require .Equal (t , tt .expected , result )
43- })
44- }
45- }
46-
47- func TestHumanizeDurationSubsecondAndFractionalSecondsFloat64 (t * testing.T ) {
48- tc := []struct {
49- name string
50- input float64
51- expected string
52- }{
5339 {name : "millseconds" , input : .1 , expected : "100ms" },
5440 {name : "nanoseconds" , input : .0001 , expected : "100us" },
5541 {name : "milliseconds + nanoseconds" , input : .12345 , expected : "123.5ms" },
5642 {name : "minute + millisecond" , input : 60.1 , expected : "1m 0s" },
5743 {name : "minute + milliseconds" , input : 60.5 , expected : "1m 0s" },
5844 {name : "second + milliseconds" , input : 1.2345 , expected : "1.234s" },
5945 {name : "second + milliseconds rounded" , input : 12.345 , expected : "12.35s" },
60- }
61-
62- for _ , tt := range tc {
63- t .Run (tt .name , func (t * testing.T ) {
64- result , err := HumanizeDuration (tt .input )
65- require .NoError (t , err )
66- require .Equal (t , tt .expected , result )
67- })
68- }
69- }
70-
71- func TestHumanizeDurationErrorString (t * testing.T ) {
72- _ , err := HumanizeDuration ("one" )
73- require .Error (t , err )
74- }
75-
76- func TestHumanizeDurationSecondsString (t * testing.T ) {
77- tc := []struct {
78- name string
79- input string
80- expected string
81- }{
46+ // String
8247 {name : "zero" , input : "0" , expected : "0s" },
8348 {name : "second" , input : "1" , expected : "1s" },
8449 {name : "minute" , input : "60" , expected : "1m 0s" },
8550 {name : "hour" , input : "3600" , expected : "1h 0m 0s" },
8651 {name : "day" , input : "86400" , expected : "1d 0h 0m 0s" },
87- }
88-
89- for _ , tt := range tc {
90- t .Run (tt .name , func (t * testing.T ) {
91- result , err := HumanizeDuration (tt .input )
92- require .NoError (t , err )
93- require .Equal (t , tt .expected , result )
94- })
95- }
96- }
97-
98- func TestHumanizeDurationSubsecondAndFractionalSecondsString (t * testing.T ) {
99- tc := []struct {
100- name string
101- input string
102- expected string
103- }{
52+ // String with fractions
10453 {name : "millseconds" , input : ".1" , expected : "100ms" },
10554 {name : "nanoseconds" , input : ".0001" , expected : "100us" },
10655 {name : "milliseconds + nanoseconds" , input : ".12345" , expected : "123.5ms" },
10756 {name : "minute + millisecond" , input : "60.1" , expected : "1m 0s" },
10857 {name : "minute + milliseconds" , input : "60.5" , expected : "1m 0s" },
10958 {name : "second + milliseconds" , input : "1.2345" , expected : "1.234s" },
11059 {name : "second + milliseconds rounded" , input : "12.345" , expected : "12.35s" },
60+ // Int
61+ {name : "zero" , input : 0 , expected : "0s" },
62+ {name : "negative" , input : - 1 , expected : "-1s" },
63+ {name : "second" , input : 1 , expected : "1s" },
64+ {name : "days" , input : 1234567 , expected : "14d 6h 56m 7s" },
11165 }
11266
11367 for _ , tt := range tc {
@@ -119,23 +73,51 @@ func TestHumanizeDurationSubsecondAndFractionalSecondsString(t *testing.T) {
11973 }
12074}
12175
122- func TestHumanizeDurationSecondsInt (t * testing.T ) {
76+ func TestHumanizeDurationErrorString (t * testing.T ) {
77+ _ , err := HumanizeDuration ("one" )
78+ require .Error (t , err )
79+ }
80+
81+ func TestHumanizeTimestamp (t * testing.T ) {
12382 tc := []struct {
12483 name string
125- input int
84+ input interface {}
12685 expected string
12786 }{
128- {name : "zero" , input : 0 , expected : "0s" },
129- {name : "negative" , input : - 1 , expected : "-1s" },
130- {name : "second" , input : 1 , expected : "1s" },
131- {name : "days" , input : 1234567 , expected : "14d 6h 56m 7s" },
87+ // Int
88+ {name : "zero" , input : 0 , expected : "1970-01-01 00:00:00 +0000 UTC" },
89+ {name : "negative" , input : - 1 , expected : "1969-12-31 23:59:59 +0000 UTC" },
90+ {name : "one" , input : 1 , expected : "1970-01-01 00:00:01 +0000 UTC" },
91+ {name : "past" , input : 1234567 , expected : "1970-01-15 06:56:07 +0000 UTC" },
92+ {name : "future" , input : 9223372036 , expected : "2262-04-11 23:47:16 +0000 UTC" },
93+ // Uint
94+ {name : "zero" , input : uint64 (0 ), expected : "1970-01-01 00:00:00 +0000 UTC" },
95+ {name : "one" , input : uint64 (1 ), expected : "1970-01-01 00:00:01 +0000 UTC" },
96+ {name : "past" , input : uint64 (1234567 ), expected : "1970-01-15 06:56:07 +0000 UTC" },
97+ {name : "future" , input : uint64 (9223372036 ), expected : "2262-04-11 23:47:16 +0000 UTC" },
98+ // NaN/Inf, strings
99+ {name : "infinity" , input : "+Inf" , expected : "+Inf" },
100+ {name : "minus infinity" , input : "-Inf" , expected : "-Inf" },
101+ {name : "NaN" , input : "NaN" , expected : "NaN" },
102+ // Nan/Inf, float64
103+ {name : "infinity" , input : math .Inf (1 ), expected : "+Inf" },
104+ {name : "minus infinity" , input : math .Inf (- 1 ), expected : "-Inf" },
105+ {name : "NaN" , input : math .NaN (), expected : "NaN" },
106+ // Sampled data
107+ {name : "sample float64" , input : 1435065584.128 , expected : "2015-06-23 13:19:44.128 +0000 UTC" },
108+ {name : "sample string" , input : "1435065584.128" , expected : "2015-06-23 13:19:44.128 +0000 UTC" },
132109 }
133110
134111 for _ , tt := range tc {
135112 t .Run (tt .name , func (t * testing.T ) {
136- result , err := HumanizeDuration (tt .input )
113+ result , err := HumanizeTimestamp (tt .input )
137114 require .NoError (t , err )
138115 require .Equal (t , tt .expected , result )
139116 })
140117 }
141118}
119+
120+ func TestHumanizeTimestampError (t * testing.T ) {
121+ _ , err := HumanizeTimestamp (math .MaxInt64 )
122+ require .Error (t , err )
123+ }
0 commit comments