forked from BurntSushi/toml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode_test.go
759 lines (677 loc) · 17.7 KB
/
decode_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
package toml
import (
"fmt"
"io/ioutil"
"math"
"os"
"reflect"
"strings"
"testing"
"time"
"github.com/BurntSushi/toml/internal"
)
func TestDecodeReader(t *testing.T) {
var i struct{ A int }
meta, err := DecodeReader(strings.NewReader("a = 42"), &i)
if err != nil {
t.Fatal(err)
}
have := fmt.Sprintf("%v %v %v", i, meta.Keys(), meta.Type("a"))
want := "{42} [a] Integer"
if have != want {
t.Errorf("\nhave: %s\nwant: %s", have, want)
}
}
func TestDecodeFile(t *testing.T) {
tmp, err := ioutil.TempFile("", "toml-")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmp.Name())
if _, err := tmp.WriteString("a = 42"); err != nil {
t.Fatal(err)
}
if err := tmp.Close(); err != nil {
t.Fatal(err)
}
var i struct{ A int }
meta, err := DecodeFile(tmp.Name(), &i)
if err != nil {
t.Fatal(err)
}
have := fmt.Sprintf("%v %v %v", i, meta.Keys(), meta.Type("a"))
want := "{42} [a] Integer"
if have != want {
t.Errorf("\nhave: %s\nwant: %s", have, want)
}
}
func TestDecodeBOM(t *testing.T) {
for _, tt := range [][]byte{
[]byte("\xff\xfea = \"b\""),
[]byte("\xfe\xffa = \"b\""),
} {
t.Run("", func(t *testing.T) {
var s struct{ A string }
_, err := Decode(string(tt), &s)
if err != nil {
t.Fatal(err)
}
if s.A != "b" {
t.Errorf(`s.A is not "b" but %q`, s.A)
}
})
}
}
func TestDecodeEmbedded(t *testing.T) {
type Dog struct{ Name string }
type Age int
type cat struct{ Name string }
for _, test := range []struct {
label string
input string
decodeInto interface{}
wantDecoded interface{}
}{
{
label: "embedded struct",
input: `Name = "milton"`,
decodeInto: &struct{ Dog }{},
wantDecoded: &struct{ Dog }{Dog{"milton"}},
},
{
label: "embedded non-nil pointer to struct",
input: `Name = "milton"`,
decodeInto: &struct{ *Dog }{},
wantDecoded: &struct{ *Dog }{&Dog{"milton"}},
},
{
label: "embedded nil pointer to struct",
input: ``,
decodeInto: &struct{ *Dog }{},
wantDecoded: &struct{ *Dog }{nil},
},
{
label: "unexported embedded struct",
input: `Name = "socks"`,
decodeInto: &struct{ cat }{},
wantDecoded: &struct{ cat }{cat{"socks"}},
},
{
label: "embedded int",
input: `Age = -5`,
decodeInto: &struct{ Age }{},
wantDecoded: &struct{ Age }{-5},
},
} {
_, err := Decode(test.input, test.decodeInto)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(test.wantDecoded, test.decodeInto) {
t.Errorf("%s: want decoded == %+v, got %+v",
test.label, test.wantDecoded, test.decodeInto)
}
}
}
func TestDecodeIgnoreFields(t *testing.T) {
const input = `
Number = 123
- = 234
`
var s struct {
Number int `toml:"-"`
}
if _, err := Decode(input, &s); err != nil {
t.Fatal(err)
}
if s.Number != 0 {
t.Errorf("got: %d; want 0", s.Number)
}
}
func TestDecodeTableArrays(t *testing.T) {
var tomlTableArrays = `
[[albums]]
name = "Born to Run"
[[albums.songs]]
name = "Jungleland"
[[albums.songs]]
name = "Meeting Across the River"
[[albums]]
name = "Born in the USA"
[[albums.songs]]
name = "Glory Days"
[[albums.songs]]
name = "Dancing in the Dark"
`
type Song struct {
Name string
}
type Album struct {
Name string
Songs []Song
}
type Music struct {
Albums []Album
}
expected := Music{[]Album{
{"Born to Run", []Song{{"Jungleland"}, {"Meeting Across the River"}}},
{"Born in the USA", []Song{{"Glory Days"}, {"Dancing in the Dark"}}},
}}
var got Music
if _, err := Decode(tomlTableArrays, &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, got) {
t.Fatalf("\n%#v\n!=\n%#v\n", expected, got)
}
}
func TestDecodePointers(t *testing.T) {
type Object struct {
Type string
Description string
}
type Dict struct {
NamedObject map[string]*Object
BaseObject *Object
Strptr *string
Strptrs []*string
}
s1, s2, s3 := "blah", "abc", "def"
expected := &Dict{
Strptr: &s1,
Strptrs: []*string{&s2, &s3},
NamedObject: map[string]*Object{
"foo": {"FOO", "fooooo!!!"},
"bar": {"BAR", "ba-ba-ba-ba-barrrr!!!"},
},
BaseObject: &Object{"BASE", "da base"},
}
ex1 := `
Strptr = "blah"
Strptrs = ["abc", "def"]
[NamedObject.foo]
Type = "FOO"
Description = "fooooo!!!"
[NamedObject.bar]
Type = "BAR"
Description = "ba-ba-ba-ba-barrrr!!!"
[BaseObject]
Type = "BASE"
Description = "da base"
`
dict := new(Dict)
_, err := Decode(ex1, dict)
if err != nil {
t.Errorf("Decode error: %v", err)
}
if !reflect.DeepEqual(expected, dict) {
t.Fatalf("\n%#v\n!=\n%#v\n", expected, dict)
}
}
func TestDecodeBadDatetime(t *testing.T) {
var x struct{ T time.Time }
for _, s := range []string{"123", "1230"} {
input := "T = " + s
if _, err := Decode(input, &x); err == nil {
t.Errorf("Expected invalid DateTime error for %q", s)
}
}
}
type sphere struct {
Center [3]float64
Radius float64
}
func TestDecodeArrayWrongSize(t *testing.T) {
var s1 sphere
if _, err := Decode(`center = [0.1, 2.3]`, &s1); err == nil {
t.Fatal("Expected array type mismatch error")
}
}
func TestDecodeIntOverflow(t *testing.T) {
type table struct {
Value int8
}
var tab table
if _, err := Decode(`value = 500`, &tab); err == nil {
t.Fatal("Expected integer out-of-bounds error.")
}
}
func TestDecodeFloatOverflow(t *testing.T) {
tests := []struct {
value string
overflow bool
}{
{fmt.Sprintf(`F32 = %f`, math.MaxFloat64), true},
{fmt.Sprintf(`F32 = %f`, -math.MaxFloat64), true},
{fmt.Sprintf(`F32 = %f`, math.MaxFloat32*1.1), true},
{fmt.Sprintf(`F32 = %f`, -math.MaxFloat32*1.1), true},
{fmt.Sprintf(`F32 = %d`, maxSafeFloat32Int+1), true},
{fmt.Sprintf(`F32 = %d`, -maxSafeFloat32Int-1), true},
{fmt.Sprintf(`F64 = %d`, maxSafeFloat64Int+1), true},
{fmt.Sprintf(`F64 = %d`, -maxSafeFloat64Int-1), true},
{fmt.Sprintf(`F32 = %f`, math.MaxFloat32), false},
{fmt.Sprintf(`F32 = %f`, -math.MaxFloat32), false},
{fmt.Sprintf(`F32 = %d`, maxSafeFloat32Int), false},
{fmt.Sprintf(`F32 = %d`, -maxSafeFloat32Int), false},
{fmt.Sprintf(`F64 = %f`, math.MaxFloat64), false},
{fmt.Sprintf(`F64 = %f`, -math.MaxFloat64), false},
{fmt.Sprintf(`F64 = %f`, math.MaxFloat32), false},
{fmt.Sprintf(`F64 = %f`, -math.MaxFloat32), false},
{fmt.Sprintf(`F64 = %d`, maxSafeFloat64Int), false},
{fmt.Sprintf(`F64 = %d`, -maxSafeFloat64Int), false},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
var tab struct {
F32 float32
F64 float64
}
_, err := Decode(tt.value, &tab)
if tt.overflow && err == nil {
t.Fatal("expected error, but err is nil")
}
if (tt.overflow && !errorContains(err, "out of range")) || (!tt.overflow && err != nil) {
t.Fatalf("unexpected error:\n%v", err)
}
})
}
}
func TestDecodeSizedInts(t *testing.T) {
type table struct {
U8 uint8
U16 uint16
U32 uint32
U64 uint64
U uint
I8 int8
I16 int16
I32 int32
I64 int64
I int
}
answer := table{1, 1, 1, 1, 1, -1, -1, -1, -1, -1}
toml := `
u8 = 1
u16 = 1
u32 = 1
u64 = 1
u = 1
i8 = -1
i16 = -1
i32 = -1
i64 = -1
i = -1
`
var tab table
if _, err := Decode(toml, &tab); err != nil {
t.Fatal(err.Error())
}
if answer != tab {
t.Fatalf("Expected %#v but got %#v", answer, tab)
}
}
type NopUnmarshalTOML int
func (NopUnmarshalTOML) UnmarshalTOML(p interface{}) error { return nil }
func TestDecodeTypes(t *testing.T) {
type mystr string
for _, tt := range []struct {
v interface{}
want string
}{
{new(map[string]bool), ""},
{new(map[mystr]bool), ""},
{new(NopUnmarshalTOML), ""},
{3, `toml: cannot decode to non-pointer "int"`},
{map[string]interface{}{}, `toml: cannot decode to non-pointer "map[string]interface {}"`},
{(*int)(nil), `toml: cannot decode to nil value of "*int"`},
{(*Unmarshaler)(nil), `toml: cannot decode to nil value of "*toml.Unmarshaler"`},
{nil, `toml: cannot decode to non-pointer <nil>`},
{new(map[int]string), "cannot decode to a map with non-string key type"},
{new(map[interface{}]string), "cannot decode to a map with non-string key type"},
{new(struct{ F int }), `toml: incompatible types: TOML key "F" has type bool; destination has type integer`},
{new(map[string]int), `toml: incompatible types: TOML key "F" has type bool; destination has type integer`},
{new(int), `toml: cannot decode to type int`},
{new([]int), "toml: cannot decode to type []int"},
} {
t.Run(fmt.Sprintf("%T", tt.v), func(t *testing.T) {
_, err := Decode(`F = true`, tt.v)
if !errorContains(err, tt.want) {
t.Errorf("wrong error\nhave: %q\nwant: %q", err, tt.want)
}
})
}
}
func TestUnmarshaler(t *testing.T) {
var tomlBlob = `
[dishes.hamboogie]
name = "Hamboogie with fries"
price = 10.99
[[dishes.hamboogie.ingredients]]
name = "Bread Bun"
[[dishes.hamboogie.ingredients]]
name = "Lettuce"
[[dishes.hamboogie.ingredients]]
name = "Real Beef Patty"
[[dishes.hamboogie.ingredients]]
name = "Tomato"
[dishes.eggsalad]
name = "Egg Salad with rice"
price = 3.99
[[dishes.eggsalad.ingredients]]
name = "Egg"
[[dishes.eggsalad.ingredients]]
name = "Mayo"
[[dishes.eggsalad.ingredients]]
name = "Rice"
`
m := &menu{}
if _, err := Decode(tomlBlob, m); err != nil {
t.Fatal(err)
}
if len(m.Dishes) != 2 {
t.Log("two dishes should be loaded with UnmarshalTOML()")
t.Errorf("expected %d but got %d", 2, len(m.Dishes))
}
eggSalad := m.Dishes["eggsalad"]
if _, ok := interface{}(eggSalad).(dish); !ok {
t.Errorf("expected a dish")
}
if eggSalad.Name != "Egg Salad with rice" {
t.Errorf("expected the dish to be named 'Egg Salad with rice'")
}
if len(eggSalad.Ingredients) != 3 {
t.Log("dish should be loaded with UnmarshalTOML()")
t.Errorf("expected %d but got %d", 3, len(eggSalad.Ingredients))
}
found := false
for _, i := range eggSalad.Ingredients {
if i.Name == "Rice" {
found = true
break
}
}
if !found {
t.Error("Rice was not loaded in UnmarshalTOML()")
}
// test on a value - must be passed as *
o := menu{}
if _, err := Decode(tomlBlob, &o); err != nil {
t.Fatal(err)
}
}
func TestDecodeInlineTable(t *testing.T) {
input := `
[CookieJar]
Types = {Chocolate = "yummy", Oatmeal = "best ever"}
[Seasons]
Locations = {NY = {Temp = "not cold", Rating = 4}, MI = {Temp = "freezing", Rating = 9}}
`
type cookieJar struct {
Types map[string]string
}
type properties struct {
Temp string
Rating int
}
type seasons struct {
Locations map[string]properties
}
type wrapper struct {
CookieJar cookieJar
Seasons seasons
}
var got wrapper
meta, err := Decode(input, &got)
if err != nil {
t.Fatal(err)
}
want := wrapper{
CookieJar: cookieJar{
Types: map[string]string{
"Chocolate": "yummy",
"Oatmeal": "best ever",
},
},
Seasons: seasons{
Locations: map[string]properties{
"NY": {
Temp: "not cold",
Rating: 4,
},
"MI": {
Temp: "freezing",
Rating: 9,
},
},
},
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("after decode, got:\n\n%#v\n\nwant:\n\n%#v", got, want)
}
if len(meta.keys) != 12 {
t.Errorf("after decode, got %d meta keys; want 12", len(meta.keys))
}
if len(meta.types) != 12 {
t.Errorf("after decode, got %d meta types; want 12", len(meta.types))
}
}
func TestDecodeInlineTableArray(t *testing.T) {
type point struct {
X, Y, Z int
}
var got struct {
Points []point
}
// Example inline table array from the spec.
const in = `
points = [ { x = 1, y = 2, z = 3 },
{ x = 7, y = 8, z = 9 },
{ x = 2, y = 4, z = 8 } ]
`
if _, err := Decode(in, &got); err != nil {
t.Fatal(err)
}
want := []point{
{X: 1, Y: 2, Z: 3},
{X: 7, Y: 8, Z: 9},
{X: 2, Y: 4, Z: 8},
}
if !reflect.DeepEqual(got.Points, want) {
t.Errorf("got %#v; want %#v", got.Points, want)
}
}
type menu struct {
Dishes map[string]dish
}
func (m *menu) UnmarshalTOML(p interface{}) error {
m.Dishes = make(map[string]dish)
data, _ := p.(map[string]interface{})
dishes := data["dishes"].(map[string]interface{})
for n, v := range dishes {
if d, ok := v.(map[string]interface{}); ok {
nd := dish{}
nd.UnmarshalTOML(d)
m.Dishes[n] = nd
} else {
return fmt.Errorf("not a dish")
}
}
return nil
}
type dish struct {
Name string
Price float32
Ingredients []ingredient
}
func (d *dish) UnmarshalTOML(p interface{}) error {
data, _ := p.(map[string]interface{})
d.Name, _ = data["name"].(string)
d.Price, _ = data["price"].(float32)
ingredients, _ := data["ingredients"].([]map[string]interface{})
for _, e := range ingredients {
n, _ := interface{}(e).(map[string]interface{})
name, _ := n["name"].(string)
i := ingredient{name}
d.Ingredients = append(d.Ingredients, i)
}
return nil
}
type ingredient struct {
Name string
}
func TestDecodeSlices(t *testing.T) {
type T struct {
S []string
}
for i, tt := range []struct {
v T
input string
want T
}{
{T{}, "", T{}},
{T{[]string{}}, "", T{[]string{}}},
{T{[]string{"a", "b"}}, "", T{[]string{"a", "b"}}},
{T{}, "S = []", T{[]string{}}},
{T{[]string{}}, "S = []", T{[]string{}}},
{T{[]string{"a", "b"}}, "S = []", T{[]string{}}},
{T{}, `S = ["x"]`, T{[]string{"x"}}},
{T{[]string{}}, `S = ["x"]`, T{[]string{"x"}}},
{T{[]string{"a", "b"}}, `S = ["x"]`, T{[]string{"x"}}},
} {
if _, err := Decode(tt.input, &tt.v); err != nil {
t.Errorf("[%d] %s", i, err)
continue
}
if !reflect.DeepEqual(tt.v, tt.want) {
t.Errorf("[%d] got %#v; want %#v", i, tt.v, tt.want)
}
}
}
func TestDecodePrimitive(t *testing.T) {
type S struct {
P Primitive
}
type T struct {
S []int
}
slicep := func(s []int) *[]int { return &s }
arrayp := func(a [2]int) *[2]int { return &a }
mapp := func(m map[string]int) *map[string]int { return &m }
for i, tt := range []struct {
v interface{}
input string
want interface{}
}{
// slices
{slicep(nil), "", slicep(nil)},
{slicep([]int{}), "", slicep([]int{})},
{slicep([]int{1, 2, 3}), "", slicep([]int{1, 2, 3})},
{slicep(nil), "P = [1,2]", slicep([]int{1, 2})},
{slicep([]int{}), "P = [1,2]", slicep([]int{1, 2})},
{slicep([]int{1, 2, 3}), "P = [1,2]", slicep([]int{1, 2})},
// arrays
{arrayp([2]int{2, 3}), "", arrayp([2]int{2, 3})},
{arrayp([2]int{2, 3}), "P = [3,4]", arrayp([2]int{3, 4})},
// maps
{mapp(nil), "", mapp(nil)},
{mapp(map[string]int{}), "", mapp(map[string]int{})},
{mapp(map[string]int{"a": 1}), "", mapp(map[string]int{"a": 1})},
{mapp(nil), "[P]\na = 2", mapp(map[string]int{"a": 2})},
{mapp(map[string]int{}), "[P]\na = 2", mapp(map[string]int{"a": 2})},
{mapp(map[string]int{"a": 1, "b": 3}), "[P]\na = 2", mapp(map[string]int{"a": 2, "b": 3})},
// structs
{&T{nil}, "[P]", &T{nil}},
{&T{[]int{}}, "[P]", &T{[]int{}}},
{&T{[]int{1, 2, 3}}, "[P]", &T{[]int{1, 2, 3}}},
{&T{nil}, "[P]\nS = [1,2]", &T{[]int{1, 2}}},
{&T{[]int{}}, "[P]\nS = [1,2]", &T{[]int{1, 2}}},
{&T{[]int{1, 2, 3}}, "[P]\nS = [1,2]", &T{[]int{1, 2}}},
} {
var s S
md, err := Decode(tt.input, &s)
if err != nil {
t.Errorf("[%d] Decode error: %s", i, err)
continue
}
if err := md.PrimitiveDecode(s.P, tt.v); err != nil {
t.Errorf("[%d] PrimitiveDecode error: %s", i, err)
continue
}
if !reflect.DeepEqual(tt.v, tt.want) {
t.Errorf("[%d] got %#v; want %#v", i, tt.v, tt.want)
}
}
}
func TestDecodeDatetime(t *testing.T) {
// Test here in addition to toml-test to ensure the TZs are correct.
tz7 := time.FixedZone("", -3600*7)
for _, tt := range []struct {
in string
want time.Time
}{
// Offset datetime
{"1979-05-27T07:32:00Z", time.Date(1979, 05, 27, 07, 32, 0, 0, time.UTC)},
{"1979-05-27T07:32:00.999999Z", time.Date(1979, 05, 27, 07, 32, 0, 999999000, time.UTC)},
{"1979-05-27T00:32:00-07:00", time.Date(1979, 05, 27, 00, 32, 0, 0, tz7)},
{"1979-05-27T00:32:00.999999-07:00", time.Date(1979, 05, 27, 00, 32, 0, 999999000, tz7)},
{"1979-05-27T00:32:00.24-07:00", time.Date(1979, 05, 27, 00, 32, 0, 240000000, tz7)},
{"1979-05-27 07:32:00Z", time.Date(1979, 05, 27, 07, 32, 0, 0, time.UTC)},
{"1979-05-27t07:32:00z", time.Date(1979, 05, 27, 07, 32, 0, 0, time.UTC)},
// Make sure the space between the datetime and "#" isn't lexed.
{"1979-05-27T07:32:12-07:00 # c", time.Date(1979, 05, 27, 07, 32, 12, 0, tz7)},
// Local times.
{"1979-05-27T07:32:00", time.Date(1979, 05, 27, 07, 32, 0, 0, internal.LocalDatetime)},
{"1979-05-27T07:32:00.999999", time.Date(1979, 05, 27, 07, 32, 0, 999999000, internal.LocalDatetime)},
{"1979-05-27T07:32:00.25", time.Date(1979, 05, 27, 07, 32, 0, 250000000, internal.LocalDatetime)},
{"1979-05-27", time.Date(1979, 05, 27, 0, 0, 0, 0, internal.LocalDate)},
{"07:32:00", time.Date(0, 1, 1, 07, 32, 0, 0, internal.LocalTime)},
{"07:32:00.999999", time.Date(0, 1, 1, 07, 32, 0, 999999000, internal.LocalTime)},
} {
t.Run(tt.in, func(t *testing.T) {
var x struct{ D time.Time }
input := "d = " + tt.in
if _, err := Decode(input, &x); err != nil {
t.Fatalf("got error: %s", err)
}
if h, w := x.D.Format(time.RFC3339Nano), tt.want.Format(time.RFC3339Nano); h != w {
t.Errorf("\nhave: %s\nwant: %s", h, w)
}
})
}
}
func TestMetaDotConflict(t *testing.T) {
var m map[string]interface{}
meta, err := Decode(`
"a.b" = "str"
a.b = 1
"" = 2
`, &m)
if err != nil {
t.Fatal(err)
}
want := `"a.b"=String; a.b=Integer; ""=Integer`
have := ""
for i, k := range meta.Keys() {
if i > 0 {
have += "; "
}
have += k.String() + "=" + meta.Type(k...)
}
if have != want {
t.Errorf("\nhave: %s\nwant: %s", have, want)
}
}
// errorContains checks if the error message in have contains the text in
// want.
//
// This is safe when have is nil. Use an empty string for want if you want to
// test that err is nil.
func errorContains(have error, want string) bool {
if have == nil {
return want == ""
}
if want == "" {
return false
}
return strings.Contains(have.Error(), want)
}