-
Notifications
You must be signed in to change notification settings - Fork 3
/
reader_test.go
260 lines (252 loc) · 7.93 KB
/
reader_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
package brigodier
import (
"errors"
"github.com/stretchr/testify/require"
"testing"
)
func TestStringReader_CanRead(t *testing.T) {
r := StringReader{String: "abc"}
require.True(t, r.CanRead())
r.Skip() // a
require.True(t, r.CanRead())
r.Skip() // b
require.True(t, r.CanRead())
r.Skip() // c
require.False(t, r.CanRead())
}
func TestStringReader_RemainingLen(t *testing.T) {
r := StringReader{String: "abc"}
require.Equal(t, 3, r.RemainingLen())
r.Skip()
require.Equal(t, 2, r.RemainingLen())
r.Skip()
require.Equal(t, 1, r.RemainingLen())
r.Skip()
require.Equal(t, 0, r.RemainingLen())
}
func TestStringReader_CanReadLen(t *testing.T) {
r := StringReader{String: "abc"}
require.Equal(t, 'a', r.Peek())
require.Equal(t, 0, r.Cursor)
r.Cursor = 2
require.Equal(t, 'c', r.Peek())
require.Equal(t, 2, r.Cursor)
}
func TestStringReader_Read(t *testing.T) {
r := StringReader{String: "abc"}
require.Equal(t, 'a', r.Read())
require.Equal(t, 'b', r.Read())
require.Equal(t, 'c', r.Read())
require.Equal(t, 3, r.Cursor)
}
func TestStringReader_Skip(t *testing.T) {
r := StringReader{String: "abc"}
r.Skip()
require.Equal(t, 1, r.Cursor)
}
func TestStringReader_Remaining(t *testing.T) {
r := StringReader{String: "Hello!"}
require.Equal(t, "Hello!", r.Remaining())
r.Cursor = 3
require.Equal(t, "lo!", r.Remaining())
r.Cursor = 6
require.Equal(t, "", r.Remaining())
}
func TestStringReader_ReadUnquotedString(t *testing.T) {
r := StringReader{String: "hello world"}
require.Equal(t, "hello", r.ReadUnquotedString())
require.Equal(t, " world", r.Remaining())
}
func TestStringReader_ReadUnquotedString_Empty_WithRemaining(t *testing.T) {
r := StringReader{String: " hello world"}
require.Equal(t, "", r.ReadUnquotedString())
require.Equal(t, " hello world", r.Remaining())
}
func TestStringReader_ReadQuotedString(t *testing.T) {
r := StringReader{String: `"hello world"`}
s, err := r.ReadQuotedString()
require.NoError(t, err)
require.Equal(t, "hello world", s)
require.Empty(t, r.Remaining())
}
func TestStringReader_ReadQuotedString_Mixed_DoubleInsideSingle(t *testing.T) {
r := StringReader{String: `'hello "world"'`}
s, err := r.ReadQuotedString()
require.NoError(t, err)
require.Equal(t, `hello "world"`, s)
require.Empty(t, r.Remaining())
}
func TestStringReader_ReadQuotedString_SingleInsideDouble(t *testing.T) {
r := StringReader{String: `"hello 'world'"`}
s, err := r.ReadQuotedString()
require.NoError(t, err)
require.Equal(t, `hello 'world'`, s)
require.Empty(t, r.Remaining())
}
func TestStringReader_ReadQuotedString_Empty(t *testing.T) {
r := StringReader{}
s, err := r.ReadQuotedString()
require.NoError(t, err)
require.Empty(t, s)
require.Empty(t, r.Remaining())
}
func TestStringReader_ReadQuotedString_EmptyQuoted(t *testing.T) {
r := StringReader{String: `""`}
s, err := r.ReadQuotedString()
require.NoError(t, err)
require.Empty(t, s)
require.Empty(t, r.Remaining())
}
func TestStringReader_ReadQuotedString_EmptyQuoted_WithRemaining(t *testing.T) {
r := StringReader{String: `"" hello world`}
s, err := r.ReadQuotedString()
require.NoError(t, err)
require.Empty(t, s)
require.Equal(t, " hello world", r.Remaining())
}
func TestStringReader_ReadQuotedString_WithEscapeQuote(t *testing.T) {
r := StringReader{String: "\"hello \\\"world\\\"\""}
s, err := r.ReadQuotedString()
require.NoError(t, err)
require.Equal(t, "hello \"world\"", s)
require.Empty(t, r.Remaining())
}
func TestStringReader_ReadQuotedString_WithEscapedEscapes(t *testing.T) {
r := StringReader{String: "\"\\\\o/\""}
s, err := r.ReadQuotedString()
require.NoError(t, err)
require.Equal(t, "\\o/", s)
require.Empty(t, r.Remaining())
}
func TestStringReader_ReadQuotedString_WithRemaining(t *testing.T) {
r := StringReader{String: "\"hello world\" foo bar"}
s, err := r.ReadQuotedString()
require.NoError(t, err)
require.Equal(t, "hello world", s)
require.Equal(t, " foo bar", r.Remaining())
}
func TestStringReader_ReadQuotedString_WithImmediateRemaining(t *testing.T) {
r := StringReader{String: "\"hello world\"foo bar"}
s, err := r.ReadQuotedString()
require.NoError(t, err)
require.Equal(t, "hello world", s)
require.Equal(t, "foo bar", r.Remaining())
}
func TestStringReader_ReadQuotedString_NoOpen(t *testing.T) {
r := StringReader{String: "hello world\""}
_, err := r.ReadQuotedString()
require.ErrorIs(t, err, ErrReaderExpectedStartOfQuote)
var rErr *ReaderError
require.True(t, errors.As(err, &rErr))
require.Equal(t, 0, rErr.Reader.Cursor)
}
func TestStringReader_ReadQuotedString_NoClose(t *testing.T) {
r := StringReader{String: "\"hello world"}
_, err := r.ReadQuotedString()
require.ErrorIs(t, err, ErrReaderExpectedEndOfQuote)
var rErr *ReaderError
require.True(t, errors.As(err, &rErr))
require.Equal(t, 12, rErr.Reader.Cursor)
}
func TestStringReader_ReadQuotedString_InvalidEscape(t *testing.T) {
r := StringReader{String: "\"hello\\nworld\""}
_, err := r.ReadQuotedString()
require.ErrorIs(t, err, ErrReaderInvalidEscape)
var rErr *ReaderError
require.True(t, errors.As(err, &rErr))
require.Equal(t, 7, rErr.Reader.Cursor)
}
func TestStringReader_ReadQuotedString_InvalidQuoteEscape(t *testing.T) {
r := StringReader{String: "'hello\\\"'world"}
_, err := r.ReadQuotedString()
require.ErrorIs(t, err, ErrReaderInvalidEscape)
var rErr *ReaderError
require.True(t, errors.As(err, &rErr))
require.Equal(t, 7, rErr.Reader.Cursor)
}
func TestStringReader_ReadString_NoQuotes(t *testing.T) {
r := StringReader{String: "hello world"}
s, err := r.ReadString()
require.NoError(t, err)
require.Equal(t, "hello", s)
require.Equal(t, " world", r.Remaining())
}
func TestStringReader_ReadString_SingleQuotes(t *testing.T) {
r := StringReader{String: "'hello world'"}
s, err := r.ReadString()
require.NoError(t, err)
require.Equal(t, "hello world", s)
require.Empty(t, r.Remaining())
}
func TestStringReader_ReadString_DoubleQuotes(t *testing.T) {
r := StringReader{String: `"hello world"`}
s, err := r.ReadString()
require.NoError(t, err)
require.Equal(t, "hello world", s)
require.Empty(t, r.Remaining())
}
func TestStringReader_ReadInt(t *testing.T) {
r := StringReader{String: "1234567890"}
i, err := r.ReadInt()
require.NoError(t, err)
require.Equal(t, 1234567890, i)
require.Empty(t, r.Remaining())
}
func TestStringReader_ReadInt_Negative(t *testing.T) {
r := StringReader{String: "-1234567890"}
i, err := r.ReadInt()
require.NoError(t, err)
require.Equal(t, -1234567890, i)
require.Empty(t, r.Remaining())
}
func TestStringReader_ReadInt_Invalid(t *testing.T) {
r := StringReader{String: "12.34"}
_, err := r.ReadInt()
require.ErrorIs(t, err, ErrReaderInvalidInt)
var rErr *ReaderError
require.True(t, errors.As(err, &rErr))
require.Equal(t, 0, rErr.Reader.Cursor)
}
func TestStringReader_ReadInt_None(t *testing.T) {
r := StringReader{}
_, err := r.ReadInt()
require.ErrorIs(t, err, ErrReaderExpectedInt)
var rErr *ReaderError
require.True(t, errors.As(err, &rErr))
require.Equal(t, 0, rErr.Reader.Cursor)
}
func TestStringReader_ReadInt_WithRemainingImmediate(t *testing.T) {
r := StringReader{String: "1234567890foo bar"}
i, err := r.ReadInt()
require.NoError(t, err)
require.Equal(t, 1234567890, i)
require.Equal(t, "foo bar", r.Remaining())
}
func TestStringReader_ReadBool(t *testing.T) {
r := StringReader{String: "true"}
b, err := r.ReadBool()
require.NoError(t, err)
require.True(t, b)
require.Empty(t, r.Remaining())
}
func TestStringReader_ReadBool_Incorrect(t *testing.T) {
r := StringReader{String: "tuesday"}
_, err := r.ReadBool()
require.Error(t, err)
var (
rErr *ReaderError
vErr *ReaderInvalidValueError
)
require.True(t, errors.As(err, &rErr))
require.True(t, errors.As(err, &vErr))
require.Equal(t, "tuesday", vErr.Value)
require.Equal(t, 0, rErr.Reader.Cursor)
}
func TestStringReader_ReadBool_None(t *testing.T) {
r := StringReader{}
_, err := r.ReadBool()
require.ErrorIs(t, err, ErrReaderExpectedBool)
var rErr *ReaderError
require.True(t, errors.As(err, &rErr))
require.Equal(t, 0, rErr.Reader.Cursor)
}