website/pkg/utility/dob_test.go

75 lines
1.0 KiB
Go
Raw Permalink Normal View History

package utility_test
import (
"testing"
"time"
2022-08-26 04:21:46 +00:00
"code.nonshy.com/nonshy/website/pkg/utility"
)
func TestAge(t *testing.T) {
var now = time.Date(2022, 6, 15, 12, 0, 0, 0, time.UTC)
var tests = []struct {
In string
Expect int
}{
{
In: "1987-10-12",
Expect: 34,
},
{
In: "2022-06-15",
Expect: 0,
},
{
In: "1993-07-04",
Expect: 28,
},
{
In: "1996-06-17",
2023-10-08 22:14:09 +00:00
Expect: 25,
},
{
In: "1996-06-15",
Expect: 26,
},
{
In: "1996-06-14",
2023-10-08 22:14:09 +00:00
Expect: 26,
},
{
In: "2000-01-01",
Expect: 22,
},
{
In: "2000-05-30",
Expect: 22,
},
{
In: "2000-06-12",
2023-10-08 22:14:09 +00:00
Expect: 22,
},
{
In: "2000-06-14",
2023-10-08 22:14:09 +00:00
Expect: 22,
},
{
In: "2000-06-15",
Expect: 22,
},
{
In: "2000-06-16",
2023-10-08 22:14:09 +00:00
Expect: 21,
},
}
for _, test := range tests {
dob, _ := time.Parse("2006-01-02", test.In)
actual := utility.AgeAt(dob, now)
if actual != test.Expect {
t.Errorf("Expected %s to be age %d but got %d", test.In, test.Expect, actual)
}
}
}