Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ func copyExportedFields(expected interface{}) interface{} {
for _, k := range expectedValue.MapKeys() {
index := expectedValue.MapIndex(k)
unexportedRemoved := copyExportedFields(index.Interface())
if unexportedRemoved == nil {
// reflect.ValueOf(nil) is the zero Value, which
// SetMapIndex interprets as "delete this key". Store the
// element type's zero value instead so nil map values are
// preserved and the entry keeps taking part in the
// comparison.
result.SetMapIndex(k, reflect.Zero(expectedType.Elem()))
continue
}
result.SetMapIndex(k, reflect.ValueOf(unexportedRemoved))
}
return result.Interface()
Expand Down
23 changes: 23 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,29 @@ func TestEqualExportedValues(t *testing.T) {
+ Exported: (int) 2,
notExported: (interface {}) <nil>`,
},
// A map entry holding a nil interface value is still an entry: the
// key must not be dropped while copying, otherwise maps with
// different keys would wrongly compare as equal.
{
value1: map[string]interface{}{"a": nil},
value2: map[string]interface{}{"a": nil},
expectedEqual: true,
},
{
value1: map[string]interface{}{"a": nil},
value2: map[string]interface{}{},
expectedEqual: false,
},
{
value1: map[string]interface{}{"a": nil, "b": 1},
value2: map[string]interface{}{"b": 1},
expectedEqual: false,
},
{
value1: map[string]interface{}{"a": nil},
value2: map[string]interface{}{"a": 1},
expectedEqual: false,
},
}

for _, c := range cases {
Expand Down
Loading