Skip to content

Commit

Permalink
update lines
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitpaulk committed Oct 2, 2023
1 parent ff484d1 commit 3b891be
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
50 changes: 25 additions & 25 deletions bytes_diff_visualizer/bytes_diff_visualizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,54 +35,54 @@ func VisualizeByteDiff(actual []byte, expected []byte) []string {
}
}

byteCountToDisplay := 20
byteRangeStart := intmax(0, firstDiffIndex-(byteCountToDisplay/2))
byteRangeEnd := byteRangeStart + byteCountToDisplay
totalByteCountToDisplay := 100
byteCountPerLine := 20
byteRangeStart := intmax(0, firstDiffIndex-(totalByteCountToDisplay/2))
byteRangeEnd := intmin(byteRangeStart+totalByteCountToDisplay, intmax(len(actual), len(expected)))

linesBuffer := bytes.NewBuffer([]byte{})
tabWriter := tabwriter.NewWriter(linesBuffer, 20, 0, 1, ' ', tabwriter.Debug)

tabWriter.Write([]byte(fmt.Sprintf("Expected (bytes %v-%v), hexadecimal:\t Printable characters:\n", byteRangeStart, byteRangeEnd)))
tabWriter.Write([]byte(fmt.Sprintf("%v\t %v\n", formatBytesAsHex(expected[byteRangeStart:intmin(byteRangeEnd, len(expected))], byteCountToDisplay), formatBytesAsAscii(expected[byteRangeStart:intmin(byteRangeEnd, len(expected))], byteCountToDisplay))))
tabWriter.Write([]byte(fmt.Sprintf("Expected (bytes %v-%v), hexadecimal: \t Printable characters:\n", byteRangeStart, byteRangeEnd)))

for i := byteRangeStart; i < intmin(byteRangeEnd, len(expected)); i += byteCountPerLine {
end := intmin(i+byteCountPerLine, len(expected))
tabWriter.Write([]byte(fmt.Sprintf("%v\t %v\n", formatBytesAsHex(expected[i:end]), formatBytesAsAscii(expected[i:end]))))
}

tabWriter.Write([]byte("\n"))
tabWriter.Write([]byte(fmt.Sprintf("Actual (bytes %v-%v), hexadecimal:\t Printable characters:\n", byteRangeStart, byteRangeEnd)))
tabWriter.Write([]byte(fmt.Sprintf("%v\t %v\n", formatBytesAsHex(actual[byteRangeStart:intmin(byteRangeEnd, len(actual))], byteCountToDisplay), formatBytesAsAscii(actual[byteRangeStart:intmin(byteRangeEnd, len(actual))], byteCountToDisplay))))
tabWriter.Write([]byte(fmt.Sprintf("Actual (bytes %v-%v), hexadecimal: \t Printable characters:\n", byteRangeStart, byteRangeEnd)))

for i := byteRangeStart; i < intmin(byteRangeEnd, len(actual)); i += byteCountPerLine {
end := intmin(i+byteCountPerLine, len(actual))
tabWriter.Write([]byte(fmt.Sprintf("%v\t %v\n", formatBytesAsHex(actual[i:end]), formatBytesAsAscii(actual[i:end]))))
}

tabWriter.Flush()

return strings.Split(string(linesBuffer.Bytes()), "\n")
}

func formatBytesAsHexAndAscii(value []byte, expectedCount int) string {
return fmt.Sprintf("%v | %v", formatBytesAsHex(value, expectedCount), formatBytesAsAscii(value, expectedCount))
}

