-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Lazily render mock diff output on successful match #1615
Open
mikeauclair
wants to merge
5
commits into
stretchr:master
Choose a base branch
from
DevotedHealth:mauclair-mock-match-sprintf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dd72533
wip
mikeauclair 7ecde95
Stop querying for stack frames multiple times on CallerInfo() (#7)
mikeauclair 2b53603
wip
mikeauclair 6147cf0
Merge branch 'master' into mauclair-mock-match-sprintf
mikeauclair d45244f
cleanup
mikeauclair File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -923,56 +923,73 @@ func (args Arguments) Is(objects ...interface{}) bool { | |||||
return true | ||||||
} | ||||||
|
||||||
type outputRenderer interface{} | ||||||
|
||||||
// Diff gets a string describing the differences between the arguments | ||||||
// and the specified objects. | ||||||
// | ||||||
// Returns the diff string and number of differences found. | ||||||
func (args Arguments) Diff(objects []interface{}) (string, int) { | ||||||
// TODO: could return string as error and nil for No difference | ||||||
|
||||||
output := "\n" | ||||||
var outputBuilder strings.Builder | ||||||
var differences int | ||||||
|
||||||
maxArgCount := len(args) | ||||||
if len(objects) > maxArgCount { | ||||||
maxArgCount = len(objects) | ||||||
} | ||||||
|
||||||
for i := 0; i < maxArgCount; i++ { | ||||||
outputRenderers := []outputRenderer{} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interface{} says nothing, the case for this being type
Suggested change
|
||||||
|
||||||
for j := 0; j < maxArgCount; j++ { | ||||||
i := j | ||||||
var actual, expected interface{} | ||||||
var actualFmt, expectedFmt string | ||||||
var actualFmt, expectedFmt func() string | ||||||
|
||||||
if len(objects) <= i { | ||||||
actual = "(Missing)" | ||||||
actualFmt = "(Missing)" | ||||||
actualFmt = func() string { | ||||||
return "(Missing)" | ||||||
} | ||||||
} else { | ||||||
actual = objects[i] | ||||||
actualFmt = fmt.Sprintf("(%[1]T=%[1]v)", actual) | ||||||
actualFmt = func() string { | ||||||
return fmt.Sprintf("(%[1]T=%[1]v)", actual) | ||||||
} | ||||||
} | ||||||
|
||||||
if len(args) <= i { | ||||||
expected = "(Missing)" | ||||||
expectedFmt = "(Missing)" | ||||||
expectedFmt = func() string { | ||||||
return "(Missing)" | ||||||
} | ||||||
} else { | ||||||
expected = args[i] | ||||||
expectedFmt = fmt.Sprintf("(%[1]T=%[1]v)", expected) | ||||||
expectedFmt = func() string { | ||||||
return fmt.Sprintf("(%[1]T=%[1]v)", expected) | ||||||
} | ||||||
} | ||||||
|
||||||
if matcher, ok := expected.(argumentMatcher); ok { | ||||||
var matches bool | ||||||
func() { | ||||||
defer func() { | ||||||
if r := recover(); r != nil { | ||||||
actualFmt = fmt.Sprintf("panic in argument matcher: %v", r) | ||||||
actualFmt = func() string { | ||||||
return fmt.Sprintf("panic in argument matcher: %v", r) | ||||||
} | ||||||
} | ||||||
}() | ||||||
matches = matcher.Matches(actual) | ||||||
}() | ||||||
if matches { | ||||||
output = fmt.Sprintf("%s\t%d: PASS: %s matched by %s\n", output, i, actualFmt, matcher) | ||||||
outputRenderers = append(outputRenderers, func() string { | ||||||
return fmt.Sprintf("\t%d: PASS: %s matched by %s\n", i, actualFmt(), matcher) | ||||||
}) | ||||||
} else { | ||||||
differences++ | ||||||
output = fmt.Sprintf("%s\t%d: FAIL: %s not matched by %s\n", output, i, actualFmt, matcher) | ||||||
outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: %s not matched by %s\n", i, actualFmt(), matcher)) | ||||||
} | ||||||
} else { | ||||||
switch expected := expected.(type) { | ||||||
|
@@ -981,13 +998,13 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { | |||||
if reflect.TypeOf(actual).Name() != string(expected) && reflect.TypeOf(actual).String() != string(expected) { | ||||||
// not match | ||||||
differences++ | ||||||
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt) | ||||||
outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, expected, reflect.TypeOf(actual).Name(), actualFmt())) | ||||||
} | ||||||
case *IsTypeArgument: | ||||||
actualT := reflect.TypeOf(actual) | ||||||
if actualT != expected.t { | ||||||
differences++ | ||||||
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected.t.Name(), actualT.Name(), actualFmt) | ||||||
outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, expected.t.Name(), actualT.Name(), actualFmt())) | ||||||
} | ||||||
case *FunctionalOptionsArgument: | ||||||
t := expected.value | ||||||
|
@@ -1001,26 +1018,30 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { | |||||
tName := reflect.TypeOf(t).Name() | ||||||
if name != reflect.TypeOf(actual).String() && tValue.Len() != 0 { | ||||||
differences++ | ||||||
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, tName, reflect.TypeOf(actual).Name(), actualFmt) | ||||||
outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, tName, reflect.TypeOf(actual).Name(), actualFmt())) | ||||||
} else { | ||||||
if ef, af := assertOpts(t, actual); ef == "" && af == "" { | ||||||
// match | ||||||
output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, tName, tName) | ||||||
outputRenderers = append(outputRenderers, func() string { | ||||||
return fmt.Sprintf("\t%d: PASS: %s == %s\n", i, tName, tName) | ||||||
}) | ||||||
} else { | ||||||
// not match | ||||||
differences++ | ||||||
output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, af, ef) | ||||||
outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: %s != %s\n", i, af, ef)) | ||||||
} | ||||||
} | ||||||
|
||||||
default: | ||||||
if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) { | ||||||
// match | ||||||
output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, actualFmt, expectedFmt) | ||||||
outputRenderers = append(outputRenderers, func() string { | ||||||
return fmt.Sprintf("\t%d: PASS: %s == %s\n", i, actualFmt(), expectedFmt()) | ||||||
}) | ||||||
} else { | ||||||
// not match | ||||||
differences++ | ||||||
output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, actualFmt, expectedFmt) | ||||||
outputRenderers = append(outputRenderers, fmt.Sprintf("\t%d: FAIL: %s != %s\n", i, actualFmt(), expectedFmt())) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
@@ -1031,7 +1052,19 @@ func (args Arguments) Diff(objects []interface{}) (string, int) { | |||||
return "No differences.", differences | ||||||
} | ||||||
|
||||||
return output, differences | ||||||
outputBuilder.WriteString("\n") | ||||||
for _, renderer := range outputRenderers { | ||||||
switch r := renderer.(type) { | ||||||
case string: | ||||||
outputBuilder.WriteString(r) | ||||||
case func() string: | ||||||
outputBuilder.WriteString(r()) | ||||||
default: | ||||||
panic("Invalid Output Renderer") | ||||||
} | ||||||
} | ||||||
|
||||||
return outputBuilder.String(), differences | ||||||
} | ||||||
|
||||||
// Assert compares the arguments with the specified objects and fails if | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of
more
andmaybeMore
, this code is actually two nested loops and I think it would be more readable if you coded it that way. I think that pure behaviour is usually better than data (varibles) used only to alter behaviour.This code is also from #1614, it might be quicker to review this change if you keep the two PRs separate.