JM-WechatMini/utils/human_duration_test.go

50 lines
902 B
Go
Raw Permalink Normal View History

2023-11-02 04:34:46 +08:00
package utils
import (
"testing"
"time"
)
func TestParseDuration(t *testing.T) {
type args struct {
d string
}
tests := []struct {
name string
args args
want time.Duration
wantErr bool
}{
{
name: "5h20m",
args: args{"5h20m"},
want: time.Hour*5 + 20*time.Minute,
wantErr: false,
},
{
name: "1d5h20m",
args: args{"1d5h20m"},
want: 24*time.Hour + time.Hour*5 + 20*time.Minute,
wantErr: false,
},
{
name: "1d",
args: args{"1d"},
want: 24 * time.Hour,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseDuration(tt.args.d)
if (err != nil) != tt.wantErr {
t.Errorf("ParseDuration() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ParseDuration() got = %v, want %v", got, tt.want)
}
})
}
}