-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreplace_test.go
579 lines (546 loc) · 18.2 KB
/
replace_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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
package anytime
import (
"fmt"
"math/rand"
"reflect"
"regexp"
"strings"
"testing"
"time"
)
func BenchmarkReplaceAllRangesByFunc_doItNowPoem(b *testing.B) {
now := time.UnixMilli(rand.Int63())
s := `
Do It Now
by Berton Braley
IF WITH PLEASURE you are viewing any work a man is doing,
If you like him or you love him, tell him now;
Don't withhold your approbation till the parson makes oration
And he lies with snowy lilies on his brow;
No matter how you shout it he won't really care about it;
He won't know how many teardrops you have shed;
If you think some praise is due him now's the time to slip it to him,
For he cannot read his tombstone when he's dead.`
nowRx := regexp.MustCompile(`(?i)\bnow\b`)
want := nowRx.ReplaceAllString(s, fmt.Sprintf("%v", now.UnixMilli()))
f := func(_ string, r Range) string {
return fmt.Sprintf("%v", r.Start().UnixMilli())
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
got := ReplaceAllRangesByFunc(s, now, Past, f)
if got != want {
b.Errorf("got = %v\n\nwant %v", got, want)
}
}
}
func BenchmarkReplaceTimesByFunc_jabberwockyPoem(b *testing.B) {
now := time.UnixMilli(rand.Int63())
s := `'Twas brillig and the slithy toves did gyre and gimble in 50 wabe. All mimsy were the borogroves and the mome raths outgrabe. Beware the jabberwock my son, the jaws that bite January 2020 the claws that catch. Avoid the jubjub bird and shun the frumious bandersnatch. He took his vorpal sword in hand, longtime...`
want := strings.ReplaceAll(s, "January 2020", "<range>")
for i := 0; i < b.N; i++ {
got := ReplaceAllRangesByFunc(s, now, Future, func(src string, r Range) string {
return "<range>"
})
if got != want {
b.Fatalf("\ngot %q\nwant %q", got, want)
}
}
}
func TestReplaceAllRangesByFunc_nows(t *testing.T) {
now := time.UnixMilli(rand.Int63())
nowRx := regexp.MustCompile(`(?i)\bnow\b`)
f := func(src string, r Range) string {
return fmt.Sprintf("%v", r.Start().UnixMilli())
}
tests := []struct {
name string
input string
}{
{"empty", ""},
{"one space", " "},
{"verbiage", "This string contains no times or dates."},
{"now", "now"},
{"space now", " now"},
{"now space", "now "},
{"space now space", " now "},
{"all caps now", "NOW"},
{"now verbiage", "now is the time"},
{"verbiage now", "the time is now"},
{"verbiage now verbiage", "Without a doubt now is the time."},
{"verbiage now punctuation verbiage", "If you don't know me by now, you will never know me."},
{"now now", "now now"},
{"nownow", "nownow"},
{"noise with two nows", "a;slas 😅dflasdjfla now laksjdfsdf xxc,mnv as2w0 @#R$@$ 😑now😵💫 ;xlc x;c,nv.s,hriop4qu-u98dsvfjkldfljs $!@@#$WERTwe5u682470sZ)(*&Y)*("},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ReplaceAllRangesByFunc(tt.input, now, Future, f)
want := nowRx.ReplaceAllString(tt.input, fmt.Sprintf("%v", now.UnixMilli()))
if got != want {
t.Errorf("\ngot\n%q\nwant\n%q", got, want)
}
})
}
}
func TestReplaceAllRangesByFunc_noReplacementCrossTalk(t *testing.T) {
input := "last year week"
f := func(src string, r Range) string {
return "maybe next"
}
want := "maybe next week"
got := ReplaceAllRangesByFunc(input, time.Time{}, Future, f)
if got != want {
t.Fatalf("got = %q, want %q", got, want)
}
}
func TestReplaceAllRangesByFunc_lastYearReplacements(t *testing.T) {
now := time.UnixMilli(rand.Int63())
ly := lastYear(now)
f := func(src string, r Range) string {
return fmt.Sprintf("%v - %v", r.Start().UnixMilli(), r.End().UnixMilli())
}
inputs := []string{
"last year",
`"last year"`,
"a last year",
"last year a",
"a last year a",
"a last year last year a",
"a (last year, last year) a",
}
for _, input := range inputs {
t.Run(input, func(t *testing.T) {
got := ReplaceAllRangesByFunc(input, now, Past, f)
want := strings.ReplaceAll(input, "last year", fmt.Sprintf("%v - %v", ly.Start().UnixMilli(), ly.End().UnixMilli()))
if got != want {
t.Errorf("got = %v, want %v", got, want)
}
})
}
}
func TestReplaceAllRangesByFunc_lastYearPlusVerbiage(t *testing.T) {
now := time.UnixMilli(rand.Int63())
wantRange := truncateYear(now.AddDate(-1, 0, 0))
f := func(src string, r Range) string {
return fmt.Sprintf("%v", r.Start().UnixMilli())
}
inputStr := `foo last year bar`
gotStr := ReplaceAllRangesByFunc(inputStr, now, Past, f)
unixStr := fmt.Sprintf("%v", wantRange.Start().UnixMilli())
wantStr := strings.ReplaceAll(inputStr, "last year", unixStr)
if gotStr != wantStr {
t.Errorf("gotStr = %v, wantStr %v", gotStr, wantStr)
}
}
func TestReplaceAllRangesByFunc_ok(t *testing.T) {
now := time.Date(2022, 9, 29, 2, 48, 33, 123, time.UTC)
var cases = []struct {
Input string
WantRange Range
}{
// years
{`Last year`, truncateYear(now.AddDate(-1, 0, 0))},
{`last year`, truncateYear(now.AddDate(-1, 0, 0))},
{`This year`, truncateYear(now)},
{`Next year`, truncateYear(now.AddDate(1, 0, 0))},
// yesterday
{`Yesterday`, truncateDay(now.AddDate(0, 0, -1))},
// today
{`Today`, truncateDay(now)},
// tomorrow
{`Tomorrow`, truncateDay(now.AddDate(0, 0, 1))},
//// weeks
{`Last week`, truncateWeek(now.AddDate(0, 0, -7))},
{`This week`, truncateWeek(now)},
{`Next week`, truncateWeek(now.AddDate(0, 0, 7))},
// months
{`Last month`, truncateMonth(now.AddDate(0, -1, 0))},
{`This month`, truncateMonth(now)},
{`Next month`, truncateMonth(now.AddDate(0, 1, 0))},
// absolute dates
{"January 2017", truncateMonth(time.Date(2017, 1, 1, 0, 0, 0, 0, now.Location()))},
{"Jan 2017", truncateMonth(time.Date(2017, 1, 1, 0, 0, 0, 0, now.Location()))},
{"March 31", truncateDay(time.Date(2023, 3, 31, 0, 0, 0, 0, now.Location()))},
{"January, 2017", truncateMonth(time.Date(2017, 1, 1, 0, 0, 0, 0, now.Location()))},
{"Jan, 2017", truncateMonth(time.Date(2017, 1, 1, 0, 0, 0, 0, now.Location()))},
{"April 3 2017", truncateDay(time.Date(2017, 4, 3, 0, 0, 0, 0, now.Location()))},
{"April 3, 2017", truncateDay(time.Date(2017, 4, 3, 0, 0, 0, 0, now.Location()))},
{"Oct 7, 1970", truncateDay(time.Date(1970, 10, 7, 0, 0, 0, 0, now.Location()))},
{"Oct 7, 1970 UTC+3", truncateDay(time.Date(1970, 10, 7, 0, 0, 0, 0, fixedZone(3)))},
{"Oct 7 1970", truncateDay(time.Date(1970, 10, 7, 0, 0, 0, 0, now.Location()))},
{"Oct. 7, 1970", truncateDay(time.Date(1970, 10, 7, 0, 0, 0, 0, now.Location()))},
{"September 17, 2012 UTC+7", truncateDay(time.Date(2012, 9, 17, 10, 9, 0, 0, fixedZone(7)))},
{"September 17, 2012 UTC-7", truncateDay(time.Date(2012, 9, 17, 10, 9, 0, 0, fixedZone(-7)))},
{"September 17, 2012", truncateDay(time.Date(2012, 9, 17, 10, 9, 0, 0, now.Location()))},
{"7 oct 1970", truncateDay(time.Date(1970, 10, 7, 0, 0, 0, 0, now.Location()))},
{"7 oct, 1970", truncateDay(time.Date(1970, 10, 7, 0, 0, 0, 0, now.Location()))},
{"03 February 2013", truncateDay(time.Date(2013, 2, 3, 0, 0, 0, 0, now.Location()))},
{"2 July 2013", truncateDay(time.Date(2013, 7, 2, 0, 0, 0, 0, now.Location()))},
{"2022 Feb 1", truncateDay(time.Date(2022, 2, 1, 0, 0, 0, 0, now.Location()))},
//// yyyy/mm/dd, dd/mm/yyyy etc.
{"2014/3/31", truncateDay(time.Date(2014, 3, 31, 0, 0, 0, 0, now.Location()))},
{"2014/3/31 UTC", truncateDay(time.Date(2014, 3, 31, 0, 0, 0, 0, time.UTC))},
{"2014/3/31 UTC+1", truncateDay(time.Date(2014, 3, 31, 0, 0, 0, 0, fixedZone(1)))},
{"2014/03/31", truncateDay(time.Date(2014, 3, 31, 0, 0, 0, 0, now.Location()))},
{"2014/03/31 UTC-1", truncateDay(time.Date(2014, 3, 31, 0, 0, 0, 0, fixedZone(-1)))},
{"2014-04-26", truncateDay(time.Date(2014, 4, 26, 0, 0, 0, 0, now.Location()))},
{"2014-4-26", truncateDay(time.Date(2014, 4, 26, 0, 0, 0, 0, now.Location()))},
{"2014-4-6", truncateDay(time.Date(2014, 4, 6, 0, 0, 0, 0, now.Location()))},
{"31/3/2014 UTC-8", truncateDay(time.Date(2014, 3, 31, 0, 0, 0, 0, fixedZone(-8)))},
{"31-3-2014 UTC-8", truncateDay(time.Date(2014, 3, 31, 0, 0, 0, 0, fixedZone(-8)))},
{"31/3/2014", truncateDay(time.Date(2014, 3, 31, 0, 0, 0, 0, now.Location()))},
{"31-3-2014", truncateDay(time.Date(2014, 3, 31, 0, 0, 0, 0, now.Location()))},
//// days
{`One day ago`, truncateDay(now.Add(-24 * time.Hour))},
{`1 day ago`, truncateDay(now.Add(-24 * time.Hour))},
{`3 days ago`, truncateDay(now.Add(-3 * 24 * time.Hour))},
{`Three days ago`, truncateDay(now.Add(-3 * 24 * time.Hour))},
{`1 day from now`, truncateDay(now.Add(24 * time.Hour))},
{`two days from now`, truncateDay(now.AddDate(0, 0, 2))},
{`two days from today`, truncateDay(now.AddDate(0, 0, 2))},
{`two days hence`, truncateDay(now.AddDate(0, 0, 2))},
// weeks
{`1 week ago`, truncateWeek(now.Add(-7 * 24 * time.Hour))},
{`2 weeks ago`, truncateWeek(now.Add(-2 * 7 * 24 * time.Hour))},
{`A week from now`, truncateWeek(now.Add(7 * 24 * time.Hour))},
{`A week from today`, truncateWeek(now.Add(7 * 24 * time.Hour))},
{`2 weeks hence`, truncateWeek(now.Add(2 * 7 * 24 * time.Hour))},
// months
{`A month ago`, truncateMonth(now.AddDate(0, -1, 0))},
{`1 month ago`, truncateMonth(now.AddDate(0, -1, 0))},
{`2 months ago`, truncateMonth(now.AddDate(0, -2, 0))},
{`12 months ago`, truncateMonth(now.AddDate(0, -12, 0))},
{`twelve months ago`, truncateMonth(now.AddDate(0, -12, 0))},
{`A month from now`, truncateMonth(now.AddDate(0, 1, 0))},
{`One month hence`, truncateMonth(now.AddDate(0, 1, 0))},
{`1 month from now`, truncateMonth(now.AddDate(0, 1, 0))},
{`2 months from now`, truncateMonth(now.AddDate(0, 2, 0))},
// years
{`One year ago`, truncateYear(now.AddDate(-1, 0, 0))},
{`One year from now`, truncateYear(now.AddDate(1, 0, 0))},
{`One year from today`, truncateYear(now.AddDate(1, 0, 0))},
{`One year hence`, truncateYear(now.AddDate(1, 0, 0))},
{`Two years ago`, truncateYear(now.AddDate(-2, 0, 0))},
{`2 years ago`, truncateYear(now.AddDate(-2, 0, 0))},
{`This year`, truncateYear(now)},
{`1999AD`, truncateYear(time.Date(1999, 1, 1, 0, 0, 0, 0, now.Location()))},
{`1999 AD`, truncateYear(time.Date(1999, 1, 1, 0, 0, 0, 0, now.Location()))},
{`2008CE`, truncateYear(time.Date(2008, 1, 1, 0, 0, 0, 0, now.Location()))},
{`2008 CE`, truncateYear(time.Date(2008, 1, 1, 0, 0, 0, 0, now.Location()))},
// RFC3339
{"2006-01-02T15:04:05Z", Range{time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC), time.Second}},
{"1990-12-31T15:59:59-08:00", Range{time.Date(1990, 12, 31, 15, 59, 59, 0, time.FixedZone("", -8*60*60)), time.Second}},
// from A to B
{
"From 3 feb 2022 to 6 oct 2022",
RangeFromTimes(
time.Date(2022, 2, 3, 0, 0, 0, 0, now.Location()),
time.Date(2022, 10, 6, 0, 0, 0, 0, now.Location()),
),
},
// A to B
{
"3 feb 2022 to 6 oct 2022",
RangeFromTimes(
time.Date(2022, 2, 3, 0, 0, 0, 0, now.Location()),
time.Date(2022, 10, 6, 0, 0, 0, 0, now.Location()),
),
},
// A - B
{
"3 feb 2022 - 6 oct 2022",
RangeFromTimes(
time.Date(2022, 2, 3, 0, 0, 0, 0, now.Location()),
time.Date(2022, 10, 6, 0, 0, 0, 0, now.Location()),
),
},
{
"2016/08/01 - 2016/08/31",
RangeFromTimes(
time.Date(2016, 8, 1, 0, 0, 0, 0, now.Location()),
time.Date(2016, 8, 31, 0, 0, 0, 0, now.Location()),
),
},
// A through B
{
"3 feb 2022 through 6 oct 2022",
RangeFromTimes(
time.Date(2022, 2, 3, 0, 0, 0, 0, now.Location()),
time.Date(2022, 10, 6, 0, 0, 0, 0, now.Location()),
),
},
// from A until B
{
"from 3 feb 2022 until 6 oct 2022",
RangeFromTimes(
time.Date(2022, 2, 3, 0, 0, 0, 0, now.Location()),
time.Date(2022, 10, 6, 0, 0, 0, 0, now.Location()),
),
},
}
for _, c := range cases {
t.Run(c.Input, func(t *testing.T) {
var foundRanges []Range
gotStr := ReplaceAllRangesByFunc(c.Input, now, Future, func(rs string, r Range) string {
foundRanges = append(foundRanges, r)
return "<range>"
})
if len(foundRanges) == 0 {
t.Fatal("no ranges found")
}
if len(foundRanges) > 1 {
t.Fatalf("got %d ranges, want 1", len(foundRanges))
}
gotRange := foundRanges[0]
if !gotRange.Equal(c.WantRange) {
t.Fatalf("got range %v, want %v", gotRange, c.WantRange)
}
wantStr := "<range>"
if gotStr != wantStr {
t.Fatalf("got string %q, want %q", gotStr, wantStr)
}
})
}
}
func TestReplaceAllRangesByFunc_colorMonth(t *testing.T) {
now := time.UnixMilli(rand.Int63())
var cases = []struct {
Input string
WantRange Range
}{
// color month
// http://www.jdawiseman.com/papers/trivia/futures.html
{"White october", nextSpecificMonth(now, time.October)},
{"Red october", truncateMonth(nextSpecificMonth(now, time.October).Start().AddDate(1, 0, 0))},
{"Green october", truncateMonth(nextSpecificMonth(now, time.October).Start().AddDate(2, 0, 0))},
{"Blue october", truncateMonth(nextSpecificMonth(now, time.October).Start().AddDate(3, 0, 0))},
{"Gold october", truncateMonth(nextSpecificMonth(now, time.October).Start().AddDate(4, 0, 0))},
{"Purple october", truncateMonth(nextSpecificMonth(now, time.October).Start().AddDate(5, 0, 0))},
{"Orange october", truncateMonth(nextSpecificMonth(now, time.October).Start().AddDate(6, 0, 0))},
{"Pink october", truncateMonth(nextSpecificMonth(now, time.October).Start().AddDate(7, 0, 0))},
{"Silver october", truncateMonth(nextSpecificMonth(now, time.October).Start().AddDate(8, 0, 0))},
{"Copper october", truncateMonth(nextSpecificMonth(now, time.October).Start().AddDate(9, 0, 0))},
}
for _, c := range cases {
t.Run(c.Input, func(t *testing.T) {
var gotRanges []Range
gotStr := ReplaceAllRangesByFunc(c.Input, now, Past, func(src string, r Range) string {
gotRanges = append(gotRanges, r)
return "<range>"
})
wantStr := "<range>"
if gotStr != wantStr {
t.Fatalf("gotStr = %q, wantStr %q", gotStr, wantStr)
}
wantRanges := []Range{c.WantRange}
if !reflect.DeepEqual(gotRanges, wantRanges) {
t.Fatalf("gotRanges = %#v, wantRanges %#v", gotRanges, wantRanges)
}
})
}
}
// TestParse_futurePast tests dates and times that are ambiguously in the past
// or the future.
func TestReplaceAllRangesByFunc_ambiguitiesResolvedByDirectionPreference(t *testing.T) {
var now = time.Date(2022, 9, 29, 2, 48, 33, 123, time.UTC)
tests := []struct {
input string
wantFuture Range
wantPast Range
}{
{
"December",
truncateMonth(nextMonthDayTime(now, time.December, 20, 0, 0, 0)),
truncateMonth(prevMonthDayTime(now, time.December, 20, 0, 0, 0)),
},
{
"December 20",
truncateDay(nextMonthDayTime(now, time.December, 20, 0, 0, 0)),
truncateDay(prevMonthDayTime(now, time.December, 20, 0, 0, 0)),
},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
// Future
var gotRanges []Range
_ = ReplaceAllRangesByFunc(tt.input, now, Future, func(src string, r Range) string {
gotRanges = append(gotRanges, r)
return ""
})
if len(gotRanges) != 1 {
t.Fatalf("got %d ranges, want 1", len(gotRanges))
}
gotRange := gotRanges[0]
if !gotRange.Equal(tt.wantFuture) {
t.Fatalf("got range %v, want %v", gotRange, tt.wantFuture)
}
// Past
gotRanges = nil
_ = ReplaceAllRangesByFunc(tt.input, now, Past, func(src string, r Range) string {
gotRanges = append(gotRanges, r)
return ""
})
if len(gotRanges) != 1 {
t.Fatalf("got %d ranges, want 1", len(gotRanges))
}
gotRange = gotRanges[0]
if !gotRange.Equal(tt.wantPast) {
t.Fatalf("got range %v, want %v", gotRange, tt.wantPast)
}
})
}
}
func TestReplaceAllRangesByFunc_identity(t *testing.T) {
var now = time.Date(2022, 9, 29, 2, 48, 33, 123, time.UTC)
tests := []struct {
input string
want string
}{
{"", ""},
{"�", "�"},
{" ", " "},
{"a", "a"},
{"a b", "a b"},
{"a b c", "a b c"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got := ReplaceAllRangesByFunc(tt.input, now, Past, func(src string, r Range) string {
return src
})
if got != tt.want {
t.Fatalf("got %q, want %q", got, tt.want)
}
})
}
}
var rangeStrs = []string{
`Last year`,
`last year`,
`This year`,
`Next year`,
`Yesterday`,
`Today`,
`Tomorrow`,
`Last week`,
`This week`,
`Next week`,
`Last month`,
`This month`,
`Next month`,
"January 2017",
"Jan 2017",
"March 31",
"January",
"Jan",
"April 3 2017",
"April 3",
"Oct 7",
"Oct 7",
"Oct 7 1970",
"Oct. 7",
"September 17",
"September 17",
"September 17",
"7 oct 1970",
"7 oct",
"03 February 2013",
"2 July 2013",
"2022 Feb 1",
"2014/3/31",
"2014/3/31 UTC",
"2014/3/31 UTC+1",
"2014/03/31",
"2014/03/31 UTC-1",
"2014-04-26",
"2014-4-26",
"2014-4-6",
"31/3/2014 UTC-8",
"31-3-2014 UTC-8",
"31/3/2014",
"31-3-2014",
`One day ago`,
`1 day ago`,
`3 days ago`,
`Three days ago`,
`1 day from now`,
`two days from now`,
`two days from today`,
`two days hence`,
`1 week ago`,
`2 weeks ago`,
`A week from now`,
`A week from today`,
`2 weeks hence`,
`A month ago`,
`1 month ago`,
`2 months ago`,
`12 months ago`,
`twelve months ago`,
`A month from now`,
`One month hence`,
`1 month from now`,
`2 months from now`,
`One year ago`,
`One year from now`,
`One year from today`,
`One year hence`,
`Two years ago`,
`2 years ago`,
`This year`,
`1999AD`,
`1999 AD`,
`2008CE`,
`2008 CE`,
"2006-01-02T15:04:05Z",
"1990-12-31T15:59:59-08:00",
"From 3 feb 2022 to 6 oct 2022",
"3 feb 2022 to 6 oct 2022",
"3 feb 2022 through 6 oct 2022",
"from 3 feb 2022 until 6 oct 2022",
}
func FuzzReplaceAllRangesByFunc_stringsUnchangedWhenFReturnsSrc(f *testing.F) {
f.Add("")
for _, rs := range rangeStrs {
f.Add(rs)
}
f.Fuzz(func(t *testing.T, s string) {
s2 := ReplaceAllRangesByFunc(s, time.Time{}, Future, func(src string, r Range) string {
return src
})
if s2 != s {
t.Fatalf("got %q, want %q", s2, s)
}
})
}
func FuzzReplaceAllRangesByFunc_allRanges(f *testing.F) {
f.Add([]byte("a0"))
f.Add([]byte{})
f.Add([]byte{0})
f.Add([]byte{1, 2})
f.Add([]byte{0, byte(len(rangeStrs) - 1)})
f.Fuzz(func(t *testing.T, rangeIDs []byte) {
var parts []string
for _, ridb := range rangeIDs {
if ridb < 0 {
ridb = -ridb
}
rid := int(ridb) % len(rangeStrs)
rs := rangeStrs[rid]
parts = append(parts, rs)
}
s := strings.Join(parts, " x ")
s2 := ReplaceAllRangesByFunc(s, time.Time{}, Future, func(src string, r Range) string {
return ""
})
n := len(parts) - 1
if n < 0 {
n = 0
}
want := strings.Repeat(" x ", n)
if s2 != want {
t.Fatalf("got %q, want %q", s2, want)
}
})
}