website/pkg/utility/time_test.go

144 lines
2.2 KiB
Go
Raw Permalink Normal View History

package utility_test
import (
"testing"
"time"
"code.nonshy.com/nonshy/website/pkg/utility"
)
func TestFormatDurationCoarse(t *testing.T) {
var tests = []struct {
In time.Duration
Expect string
}{
{
In: 0,
Expect: "0 seconds",
},
{
In: 1 * time.Second,
Expect: "1 second",
},
{
In: 2 * time.Second,
Expect: "2 seconds",
},
{
In: 25 * time.Second,
Expect: "25 seconds",
},
{
In: 59 * time.Second,
Expect: "59 seconds",
},
{
In: 60 * time.Second,
Expect: "1 minute",
},
{
In: 90 * time.Second,
Expect: "1 minute",
},
{
In: 119 * time.Second,
Expect: "1 minute",
},
{
In: 120 * time.Second,
Expect: "2 minutes",
},
{
In: 15 * time.Minute,
Expect: "15 minutes",
},
{
In: 59 * time.Minute,
Expect: "59 minutes",
},
{
In: 60 * time.Minute,
Expect: "1 hour",
},
{
In: 75 * time.Minute,
Expect: "1 hour",
},
{
In: 119 * time.Minute,
Expect: "1 hour",
},
{
In: 120 * time.Minute,
Expect: "2 hours",
},
{
In: 14 * time.Hour,
Expect: "14 hours",
},
{
In: 23 * time.Hour,
Expect: "23 hours",
},
{
In: 24 * time.Hour,
Expect: "1 day",
},
{
In: 36 * time.Hour,
Expect: "1 day",
},
{
In: 48 * time.Hour,
Expect: "2 days",
},
{
In: time.Hour * 24 * 60,
Expect: "2 months",
},
{
In: time.Hour * 24 * 365,
Expect: "1 year",
},
{
In: time.Hour * 24 * 30 * 12,
Expect: "1 year",
},
{
In: time.Hour*24*30*12 + time.Hour*12,
Expect: "1 year",
},
{
In: time.Hour * 24 * 30 * 13,
Expect: "1.1 years",
},
{
In: time.Hour * 24 * 30 * 18,
Expect: "1.5 years",
},
{
In: time.Hour * 24 * 30 * 22,
Expect: "1.8 years",
},
{
In: time.Hour * 24 * 30 * 24,
Expect: "2 years",
},
{
In: time.Hour * 24 * 30 * 36,
Expect: "3 years",
},
{
In: time.Hour * 24 * 30 * 49,
Expect: "4 years",
},
}
for _, test := range tests {
actual := utility.FormatDurationCoarse(test.In)
if actual != test.Expect {
t.Errorf("Expected %d to be '%s' but got '%s'", test.In, test.Expect, actual)
}
}
}