Skip to content

Commit

Permalink
Add test for #384
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed May 19, 2023
1 parent 1a6ca6e commit 2967a1e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
24 changes: 24 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,30 @@ func TestCustomDecode(t *testing.T) {
}
}

// TODO: this should be improved for v2:
// https://github.com/BurntSushi/toml/issues/384
func TestDecodeDoubleTags(t *testing.T) {
var s struct {
A int `toml:"a"`
B int `toml:"a"`
C int `toml:"c"`
}
_, err := Decode(`
a = 1
b = 2
c = 3
`, &s)
if err != nil {
t.Fatal(err)
}

want := `{0 0 3}`
have := fmt.Sprintf("%v", s)
if want != have {
t.Errorf("\nhave: %s\nwant: %s\n", have, want)
}
}

// errorContains checks if the error message in have contains the text in
// want.
//
Expand Down
22 changes: 22 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,28 @@ ArrayOfMixedSlices = [[1, 2], ["a", "b"]]
}
}

func TestEncodeDoubleTags(t *testing.T) {
// TODO: this needs fixing; it shouldn't emit two 'a =' keys.
s := struct {
A int `toml:"a"`
B int `toml:"a"`
C int `toml:"c"`
}{1, 2, 3}
buf := new(strings.Builder)
err := NewEncoder(buf).Encode(s)
if err != nil {
t.Fatal(err)
}

want := `a = 1
a = 2
c = 3
`
if want != buf.String() {
t.Errorf("\nhave: %s\nwant: %s\n", buf.String(), want)
}
}

func encodeExpected(t *testing.T, label string, val interface{}, want string, wantErr error) {
t.Helper()
t.Run(label, func(t *testing.T) {
Expand Down

0 comments on commit 2967a1e

Please sign in to comment.