Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
derinil committed Sep 28, 2022
1 parent 6c79d68 commit 23f31d1
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion datetime/period.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (p Period) Within(o Period) bool {
(p.End.Equal(o.End) || p.End.Before(o.End))
}

func (p Period) ContainsDate(t time.Time) bool {
func (p Period) Contains(t time.Time) bool {
return (p.Begin.Equal(t) || p.Begin.Before(t)) &&
(p.End.Equal(t) || p.End.After(t))
}
Expand Down
94 changes: 94 additions & 0 deletions datetime/period_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,97 @@ func TestWithin(t *testing.T) {
})
}
}

func TestContainsDate(t *testing.T) {
testCases := []struct {
name string
p datetime.Period
t time.Time
ret bool
}{
{
name: "zero period and zero time",
p: datetime.Period{},
ret: true,
},
{
name: "now period and zero time",
p: datetime.Period{
Begin: time.Now(),
End: time.Now(),
},
ret: false,
},
{
name: "zero period and now time",
p: datetime.Period{},
t: time.Now(),
ret: false,
},
{
name: "zero to infinite period and now time",
p: datetime.Period{
End: time.Now().Add(10000 * time.Minute),
},
t: time.Now(),
ret: true,
},
{
name: "zero to infinite period and zero time",
p: datetime.Period{
End: time.Now().Add(10000 * time.Minute),
},
ret: true,
},
{
name: "zero to infinite period and now time",
p: datetime.Period{
End: time.Now().Add(10000 * time.Minute),
},
t: time.Now(),
ret: true,
},
{
name: "1 hour period and infinite time",
p: datetime.Period{
Begin: time.Now(),
End: time.Now().Add(time.Hour),
},
t: time.Now().Add(10000 * time.Minute),
ret: false,
},
{
name: "1 hour period and now time",
p: datetime.Period{
Begin: time.Now(),
End: time.Now().Add(time.Hour),
},
t: time.Now(),
ret: true,
},
{
name: "begin in 1 hour period and infinite time",
p: datetime.Period{
Begin: time.Now().Add(-time.Hour),
End: time.Now().Add(time.Hour),
},
t: time.Now().Add(10000 * time.Minute),
ret: false,
},
{
name: "begin in 1 hour period and now time",
p: datetime.Period{
Begin: time.Now().Add(-time.Hour),
End: time.Now().Add(time.Hour),
},
t: time.Now(),
ret: true,
},
}

for _, c := range testCases {
t.Run(c.name, func(t *testing.T) {
require.Equal(t, c.ret, c.p.Contains(c.t))
})
}
}

0 comments on commit 23f31d1

Please sign in to comment.