func formatBytesAsAscii(value []byte, expectedCount int) string {
func formatBytesAsAscii(value []byte) string {
var asciiRepresentations []string

for i := 0; i < expectedCount; i++ {
if i >= len(value) {
// Pad with spaces if we're out of bytes
asciiRepresentations = append(asciiRepresentations, " ")
} else if value[i] < 32 || value[i] > 126 {
for _, b := range value {
if b < 32 || b > 126 {
// If the byte is not printable, replace it with a dot
asciiRepresentations = append(asciiRepresentations, ".")
} else {
asciiRepresentations = append(asciiRepresentations, string(value[i]))
asciiRepresentations = append(asciiRepresentations, string(b))
}
}

return strings.Join(asciiRepresentations, "")
}

func formatBytesAsHex(value []byte, expectedCount int) string {
func formatBytesAsHex(value []byte) string {
var hexadecimalRepresentations []string

for i := 0; i < expectedCount; i++ {
if i >= len(value) {
hexadecimalRepresentations = append(hexadecimalRepresentations, " ")
} else {
hexadecimalRepresentations = append(hexadecimalRepresentations, fmt.Sprintf("%02x", value[i]))
}
for _, b := range value {
hexadecimalRepresentations = append(hexadecimalRepresentations, fmt.Sprintf("%02x", b))
}

return strings.Join(hexadecimalRepresentations, " ")
Expand Down
38 changes: 23 additions & 15 deletions bytes_diff_visualizer/bytes_diff_visualizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ func TestVisualizeByteDiffWorksWithStrings(t *testing.T) {
}

expectedLines := []string{
"Expected (bytes 0-20), hexadecimal: | Printable characters:",
"48 65 6c 6c 6f 2c 20 47 6f 21 | Hello, Go! ",
"Expected (bytes 0-13), hexadecimal: | Printable characters:",
"48 65 6c 6c 6f 2c 20 47 6f 21 | Hello, Go!",
"",
"Actual (bytes 0-20), hexadecimal: | Printable characters:",
"48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21 | Hello, World! ",
"Actual (bytes 0-13), hexadecimal: | Printable characters:",
"48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21 | Hello, World!",
}

for i, expectedLine := range expectedLines {
if i >= len(result) {
t.Fatalf("Expected %v lines, but only got %v", len(expectedLines), len(result))
}

assert.Equal(t, expectedLine, result[i])
if !assert.Equal(t, expectedLine, result[i]) {
t.FailNow()
}
}
}

Expand All @@ -44,19 +46,21 @@ func TestVisualizeByteDiffWorksWithNonPrintableCharacters(t *testing.T) {
}

expectedLines := []string{
"Expected (bytes 0-20), hexadecimal: | Printable characters:",
"62 6c 6f 62 00 00 68 65 61 64 65 72 | blob..header ",
"Expected (bytes 0-12), hexadecimal: | Printable characters:",
"62 6c 6f 62 00 00 68 65 61 64 65 72 | blob..header",
"",
"Actual (bytes 0-20), hexadecimal: | Printable characters:",
"62 6c 6f 62 00 68 65 61 64 65 72 | blob.header ",
"Actual (bytes 0-12), hexadecimal: | Printable characters:",
"62 6c 6f 62 00 68 65 61 64 65 72 | blob.header",
}

for i, expectedLine := range expectedLines {
if i >= len(result) {
t.Fatalf("Expected %v lines, but only got %v", len(expectedLines), len(result))
}

assert.Equal(t, expectedLine, result[i])
if !assert.Equal(t, expectedLine, result[i]) {
t.FailNow()
}
}
}

Expand All @@ -71,18 +75,22 @@ func TestVisualizeByteDiffWorksWithLongerSequences(t *testing.T) {
}

expectedLines := []string{
"Expected (bytes 18-38), hexadecimal: | Printable characters:",
"30 31 32 33 34 35 37 38 39 30 61 62 63 64 | 0123457890abcd ",
"Expected (bytes 0-32), hexadecimal: | Printable characters:",
"31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 37 38 39 30 31 | 12345678901234578901",
"32 33 34 35 37 38 39 30 61 62 63 64 | 23457890abcd",
"",
"Actual (bytes 18-38), hexadecimal: | Printable characters:",
"30 31 32 33 34 35 37 38 39 30 65 66 67 68 | 0123457890efgh ",
"Actual (bytes 0-32), hexadecimal: | Printable characters:",
"31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 37 38 39 30 31 | 12345678901234578901",
"32 33 34 35 37 38 39 30 65 66 67 68 | 23457890efgh",
}

for i, expectedLine := range expectedLines {
if i >= len(result) {
t.Fatalf("Expected %v lines, but only got %v", len(expectedLines), len(result))
}

assert.Equal(t, expectedLine, result[i])
if !assert.Equal(t, expectedLine, result[i]) {
t.FailNow()
}
}
}

0 comments on commit 3b891be

Please sign in to comment.