From e93e8b8a6b60f068f6f6ccb0354bceac819a8bee Mon Sep 17 00:00:00 2001 From: Mitch Kelley Date: Mon, 29 Apr 2019 11:58:21 -0400 Subject: [PATCH 1/6] export format property variables --- format/format.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/format/format.go b/format/format.go index 6559525f1..b41a6ec1f 100644 --- a/format/format.go +++ b/format/format.go @@ -33,6 +33,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 = 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 = 5 + // Ctx interface defined here to keep backwards compatability with go < 1.7 // It matches the context.Context interface type Ctx interface { @@ -85,7 +93,7 @@ to equal | */ func MessageWithDiff(actual, message, expected string) string { - if TruncatedDiff && len(actual) >= truncateThreshold && len(expected) >= truncateThreshold { + if TruncatedDiff && len(actual) >= TruncateThreshold && len(expected) >= TruncateThreshold { diffPoint := findFirstMismatch(actual, expected) formattedActual := truncateAndFormat(actual, diffPoint) formattedExpected := truncateAndFormat(expected, diffPoint) @@ -104,7 +112,7 @@ func truncateAndFormat(str string, index int) string { leftPadding := `...` rightPadding := `...` - start := index - charactersAroundMismatchToInclude + start := index - CharactersAroundMismatchToInclude if start < 0 { start = 0 leftPadding = "" @@ -112,7 +120,7 @@ func truncateAndFormat(str string, index int) string { // slice index must include the mis-matched character lengthOfMismatchedCharacter := 1 - end := index + charactersAroundMismatchToInclude + lengthOfMismatchedCharacter + end := index + CharactersAroundMismatchToInclude + lengthOfMismatchedCharacter if end > len(str) { end = len(str) rightPadding = "" @@ -141,11 +149,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. From 8e048e2427425add570e0997ad27884b79ab2642 Mon Sep 17 00:00:00 2001 From: Mitch Kelley Date: Mon, 26 Aug 2019 15:47:03 -0400 Subject: [PATCH 2/6] add test with different length diffs --- format/format_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/format/format_test.go b/format/format_test.go index 804ddad89..39b1047b6 100644 --- a/format/format_test.go +++ b/format/format_test.go @@ -190,6 +190,22 @@ var _ = Describe("Format", func() { Expect(MessageWithDiff(stringWithB, "to equal", stringWithZ)).Should(Equal(expectedFullFailureDiff)) }) }) + + Context("With alternate diff lengths", func() { + It("long strings that differ only in length", func() { + smallString := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + largeString := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + + Expect(MessageWithDiff(largeString, "to equal", smallString)).Should(Equal(expectedTruncatedStartSizeFailureMessage)) + Expect(MessageWithDiff(smallString, "to equal", largeString)).Should(Equal(expectedTruncatedStartSizeSwappedFailureMessage)) + initialValue := CharactersAroundMismatchToInclude // 5 by default + CharactersAroundMismatchToInclude = 10 + Expect(MessageWithDiff(largeString, "to equal", smallString)).Should(Equal(expectedTruncatedStartSizeFailureMessageExtraDiff)) + Expect(MessageWithDiff(smallString, "to equal", largeString)).Should(Equal(expectedTruncatedStartSizeSwappedFailureMessageExtraDiff)) + CharactersAroundMismatchToInclude = initialValue + }) + + }) }) Describe("IndentString", func() { @@ -613,12 +629,24 @@ 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" From 80eff8be23dbff42108706746066980c25f10bbf Mon Sep 17 00:00:00 2001 From: Mitch Kelley Date: Mon, 26 Aug 2019 15:51:29 -0400 Subject: [PATCH 3/6] add test with different diff threshold --- format/format_test.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/format/format_test.go b/format/format_test.go index 39b1047b6..7f22cb6f4 100644 --- a/format/format_test.go +++ b/format/format_test.go @@ -191,6 +191,23 @@ var _ = Describe("Format", func() { }) }) + Context("With alternate diff lengths", func() { + initialValue := TruncateThreshold // 5 by default + BeforeEach(func() { + TruncateThreshold = 10000 + }) + + AfterEach(func() { + TruncateThreshold = initialValue + }) + It("should show the full diff", func() { + stringWithB := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + stringWithZ := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + + Expect(MessageWithDiff(stringWithB, "to equal", stringWithZ)).Should(Equal(expectedFullFailureDiff)) + }) + }) + Context("With alternate diff lengths", func() { It("long strings that differ only in length", func() { smallString := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" @@ -204,7 +221,6 @@ var _ = Describe("Format", func() { Expect(MessageWithDiff(smallString, "to equal", largeString)).Should(Equal(expectedTruncatedStartSizeSwappedFailureMessageExtraDiff)) CharactersAroundMismatchToInclude = initialValue }) - }) }) From 30e1d5c95cf7631281e4f2d0ef5feac94c989c7b Mon Sep 17 00:00:00 2001 From: Mitch Kelley Date: Tue, 27 Aug 2019 20:14:41 -0400 Subject: [PATCH 4/6] use uint for exported parameters --- format/format.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/format/format.go b/format/format.go index e00c1581d..fae25adce 100644 --- a/format/format.go +++ b/format/format.go @@ -38,11 +38,11 @@ var TruncatedDiff = true // TruncateThreshold (default 50) specifies the maximum length string to print in string comparison assertion error // messages. -var TruncateThreshold = 50 +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 = 5 +var CharactersAroundMismatchToInclude uint = 5 // Ctx interface defined here to keep backwards compatibility with go < 1.7 // It matches the context.Context interface @@ -96,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) @@ -124,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 = "" @@ -132,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 = "" From 28d54b5e4a294dd8a21a60970727698f09f5e3b3 Mon Sep 17 00:00:00 2001 From: Mitch Kelley Date: Tue, 27 Aug 2019 20:15:01 -0400 Subject: [PATCH 5/6] use BDD style descriptions --- format/format_test.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/format/format_test.go b/format/format_test.go index 7f22cb6f4..cc71ab2a3 100644 --- a/format/format_test.go +++ b/format/format_test.go @@ -200,7 +200,7 @@ var _ = Describe("Format", func() { AfterEach(func() { TruncateThreshold = initialValue }) - It("should show the full diff", func() { + It("should show the full diff when truncate threshold is increased beyond length of strings", func() { stringWithB := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" stringWithZ := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" @@ -208,18 +208,21 @@ var _ = Describe("Format", func() { }) }) - Context("With alternate diff lengths", func() { - It("long strings that differ only in length", func() { + 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(expectedTruncatedStartSizeFailureMessage)) - Expect(MessageWithDiff(smallString, "to equal", largeString)).Should(Equal(expectedTruncatedStartSizeSwappedFailureMessage)) - initialValue := CharactersAroundMismatchToInclude // 5 by default - CharactersAroundMismatchToInclude = 10 Expect(MessageWithDiff(largeString, "to equal", smallString)).Should(Equal(expectedTruncatedStartSizeFailureMessageExtraDiff)) Expect(MessageWithDiff(smallString, "to equal", largeString)).Should(Equal(expectedTruncatedStartSizeSwappedFailureMessageExtraDiff)) - CharactersAroundMismatchToInclude = initialValue }) }) }) From 6d225f2425485cee6748dd88e3d39aa4b8786c59 Mon Sep 17 00:00:00 2001 From: Mitch Kelley Date: Wed, 28 Aug 2019 07:33:38 -0400 Subject: [PATCH 6/6] test extremes of config --- format/format_test.go | 80 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/format/format_test.go b/format/format_test.go index cc71ab2a3..4fed17652 100644 --- a/format/format_test.go +++ b/format/format_test.go @@ -192,7 +192,7 @@ var _ = Describe("Format", func() { }) Context("With alternate diff lengths", func() { - initialValue := TruncateThreshold // 5 by default + initialValue := TruncateThreshold // 50 by default BeforeEach(func() { TruncateThreshold = 10000 }) @@ -200,6 +200,7 @@ var _ = Describe("Format", func() { AfterEach(func() { TruncateThreshold = initialValue }) + It("should show the full diff when truncate threshold is increased beyond length of strings", func() { stringWithB := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" stringWithZ := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" @@ -217,6 +218,7 @@ var _ = Describe("Format", func() { AfterEach(func() { CharactersAroundMismatchToInclude = initialValue }) + It("it shows more characters around a line length mismatch", func() { smallString := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" largeString := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" @@ -225,6 +227,62 @@ var _ = Describe("Format", func() { 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() { @@ -672,14 +730,12 @@ Expected to equal | : "...tuvwxyz" `) - var expectedFullFailureDiff = strings.TrimSpace(` Expected : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa to equal : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa `) - var expectedSpecialCharacterFailureMessage = strings.TrimSpace(` Expected : \n @@ -687,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..." +`)