diff --git a/format/format.go b/format/format.go index 91b11ec60..fae25adce 100644 --- a/format/format.go +++ b/format/format.go @@ -36,6 +36,14 @@ var PrintContextObjects = false // TruncatedDiff choose if we should display a truncated pretty diff or not var TruncatedDiff = true +// TruncateThreshold (default 50) specifies the maximum length string to print in string comparison assertion error +// messages. +var TruncateThreshold uint = 50 + +// CharactersAroundMismatchToInclude (default 5) specifies how many contextual characters should be printed before and +// after the first diff location in a truncated string assertion error message. +var CharactersAroundMismatchToInclude uint = 5 + // Ctx interface defined here to keep backwards compatibility with go < 1.7 // It matches the context.Context interface type Ctx interface { @@ -88,7 +96,7 @@ to equal | */ func MessageWithDiff(actual, message, expected string) string { - if TruncatedDiff && len(actual) >= truncateThreshold && len(expected) >= truncateThreshold { + if TruncatedDiff && len(actual) >= int(TruncateThreshold) && len(expected) >= int(TruncateThreshold) { diffPoint := findFirstMismatch(actual, expected) formattedActual := truncateAndFormat(actual, diffPoint) formattedExpected := truncateAndFormat(expected, diffPoint) @@ -116,7 +124,7 @@ func truncateAndFormat(str string, index int) string { leftPadding := `...` rightPadding := `...` - start := index - charactersAroundMismatchToInclude + start := index - int(CharactersAroundMismatchToInclude) if start < 0 { start = 0 leftPadding = "" @@ -124,7 +132,7 @@ func truncateAndFormat(str string, index int) string { // slice index must include the mis-matched character lengthOfMismatchedCharacter := 1 - end := index + charactersAroundMismatchToInclude + lengthOfMismatchedCharacter + end := index + int(CharactersAroundMismatchToInclude) + lengthOfMismatchedCharacter if end > len(str) { end = len(str) rightPadding = "" @@ -153,11 +161,6 @@ func findFirstMismatch(a, b string) int { return 0 } -const ( - truncateThreshold = 50 - charactersAroundMismatchToInclude = 5 -) - /* Pretty prints the passed in object at the passed in indentation level. diff --git a/format/format_test.go b/format/format_test.go index 804ddad89..4fed17652 100644 --- a/format/format_test.go +++ b/format/format_test.go @@ -190,6 +190,99 @@ var _ = Describe("Format", func() { Expect(MessageWithDiff(stringWithB, "to equal", stringWithZ)).Should(Equal(expectedFullFailureDiff)) }) }) + + Context("With alternate diff lengths", func() { + initialValue := TruncateThreshold // 50 by default + BeforeEach(func() { + TruncateThreshold = 10000 + }) + + AfterEach(func() { + TruncateThreshold = initialValue + }) + + It("should show the full diff when truncate threshold is increased beyond length of strings", func() { + stringWithB := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + stringWithZ := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + + Expect(MessageWithDiff(stringWithB, "to equal", stringWithZ)).Should(Equal(expectedFullFailureDiff)) + }) + }) + + Context("with alternative number of characters to include around mismatch", func() { + initialValue := CharactersAroundMismatchToInclude // 5 by default + BeforeEach(func() { + CharactersAroundMismatchToInclude = 10 + }) + + AfterEach(func() { + CharactersAroundMismatchToInclude = initialValue + }) + + It("it shows more characters around a line length mismatch", func() { + smallString := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + largeString := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + + Expect(MessageWithDiff(largeString, "to equal", smallString)).Should(Equal(expectedTruncatedStartSizeFailureMessageExtraDiff)) + Expect(MessageWithDiff(smallString, "to equal", largeString)).Should(Equal(expectedTruncatedStartSizeSwappedFailureMessageExtraDiff)) + }) + }) + + Describe("At extremes of configurable values", func() { + Context("with zero-length threshold", func() { + initialValue := TruncateThreshold // 50 by default + BeforeEach(func() { + TruncateThreshold = 0 + }) + + AfterEach(func() { + TruncateThreshold = initialValue + }) + + It("should show the full diff when truncate threshold is increased beyond length of strings", func() { + stringWithB := "aba" + stringWithZ := "aza" + Expect(MessageWithDiff(stringWithB, "to equal", stringWithZ)).Should(Equal(expectedDiffSmallThreshold)) + }) + }) + + Context("with zero characters around mismatch", func() { + initialValue := CharactersAroundMismatchToInclude // 5 by default + BeforeEach(func() { + CharactersAroundMismatchToInclude = 0 + }) + + AfterEach(func() { + CharactersAroundMismatchToInclude = initialValue + }) + + It("", func() { + stringWithB := "aba" + stringWithZ := "aza" + Expect(MessageWithDiff(stringWithB, "to equal", stringWithZ)).Should(Equal(expectedDiffZeroMismatch)) + }) + }) + + Context("with zero-length threshold and zero characters around mismatch", func() { + initialCharactersAroundMismatch := CharactersAroundMismatchToInclude + initialTruncateThreshold := TruncateThreshold + BeforeEach(func() { + CharactersAroundMismatchToInclude = 0 + TruncateThreshold = 0 + }) + + AfterEach(func() { + CharactersAroundMismatchToInclude = initialCharactersAroundMismatch + TruncateThreshold = initialTruncateThreshold + }) + + It("", func() { + stringWithB := "aba" + stringWithZ := "aza" + Expect(MessageWithDiff(stringWithB, "to equal", stringWithZ)).Should(Equal(expectedDiffSmallThresholdZeroMismatch)) + }) + }) + }) }) Describe("IndentString", func() { @@ -613,26 +706,36 @@ Expected to equal | : "...aaaaa" `) +var expectedTruncatedStartSizeFailureMessageExtraDiff = strings.TrimSpace(` +Expected + : "...aaaaaaaaaaa" +to equal | + : "...aaaaaaaaaa" +`) var expectedTruncatedStartSizeSwappedFailureMessage = strings.TrimSpace(` Expected : "...aaaa" to equal | : "...aaaaa" `) +var expectedTruncatedStartSizeSwappedFailureMessageExtraDiff = strings.TrimSpace(` +Expected + : "...aaaaaaaaa" +to equal | + : "...aaaaaaaaaa" +`) var expectedTruncatedMultiByteFailureMessage = strings.TrimSpace(` Expected : "...tuvwxyz1" to equal | : "...tuvwxyz" `) - var expectedFullFailureDiff = strings.TrimSpace(` Expected : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa to equal : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa `) - var expectedSpecialCharacterFailureMessage = strings.TrimSpace(` Expected : \n @@ -640,3 +743,21 @@ to equal : something_else `) +var expectedDiffSmallThreshold = strings.TrimSpace(` +Expected + : "aba" +to equal | + : "aza" +`) +var expectedDiffZeroMismatch = strings.TrimSpace(` +Expected + : aba +to equal + : aza +`) +var expectedDiffSmallThresholdZeroMismatch = strings.TrimSpace(` +Expected + : "...b..." +to equal | + : "...z..." +`)