-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
102 lines (93 loc) · 2.25 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
package window
import (
"fmt"
"testing"
)
func Test_consumeRE(t *testing.T) {
type test struct {
text, pattern, result string
}
tests := []test{
{"", "", ""},
{"", "a", ""},
{"a", "a", "a"},
{"aab", "a", "a"},
{"aab", "a+", "aa"},
{"2 days", `\d+`, "2"},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
p := startParsing(tt.text)
if r := p.consumeRE(tt.pattern); r != tt.result {
t.Errorf("unexpected result [%s] when expected [%s]", r, tt.result)
}
})
}
}
func Test_expect(t *testing.T) {
type test struct {
text string
alts []string
result string
}
tests := []test{
{"", []string{""}, ""},
{"", []string{"a"}, ""},
{"a", []string{"a"}, "a"},
{"a", []string{"b"}, ""},
{"a", []string{"b", "a"}, "a"},
{"ab", []string{"b", "a"}, "a"},
{"abc", []string{"a", "ab", "abc"}, "a"}, // picks the first match
}
for i, tt := range tests {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
p := startParsing(tt.text)
if r := p.expectAny(tt.alts); r != tt.result {
t.Errorf("unexpected result [%s] when expected [%s]", r, tt.result)
}
})
}
}
func Test_consumeUntil(t *testing.T) {
type test struct {
text string
alts []string
consumed, matched string
}
tests := []test{
{"", []string{""}, "", ""},
{"", []string{"a"}, "", ""},
{"a", []string{"a"}, "", "a"},
{"abc", []string{"b"}, "a", "b"},
{"abc", []string{"b", "c"}, "a", "b"}, // picks the first alt
}
for i, tt := range tests {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
p := startParsing(tt.text)
if consumed, matched := p.consumeUntil(tt.alts); consumed != tt.consumed || matched != tt.matched {
t.Errorf("unexpected result [%s, %s] when expected [%s,%s]", consumed, matched, tt.consumed, tt.matched)
}
})
}
}
func Test_eatWs(t *testing.T) {
type test struct {
text, result string
}
tests := []test{
{"", ""},
{" ", ""},
{"\n", ""},
{"\t", ""},
{" a", "a"},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
p := startParsing(tt.text)
p.eatWs()
if p.getRemainder() != tt.result {
t.Errorf("unexpected result [%s] when expected [%s]", p.getRemainder(), tt.result)
}
})
}
}