diff options
author | Joe Tsai <joetsai@digital-static.net> | 2020-06-10 11:11:33 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-10 11:11:33 -0700 |
commit | a171aa74446ac6ce47f4f09b10deb7d9afc7dc20 (patch) | |
tree | f7a14d0a558eafba4aa1b449569d54562fe7891e | |
parent | 88849e8bc9a647e7dde31569a79f5ea1f624ef2b (diff) | |
download | go-cmp-a171aa74446ac6ce47f4f09b10deb7d9afc7dc20.tar.gz |
Use raw string literal syntax only for valid UTF-8 (#211)
-rw-r--r-- | cmp/compare_test.go | 5 | ||||
-rw-r--r-- | cmp/report_reflect.go | 3 | ||||
-rw-r--r-- | cmp/testdata/diffs | 5 |
3 files changed, 12 insertions, 1 deletions
diff --git a/cmp/compare_test.go b/cmp/compare_test.go index a7f78e8..7f0a395 100644 --- a/cmp/compare_test.go +++ b/cmp/compare_test.go @@ -822,6 +822,11 @@ func reporterTests() []test { ) return []test{{ + label: "/InvalidUTF8", + x: MyString("\xed\xa0\x80"), + wantEqual: false, + reason: "invalid UTF-8 should format as quoted string", + }, { label: label, x: MyComposite{IntsA: []int8{11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}}, y: MyComposite{IntsA: []int8{10, 11, 21, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}}, diff --git a/cmp/report_reflect.go b/cmp/report_reflect.go index 642622e..9bc5eb3 100644 --- a/cmp/report_reflect.go +++ b/cmp/report_reflect.go @@ -10,6 +10,7 @@ import ( "strconv" "strings" "unicode" + "unicode/utf8" "github.com/google/go-cmp/cmp/internal/flags" "github.com/google/go-cmp/cmp/internal/value" @@ -235,7 +236,7 @@ func formatString(s string) string { rawInvalid := func(r rune) bool { return r == '`' || r == '\n' || !(unicode.IsPrint(r) || r == '\t') } - if strings.IndexFunc(s, rawInvalid) < 0 { + if utf8.ValidString(s) && strings.IndexFunc(s, rawInvalid) < 0 { return "`" + s + "`" } return qs diff --git a/cmp/testdata/diffs b/cmp/testdata/diffs index 4d8e71d..2be80af 100644 --- a/cmp/testdata/diffs +++ b/cmp/testdata/diffs @@ -258,6 +258,11 @@ })), } >>> TestDiff/Transformer#05 +<<< TestDiff//InvalidUTF8 + interface{}( +- cmp_test.MyString("\xed\xa0\x80"), + ) +>>> TestDiff//InvalidUTF8 <<< TestDiff/Reporter cmp_test.MyComposite{ ... // 3 identical fields |