-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
naturaldate_test.go
270 lines (243 loc) · 10.4 KB
/
naturaldate_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
package naturaldate
import (
"log"
"testing"
"time"
"github.com/tj/assert"
)
// base time.
var base = time.Unix(1574687238, 0).UTC()
// pastCases are test cases for the past direction.
var pastCases = []struct {
Input string
Output string
}{
// now
{`now`, `2019-11-25 13:07:18 +0000 UTC`},
{`right now`, `2019-11-25 13:07:18 +0000 UTC`},
{` right now `, `2019-11-25 13:07:18 +0000 UTC`},
// minutes
{`1 minute`, `2019-11-25 13:06:18 +0000 UTC`},
{`next minute`, `2019-11-25 13:08:18 +0000 UTC`},
{`last minute`, `2019-11-25 13:06:18 +0000 UTC`},
{`one minute`, `2019-11-25 13:06:18 +0000 UTC`},
{`1 minute ago`, `2019-11-25 13:06:18 +0000 UTC`},
{`5 minutes ago`, `2019-11-25 13:02:18 +0000 UTC`},
{`five minutes ago`, `2019-11-25 13:02:18 +0000 UTC`},
{` 5 minutes ago `, `2019-11-25 13:02:18 +0000 UTC`},
{`2 minutes from now`, `2019-11-25 13:09:18 +0000 UTC`},
{`two minutes from now`, `2019-11-25 13:09:18 +0000 UTC`},
{`Message me in 2 minutes`, `2019-11-25 13:09:18 +0000 UTC`},
{`Message me in 2 minutes from now`, `2019-11-25 13:09:18 +0000 UTC`},
// hours
{`1 hour`, `2019-11-25 12:07:18 +0000 UTC`},
{`last hour`, `2019-11-25 12:07:18 +0000 UTC`},
{`next hour`, `2019-11-25 14:07:18 +0000 UTC`},
{`1 hour ago`, `2019-11-25 12:07:18 +0000 UTC`},
{`6 hours ago`, `2019-11-25 07:07:18 +0000 UTC`},
{`1 hour from now`, `2019-11-25 14:07:18 +0000 UTC`},
{`Remind me in 1 hour`, `2019-11-25 14:07:18 +0000 UTC`},
{`Remind me in 1 hour from now`, `2019-11-25 14:07:18 +0000 UTC`},
{`Remind me in 1 hour and 3 minutes from now`, `2019-11-25 14:10:18 +0000 UTC`},
{`Remind me in an hour`, `2019-11-25 14:07:18 +0000 UTC`},
{`Remind me in an hour from now`, `2019-11-25 14:07:18 +0000 UTC`},
// days
{`1 day`, `2019-11-24 00:00:00 +0000 UTC`},
{`next day`, `2019-11-26 00:00:00 +0000 UTC`},
{`1 day ago`, `2019-11-24 00:00:00 +0000 UTC`},
{`3 days ago`, `2019-11-22 00:00:00 +0000 UTC`},
{`3 days ago at 11:25am`, `2019-11-22 11:25:00 +0000 UTC`},
{`1 day from now`, `2019-11-26 13:07:18 +0000 UTC`},
{`Remind me one day from now`, `2019-11-26 13:07:18 +0000 UTC`},
{`Remind me in a day`, `2019-11-26 13:07:18 +0000 UTC`},
{`Remind me in one day`, `2019-11-26 13:07:18 +0000 UTC`},
{`Remind me in one day from now`, `2019-11-26 13:07:18 +0000 UTC`},
// weeks
{`1 week`, `2019-11-18 00:00:00 +0000 UTC`},
{`1 week ago`, `2019-11-18 00:00:00 +0000 UTC`},
{`2 weeks ago`, `2019-11-11 00:00:00 +0000 UTC`},
{`2 weeks ago at 8am`, `2019-11-11 08:00:00 +0000 UTC`},
{`next week`, `2019-12-02 00:00:00 +0000 UTC`},
{`Message me in a week`, `2019-12-02 13:07:18 +0000 UTC`},
{`Message me in one week`, `2019-12-02 13:07:18 +0000 UTC`},
{`Message me in one week from now`, `2019-12-02 13:07:18 +0000 UTC`},
{`Message me in two weeks from now`, `2019-12-09 13:07:18 +0000 UTC`},
{`Message me two weeks from now`, `2019-12-09 13:07:18 +0000 UTC`},
{`Message me in two weeks`, `2019-12-09 13:07:18 +0000 UTC`},
// months
{`1 month ago`, `2019-10-25 13:07:18 +0000 UTC`},
{`last month`, `2019-10-25 13:07:18 +0000 UTC`},
{`next month`, `2019-12-25 13:07:18 +0000 UTC`},
{`1 month ago at 9:30am`, `2019-10-25 09:30:00 +0000 UTC`},
{`2 months ago`, `2019-09-25 13:07:18 +0000 UTC`},
{`12 months ago`, `2018-11-25 13:07:18 +0000 UTC`},
{`1 month from now`, `2019-12-25 13:07:18 +0000 UTC`},
{`next 2 months`, `2020-01-25 13:07:18 +0000 UTC`},
{`2 months from now`, `2020-01-25 13:07:18 +0000 UTC`},
{`12 months from now at 6am`, `2020-11-25 06:00:00 +0000 UTC`},
{`Remind me in 12 months from now at 6am`, `2020-11-25 06:00:00 +0000 UTC`},
{`Remind me in a month`, `2019-12-25 13:07:18 +0000 UTC`},
{`Remind me in 2 months`, `2020-01-25 13:07:18 +0000 UTC`},
{`Remind me in a month from now`, `2019-12-25 13:07:18 +0000 UTC`},
{`Remind me in 2 months from now`, `2020-01-25 13:07:18 +0000 UTC`},
// years
{`last year`, `2018-11-25 13:07:18 +0000 UTC`},
{`next year`, `2020-11-25 13:07:18 +0000 UTC`},
{`one year ago`, `2018-11-25 13:07:18 +0000 UTC`},
{`one year from now`, `2020-11-25 13:07:18 +0000 UTC`},
{`two years ago`, `2017-11-25 13:07:18 +0000 UTC`},
{`2 years ago`, `2017-11-25 13:07:18 +0000 UTC`},
{`Remind me in one year from now`, `2020-11-25 13:07:18 +0000 UTC`},
{`Remind me in a year`, `2020-11-25 13:07:18 +0000 UTC`},
{`Remind me in a year from now`, `2020-11-25 13:07:18 +0000 UTC`},
// today
{`today`, `2019-11-25 00:00:00 +0000 UTC`},
{`today at 10am`, `2019-11-25 10:00:00 +0000 UTC`},
// yesterday
{`yesterday`, `2019-11-24 00:00:00 +0000 UTC`},
{`yesterday 10am`, `2019-11-24 10:00:00 +0000 UTC`},
{`yesterday at 10am`, `2019-11-24 10:00:00 +0000 UTC`},
{`yesterday at 10:15am`, `2019-11-24 10:15:00 +0000 UTC`},
// tomorrow
{`tomorrow`, `2019-11-26 00:00:00 +0000 UTC`},
{`tomorrow 10am`, `2019-11-26 10:00:00 +0000 UTC`},
{`tomorrow at 10am`, `2019-11-26 10:00:00 +0000 UTC`},
{`tomorrow at 10:15am`, `2019-11-26 10:15:00 +0000 UTC`},
// past weekdays
{`sunday`, `2019-11-24 00:00:00 +0000 UTC`},
{`monday`, `2019-11-18 00:00:00 +0000 UTC`},
{`tuesday`, `2019-11-19 00:00:00 +0000 UTC`},
{`wednesday`, `2019-11-20 00:00:00 +0000 UTC`},
{`thursday`, `2019-11-21 00:00:00 +0000 UTC`},
{`friday`, `2019-11-22 00:00:00 +0000 UTC`},
{`saturday`, `2019-11-23 00:00:00 +0000 UTC`},
{`last sunday`, `2019-11-24 00:00:00 +0000 UTC`},
{`past sunday`, `2019-11-24 00:00:00 +0000 UTC`},
{`last monday`, `2019-11-18 00:00:00 +0000 UTC`},
{`last tuesday`, `2019-11-19 00:00:00 +0000 UTC`},
{`last wednesday`, `2019-11-20 00:00:00 +0000 UTC`},
{`last thursday`, `2019-11-21 00:00:00 +0000 UTC`},
{`last friday`, `2019-11-22 00:00:00 +0000 UTC`},
{`last saturday`, `2019-11-23 00:00:00 +0000 UTC`},
// future weekdays
{`next tuesday`, `2019-11-26 00:00:00 +0000 UTC`},
{`next wednesday`, `2019-11-27 00:00:00 +0000 UTC`},
{`next thursday`, `2019-11-28 00:00:00 +0000 UTC`},
{`next friday`, `2019-11-29 00:00:00 +0000 UTC`},
{`next saturday`, `2019-11-30 00:00:00 +0000 UTC`},
{`next sunday`, `2019-12-01 00:00:00 +0000 UTC`},
{`next monday`, `2019-12-02 00:00:00 +0000 UTC`},
// months
{`last january`, `2019-01-25 13:07:18 +0000 UTC`},
{`next january`, `2020-01-25 13:07:18 +0000 UTC`},
{`january`, `2019-01-25 13:07:18 +0000 UTC`},
{`february`, `2019-02-25 13:07:18 +0000 UTC`},
{`march`, `2019-03-25 13:07:18 +0000 UTC`},
{`april`, `2019-04-25 13:07:18 +0000 UTC`},
{`may`, `2019-05-25 13:07:18 +0000 UTC`},
{`june`, `2019-06-25 13:07:18 +0000 UTC`},
{`july`, `2019-07-25 13:07:18 +0000 UTC`},
{`august`, `2019-08-25 13:07:18 +0000 UTC`},
{`september`, `2019-09-25 13:07:18 +0000 UTC`},
{`october`, `2019-10-25 13:07:18 +0000 UTC`},
{`november`, `2018-11-25 13:07:18 +0000 UTC`},
// ordinal dates
{`november 15th`, `2018-11-15 13:07:18 +0000 UTC`},
{`december 1st`, `2018-12-01 13:07:18 +0000 UTC`},
{`december 2nd`, `2018-12-02 13:07:18 +0000 UTC`},
{`december 3rd`, `2018-12-03 13:07:18 +0000 UTC`},
{`december 4th`, `2018-12-04 13:07:18 +0000 UTC`},
{`december 15th`, `2018-12-15 13:07:18 +0000 UTC`},
{`december 23rd`, `2018-12-23 13:07:18 +0000 UTC`},
{`december 23rd 5pm`, `2018-12-23 17:00:00 +0000 UTC`},
{`december 23rd at 5pm`, `2018-12-23 17:00:00 +0000 UTC`},
{`december 23rd at 5:25pm`, `2018-12-23 17:25:00 +0000 UTC`},
// 12-hour clock
{`10am`, `2019-11-25 10:00:00 +0000 UTC`},
{`10 am`, `2019-11-25 10:00:00 +0000 UTC`},
{`5pm`, `2019-11-25 17:00:00 +0000 UTC`},
{`10:25am`, `2019-11-25 10:25:00 +0000 UTC`},
{`1:05pm`, `2019-11-25 13:05:00 +0000 UTC`},
{`10:25:10am`, `2019-11-25 10:25:10 +0000 UTC`},
{`1:05:10pm`, `2019-11-25 13:05:10 +0000 UTC`},
// 24-hour clock
{`10`, `2019-11-25 10:00:00 +0000 UTC`},
{`10:25`, `2019-11-25 10:25:00 +0000 UTC`},
{`10:25:30`, `2019-11-25 10:25:30 +0000 UTC`},
{`17`, `2019-11-25 17:00:00 +0000 UTC`},
{`17:25:30`, `2019-11-25 17:25:30 +0000 UTC`},
// case sensitivity
{`December 23rd AT 5:25 PM`, `2018-12-23 17:25:00 +0000 UTC`},
{`next December 23rd AT 5:25 PM`, `2019-12-23 17:25:00 +0000 UTC`},
// QA
{`Restart the server in 2 days from now`, `2019-11-27 13:07:18 +0000 UTC`},
{`Remind me on the 5th of next month`, `2019-12-05 13:07:18 +0000 UTC`},
{`Remind me on the 5th of next month at 7am`, `2019-12-05 07:00:00 +0000 UTC`},
{`Remind me at 7am on the 5th of next month`, `2019-12-05 07:00:00 +0000 UTC`},
{`Remind me in one month from now`, `2019-12-25 13:07:18 +0000 UTC`},
{`Remind me in one month from now at 7am`, `2019-12-25 07:00:00 +0000 UTC`},
// errors
{`10:am`, "\nparse error near PegText (line 1 symbol 1 - line 1 symbol 3):\n\"10\"\n"},
}
// futureCases are test cases for the future direction.
var futureCases = []struct {
Input string
Output string
}{
{`now`, `2019-11-25 13:07:18 +0000 UTC`},
{`1 minute`, `2019-11-25 13:08:18 +0000 UTC`},
{`1 hour`, `2019-11-25 14:07:18 +0000 UTC`},
{`1 day`, `2019-11-26 00:00:00 +0000 UTC`},
{`1 week`, `2019-12-02 00:00:00 +0000 UTC`},
{`previous tuesday`, `2019-11-19 00:00:00 +0000 UTC`},
{`tuesday`, `2019-11-26 00:00:00 +0000 UTC`},
{`wednesday`, `2019-11-27 00:00:00 +0000 UTC`},
{`thursday`, `2019-11-28 00:00:00 +0000 UTC`},
{`friday`, `2019-11-29 00:00:00 +0000 UTC`},
{`saturday`, `2019-11-30 00:00:00 +0000 UTC`},
{`sunday`, `2019-12-01 00:00:00 +0000 UTC`},
{`monday`, `2019-12-02 00:00:00 +0000 UTC`},
{`last january`, `2019-01-25 13:07:18 +0000 UTC`},
{`january`, `2020-01-25 13:07:18 +0000 UTC`},
{`next january`, `2020-01-25 13:07:18 +0000 UTC`},
{`Remind me on the December 25th at 7am`, `2019-12-25 07:00:00 +0000 UTC`},
{`Remind me at 7am on December 25th`, `2019-12-25 07:00:00 +0000 UTC`},
{`Remind me on the 25th of December at 7am`, `2019-12-25 07:00:00 +0000 UTC`},
{`Check logs in the past 5 minutes`, `2019-11-25 13:02:18 +0000 UTC`},
}
// Test parsing with past direction.
func TestParse_past(t *testing.T) {
for _, c := range pastCases {
t.Run(c.Input, func(t *testing.T) {
v, err := Parse(c.Input, base)
if err != nil {
assert.Equal(t, c.Output, err.Error())
return
}
assert.Equal(t, c.Output, v.UTC().String())
})
}
}
// Test parsing with future direction.
func TestParse_future(t *testing.T) {
for _, c := range futureCases {
t.Run(c.Input, func(t *testing.T) {
v, err := Parse(c.Input, base, WithDirection(Future))
if err != nil {
assert.Equal(t, c.Output, err.Error())
return
}
assert.Equal(t, c.Output, v.UTC().String())
})
}
}
// Benchmark parsing.
func BenchmarkParse(b *testing.B) {
b.SetBytes(1)
for i := 0; i < b.N; i++ {
_, err := Parse(`december 23rd at 5:25pm`, base)
if err != nil {
log.Fatalf("error: %s", err)
}
}
}