Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export format property variables #347

Merged
merged 7 commits into from Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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..."
`)