Skip to content

Commit 2967a1e

Browse files
committed
Add test for #384
1 parent 1a6ca6e commit 2967a1e

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

decode_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,30 @@ func TestCustomDecode(t *testing.T) {
11471147
}
11481148
}
11491149

1150+
// TODO: this should be improved for v2:
1151+
// https://github.com/BurntSushi/toml/issues/384
1152+
func TestDecodeDoubleTags(t *testing.T) {
1153+
var s struct {
1154+
A int `toml:"a"`
1155+
B int `toml:"a"`
1156+
C int `toml:"c"`
1157+
}
1158+
_, err := Decode(`
1159+
a = 1
1160+
b = 2
1161+
c = 3
1162+
`, &s)
1163+
if err != nil {
1164+
t.Fatal(err)
1165+
}
1166+
1167+
want := `{0 0 3}`
1168+
have := fmt.Sprintf("%v", s)
1169+
if want != have {
1170+
t.Errorf("\nhave: %s\nwant: %s\n", have, want)
1171+
}
1172+
}
1173+
11501174
// errorContains checks if the error message in have contains the text in
11511175
// want.
11521176
//

encode_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,28 @@ ArrayOfMixedSlices = [[1, 2], ["a", "b"]]
12391239
}
12401240
}
12411241

1242+
func TestEncodeDoubleTags(t *testing.T) {
1243+
// TODO: this needs fixing; it shouldn't emit two 'a =' keys.
1244+
s := struct {
1245+
A int `toml:"a"`
1246+
B int `toml:"a"`
1247+
C int `toml:"c"`
1248+
}{1, 2, 3}
1249+
buf := new(strings.Builder)
1250+
err := NewEncoder(buf).Encode(s)
1251+
if err != nil {
1252+
t.Fatal(err)
1253+
}
1254+
1255+
want := `a = 1
1256+
a = 2
1257+
c = 3
1258+
`
1259+
if want != buf.String() {
1260+
t.Errorf("\nhave: %s\nwant: %s\n", buf.String(), want)
1261+
}
1262+
}
1263+
12421264
func encodeExpected(t *testing.T, label string, val interface{}, want string, wantErr error) {
12431265
t.Helper()
12441266
t.Run(label, func(t *testing.T) {

0 commit comments

Comments
 (0)