-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_test.go
253 lines (227 loc) · 4.76 KB
/
basic_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
package chomp_test
import (
"testing"
"github.com/purpleclay/chomp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTag(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
tag string
rem string
}{
{
name: "Ascii",
input: "hello and good morning",
tag: "hello",
rem: " and good morning",
},
{
name: "Unicode",
input: "こんにちは、おはよう",
tag: "こんにちは",
rem: "、おはよう",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
rem, tag, err := chomp.Tag(tt.tag)(tt.input)
require.NoError(t, err)
assert.Equal(t, tt.rem, rem)
assert.Equal(t, tt.tag, tag)
})
}
}
func TestAny(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
any string
rem string
ext string
}{
{
name: "Ascii",
input: "dark knight",
any: "krad ",
rem: "night",
ext: "dark k",
},
{
name: "Unicode",
input: "ダークナイト",
any: "ダー",
rem: "クナイト",
ext: "ダー",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
rem, ext, err := chomp.Any(tt.any)(tt.input)
require.NoError(t, err)
assert.Equal(t, tt.rem, rem)
assert.Equal(t, tt.ext, ext)
})
}
}
func TestNot(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
not string
rem string
ext string
}{
{
name: "Ascii",
input: "dark knight",
not: "tighn",
rem: "night",
ext: "dark k",
},
{
name: "Unicode",
input: "ダークナイト",
not: "トイ",
rem: "イト",
ext: "ダークナ",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
rem, ext, err := chomp.Not(tt.not)(tt.input)
require.NoError(t, err)
assert.Equal(t, tt.rem, rem)
assert.Equal(t, tt.ext, ext)
})
}
}
func TestUntil(t *testing.T) {
t.Parallel()
tests := []struct {
name string
until string
input string
rem string
ext string
}{
{
name: "Ascii",
until: "jumps",
input: "the quick brown fox jumps over the lazy dog",
rem: "jumps over the lazy dog",
ext: "the quick brown fox ",
},
{
name: "Unicode",
until: "の",
input: "素早い茶色のキツネが怠惰な犬を飛び越える",
rem: "のキツネが怠惰な犬を飛び越える",
ext: "素早い茶色",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
rem, ext, err := chomp.Until(tt.until)(tt.input)
require.NoError(t, err)
assert.Equal(t, tt.rem, rem)
assert.Equal(t, tt.ext, ext)
})
}
}
func TestOneOf(t *testing.T) {
t.Parallel()
tests := []struct {
name string
oneOf string
input string
rem string
ext string
}{
{
name: "Ascii",
oneOf: "!,eH",
input: "Hello, World!",
rem: "ello, World!",
ext: "H",
},
{
name: "Unicode",
oneOf: "はおうこ、",
input: "こんにちは、おはよう",
rem: "んにちは、おはよう",
ext: "こ",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
rem, ext, err := chomp.OneOf(tt.oneOf)(tt.input)
require.NoError(t, err)
assert.Equal(t, tt.rem, rem)
assert.Equal(t, tt.ext, ext)
})
}
}
func TestNoneOf(t *testing.T) {
t.Parallel()
tests := []struct {
name string
noneOf string
input string
rem string
ext string
}{
{
name: "Ascii",
noneOf: "eqzygoqui",
input: "the quick brown fox jumps over the lazy dog",
rem: "he quick brown fox jumps over the lazy dog",
ext: "t",
},
{
name: "Unicode",
noneOf: "が早越ネをのる",
input: "素早い茶色のキツネが怠惰な犬を飛び越える",
rem: "早い茶色のキツネが怠惰な犬を飛び越える",
ext: "素",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
rem, ext, err := chomp.NoneOf(tt.noneOf)(tt.input)
require.NoError(t, err)
assert.Equal(t, tt.rem, rem)
assert.Equal(t, tt.ext, ext)
})
}
}
func TestCombinatorError(t *testing.T) {
t.Parallel()
_, _, err := chomp.OneOf("!h")("Happy Monday")
assert.EqualError(t, err, "(one_of) combinator failed to parse text 'Happy Monday' with input '!h'")
}
func TestParserCombinatorError(t *testing.T) {
t.Parallel()
_, _, err := chomp.All(
chomp.Tag("the legend of batman"),
chomp.Tag(":"),
chomp.Tag("marvel"))("the legend of batman:dc:9781801260336:£19.99")
assert.EqualError(t, err, "(all) parser failed. (tag) combinator failed to parse text 'dc:9781801260336:£19.99' with input 'marvel'")
}