1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
package tools
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestTimeAtOrInNoDuration(t *testing.T) {
now := time.Now()
then := time.Now().Add(24 * time.Hour)
got := TimeAtOrIn(now, then, time.Duration(0))
assert.Equal(t, then, got)
}
func TestTimeAtOrInWithDuration(t *testing.T) {
now := time.Now()
duration := 5 * time.Minute
expected := now.Add(duration)
got := TimeAtOrIn(now, now, duration)
assert.Equal(t, expected, got)
}
func TestTimeAtOrInZeroTime(t *testing.T) {
now := time.Now()
zero := time.Time{}
got := TimeAtOrIn(now, zero, 0)
assert.Equal(t, zero, got)
}
func TestIsExpiredAtOrInWithNonZeroTime(t *testing.T) {
now := time.Now()
within := 5 * time.Minute
at := now.Add(10 * time.Minute)
in := time.Duration(0)
expired, ok := IsExpiredAtOrIn(now, within, at, in)
assert.False(t, ok)
assert.Equal(t, at, expired)
}
func TestIsExpiredAtOrInWithNonZeroDuration(t *testing.T) {
now := time.Now()
within := 5 * time.Minute
at := time.Time{}
in := 10 * time.Minute
expired, ok := IsExpiredAtOrIn(now, within, at, in)
assert.Equal(t, now.Add(in), expired)
assert.False(t, ok)
}
func TestIsExpiredAtOrInWithNonZeroTimeExpired(t *testing.T) {
now := time.Now()
within := 5 * time.Minute
at := now.Add(3 * time.Minute)
in := time.Duration(0)
expired, ok := IsExpiredAtOrIn(now, within, at, in)
assert.True(t, ok)
assert.Equal(t, at, expired)
}
func TestIsExpiredAtOrInWithNonZeroDurationExpired(t *testing.T) {
now := time.Now()
within := 5 * time.Minute
at := time.Time{}
in := -10 * time.Minute
expired, ok := IsExpiredAtOrIn(now, within, at, in)
assert.Equal(t, now.Add(in), expired)
assert.True(t, ok)
}
func TestIsExpiredAtOrInWithAmbiguousTime(t *testing.T) {
now := time.Now()
within := 5 * time.Minute
at := now.Add(-10 * time.Minute)
in := 10 * time.Minute
expired, ok := IsExpiredAtOrIn(now, within, at, in)
assert.Equal(t, now.Add(in), expired)
assert.False(t, ok)
}
|