@@ -147,6 +147,18 @@ func (t *T) isNil(obj interface{}) bool {
147
147
return v .IsNil ()
148
148
}
149
149
150
+ // Returns a string representation of a Value that is sanitized based on what
151
+ // we are allowed to see. Private fields can not be exposed via a call to
152
+ // Interface() and trying will cause a panic, so we must use String()
153
+ // in that case.
154
+ func stringValue (v reflect.Value ) string {
155
+ if v .CanInterface () {
156
+ return fmt .Sprintf ("%#v" , v .Interface ())
157
+ } else {
158
+ return v .String ()
159
+ }
160
+ }
161
+
150
162
// Deep comparison. This is based on golang 1.2's reflect.Equal functionality.
151
163
func (t * T ) deepEqual (
152
164
desc string , have , want reflect.Value , ignores []string ,
@@ -206,13 +218,13 @@ func (t *T) deepEqual(
206
218
checkNil := func () bool {
207
219
if want .IsNil () && ! have .IsNil () {
208
220
diffs = append (diffs , fmt .Sprintf ("%s: not equal." , desc ))
209
- diffs = append (diffs , fmt .Sprintf (" have: %#v " , have . Interface ( )))
221
+ diffs = append (diffs , fmt .Sprintf (" have: %s " , stringValue ( have )))
210
222
diffs = append (diffs , " want: nil" )
211
223
return true
212
224
} else if ! want .IsNil () && have .IsNil () {
213
225
diffs = append (diffs , fmt .Sprintf ("%s: not equal." , desc ))
214
226
diffs = append (diffs , " have: nil" )
215
- diffs = append (diffs , fmt .Sprintf (" want: %#v " , want . Interface ( )))
227
+ diffs = append (diffs , fmt .Sprintf (" want: %s " , stringValue ( want )))
216
228
return true
217
229
}
218
230
return false
@@ -224,8 +236,8 @@ func (t *T) deepEqual(
224
236
diffs = append (diffs , fmt .Sprintf (
225
237
"%s: (len(have): %d, len(want): %d)" ,
226
238
desc , have .Len (), want .Len ()))
227
- diffs = append (diffs , fmt .Sprintf (" have: %#v " , have . Interface ( )))
228
- diffs = append (diffs , fmt .Sprintf (" want: %#v " , want . Interface ( )))
239
+ diffs = append (diffs , fmt .Sprintf (" have: %s " , stringValue ( have )))
240
+ diffs = append (diffs , fmt .Sprintf (" want: %s " , stringValue ( want )))
229
241
return true
230
242
}
231
243
return false
0 commit comments