Skip to content

Commit

Permalink
test: add failing test for mitchellhGH-347
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sagi-Kazar <[email protected]>
  • Loading branch information
sagikazarmark committed Dec 8, 2023
1 parent bf980b3 commit 0d2538a
Showing 1 changed file with 107 additions and 3 deletions.
110 changes: 107 additions & 3 deletions mapstructure_bugs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ func TestNestedTypePointerWithDefaults(t *testing.T) {
if result.Vbar.Vuint != 42 {
t.Errorf("vuint value should be 42: %#v", result.Vbar.Vuint)
}

}

type NestedSlice struct {
Expand Down Expand Up @@ -284,7 +283,6 @@ func TestNestedTypeWithDefaults(t *testing.T) {
if result.Vbar.Vuint != 42 {
t.Errorf("vuint value should be 42: %#v", result.Vbar.Vuint)
}

}

// #67 panic() on extending slices (decodeSlice with disabled ZeroValues)
Expand Down Expand Up @@ -526,7 +524,6 @@ func TestDecodeIntermediateMapsSettable(t *testing.T) {
return data, nil
},
})

if err != nil {
t.Fatalf("failed to create decoder: %v", err)
}
Expand Down Expand Up @@ -625,3 +622,110 @@ func TestMapOmitEmptyWithEmptyFieldnameInTag(t *testing.T) {
t.Fatalf("fail: %#v", m)
}
}

// GH-347: Decoding maps with multiple indirection results in an error
func TestDecodeToMapWithMultipleIndirection(t *testing.T) {
t.Run("Struct", func(t *testing.T) {
type Struct struct {
Foo string `mapstructure:"foo"`
}

v := Struct{
Foo: "bar",
}

i := &v
ii := &i
iii := &ii

var actual map[string]interface{}

if err := Decode(iii, &actual); err != nil {
t.Fatal(err)
}

expected := map[string]interface{}{
"foo": "bar",
}

if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected: %#v, got: %#v", expected, actual)
}
})

t.Run("Map", func(t *testing.T) {
v := map[string]interface{}{
"foo": "bar",
}

i := &v
ii := &i
iii := &ii

var actual map[string]interface{}

if err := Decode(iii, &actual); err != nil {
t.Fatal(err)
}

expected := map[string]interface{}{
"foo": "bar",
}

if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected: %#v, got: %#v", expected, actual)
}
})

t.Run("Array", func(t *testing.T) {
v := [1]map[string]interface{}{
{
"foo": "bar",
},
}

i := &v
ii := &i
iii := &ii

var actual map[string]interface{}

if err := WeakDecode(iii, &actual); err != nil {
t.Fatal(err)
}

expected := map[string]interface{}{
"foo": "bar",
}

if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected: %#v, got: %#v", expected, actual)
}
})

t.Run("Slice", func(t *testing.T) {
v := []map[string]interface{}{
{
"foo": "bar",
},
}

i := &v
ii := &i
iii := &ii

var actual map[string]interface{}

if err := WeakDecode(iii, &actual); err != nil {
t.Fatal(err)
}

expected := map[string]interface{}{
"foo": "bar",
}

if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected: %#v, got: %#v", expected, actual)
}
})
}

0 comments on commit 0d2538a

Please sign in to comment.