-
Notifications
You must be signed in to change notification settings - Fork 0
/
age_test.go
67 lines (58 loc) · 1.41 KB
/
age_test.go
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
package age
import (
"testing"
"time"
)
var peopleList = []struct {
TestDate time.Time
Birthdate time.Time
ExpAge int
}{
{
time.Date(2016, time.July, 01, 15, 0, 0, 0, time.UTC),
time.Date(1986, time.January, 03, 10, 0, 0, 0, time.UTC),
30,
},
{
time.Date(2016, time.July, 01, 15, 0, 0, 0, time.UTC),
time.Date(1986, time.August, 03, 10, 0, 0, 0, time.UTC),
29,
},
{
time.Date(2016, time.July, 01, 15, 0, 0, 0, time.UTC),
time.Date(1980, time.March, 18, 10, 0, 0, 0, time.UTC),
36,
},
{
time.Date(2016, time.July, 01, 15, 0, 0, 0, time.UTC),
time.Date(1980, time.October, 18, 10, 0, 0, 0, time.UTC),
35,
},
{
time.Date(2016, time.July, 01, 15, 0, 0, 0, time.UTC),
time.Date(1986, time.July, 01, 15, 0, 0, 0, time.UTC),
30,
},
}
func TestAge(t *testing.T) {
for _, p := range peopleList {
age, err := ageAt(p.TestDate, p.Birthdate)
if err != nil {
t.Errorf("expect the error to be nil but got: %v", err)
}
if p.ExpAge != age {
t.Errorf("expect the age to be %v but was %v", p.ExpAge, age)
}
}
}
func TestBirthdateAfterNow(t *testing.T) {
testDate := time.Date(2016, time.July, 01, 15, 0, 0, 0, time.UTC)
birthdate := time.Date(2016, time.October, 18, 10, 0, 0, 0, time.UTC)
age, err := ageAt(testDate, birthdate)
if err == nil {
t.Error("expect the error to not be nil but got nil")
}
if age != 0 {
t.Errorf("expect the age to be 0 but was %v", age)
}
}