-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat(generator): add Expectation structs and functions #3
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: gitworkflows <[email protected]>
🚨 Warning: Approaching Monthly Automation Limit Monthly PRs automated: 247/250 Your organization has used over 90% of its monthly quota for gitStream automations. Once the quota is reached, new pull requests for this month will not trigger gitStream automations and will be marked as “Skipped”. To avoid interruptions, consider optimizing your automation usage or upgrading your plan by contacting LinearB. |
Reviewer's Guide by SourceryThis pull request introduces a new Sequence diagram for expectation applicationsequenceDiagram
participant User
participant MockObject
participant gomock
User->>MockObject: ApplyMethodExpectation(expectation)
activate MockObject
MockObject->>gomock: On("MethodName", args...)
activate gomock
alt Returns are defined
gomock->>gomock: Return(returnValues)
end
deactivate gomock
deactivate MockObject
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
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.
Hey @gitworkflows - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider adding a comment to explain the purpose of the
generateExpectation
function. - The code duplication in cloning
params
andreturns
could be reduced by creating a helper function.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
@@ -903,6 +906,85 @@ func (_m *{{.MockName}}{{.InstantiatedTypeString}}) {{.FunctionName}}({{join .Pa | |||
} | |||
} | |||
|
|||
func (g *Generator) generateExpectation(fname string, params, returns *paramList) { |
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.
issue (complexity): Consider refactoring repetitive logic in generateExpectation
into helper functions to improve readability and testability.
The new generateExpectation
function does add significant nested logic which may hurt maintainability. Consider refactoring repetitive tasks (such as cloning lists and capitalizing names) into dedicated helper functions. This reduces the function’s nesting and improves readability and testability.
For example, extract a helper for capitalization:
func capitalize(name string) string {
if name == "" {
return name
}
return strings.ToUpper(name[:1]) + name[1:]
}
Then update the loops:
for i, name := range params.Names {
params.Names[i] = capitalize(name)
}
for i, name := range returns.Names {
returns.Names[i] = capitalize(name)
}
Additionally, consider a helper to clone and adjust a parameter list if the same logic applies in multiple places:
func cloneAndCapitalize(pl *paramList) *paramList {
return ¶mList{
Names: slices.Map(slices.Clone(pl.Names), capitalize),
Types: slices.Clone(pl.Types),
}
}
Now your function can be refactored to:
func (g *Generator) generateExpectation(fname string, params, returns *paramList) {
params = cloneAndCapitalize(params)
returns = cloneAndCapitalize(returns)
data := struct {
FunctionName string
InterfaceName string
MockName string
Params *paramList
Returns *paramList
}{
FunctionName: fname,
InterfaceName: g.iface.Name,
MockName: g.mockName(),
Params: params,
Returns: returns,
}
g.printTemplate(data, `
...large multiline template...
`)
}
These changes reduce the complexity without altering functionality.
Signed-off-by: gitworkflows <[email protected]>
Signed-off-by: gitworkflows <[email protected]>
Signed-off-by: gitworkflows <[email protected]>
Co-authored-by: qodo-merge-pro-for-open-source[bot] <189517486+qodo-merge-pro-for-open-source[bot]@users.noreply.github.com> Signed-off-by: gitworkflows <[email protected]>
Signed-off-by: gitworkflows <[email protected]>
Signed-off-by: gitworkflows <[email protected]>
Signed-off-by: gitworkflows <[email protected]>
User description
Description
Please include a summary of the changes and the related issue. Please also include relevant motivation and context.
Type of change
Version of Go used when building/testing:
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Checklist
PR Type
Enhancement
Description
Added
generateExpectation
method for mock generation enhancements.Introduced new struct types for function arguments and returns.
Enhanced mock functionality with
ApplyExpectation
method.Imported
slices
package for slice cloning operations.Changes walkthrough 📝
generator.go
Enhance mock generation with new methods and structs
pkg/generator.go
generateExpectation
method for generating mock expectations.ApplyExpectation
method to apply mock expectations.slices
package for improved slice handling.