Skip to content

Commit

Permalink
Use CanInterface() to avoid panic on unexported fields
Browse files Browse the repository at this point in the history
This needs tests before merging.
  • Loading branch information
philpennock committed Nov 19, 2016
1 parent aa5f957 commit 259ff64
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,21 @@ func (t *T) deepEqual(
checkNil := func() bool {
if want.IsNil() && !have.IsNil() {
diffs = append(diffs, fmt.Sprintf("%s: not equal.", desc))
diffs = append(diffs, fmt.Sprintf(" have: %#v", have.Interface()))
if have.CanInterface() {
diffs = append(diffs, fmt.Sprintf(" have: %#v", have.Interface()))
} else {
diffs = append(diffs, " have <unexported field>")
}
diffs = append(diffs, " want: nil")
return true
} else if !want.IsNil() && have.IsNil() {
diffs = append(diffs, fmt.Sprintf("%s: not equal.", desc))
diffs = append(diffs, " have: nil")
diffs = append(diffs, fmt.Sprintf(" want: %#v", want.Interface()))
if want.CanInterface() {
diffs = append(diffs, fmt.Sprintf(" want: %#v", want.Interface()))
} else {
diffs = append(diffs, " want <unexported field>")
}
return true
}
return false
Expand Down

0 comments on commit 259ff64

Please sign in to comment.