Skip to content

Commit

Permalink
display special chars on error
Browse files Browse the repository at this point in the history
previously, special chars would be rendered via the terminal
making it difficult to see errors

use %q to safely display special chars on the terminal
this addresses issue #328

Signed-off-by: Paul Nikonowicz <pnikonowicz@pivotal.io>
Signed-off-by: Margo Crawford <margaretmcrawf@gmail.com>
Signed-off-by: Paul Nikonowicz <pnikonowicz@pivotal.io>
  • Loading branch information
margocrawf authored and williammartin committed May 19, 2019
1 parent 5c249dc commit 41e1b26
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,18 @@ func MessageWithDiff(actual, message, expected string) string {
padding := strings.Repeat(" ", spaceFromMessageToActual+spacesBeforeFormattedMismatch) + "|"
return Message(formattedActual, message+padding, formattedExpected)
}

actual = escapedWithGoSyntax(actual)
expected = escapedWithGoSyntax(expected)

return Message(actual, message, expected)
}

func escapedWithGoSyntax(str string) string {
withQuotes := fmt.Sprintf("%q", str)
return withQuotes[1 : len(withQuotes)-1]
}

func truncateAndFormat(str string, index int) string {
leftPadding := `...`
rightPadding := `...`
Expand Down
15 changes: 15 additions & 0 deletions format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ var _ = Describe("Format", func() {
Expect(MessageWithDiff(stringA, "to equal", stringB)).Should(Equal(expectedTruncatedMultiByteFailureMessage))
})

It("prints special characters", func() {
stringA := "\n"
stringB := "something_else"

Expect(MessageWithDiff(stringA, "to equal", stringB)).Should(Equal(expectedSpecialCharacterFailureMessage))
})

Context("With truncated diff disabled", func() {
BeforeEach(func() {
TruncatedDiff = false
Expand Down Expand Up @@ -625,3 +632,11 @@ Expected
to equal
<string>: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
`)

var expectedSpecialCharacterFailureMessage = strings.TrimSpace(`
Expected
<string>: \n
to equal
<string>: something_else
`)

0 comments on commit 41e1b26

Please sign in to comment.