51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package tools
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestParseDate(t *testing.T) {
|
|
now := time.Now().Local()
|
|
sec := now.Truncate(time.Second)
|
|
today := now.Truncate(day)
|
|
tomorrow := today.Add(day)
|
|
yesterday := today.Add(-day)
|
|
twoMin := now.Add(2 * time.Minute).Truncate(time.Minute)
|
|
twoHour := now.Add(2 * time.Hour).Truncate(time.Hour)
|
|
tests := []struct {
|
|
name string
|
|
exp time.Time
|
|
err string
|
|
}{
|
|
{"now", sec, ""},
|
|
{"today", today, ""},
|
|
{"tomorrow", tomorrow, ""},
|
|
{"yesterday", yesterday, ""},
|
|
{"in two minutes", twoMin, ""},
|
|
{"in two hours", twoHour, ""},
|
|
{"min", MinTime, ""},
|
|
{"max", MaxTime, ""},
|
|
{"not a date", now, fmt.Sprintf(`failed to parse "%s": unknown format`, "not a date")},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, getDateTest(tt.name, tt.exp, tt.err))
|
|
}
|
|
}
|
|
|
|
func getDateTest(in string, exp time.Time, err string) func(t *testing.T) {
|
|
return func(t *testing.T) {
|
|
out, er := ParseDate(in)
|
|
if err != "" {
|
|
assert.ErrorContains(t, er, err)
|
|
} else {
|
|
require.NoError(t, er)
|
|
|
|
assert.Equal(t, exp, out)
|
|
}
|
|
}
|
|
}
|