Skip to content

Commit

Permalink
decode: add (failing) test for undecoded fields in a struct
Browse files Browse the repository at this point in the history
This commit adds a failing test for the scenario decribed in issue
#425
  • Loading branch information
mvo5 committed Sep 24, 2024
1 parent eb72747 commit e4d6f99
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ func TestCustomEncode(t *testing.T) {
// Test for #341
func TestCustomDecode(t *testing.T) {
var outer Outer
_, err := Decode(`
meta, err := Decode(`
Int = 10
Enum = "OTHER_VALUE"
Slice = ["text1", "text2"]
Expand All @@ -1036,6 +1036,9 @@ func TestCustomDecode(t *testing.T) {
if fmt.Sprint(outer.Slice.value) != fmt.Sprint([]string{"text1", "text2"}) {
t.Errorf("\nhave:\n%v\nwant:\n%v\n", outer.Slice.value, []string{"text1", "text2"})
}
if len(meta.Undecoded()) > 0 {
t.Errorf("\ncustom decode leaves unencoded fields: %v\n", meta.Undecoded())
}
}

// TODO: this should be improved for v2:
Expand Down Expand Up @@ -1144,3 +1147,30 @@ func BenchmarkKey(b *testing.B) {
k.String()
}
}

type CustomStruct struct {
Foo string `json:"foo"`
}

func (cs *CustomStruct) UnmarshalTOML(data interface{}) error {
d, _ := data.(map[string]interface{})
cs.Foo = d["foo"].(string)
return nil
}

func TestDecodeCustomStruct(t *testing.T) {
var cs CustomStruct
meta, err := Decode(`
foo = "bar"
`, &cs)
if err != nil {
t.Fatalf("Decode failed: %s", err)
}

if cs.Foo != "bar" {
t.Errorf("\nhave:\n%v\nwant:\n%v\n", cs.Foo, "bar")
}
if len(meta.Undecoded()) > 0 {
t.Errorf("\ncustom decode leaves unencoded fields: %v\n", meta.Undecoded())
}
}

0 comments on commit e4d6f99

Please sign in to comment.