Skip to content

Commit

Permalink
fix a panic in MessageWithDiff with long message (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Vladev committed Nov 27, 2020
1 parent df70182 commit ea06b9b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ func MessageWithDiff(actual, message, expected string) string {

tabLength := 4
spaceFromMessageToActual := tabLength + len("<string>: ") - len(message)
padding := strings.Repeat(" ", spaceFromMessageToActual+spacesBeforeFormattedMismatch) + "|"

paddingCount := spaceFromMessageToActual + spacesBeforeFormattedMismatch
if paddingCount < 0 {
return Message(formattedActual, message, formattedExpected)
}

padding := strings.Repeat(" ", paddingCount) + "|"
return Message(formattedActual, message+padding, formattedExpected)
}

Expand Down
15 changes: 15 additions & 0 deletions format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ var _ = Describe("Format", func() {
Expect(MessageWithDiff(stringA, "to equal", stringB)).Should(Equal(expectedSpecialCharacterFailureMessage))
})

It("handles negative padding length", func() {
stringWithB := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
stringWithZ := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
longMessage := "to equal very long message"

Expect(MessageWithDiff(stringWithB, longMessage, stringWithZ)).Should(Equal(expectedDiffLongMessage))
})

Context("With truncated diff disabled", func() {
BeforeEach(func() {
TruncatedDiff = false
Expand Down Expand Up @@ -761,3 +769,10 @@ Expected
to equal |
<string>: "...z..."
`)

var expectedDiffLongMessage = strings.TrimSpace(`
Expected
<string>: "...aaaaabaaaaa..."
to equal very long message
<string>: "...aaaaazaaaaa..."
`)

0 comments on commit ea06b9b

Please sign in to comment.