-
Notifications
You must be signed in to change notification settings - Fork 37
/
date_test.go
154 lines (137 loc) · 3.23 KB
/
date_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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package faker
import (
"testing"
"time"
)
func TestDateBetween(t *testing.T) {
from := time.Now().AddDate(0, -1, 0)
examples := []struct {
from time.Time
to time.Time
}{
{from, from},
{from, from.Add(time.Microsecond)},
{from, from.Add(time.Millisecond)},
{from, from.Add(time.Second)},
{from, from.Add(time.Minute)},
{from, from.Add(time.Hour)},
{from, from.AddDate(0, 0, 1)},
{from, from.AddDate(0, 1, 0)},
{from, from.AddDate(1, 0, 0)},
{from, from.AddDate(10, 0, 0)},
}
for _, x := range examples {
d := Date().Between(x.from, x.to)
if d.Unix() < x.from.Unix() || d.Unix() > x.to.Unix() {
t.Errorf("expected %v to be in range [%v, %v]", d, x.from, x.to)
}
}
}
func TestDateBetweenInvalidRange(t *testing.T) {
defer func() {
if err := recover(); err != nil {
// ignore
}
}()
now := time.Now()
Date().Between(now.Add(time.Hour), now)
t.Errorf("expected invalid time range panic")
}
func TestDateForward(t *testing.T) {
now := time.Now()
examples := []time.Duration{
0,
time.Microsecond,
time.Millisecond,
time.Second,
time.Minute,
time.Hour,
now.AddDate(0, 0, 1).Sub(now),
now.AddDate(0, 1, 0).Sub(now),
now.AddDate(1, 0, 0).Sub(now),
now.AddDate(10, 0, 0).Sub(now),
}
for _, x := range examples {
d := Date().Forward(x)
from := now.Unix()
to := now.Add(x).Unix()
if d.Unix() < from || d.Unix() > to {
t.Errorf("expected %v to be in range [%v, %v]", d, time.Unix(from, 0), time.Unix(to, 0))
}
}
}
func TestDateForwardInvalidDuration(t *testing.T) {
defer func() {
if err := recover(); err != nil {
// ignore
}
}()
Date().Forward(-1 * time.Hour)
t.Errorf("expected invalid duration panic")
}
func TestDateBackward(t *testing.T) {
now := time.Now()
examples := []time.Duration{
0,
time.Microsecond,
time.Millisecond,
time.Second,
time.Minute,
time.Hour,
now.AddDate(0, 0, 1).Sub(now),
now.AddDate(0, 1, 0).Sub(now),
now.AddDate(1, 0, 0).Sub(now),
now.AddDate(10, 0, 0).Sub(now),
}
for _, x := range examples {
d := Date().Backward(x)
from := now.Add(-x).Unix()
to := now.Unix()
if d.Unix() < from || d.Unix() > to {
t.Errorf("expected %v to be in range [%v, %v]", d, time.Unix(from, 0), time.Unix(to, 0))
}
}
}
func TestDateBackwardInvalidDuration(t *testing.T) {
defer func() {
if err := recover(); err != nil {
// ignore
}
}()
Date().Backward(-1 * time.Second)
t.Errorf("expected invalid duration panic")
}
func TestBirthday(t *testing.T) {
now := time.Now()
examples := []struct {
minAge int
maxAge int
}{
{42, 42},
{20, 21},
{22, 32},
{21, 65},
{1, 99},
}
for _, x := range examples {
d := Date().Birthday(x.minAge, x.maxAge)
from := now.AddDate(-x.maxAge, 0, 0).Unix()
to := now.AddDate(-x.minAge, 0, 0).Unix()
if d.Unix() < from || d.Unix() > to {
t.Errorf("expected %v to be in range [%v, %v]", d, time.Unix(from, 0), time.Unix(to, 0))
}
age := now.Year() - d.Year()
if age < x.minAge || age > x.maxAge {
t.Errorf("expected age %d to be in range [%d, %d]", age, x.minAge, x.maxAge)
}
}
}
func TestDateBirthdayInvalidRange(t *testing.T) {
defer func() {
if err := recover(); err != nil {
// ignore
}
}()
Date().Birthday(45, 25)
t.Errorf("expected invalid age range panic")
}