-
Notifications
You must be signed in to change notification settings - Fork 1.7k
require: fix invalid examples in doc comments (codegen) #1780
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
c282e21
6bafd48
c68011d
edbdcdb
9986d94
e5983a9
a70ca83
82ff5b1
e46c535
90b6845
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,7 +107,8 @@ func parseTemplates() (*template.Template, *template.Template, error) { | |
| funcTemplate = string(f) | ||
| } | ||
| tmpl, err := template.New("function").Funcs(template.FuncMap{ | ||
| "replace": strings.ReplaceAll, | ||
| "replace": strings.ReplaceAll, | ||
| "requireCommentParseIf": requireCommentParseIf, | ||
| }).Parse(funcTemplate) | ||
| if err != nil { | ||
| return nil, nil, err | ||
|
|
@@ -298,6 +299,50 @@ func (f *testFunc) CommentWithoutT(receiver string) string { | |
| return strings.Replace(f.Comment(), search, replace, -1) | ||
| } | ||
|
|
||
| func requireCommentParseIf(s string) string { | ||
| lines := strings.Split(s, "\n") | ||
| out := make([]string, 0, len(lines)) | ||
| rePrefix := regexp.MustCompile(`//[ \t]*`) | ||
| ifDepth := 0 | ||
| existsIfRequire := false | ||
| SomeFunctionLineIdx := 0 | ||
|
|
||
| for i, line := range lines { | ||
| commentPrefix := rePrefix.FindString(line) | ||
| comment := strings.TrimSpace(line[2:]) | ||
|
|
||
| if strings.HasSuffix(comment, "SomeFunction()") { | ||
|
||
| SomeFunctionLineIdx = i | ||
| } | ||
|
|
||
| if ifDepth > 0 && strings.HasPrefix(comment, "}") { | ||
| ifDepth = ifDepth - 1 | ||
| continue | ||
| } | ||
|
|
||
| if strings.HasPrefix(comment, "if require.") && strings.HasSuffix(comment, "{") { | ||
| ifDepth = ifDepth + 1 | ||
| existsIfRequire = true | ||
| comment = strings.TrimPrefix(comment, "if ") | ||
| comment = strings.TrimSpace(comment) | ||
| comment = strings.TrimSuffix(comment, "{") | ||
| } | ||
|
|
||
| if ifDepth > 0 { | ||
| commentPrefix = "//\t" | ||
| } | ||
|
|
||
| out = append(out, commentPrefix+comment) | ||
| } | ||
|
|
||
| if existsIfRequire && SomeFunctionLineIdx != 0 { | ||
| comment := out[SomeFunctionLineIdx][2:] | ||
| out[SomeFunctionLineIdx] = "//\t" + strings.TrimSpace(comment) | ||
| } | ||
|
|
||
| return strings.Join(out, "\n") | ||
| } | ||
|
|
||
| // Standard header https://go.dev/s/generatedcode. | ||
| var headerTemplate = `// Code generated with github.com/stretchr/testify/_codegen; DO NOT EDIT. | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
This method should be tested
Uh oh!
There was an error while loading. Please reload this page.
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.
This is codegen. Tests are not strictly necessary: checking the generated output is enough.
However, tests would still be helpful to document what the function expects and does. Tests will also be useful for future refactorings.
Even more helpful would be some godoc for the function because the
requireprefix is misleading: on a first read I got it as verb while it is about therequirepackage.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.
An issue is that it's unclear from the function name or the code; what is this function is supposed to do. A test or a good comment before the function would help.
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.
That was my point, indeed
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.
#1780 (comment)
I wasn’t familiar with the reply feature, so I didn’t realize multiple conversations were happening. 😅
Considering tests, would it be better to separate this method and handle it individually?
Uh oh!
There was an error while loading. Please reload this page.
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.
@PCloud63514 Please start by adding a comment block to document the function.
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.
Added a doc comment for
requireCommentParseIfto clarify its purpose.requireCommentParseIf