diff --git a/format/format.go b/format/format.go index 6559525f1..b23a6ae0e 100644 --- a/format/format.go +++ b/format/format.go @@ -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 := `...` diff --git a/format/format_test.go b/format/format_test.go index 9ea781379..804ddad89 100644 --- a/format/format_test.go +++ b/format/format_test.go @@ -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 @@ -625,3 +632,11 @@ Expected to equal : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa `) + +var expectedSpecialCharacterFailureMessage = strings.TrimSpace(` +Expected + : \n +to equal + : something_else + +`)