Skip to content

Commit 402949e

Browse files
authored
Merge pull request #266 from dsnet/fix-format
Fix textual printing of byte slices
2 parents 290a6a2 + d5fcb38 commit 402949e

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

cmp/compare_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,26 @@ using the AllowUnexported option.`, "\n"),
13171317
x: "d5c14bdf6bac81c27afc5429500ed750\n25483503b557c606dad4f144d27ae10b\n90bdbcdbb6ea7156068e3dcfb7459244\n978f480a6e3cced51e297fbff9a506b7\n",
13181318
y: "Xd5c14bdf6bac81c27afc5429500ed750\nX25483503b557c606dad4f144d27ae10b\nX90bdbcdbb6ea7156068e3dcfb7459244\nX978f480a6e3cced51e297fbff9a506b7\n",
13191319
reason: "all lines are different, so diffing based on lines is pointless",
1320+
}, {
1321+
label: label + "/StringifiedBytes",
1322+
x: struct{ X []byte }{[]byte("hello, world!")},
1323+
y: struct{ X []byte }{},
1324+
reason: "[]byte should be printed as text since it is printable text",
1325+
}, {
1326+
label: label + "/NonStringifiedBytes",
1327+
x: struct{ X []byte }{[]byte("\xde\xad\xbe\xef")},
1328+
y: struct{ X []byte }{},
1329+
reason: "[]byte should not be printed as text since it is binary data",
1330+
}, {
1331+
label: label + "/StringifiedNamedBytes",
1332+
x: struct{ X MyBytes }{MyBytes("hello, world!")},
1333+
y: struct{ X MyBytes }{},
1334+
reason: "MyBytes should be printed as text since it is printable text",
1335+
}, {
1336+
label: label + "/NonStringifiedNamedBytes",
1337+
x: struct{ X MyBytes }{MyBytes("\xde\xad\xbe\xef")},
1338+
y: struct{ X MyBytes }{},
1339+
reason: "MyBytes should not be printed as text since it is binary data",
13201340
}}
13211341
}
13221342

cmp/report_reflect.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,10 @@ func (opts formatOptions) FormatValue(v reflect.Value, parentKind reflect.Kind,
207207
// Check whether this is a []byte of text data.
208208
if t.Elem() == reflect.TypeOf(byte(0)) {
209209
b := v.Bytes()
210-
isPrintSpace := func(r rune) bool { return unicode.IsPrint(r) && unicode.IsSpace(r) }
210+
isPrintSpace := func(r rune) bool { return unicode.IsPrint(r) || unicode.IsSpace(r) }
211211
if len(b) > 0 && utf8.Valid(b) && len(bytes.TrimFunc(b, isPrintSpace)) == 0 {
212212
out = opts.formatString("", string(b))
213+
skipType = true
213214
return opts.WithTypeMode(emitType).FormatType(t, out)
214215
}
215216
}

cmp/testdata/diffs

+24
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,30 @@
10961096
"978f480a6e3cced51e297fbff9a506b7\n",
10971097
}, "")
10981098
>>> TestDiff/Reporter/AllLinesDiffer
1099+
<<< TestDiff/Reporter/StringifiedBytes
1100+
struct{ X []uint8 }{
1101+
- X: []uint8("hello, world!"),
1102+
+ X: nil,
1103+
}
1104+
>>> TestDiff/Reporter/StringifiedBytes
1105+
<<< TestDiff/Reporter/NonStringifiedBytes
1106+
struct{ X []uint8 }{
1107+
- X: []uint8{0xde, 0xad, 0xbe, 0xef},
1108+
+ X: nil,
1109+
}
1110+
>>> TestDiff/Reporter/NonStringifiedBytes
1111+
<<< TestDiff/Reporter/StringifiedNamedBytes
1112+
struct{ X cmp_test.MyBytes }{
1113+
- X: cmp_test.MyBytes("hello, world!"),
1114+
+ X: nil,
1115+
}
1116+
>>> TestDiff/Reporter/StringifiedNamedBytes
1117+
<<< TestDiff/Reporter/NonStringifiedNamedBytes
1118+
struct{ X cmp_test.MyBytes }{
1119+
- X: cmp_test.MyBytes{0xde, 0xad, 0xbe, 0xef},
1120+
+ X: nil,
1121+
}
1122+
>>> TestDiff/Reporter/NonStringifiedNamedBytes
10991123
<<< TestDiff/EmbeddedStruct/ParentStructA/Inequal
11001124
teststructs.ParentStructA{
11011125
privateStruct: teststructs.privateStruct{

0 commit comments

Comments
 (0)