aboutsummaryrefslogtreecommitdiff
path: root/cmp/compare_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmp/compare_test.go')
-rw-r--r--cmp/compare_test.go71
1 files changed, 70 insertions, 1 deletions
diff --git a/cmp/compare_test.go b/cmp/compare_test.go
index 9ad9456..88b7d45 100644
--- a/cmp/compare_test.go
+++ b/cmp/compare_test.go
@@ -43,7 +43,7 @@ var update = flag.Bool("update", false, "update golden test files")
const goldenHeaderPrefix = "<<< "
const goldenFooterPrefix = ">>> "
-/// mustParseGolden parses a file as a set of key-value pairs.
+// mustParseGolden parses a file as a set of key-value pairs.
//
// The syntax is simple and looks something like:
//
@@ -104,6 +104,7 @@ func mustFormatGolden(path string, in []struct{ Name, Data string }) {
var now = time.Date(2009, time.November, 10, 23, 00, 00, 00, time.UTC)
+// TODO(≥go1.18): Define a generic function that boxes a value on the heap.
func newInt(n int) *int { return &n }
type Stringer string
@@ -885,6 +886,7 @@ func reporterTests() []test {
FloatsB []MyFloat
FloatsC MyFloats
}
+ PointerString *string
)
return []test{{
@@ -1351,6 +1353,73 @@ using the AllowUnexported option.`, "\n"),
"bar": true,
}`,
reason: "short multiline JSON should prefer triple-quoted string diff as it is more readable",
+ }, {
+ label: label + "/PointerToStringOrAny",
+ x: func() *string {
+ var v string = "hello"
+ return &v
+ }(),
+ y: func() *interface{} {
+ var v interface{} = "hello"
+ return &v
+ }(),
+ reason: "mismatched types between any and *any should print differently",
+ }, {
+ label: label + "/NamedPointer",
+ x: func() *string {
+ v := "hello"
+ return &v
+ }(),
+ y: func() PointerString {
+ v := "hello"
+ return &v
+ }(),
+ reason: "mismatched pointer types should print differently",
+ }, {
+ label: label + "/MapStringAny",
+ x: map[string]interface{}{"key": int(0)},
+ y: map[string]interface{}{"key": uint(0)},
+ reason: "mismatched underlying value within interface",
+ }, {
+ label: label + "/StructFieldAny",
+ x: struct{ X interface{} }{int(0)},
+ y: struct{ X interface{} }{uint(0)},
+ reason: "mismatched underlying value within interface",
+ }, {
+ label: label + "/SliceOfBytesText",
+ x: [][]byte{
+ []byte("hello"), []byte("foo"), []byte("barbaz"), []byte("blahdieblah"),
+ },
+ y: [][]byte{
+ []byte("foo"), []byte("foo"), []byte("barbaz"), []byte("added"), []byte("here"), []byte("hrmph"),
+ },
+ reason: "should print text byte slices as strings",
+ }, {
+ label: label + "/SliceOfBytesBinary",
+ x: [][]byte{
+ []byte("\xde\xad\xbe\xef"), []byte("\xffoo"), []byte("barbaz"), []byte("blahdieblah"),
+ },
+ y: [][]byte{
+ []byte("\xffoo"), []byte("foo"), []byte("barbaz"), []byte("added"), []byte("here"), []byte("hrmph\xff"),
+ },
+ reason: "should print text byte slices as strings except those with binary",
+ }, {
+ label: label + "/ManyEscapeCharacters",
+ x: `[
+ {"Base32": "NA======"},
+ {"Base32": "NBSQ===="},
+ {"Base32": "NBSWY==="},
+ {"Base32": "NBSWY3A="},
+ {"Base32": "NBSWY3DP"}
+]`,
+ y: `[
+ {"Base32": "NB======"},
+ {"Base32": "NBSQ===="},
+ {"Base32": "NBSWY==="},
+ {"Base32": "NBSWY3A="},
+ {"Base32": "NBSWY3DP"}
+]`,
+ reason: "should use line-based diffing since byte-based diffing is unreadable due to heavy amounts of escaping",
}}
}