-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
2296 lines (2133 loc) · 56 KB
/
parser_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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package parser_test
import (
"errors"
"fmt"
"os"
"reflect"
"strconv"
"testing"
"unicode"
"github.com/FollowTheProcess/parser"
)
func TestTake(t *testing.T) {
tests := []struct {
name string // Identifying test case name
input string // Entire input to be parsed
value string // The parsed value
remainder string // The remaining unparsed input
err string // The expected error message (if there is one)
n int // The number of chars to consume
wantErr bool // Whether it should have returned an error
}{
{
name: "empty input",
input: "",
value: "",
remainder: "",
n: 999,
wantErr: true,
err: "Take: cannot take from empty input",
},
{
name: "empty input with n zero",
input: "",
value: "",
remainder: "",
n: 0,
wantErr: true,
err: "Take: n must be a non-zero positive integer, got 0",
},
{
name: "n too large",
input: "some stuff here",
value: "",
remainder: "",
n: 999,
wantErr: true,
err: "Take: requested n (999) chars but input had only 15 utf-8 chars",
},
{
name: "n negative",
input: "some stuff here",
value: "",
remainder: "",
n: -1,
wantErr: true,
err: "Take: n must be a non-zero positive integer, got -1",
},
{
name: "n zero",
input: "some stuff here",
value: "",
remainder: "",
n: 0,
wantErr: true,
err: "Take: n must be a non-zero positive integer, got 0",
},
{
name: "n 1 more than len",
input: "This is an exact length",
value: "",
remainder: "",
n: 24,
wantErr: true,
err: "Take: requested n (24) chars but input had only 23 utf-8 chars",
},
{
name: "n 1 more than len utf8",
input: "日a本b語ç日ð本Ê語þ日¥本¼語i日©", // This is 20 utf-8 runes
value: "",
remainder: "",
n: 21,
wantErr: true,
err: "Take: requested n (21) chars but input had only 20 utf-8 chars",
},
{
name: "bad utf8",
input: "\xf8\xa1\xa1\xa1\xa1",
value: "",
remainder: "",
n: 3,
wantErr: true,
err: "Take: input not valid utf-8",
},
{
name: "simple",
input: "Hello I am some input",
value: "Hello I am",
remainder: " some input",
n: 10,
wantErr: false,
err: "",
},
{
name: "n same as len",
input: "This is an exact length",
value: "This is an exact length",
remainder: "",
n: 23,
wantErr: false,
err: "",
},
{
name: "n same as len utf8",
input: "日a本b語ç日ð本Ê語þ日¥本¼語i日©", // This is 20 utf-8 runes
value: "日a本b語ç日ð本Ê語þ日¥本¼語i日©",
remainder: "",
n: 20,
wantErr: false,
err: "",
},
{
name: "n 1 less than len",
input: "This is an exact length",
value: "This is an exact lengt",
remainder: "h",
n: 22,
wantErr: false,
err: "",
},
{
name: "n 1 less than len utf8",
input: "日a本b語ç日ð本Ê語þ日¥本¼語i日©", // This is 20 utf-8 runes
value: "日a本b語ç日ð本Ê語þ日¥本¼語i日",
remainder: "©",
n: 19,
wantErr: false,
err: "",
},
{
name: "non ascii",
input: "Hello, 世界",
value: "Hello",
remainder: ", 世界",
n: 5,
wantErr: false,
err: "",
},
{
// https://github.com/golang/exp/blob/master/utf8string/string_test.go
name: "utf8string test",
input: "日a本b語ç日ð本Ê語þ日¥本¼語i日©",
value: "日a本b語ç",
remainder: "日ð本Ê語þ日¥本¼語i日©",
n: 6,
wantErr: false,
err: "",
},
{
name: "emoji",
input: "😱 emoji works too",
value: "😱 ",
remainder: "emoji works too",
n: 2,
wantErr: false,
err: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value, remainder, err := parser.Take(tt.n)(tt.input)
result := parserTest[string]{
gotValue: value,
gotRemainder: remainder,
gotErr: err,
wantValue: tt.value,
wantRemainder: tt.remainder,
wantErr: tt.wantErr,
wantErrMsg: tt.err,
}
testParser(t, result)
})
}
}
func TestExact(t *testing.T) {
tests := []struct {
name string // Identifying test case name
input string // Entire input to be parsed
match string // The exact string to parser
value string // The parsed value
remainder string // The remaining unparsed input
err string // The expected error message (if there is one)
wantErr bool // Whether it should have returned an error
}{
{
name: "empty input",
input: "",
value: "",
remainder: "",
match: "something",
wantErr: true,
err: "Exact: cannot match on empty input",
},
{
name: "bad utf8",
input: "\xf8\xa1\xa1\xa1\xa1",
value: "",
remainder: "",
match: "something",
wantErr: true,
err: "Exact: input not valid utf-8",
},
{
name: "empty input and match",
input: "",
value: "",
remainder: "",
match: "",
wantErr: true,
err: "Exact: cannot match on empty input",
},
{
name: "empty match",
input: "some text",
value: "",
remainder: "",
match: "",
wantErr: true,
err: "Exact: match must not be empty",
},
{
name: "match longer than input",
input: "A single sentence",
value: "",
remainder: "",
match: "A single sentence but this one is longer so it can't possibly be matched",
wantErr: true,
err: "Exact: match (A single sentence but this one is longer so it can't possibly be matched) not in input",
},
{
name: "match not found",
input: "Nothing to see in here",
value: "",
remainder: "",
match: "Found me",
wantErr: true,
err: "Exact: match (Found me) not in input",
},
{
name: "wrong case",
input: "Found me, in a larger sentence",
value: "",
remainder: "",
match: "found me", // Note: lower case 'f', not an exact match
wantErr: true,
err: "Exact: match (found me) not in input",
},
{
name: "simple match",
input: "Found me, in a larger sentence",
value: "Found me",
remainder: ", in a larger sentence",
match: "Found me",
wantErr: false,
err: "",
},
{
name: "utf8 match",
input: "世界, Hello",
value: "世",
remainder: "界, Hello",
match: "世",
wantErr: false,
err: "",
},
{
// https://github.com/golang/exp/blob/master/utf8string/string_test.go
name: "utf8string test",
input: "日a本b語ç日ð本Ê語þ日¥本¼語i日©",
value: "日a本",
remainder: "b語ç日ð本Ê語þ日¥本¼語i日©",
match: "日a本",
wantErr: false,
err: "",
},
{
name: "emoji",
input: "😱 emoji works too",
value: "😱 emoji",
remainder: " works too",
match: "😱 emoji",
wantErr: false,
err: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value, remainder, err := parser.Exact(tt.match)(tt.input)
result := parserTest[string]{
gotValue: value,
gotRemainder: remainder,
gotErr: err,
wantValue: tt.value,
wantRemainder: tt.remainder,
wantErr: tt.wantErr,
wantErrMsg: tt.err,
}
testParser(t, result)
})
}
}
func TestExactCaseInsensitive(t *testing.T) {
tests := []struct {
name string // Identifying test case name
input string // Entire input to be parsed
match string // The exact string to parser
value string // The parsed value
remainder string // The remaining unparsed input
err string // The expected error message (if there is one)
wantErr bool // Whether it should have returned an error
}{
{
name: "empty input",
input: "",
match: "something",
value: "",
remainder: "",
wantErr: true,
err: "ExactCaseInsensitive: cannot match on empty input",
},
{
name: "bad utf8",
input: "\xf8\xa1\xa1\xa1\xa1",
match: "something",
value: "",
remainder: "",
wantErr: true,
err: "ExactCaseInsensitive: input not valid utf-8",
},
{
name: "empty input and match",
input: "",
match: "",
value: "",
remainder: "",
wantErr: true,
err: "ExactCaseInsensitive: cannot match on empty input",
},
{
name: "empty match",
input: "some text",
match: "",
value: "",
remainder: "",
wantErr: true,
err: "ExactCaseInsensitive: match must not be empty",
},
{
name: "match longer than input",
input: "A single sentence",
match: "A single sentence but this one is longer so it can't possibly be matched",
value: "",
remainder: "",
wantErr: true,
err: "ExactCaseInsensitive: match (A single sentence but this one is longer so it can't possibly be matched) not in input",
},
{
name: "match not found",
input: "Nothing to see in here",
match: "Found me",
value: "",
remainder: "",
wantErr: true,
err: "ExactCaseInsensitive: match (Found me) not in input",
},
{
name: "match same length as input",
input: "A single sentence",
match: "A single sentence",
value: "A single sentence",
remainder: "",
wantErr: false,
err: "",
},
{
name: "exact match",
input: "Found me, in a larger sentence",
match: "Found me",
value: "Found me",
remainder: ", in a larger sentence",
wantErr: false,
err: "",
},
{
name: "case insensitive match",
input: "Found me, in a larger sentence",
match: "found me", // Lower case f, should still match
value: "Found me",
remainder: ", in a larger sentence",
wantErr: false,
err: "",
},
{
// https://github.com/golang/exp/blob/master/utf8string/string_test.go
name: "utf8string test",
input: "日a本b語ç日ð本Ê語þ日¥本¼語i日©",
match: "日a本", // Apparently this is already lower case
value: "日a本",
remainder: "b語ç日ð本Ê語þ日¥本¼語i日©",
wantErr: false,
err: "",
},
{
name: "utf8string test upper case",
input: "日a本b語ç日ð本Ê語þ日¥本¼語i日©",
match: "日A本", // Upper case now
value: "日a本",
remainder: "b語ç日ð本Ê語þ日¥本¼語i日©",
wantErr: false,
err: "",
},
{
name: "emoji",
input: "😱 EMOJI WORKS TOO",
match: "😱 emoji",
value: "😱 EMOJI",
remainder: " WORKS TOO",
wantErr: false,
err: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value, remainder, err := parser.ExactCaseInsensitive(tt.match)(tt.input)
result := parserTest[string]{
gotValue: value,
gotRemainder: remainder,
gotErr: err,
wantValue: tt.value,
wantRemainder: tt.remainder,
wantErr: tt.wantErr,
wantErrMsg: tt.err,
}
testParser(t, result)
})
}
}
func TestChar(t *testing.T) {
tests := []struct {
name string // Identifying test case name
input string // Entire input to be parsed
value string // The parsed value
remainder string // The remaining unparsed input
err string // The expected error message (if there is one)
char rune // The exact char to match
wantErr bool // Whether it should have returned an error
}{
{
name: "empty input",
input: "",
value: "",
remainder: "",
char: 0,
wantErr: true,
err: "Char: input text is empty",
},
{
name: "bad utf8",
input: "\xf8\xa1\xa1\xa1\xa1",
value: "",
remainder: "",
char: 'x',
wantErr: true,
err: "Char: input not valid utf-8",
},
{
name: "not found",
input: "something",
value: "",
remainder: "",
char: 'x',
wantErr: true,
err: "Char: requested char (x) not found in input",
},
{
name: "wrong case",
input: "General Kenobi!",
value: "",
remainder: "",
char: 'g',
wantErr: true,
err: "Char: requested char (g) not found in input",
},
{
name: "found",
input: "General Kenobi!",
value: "G",
remainder: "eneral Kenobi!",
char: 'G',
wantErr: false,
err: "",
},
{
name: "found utf8",
input: "日a本b語ç日ð本Ê語þ日¥本¼語i日©",
value: "日",
remainder: "a本b語ç日ð本Ê語þ日¥本¼語i日©",
char: '日',
wantErr: false,
err: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value, remainder, err := parser.Char(tt.char)(tt.input)
result := parserTest[string]{
gotValue: value,
gotRemainder: remainder,
gotErr: err,
wantValue: tt.value,
wantRemainder: tt.remainder,
wantErr: tt.wantErr,
wantErrMsg: tt.err,
}
testParser(t, result)
})
}
}
func TestTakeWhile(t *testing.T) {
tests := []struct {
predicate func(r rune) bool // The predicate function that determines whether the parser should continue taking characters
name string // Identifying test case name
input string // Entire input to be parsed
value string // The parsed value
remainder string // The remaining unparsed input
err string // The expected error message (if there is one)
wantErr bool // Whether it should have returned an error
}{
{
name: "empty input",
input: "",
value: "",
remainder: "",
predicate: nil, // Shouldn't matter as it should never get called
wantErr: true,
err: "TakeWhile: input text is empty",
},
{
name: "bad utf8",
input: "\xf8\xa1\xa1\xa1\xa1",
value: "",
remainder: "",
predicate: nil, // Shouldn't matter as it should never get called
wantErr: true,
err: "TakeWhile: input not valid utf-8",
},
{
name: "nil predicate", // Good libraries don't panic
input: "some input",
value: "",
remainder: "",
predicate: nil,
wantErr: true,
err: "TakeWhile: predicate must be a non-nil function",
},
{
name: "predicate never returns false",
input: "123456", // All digits
value: "123456",
remainder: "",
predicate: unicode.IsDigit, // True for every char in input
wantErr: false,
err: "",
},
{
name: "predicate never returns true",
input: "abcdef", // All letters
value: "",
remainder: "",
predicate: unicode.IsDigit, // False for every char in input
wantErr: true,
err: "TakeWhile: predicate never returned true",
},
{
name: "consume whitespace",
input: " \t\t\n\n end of whitespace",
value: " \t\t\n\n ",
remainder: "end of whitespace",
predicate: unicode.IsSpace,
wantErr: false,
err: "",
},
{
name: "consume non ascii rune",
input: "本本本 b語ç日ð本Ê語",
value: "本本本",
remainder: " b語ç日ð本Ê語",
predicate: func(r rune) bool { return r == '本' },
wantErr: false,
err: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value, remainder, err := parser.TakeWhile(tt.predicate)(tt.input)
result := parserTest[string]{
gotValue: value,
gotRemainder: remainder,
gotErr: err,
wantValue: tt.value,
wantRemainder: tt.remainder,
wantErr: tt.wantErr,
wantErrMsg: tt.err,
}
testParser(t, result)
})
}
}
func TestTakeWhileBetween(t *testing.T) {
tests := []struct {
predicate func(r rune) bool // The predicate function that determines whether the parser should continue taking characters
name string // Identifying test case name
input string // Entire input to be parsed
value string // The parsed value
remainder string // The remaining unparsed input
err string // The expected error message (if there is one)
wantErr bool // Whether it should have returned an error
lower int // The lower limit
upper int // The upper limit
}{
{
name: "empty input",
input: "",
lower: 0, // Doesn't matter
upper: 0, // Also doesn't matter
predicate: nil, // Same
value: "",
remainder: "",
wantErr: true,
err: "TakeWhileBetween: input text is empty",
},
{
name: "invalid utf-8",
input: "\xf8\xa1\xa1\xa1\xa1",
lower: 0, // Doesn't matter
upper: 0, // Also doesn't matter
predicate: nil, // Same
value: "",
remainder: "",
wantErr: true,
err: "TakeWhileBetween: input not valid utf-8",
},
{
name: "emoji",
input: "✅🛠️🧠⚡️⚠️😎🪜",
lower: 6,
upper: 8,
predicate: unicode.IsGraphic,
value: "✅🛠️🧠⚡️⚠️",
remainder: "😎🪜",
wantErr: false,
err: "",
},
{
name: "fuzz failure", // Now correctly handled!
input: "\U0001925e0",
lower: 9,
upper: 83,
predicate: unicode.IsGraphic,
value: "",
remainder: "",
wantErr: true,
err: "TakeWhileBetween: predicate matched only 0 chars (), below lower limit (9)",
},
{
name: "nil predicate",
input: "some valid input",
lower: 0, // Doesn't matter
upper: 0, // Also doesn't matter
predicate: nil,
value: "",
remainder: "",
wantErr: true,
err: "TakeWhileBetween: predicate must be a non-nil function",
},
{
name: "lower negative",
input: "some valid input",
lower: -1, // Not valid
upper: 4,
predicate: func(r rune) bool { return true }, // Doesn't matter, but can't be nil
value: "",
remainder: "",
wantErr: true,
err: "TakeWhileBetween: lower limit (-1) not allowed, must be positive integer",
},
{
name: "upper less than lower",
input: "some valid input",
lower: 4,
upper: 2,
predicate: func(r rune) bool { return true }, // Doesn't matter, but can't be nil
value: "",
remainder: "",
wantErr: true,
err: "TakeWhileBetween: invalid range, lower (4) must be < upper (2)",
},
{
name: "nom example 1", // https://docs.rs/nom/latest/nom/bytes/complete/fn.take_while_m_n.html
input: "latin123",
lower: 3,
upper: 6,
predicate: unicode.IsLetter,
value: "latin",
remainder: "123",
wantErr: false,
err: "",
},
{
name: "nom example 2", // https://docs.rs/nom/latest/nom/bytes/complete/fn.take_while_m_n.html
input: "lengthy",
lower: 3,
upper: 6,
predicate: unicode.IsLetter,
value: "length",
remainder: "y",
wantErr: false,
err: "",
},
{
name: "nom example 3", // https://docs.rs/nom/latest/nom/bytes/complete/fn.take_while_m_n.html
input: "latin",
lower: 3,
upper: 6,
predicate: unicode.IsLetter,
value: "latin",
remainder: "",
wantErr: false,
err: "",
},
{
name: "nom example 4", // https://docs.rs/nom/latest/nom/bytes/complete/fn.take_while_m_n.html
input: "ed", // Not long enough
lower: 3,
upper: 6,
predicate: unicode.IsLetter,
value: "",
remainder: "",
wantErr: true,
err: "TakeWhileBetween: predicate matched only 2 chars (ed), below lower limit (3)",
},
{
name: "nom example 5", // https://docs.rs/nom/latest/nom/bytes/complete/fn.take_while_m_n.html
input: "12345", // Predicate never returns true
lower: 3,
upper: 6,
predicate: unicode.IsLetter,
value: "",
remainder: "",
wantErr: true,
err: "TakeWhileBetween: predicate never returned true",
},
{
name: "unicode",
input: "語ç日ð本Ê語",
lower: 2,
upper: 4,
predicate: func(r rune) bool {
switch r {
case '語', 'ç', '日':
return true
default:
return false
}
},
value: "語ç日",
remainder: "ð本Ê語",
wantErr: false,
err: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value, remainder, err := parser.TakeWhileBetween(tt.lower, tt.upper, tt.predicate)(tt.input)
result := parserTest[string]{
gotValue: value,
gotRemainder: remainder,
gotErr: err,
wantValue: tt.value,
wantRemainder: tt.remainder,
wantErr: tt.wantErr,
wantErrMsg: tt.err,
}
testParser(t, result)
})
}
}
func TestTakeUntil(t *testing.T) {
tests := []struct {
predicate func(r rune) bool // The predicate function that determines whether the parser should stop taking characters
name string // Identifying test case name
input string // Entire input to be parsed
value string // The parsed value
remainder string // The remaining unparsed input
err string // The expected error message (if there is one)
wantErr bool // Whether it should have returned an error
}{
{
name: "empty input",
input: "",
value: "",
remainder: "",
predicate: nil, // Shouldn't matter as it should never get called
wantErr: true,
err: "TakeUntil: input text is empty",
},
{
name: "bad utf8",
input: "\xf8\xa1\xa1\xa1\xa1",
value: "",
remainder: "",
predicate: nil, // Shouldn't matter as it should never get called
wantErr: true,
err: "TakeUntil: input not valid utf-8",
},
{
name: "nil predicate", // Good libraries don't panic
input: "some input",
value: "",
remainder: "",
predicate: nil,
wantErr: true,
err: "TakeUntil: predicate must be a non-nil function",
},
{
name: "predicate never returns true",
input: "fixed length input",
value: "fixed length input",
remainder: "",
predicate: func(r rune) bool { return false },
wantErr: false,
err: "",
},
{
name: "predicate never returns false",
input: "fixed length input",
value: "",
remainder: "",
predicate: func(r rune) bool { return true },
wantErr: true,
err: "TakeUntil: predicate never returned false",
},
{
name: "consume until whitespace",
input: "something <- first whitespace",
value: "something",
remainder: " <- first whitespace",
predicate: unicode.IsSpace,
wantErr: false,
err: "",
},
{
name: "consume until non-ascii",
input: "abcdef語ç日ð本Ê語",
value: "abcdef",
remainder: "語ç日ð本Ê語",
predicate: func(r rune) bool { return r > unicode.MaxASCII },
wantErr: false,
err: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value, remainder, err := parser.TakeUntil(tt.predicate)(tt.input)
result := parserTest[string]{
gotValue: value,
gotRemainder: remainder,
gotErr: err,
wantValue: tt.value,
wantRemainder: tt.remainder,
wantErr: tt.wantErr,
wantErrMsg: tt.err,
}
testParser(t, result)
})
}
}
func TestTakeTo(t *testing.T) {
tests := []struct {
name string // Identifying test case name
input string // Entire input to be parsed
match string // The exact string to stop at
value string // The parsed value
remainder string // The remaining unparsed input
err string // The expected error message (if there is one)
wantErr bool // Whether it should have returned an error
}{
{
name: "empty input",
input: "",
match: "something",
value: "",
remainder: "",
wantErr: true,
err: "TakeTo: input text is empty",
},
{
name: "bad utf8",
input: "\xf8\xa1\xa1\xa1\xa1",
match: "something",
value: "",
remainder: "",
wantErr: true,
err: "TakeTo: input not valid utf-8",
},
{
name: "empty input and match",
input: "",
match: "",
value: "",
remainder: "",
wantErr: true,
err: "TakeTo: input text is empty",
},
{
name: "empty match",
input: "some text",
match: "",
value: "",
remainder: "",
wantErr: true,
err: "TakeTo: match must not be empty",
},
{
name: "no match",
input: "a long sentence",
match: "not here",
value: "",
remainder: "",
wantErr: true,
err: "TakeTo: match (not here) not in input",
},
{
name: "simple",
input: "lots of stuff KEYWORD more stuff",
match: "KEYWORD",
value: "lots of stuff ",
remainder: "KEYWORD more stuff",
wantErr: false,
err: "",
},
{
name: "match at end of input",
input: "blah blah lots of inputeof",
match: "eof",
value: "blah blah lots of input",
remainder: "eof",
wantErr: false,
err: "",
},
{
name: "match at start of input",
input: "eofblah blah lots of input",
match: "eof",
value: "",
remainder: "eofblah blah lots of input",
wantErr: false,