Skip to content

Commit

Permalink
Merge pull request #273 from vayan/master
Browse files Browse the repository at this point in the history
Make string pretty diff user configurable
  • Loading branch information
williammartin committed Mar 21, 2018
2 parents de89e61 + eb112ce commit 649b44d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Set PrintContextObjects = true to enable printing of the context internals.
*/
var PrintContextObjects = false

// TruncatedDiff choose if we should display a truncated pretty diff or not
var TruncatedDiff = true

// Ctx interface defined here to keep backwards compatability with go < 1.7
// It matches the context.Context interface
type Ctx interface {
Expand Down Expand Up @@ -82,7 +85,7 @@ to equal |
*/

func MessageWithDiff(actual, message, expected string) string {
if 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)
Expand Down
24 changes: 24 additions & 0 deletions format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ var _ = Describe("Format", func() {

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

Context("With truncated diff disabled", func() {
BeforeEach(func() {
TruncatedDiff = false
})

AfterEach(func() {
TruncatedDiff = true
})

It("should show the full diff", func() {
stringWithB := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
stringWithZ := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

Expect(MessageWithDiff(stringWithB, "to equal", stringWithZ)).Should(Equal(expectedFullFailureDiff))
})
})
})

Describe("IndentString", func() {
Expand Down Expand Up @@ -601,3 +618,10 @@ Expected
to equal |
<string>: "...tuvwxyz"
`)

var expectedFullFailureDiff = strings.TrimSpace(`
Expected
<string>: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
to equal
<string>: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
`)

0 comments on commit 649b44d

Please sign in to comment.