-
-
Notifications
You must be signed in to change notification settings - Fork 531
/
error_test.go
421 lines (366 loc) · 11.3 KB
/
error_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
package toml_test
import (
"errors"
"fmt"
"io/fs"
"math"
"strings"
"testing"
"time"
"github.com/BurntSushi/toml"
tomltest "github.com/BurntSushi/toml/internal/toml-test"
)
func TestErrorPosition(t *testing.T) {
// Note: take care to use leading spaces (not tabs).
tests := []struct {
test, err string
}{
{"array/missing-separator-2.toml", `
toml: error: expected a comma (',') or array terminator (']'), but got '2'
At line 1, column 13:
1 | wrong = [ 1 2 3 ]
^`},
{"array/no-close-1.toml", `
toml: error: expected a comma (',') or array terminator (']'), but got end of file
At line 1, column 23:
1 | no-close-1 = [ 1, 2, 3
^`},
{"array/tables-2.toml", `
toml: error: Key 'fruit.variety' has already been defined.
At line 9, column 4-8:
7 |
8 | # This table conflicts with the previous table
9 | [fruit.variety]
^^^^^`},
{"local-date/trailing-t.toml", `
toml: error: invalid datetime: "2006-01-30T"
At line 2, column 5-15:
1 | # Date cannot end with trailing T
2 | d = 2006-01-30T
^^^^^^^^^^^`},
{"key/without-value-1.toml", `
toml: error: expected '.' or '=', but got '\n' instead
At line 1, column 4:
1 | key
^`},
// Position looks wonky, but test has trailing space, so it's correct.
{"key/without-value-2.toml", `
toml: error: expected value but found '\n' instead
At line 1, column 7:
1 | key =
^`},
}
fsys := tomltest.EmbeddedTests()
for _, tt := range tests {
t.Run(tt.test, func(t *testing.T) {
input, err := fs.ReadFile(fsys, "invalid/"+tt.test)
if err != nil {
t.Fatal(err)
}
var x any
_, err = toml.Decode(string(input), &x)
if err == nil {
t.Fatal("err is nil")
}
var pErr toml.ParseError
if !errors.As(err, &pErr) {
t.Errorf("err is not a ParseError: %T %[1]v", err)
}
tt.err = tt.err[1:] + "\n" // Remove first newline, and add trailing.
want := pErr.ErrorWithUsage()
if !strings.Contains(want, tt.err) {
t.Fatalf("\nwant:\n%s\nhave:\n%s", tt.err, want)
}
})
}
}
func TestParseError(t *testing.T) {
tests := []struct {
in any
toml, err string
}{
{
&struct{ Int int8 }{},
"Int = 200",
`| toml: error: 200 is out of range for int8
|
| At line 1, column 7-9:
|
| 1 | Int = 200
| ^^^
| Error help:
|
| This number is too large; this may be an error in the TOML, but it can also be a
| bug in the program that uses too small of an integer.
|
| The maximum and minimum values are:
|
| size │ lowest │ highest
| ───────┼────────────────┼──────────────
| int8 │ -128 │ 127
| int16 │ -32,768 │ 32,767
| int32 │ -2,147,483,648 │ 2,147,483,647
| int64 │ -9.2 × 10¹⁷ │ 9.2 × 10¹⁷
| uint8 │ 0 │ 255
| uint16 │ 0 │ 65,535
| uint32 │ 0 │ 4,294,967,295
| uint64 │ 0 │ 1.8 × 10¹⁸
|
| int refers to int32 on 32-bit systems and int64 on 64-bit systems.
`,
},
{
&struct{ Int int }{},
fmt.Sprintf("Int = %d", uint64(math.MaxInt64+1)),
`| toml: error: 9223372036854775808 is out of range for int64
|
| At line 1, column 7-25:
|
| 1 | Int = 9223372036854775808
| ^^^^^^^^^^^^^^^^^^^
| Error help:
|
| This number is too large; this may be an error in the TOML, but it can also be a
| bug in the program that uses too small of an integer.
|
| The maximum and minimum values are:
|
| size │ lowest │ highest
| ───────┼────────────────┼──────────────
| int8 │ -128 │ 127
| int16 │ -32,768 │ 32,767
| int32 │ -2,147,483,648 │ 2,147,483,647
| int64 │ -9.2 × 10¹⁷ │ 9.2 × 10¹⁷
| uint8 │ 0 │ 255
| uint16 │ 0 │ 65,535
| uint32 │ 0 │ 4,294,967,295
| uint64 │ 0 │ 1.8 × 10¹⁸
|
| int refers to int32 on 32-bit systems and int64 on 64-bit systems.
`,
},
{
&struct{ Float float32 }{},
"Float = 1.1e99",
`
| toml: error: 1.1e+99 is out of range for float32
|
| At line 1, column 9-14:
|
| 1 | Float = 1.1e99
| ^^^^^^
| Error help:
|
| This number is too large; this may be an error in the TOML, but it can also be a
| bug in the program that uses too small of an integer.
|
| The maximum and minimum values are:
|
| size │ lowest │ highest
| ───────┼────────────────┼──────────────
| int8 │ -128 │ 127
| int16 │ -32,768 │ 32,767
| int32 │ -2,147,483,648 │ 2,147,483,647
| int64 │ -9.2 × 10¹⁷ │ 9.2 × 10¹⁷
| uint8 │ 0 │ 255
| uint16 │ 0 │ 65,535
| uint32 │ 0 │ 4,294,967,295
| uint64 │ 0 │ 1.8 × 10¹⁸
|
| int refers to int32 on 32-bit systems and int64 on 64-bit systems.
`,
},
{
&struct{ D time.Duration }{},
`D = "99 bottles"`,
`
| toml: error: invalid duration: "99 bottles"
|
| At line 1, column 6-15:
|
| 1 | D = "99 bottles"
| ^^^^^^^^^^
| Error help:
|
| A duration must be as "number<unit>", without any spaces. Valid units are:
|
| ns nanoseconds (billionth of a second)
| us, µs microseconds (millionth of a second)
| ms milliseconds (thousands of a second)
| s seconds
| m minutes
| h hours
|
| You can combine multiple units; for example "5m10s" for 5 minutes and 10
| seconds.
`,
},
{
&struct{ D time.Time }{},
`D = 2006-01-99`,
`
| toml: error: invalid datetime: "2006-01-99"
|
| At line 1, column 5-14:
|
| 1 | D = 2006-01-99
| ^^^^^^^^^^
| Error help:
|
| A TOML datetime must be in one of the following formats:
|
| 2006-01-02T15:04:05Z07:00 Date and time, with timezone.
| 2006-01-02T15:04:05 Date and time, but without timezone.
| 2006-01-02 Date without a time or timezone.
| 15:04:05 Just a time, without any timezone.
|
| Seconds may optionally have a fraction, up to nanosecond precision:
|
| 15:04:05.123
| 15:04:05.856018510
`,
},
}
prep := func(s string) string {
lines := strings.Split(strings.TrimSpace(s), "\n")
for i := range lines {
if j := strings.IndexByte(lines[i], '|'); j >= 0 {
lines[i] = lines[i][j+1:]
lines[i] = strings.Replace(lines[i], " ", "", 1)
}
}
return strings.Join(lines, "\n") + "\n"
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
_, err := toml.Decode(tt.toml, tt.in)
if err == nil {
t.Fatalf("err is nil; decoded: %#v", tt.in)
}
var pErr toml.ParseError
if !errors.As(err, &pErr) {
t.Fatalf("err is not a ParseError: %#v", err)
}
tt.err = prep(tt.err)
have := pErr.ErrorWithUsage()
// have = strings.ReplaceAll(have, " ", "·")
// tt.err = strings.ReplaceAll(tt.err, " ", "·")
if have != tt.err {
t.Fatalf("\nwant:\n%s\nhave:\n%s", tt.err, have)
}
})
}
}
type Enum1 uint8
func (n *Enum1) UnmarshalText(text []byte) error {
switch t := strings.TrimSpace(string(text)); t {
case "ok":
*n = 1
default:
return fmt.Errorf("invalid value: %q", t)
}
return nil
}
// Make sure custom types are wrapped in ParseError with correct location.
func TestUnmarshalTypeError(t *testing.T) {
var c struct {
K1 string `toml:"k1"`
K2 Enum1 `toml:"k2"`
K3 Enum1 `toml:"k3"`
}
_, err := toml.Decode("k1 = 'asd'\nk2 = 'ok'\nk3 = 'invalid'\nk4 = 'ok'", &c)
if err == nil {
t.Fatal("error is nil")
}
var pErr toml.ParseError
if !errors.As(err, &pErr) {
t.Fatalf("not a ParseError: %#v", err)
}
want := `toml: error: invalid value: "invalid"
At line 3, column 7-13:
1 | k1 = 'asd'
2 | k2 = 'ok'
3 | k3 = 'invalid'
^^^^^^^
`
if have := pErr.ErrorWithUsage(); have != want {
t.Errorf("\nwant:\n%s\nhave:\n%s", want, have)
}
}
type Enum2 uint8
func (n *Enum2) UnmarshalTOML(text any) error {
switch t := strings.TrimSpace(text.(string)); t {
case "ok":
*n = 1
default:
return fmt.Errorf("invalid value: %q", t)
}
return nil
}
func TestMarhsalError(t *testing.T) {
var c struct {
K1 string `toml:"k1"`
K2 Enum2 `toml:"k2"`
K3 Enum2 `toml:"k3"`
}
_, err := toml.Decode("k1 = 'asd'\nk2 = 'ok'\nk3 = 'invalid'\nk4 = 'ok'", &c)
if err == nil {
t.Fatal("error is nil")
}
var pErr toml.ParseError
if !errors.As(err, &pErr) {
t.Fatalf("not a ParseError: %#v", err)
}
want := `toml: error: invalid value: "invalid"
At line 3, column 7-13:
1 | k1 = 'asd'
2 | k2 = 'ok'
3 | k3 = 'invalid'
^^^^^^^
`
if have := pErr.ErrorWithUsage(); have != want {
t.Errorf("\nwant:\n%s\nhave:\n%s", want, have)
}
}
func TestErrorIndent(t *testing.T) {
getErr := func(t *testing.T, tml string) toml.ParseError {
var m map[string]any
_, err := toml.Decode(tml, &m)
if err == nil {
t.Fatal(err)
}
var pErr toml.ParseError
if !errors.As(err, &pErr) {
t.Fatalf("not a ParseError: %#v", err)
}
return pErr
}
err := getErr(t, "\tspaces = xxx")
want := `toml: error: expected value but found "xxx" instead
At line 1, column 11-13:
1 | spaces = xxx
^^^
`
if have := err.ErrorWithUsage(); have != want {
t.Errorf("\nwant:\n%s\nhave:\n%s", want, have)
}
err = getErr(t, "\tspaces\t=\txxx")
want = `toml: error: expected value but found "xxx" instead
At line 1, column 11-13:
1 | spaces = xxx
^^^
`
if have := err.ErrorWithUsage(); have != want {
t.Errorf("\nwant:\n%s\nhave:\n%s", want, have)
}
err = getErr(t, "\txxx \t = \t 1\n\tspaces\t=\txxx")
want = `toml: error: expected value but found "xxx" instead
At line 2, column 11-13:
1 | xxx = 1
2 | spaces = xxx
^^^
`
if have := err.ErrorWithUsage(); have != want {
t.Errorf("\nwant:\n%s\nhave:\n%s", want, have)
}
}