Skip to content

Commit

Permalink
export format property variables (#347)
Browse files Browse the repository at this point in the history
* export format property variables

* add test with different length diffs

* add test with different diff threshold

* use uint for exported parameters

* use BDD style descriptions

* test extremes of config
  • Loading branch information
mitchdraft authored and blgm committed Aug 28, 2019
1 parent beea727 commit 642e5ba
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 10 deletions.
19 changes: 11 additions & 8 deletions format/format.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -116,15 +124,15 @@ func truncateAndFormat(str string, index int) string {
leftPadding := `...`
rightPadding := `...`

start := index - charactersAroundMismatchToInclude
start := index - int(CharactersAroundMismatchToInclude)
if start < 0 {
start = 0
leftPadding = ""
}

// 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 = ""
Expand Down Expand Up @@ -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.
Expand Down
125 changes: 123 additions & 2 deletions format/format_test.go
Expand Up @@ -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() {
Expand Down Expand Up @@ -613,30 +706,58 @@ Expected
to equal |
<string>: "...aaaaa"
`)
var expectedTruncatedStartSizeFailureMessageExtraDiff = strings.TrimSpace(`
Expected
<string>: "...aaaaaaaaaaa"
to equal |
<string>: "...aaaaaaaaaa"
`)
var expectedTruncatedStartSizeSwappedFailureMessage = strings.TrimSpace(`
Expected
<string>: "...aaaa"
to equal |
<string>: "...aaaaa"
`)
var expectedTruncatedStartSizeSwappedFailureMessageExtraDiff = strings.TrimSpace(`
Expected
<string>: "...aaaaaaaaa"
to equal |
<string>: "...aaaaaaaaaa"
`)
var expectedTruncatedMultiByteFailureMessage = strings.TrimSpace(`
Expected
<string>: "...tuvwxyz1"
to equal |
<string>: "...tuvwxyz"
`)

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

var expectedSpecialCharacterFailureMessage = strings.TrimSpace(`
Expected
<string>: \n
to equal
<string>: something_else
`)
var expectedDiffSmallThreshold = strings.TrimSpace(`
Expected
<string>: "aba"
to equal |
<string>: "aza"
`)
var expectedDiffZeroMismatch = strings.TrimSpace(`
Expected
<string>: aba
to equal
<string>: aza
`)
var expectedDiffSmallThresholdZeroMismatch = strings.TrimSpace(`
Expected
<string>: "...b..."
to equal |
<string>: "...z..."
`)

0 comments on commit 642e5ba

Please sign in to comment.