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 4 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 = 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 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) >= TruncateThreshold && len(expected) >= 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 - CharactersAroundMismatchToInclude
if start < 0 {
start = 0
leftPadding = ""
}

// 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 = ""
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
44 changes: 44 additions & 0 deletions format/format_test.go
Expand Up @@ -190,6 +190,38 @@ var _ = Describe("Format", func() {
Expect(MessageWithDiff(stringWithB, "to equal", stringWithZ)).Should(Equal(expectedFullFailureDiff))
})
})

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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context name is exactly the same as the previous test. I think it should be different and say something like with alternative number of characters to include around mismatch.

It("long strings that differ only in length", func() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally BDD style tests should read as a sentence. For example the test above reads it should show the full diff, and this one could read something like it shows more characters around a line length mismatch - test names should describe the behaviour being tested rather than the implementation of the test.

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
Copy link
Collaborator

@blgm blgm Aug 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other tests (the two above for example) set/unset things in a Before/AfterEach. It would be nice to match the style of the other tests as a file is easier to read and maintain if it follows a consistent style.

At the moment this test checks both the default and alternative behaviour. Perhaps the default testing should be left to the tests above, and this test should focus only on the alternative behaviour.

})
})
})

Describe("IndentString", func() {
Expand Down Expand Up @@ -613,12 +645,24 @@ 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"
Expand Down