themis/time_test.go

104 lines
2.0 KiB

package themis
import (
"reflect"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type TestClock struct {
now time.Time
}
func (c TestClock) Now() time.Time {
return c.now
}
func TestNextWednesday(t *testing.T) {
tests := []struct {
name string
seed string
want string
}{
{
name: "on wednesday",
seed: "2023-11-15T15:04:05Z",
want: "2023-11-15T15:04:05Z",
},
{
name: "on sunday",
seed: "2023-11-12T15:04:05Z",
want: "2023-11-15T15:04:05Z",
},
{
name: "on thursday",
seed: "2023-11-16T15:04:05Z",
want: "2023-11-22T15:04:05Z",
},
{
name: "on saturday",
seed: "2023-11-18T15:04:05Z",
want: "2023-11-22T15:04:05Z",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
seedt, err := time.Parse(time.RFC3339, tt.seed)
require.NoError(t, err)
wantt, err := time.Parse(time.RFC3339, tt.want)
require.NoError(t, err)
testClock := TestClock{now: seedt}
if got := NextOfWeekday(testClock, time.Wednesday); !reflect.DeepEqual(got, wantt) {
t.Errorf("NextWednesday() = %v, want %v", got, wantt)
}
})
}
}
func TestNextMonday(t *testing.T) {
tests := []struct {
name string
seed string
want string
}{
{
name: "on monday",
seed: "2023-11-13T15:04:05Z",
want: "2023-11-13T15:04:05Z",
},
{
name: "on sunday",
seed: "2023-11-12T15:04:05Z",
want: "2023-11-13T15:04:05Z",
},
{
name: "on tuesday",
seed: "2023-11-14T15:04:05Z",
want: "2023-11-20T15:04:05Z",
},
{
name: "on saturday",
seed: "2023-11-18T15:04:05Z",
want: "2023-11-20T15:04:05Z",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
seedt, err := time.Parse(time.RFC3339, tt.seed)
require.NoError(t, err)
wantt, err := time.Parse(time.RFC3339, tt.want)
require.NoError(t, err)
testClock := TestClock{now: seedt}
if got := NextOfWeekday(testClock, time.Monday); !reflect.DeepEqual(got, wantt) {
t.Errorf("NextWednesday() = %v, want %v", got, wantt)
}
})
}
}