forked from go-shadow/moment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoment_test.go
283 lines (239 loc) · 8.11 KB
/
moment_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package moment
import (
"reflect"
"testing"
"time"
)
var (
oneDay = time.Duration(time.Hour * 24)
)
// Test some times at the start of the day with some daylight savings
func TestStartOfDay(t *testing.T) {
lon, _ := time.LoadLocation("Europe/London")
testCases := []struct {
before *Moment
after *Moment
}{
{
// Clocks +1 hour
before: NewMoment(time.Date(2015, 3, 29, 0, 0, 0, 0, lon)),
after: NewMoment(time.Date(2015, 3, 29, 0, 0, 0, 0, lon)),
},
{
// Clocks -1 hour
before: NewMoment(time.Date(2015, 10, 25, 2, 0, 0, 0, lon)),
after: NewMoment(time.Date(2015, 10, 25, 0, 0, 0, 0, lon)),
},
{
// Clocks -1 hour (same as above)
before: NewMoment(time.Date(2015, 10, 25, 12, 0, 0, 0, lon)),
after: NewMoment(time.Date(2015, 10, 25, 0, 0, 0, 0, lon)),
},
{
// Clocks no change
before: NewMoment(time.Date(2016, 1, 01, 10, 0, 0, 0, lon)),
after: NewMoment(time.Date(2016, 1, 01, 0, 0, 0, 0, lon)),
},
}
for _, test := range testCases {
res := test.before.StartOfDay()
if !reflect.DeepEqual(res, test.after) {
t.Errorf("Moment %s does not match expected %s", res.time.String(), test.after.time.String())
}
}
}
// Test some times at the end of the day with some daylight savings
func TestEndOfDay(t *testing.T) {
lon, _ := time.LoadLocation("Europe/London")
testCases := []struct {
before *Moment
after *Moment
}{
{
// Clocks +1 hour
before: NewMoment(time.Date(2015, 3, 29, 0, 0, 0, 0, lon)),
after: NewMoment(time.Date(2015, 3, 29, 23, 59, 0, 0, lon)),
},
{
// Clocks -1 hour
before: NewMoment(time.Date(2015, 10, 25, 2, 0, 0, 0, lon)),
after: NewMoment(time.Date(2015, 10, 25, 23, 59, 0, 0, lon)),
},
{
// Clocks -1 hour (same as above)
before: NewMoment(time.Date(2015, 10, 25, 12, 0, 0, 0, lon)),
after: NewMoment(time.Date(2015, 10, 25, 23, 59, 0, 0, lon)),
},
{
// Clocks no change
before: NewMoment(time.Date(2016, 1, 01, 10, 0, 0, 0, lon)),
after: NewMoment(time.Date(2016, 1, 01, 23, 59, 0, 0, lon)),
},
}
for _, test := range testCases {
res := test.before.EndOfDay()
if !reflect.DeepEqual(res, test.after) {
t.Errorf("Moment %s does not match expected %s", res.time.String(), test.after.time.String())
}
}
}
func TestYesterdayTodayTomorrow(t *testing.T) {
now := time.Now()
today := New().Strtotime("today").GetTime()
if now.Sub(today) > time.Duration(1*time.Second) {
t.Fatalf("Expected %s, got %s", now.Format(time.RFC3339), today.Format(time.RFC3339))
}
tomorrow := New().Strtotime("tomorrow").GetTime()
if today.After(tomorrow) {
t.Fatalf("Expected %s to be before %s", today.Format(time.RFC3339), tomorrow.Format(time.RFC3339))
}
tomorrow = New().Strtotime("+1day").GetTime()
if today.After(tomorrow) {
t.Fatalf("Expected %s to be before %s", today.Format(time.RFC3339), tomorrow.Format(time.RFC3339))
}
yesterday := New().Strtotime("yesterday").GetTime()
if yesterday.After(today) {
t.Fatalf("Expected %s to be before %s", yesterday.Format(time.RFC3339), today.Format(time.RFC3339))
}
yesterday = New().Strtotime("-1day").GetTime()
if yesterday.After(today) {
t.Fatalf("Expected %s to be before %s", yesterday.Format(time.RFC3339), today.Format(time.RFC3339))
}
today = today.Truncate(oneDay)
tomorrow = tomorrow.Truncate(oneDay)
yesterday = yesterday.Truncate(oneDay)
diff := tomorrow.Sub(today)
if diff != oneDay {
t.Fatalf("Expected different between %s and %s to be one day - got %s", today.Format(time.RFC3339), tomorrow.Format(time.RFC3339), diff)
}
diff = today.Sub(yesterday)
if diff != time.Duration(time.Hour*24) {
t.Fatalf("Expected different between %s and %s to be one day - got %s", yesterday.Format(time.RFC3339), today.Format(time.RFC3339), diff)
}
}
func checkTimeFormat(now time.Time, layout string, s string) (time.Time, bool) {
timeToday := NewMoment(now).Strtotime(s).GetTime()
if timeToday.Format(layout) != s {
return timeToday, false
}
return timeToday, true
}
func TestHour(t *testing.T) {
type TimeFormatCheck struct {
Format string
Value string
}
pairs := []TimeFormatCheck{
TimeFormatCheck{
Format: "15:04",
Value: "12:45",
},
TimeFormatCheck{
Format: "15:04:05.999999999",
Value: "12:45:01.112233445",
},
}
now := time.Now().Truncate(time.Second).Add(time.Nanosecond * 112233445)
for _, p := range pairs {
if timeToCheck, ok := checkTimeFormat(now, p.Format, p.Value); !ok {
t.Fatalf("Expected %s, got %s", p.Value, timeToCheck.Format(p.Format))
}
}
}
func TestNextLast(t *testing.T) {
var n, d time.Time
// next monday
d = time.Date(2016, 1, 30, 23, 59, 59, 0, time.UTC)
nextMonday := time.Date(2016, 2, 1, 23, 59, 59, 0, time.UTC)
n = NewMoment(d).Strtotime("next monday").GetTime()
if nextMonday != n {
t.Fatalf("Expected next monday to be %v, got %v instead", nextMonday, n)
}
// last monday
d = time.Date(2016, 1, 30, 23, 59, 59, 0, time.UTC)
lastMonday := time.Date(2016, 1, 25, 23, 59, 59, 0, time.UTC)
n = NewMoment(d).Strtotime("last monday").GetTime()
if lastMonday != n {
t.Fatalf("Expected last monday to be %v, got %v instead", lastMonday, n)
}
// next week
d = time.Date(2016, 1, 30, 23, 59, 59, 0, time.UTC)
nextWeek := time.Date(2016, 2, 6, 23, 59, 59, 0, time.UTC)
n = NewMoment(d).Strtotime("next week").GetTime()
if nextWeek != n {
t.Fatalf("Expected next week to be %v, got %v instead", nextWeek, n)
}
// last week
d = time.Date(2016, 1, 30, 23, 59, 59, 0, time.UTC)
lastWeek := time.Date(2016, 1, 23, 23, 59, 59, 0, time.UTC)
n = NewMoment(d).Strtotime("last week").GetTime()
if lastWeek != n {
t.Fatalf("Expected last week to be %v, got %v instead", lastWeek, n)
}
// next month
d = time.Date(2016, 1, 30, 23, 59, 59, 0, time.UTC)
nextMonth := time.Date(2016, 3, 1, 23, 59, 59, 0, time.UTC)
n = NewMoment(d).Strtotime("next month").GetTime()
if nextMonth != n {
t.Fatalf("Expected next month to be %v, got %v instead", nextMonth, n)
}
// last month
d = time.Date(2016, 1, 30, 23, 59, 59, 0, time.UTC)
lastMonth := time.Date(2015, 12, 30, 23, 59, 59, 0, time.UTC)
n = NewMoment(d).Strtotime("last month").GetTime()
if lastMonth != n {
t.Fatalf("Expected last month to be %v, got %v instead", lastMonth, n)
}
// next year
d = time.Date(2016, 1, 30, 23, 59, 59, 0, time.UTC)
nextYear := time.Date(2017, 1, 30, 23, 59, 59, 0, time.UTC)
n = NewMoment(d).Strtotime("next year").GetTime()
if nextYear != n {
t.Fatalf("Expected next year to be %v, got %v instead", nextYear, n)
}
// last year
d = time.Date(2016, 1, 30, 23, 59, 59, 0, time.UTC)
lastYear := time.Date(2015, 1, 30, 23, 59, 59, 0, time.UTC)
n = NewMoment(d).Strtotime("last year").GetTime()
if lastYear != n {
t.Fatalf("Expected last year to be %v, got %v instead", lastYear, n)
}
}
func TestAgo(t *testing.T) {
var n, d, newDate time.Time
// one hour ago
d = time.Date(2016, 1, 30, 23, 59, 59, 0, time.UTC)
newDate = time.Date(2016, 1, 30, 22, 59, 59, 0, time.UTC)
n = NewMoment(d).Strtotime("one hour ago").GetTime()
if newDate != n {
t.Fatalf("Expected an hour ago to be %v, got %v instead", newDate, n)
}
// one day ago
d = time.Date(2016, 1, 30, 23, 59, 59, 0, time.UTC)
newDate = time.Date(2016, 1, 29, 23, 59, 59, 0, time.UTC)
n = NewMoment(d).Strtotime("one day ago").GetTime()
if newDate != n {
t.Fatalf("Expected an day ago to be %v, got %v instead", newDate, n)
}
// one week ago
d = time.Date(2016, 1, 30, 23, 59, 59, 0, time.UTC)
newDate = time.Date(2016, 1, 23, 23, 59, 59, 0, time.UTC)
n = NewMoment(d).Strtotime("one week ago").GetTime()
if newDate != n {
t.Fatalf("Expected an week ago to be %v, got %v instead", newDate, n)
}
// one month ago
d = time.Date(2016, 1, 30, 23, 59, 59, 0, time.UTC)
newDate = time.Date(2015, 12, 30, 23, 59, 59, 0, time.UTC)
n = NewMoment(d).Strtotime("one month ago").GetTime()
if newDate != n {
t.Fatalf("Expected an month ago to be %v, got %v instead", newDate, n)
}
// one year ago
d = time.Date(2016, 1, 30, 23, 59, 59, 0, time.UTC)
newDate = time.Date(2015, 1, 30, 23, 59, 59, 0, time.UTC)
n = NewMoment(d).Strtotime("one year ago").GetTime()
if newDate != n {
t.Fatalf("Expected an year ago to be %v, got %v instead", newDate, n)
}
}