]> Cypherpunks repositories - gostls13.git/commitdiff
archive/tar: adjust bytediff to print full context
authorJoe Tsai <joetsai@digital-static.net>
Fri, 11 Aug 2017 18:47:16 +0000 (11:47 -0700)
committerJoe Tsai <thebrokentoaster@gmail.com>
Mon, 14 Aug 2017 06:27:44 +0000 (06:27 +0000)
Since test files don't exceed 10KiB, print the full context of the diff,
including bytes that are equal.
Also, fix the labels for got and want; they were backwards before.

Change-Id: Ibac022e5f988d26812c3f75b643cae8b95603fc9
Reviewed-on: https://go-review.googlesource.com/55151
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>

src/archive/tar/writer_test.go

index f37d4fdcee8aaffc057bb8ef8c6735f815a961b2..3b58511d1827fca6d2e9feec29683c333d95e986 100644 (file)
@@ -19,28 +19,33 @@ import (
        "time"
 )
 
-// Render a pseudo-diff between two blocks of bytes.
-func bytediff(a []byte, b []byte) (s string) {
-       var ax = strings.Split(hex.Dump(a), "\n")
-       var bx = strings.Split(hex.Dump(b), "\n")
-       for i := 0; i < len(ax) || i < len(bx); i++ {
-               var sa, sb = "", ""
-               if i < len(ax) {
-                       sa = ax[i]
-               }
-               if i < len(bx) {
-                       sb = bx[i]
-               }
-               if sa != sb {
-                       if len(sa) > 0 {
-                               s += "+" + sa + "\n"
-                       }
-                       if len(sb) > 0 {
-                               s += "-" + sb + "\n"
-                       }
+func bytediff(a, b []byte) string {
+       const (
+               uniqueA  = "-  "
+               uniqueB  = "+  "
+               identity = "   "
+       )
+       var ss []string
+       sa := strings.Split(strings.TrimSpace(hex.Dump(a)), "\n")
+       sb := strings.Split(strings.TrimSpace(hex.Dump(b)), "\n")
+       for len(sa) > 0 && len(sb) > 0 {
+               if sa[0] == sb[0] {
+                       ss = append(ss, identity+sa[0])
+               } else {
+                       ss = append(ss, uniqueA+sa[0])
+                       ss = append(ss, uniqueB+sb[0])
                }
+               sa, sb = sa[1:], sb[1:]
+       }
+       for len(sa) > 0 {
+               ss = append(ss, uniqueA+sa[0])
+               sa = sa[1:]
+       }
+       for len(sb) > 0 {
+               ss = append(ss, uniqueB+sb[0])
+               sb = sb[1:]
        }
-       return s
+       return strings.Join(ss, "\n")
 }
 
 func TestWriter(t *testing.T) {
@@ -250,7 +255,7 @@ func TestWriter(t *testing.T) {
                                }
                                got := buf.Bytes()
                                if !bytes.Equal(want, got) {
-                                       t.Fatalf("incorrect result: (-=want, +=got)\n%v", bytediff(want, got))
+                                       t.Fatalf("incorrect result: (-got +want)\n%v", bytediff(got, want))
                                }
                        }
                })