-
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
Open
PCloud63514
wants to merge
10
commits into
stretchr:master
Choose a base branch
from
PCloud63514:issue-1776
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.
+105
−16
Open
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c282e21
fix(require): correct invalid examples in doc comments #issue-1776
PCloud63514 6bafd48
fix: rollback
PCloud63514 c68011d
fix: run go generate
PCloud63514 edbdcdb
fix: run go generate
PCloud63514 9986d94
fix(require): correct invalid examples in doc comments #issue-1776
PCloud63514 e5983a9
fix: simplify and improve requireCommentParseIf handling of require-i…
PCloud63514 a70ca83
fix: remove need for explicit SomeFunction() reference in example par…
PCloud63514 82ff5b1
fix: add comment block 'requireCommentParseIf'
PCloud63514 e46c535
test: add unit tests for requireCommentParseIf
PCloud63514 90b6845
fix: adjust comment formatting in requireCommentParseIf
PCloud63514 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 hidden or 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 |
|---|---|---|
|
|
@@ -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,61 @@ 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)) | ||
| inIf := false | ||
| var commentPrefix string | ||
|
|
||
| for _, line := range lines { | ||
| trim := strings.TrimSpace(line) | ||
|
|
||
| if !inIf { | ||
| slash := strings.Index(line, "//") | ||
| if slash < 0 { | ||
| out = append(out, line) | ||
| continue | ||
| } | ||
|
|
||
| trimmed := strings.TrimSpace(line[slash+2:]) | ||
|
|
||
| if strings.HasPrefix(trimmed, "if require.") && strings.HasSuffix(trimmed, "{") { | ||
| commentPrefix = line[:slash] + "//\t " | ||
|
|
||
| h := strings.TrimPrefix(trimmed, "if ") | ||
| h = strings.TrimSpace(h) | ||
| if strings.HasSuffix(h, "{") { | ||
| h = strings.TrimSuffix(h, "{") | ||
| } | ||
| h = strings.TrimSpace(h) | ||
|
|
||
| out = append(out, commentPrefix+strings.TrimLeft(h, "\t")) | ||
| inIf = true | ||
| continue | ||
| } | ||
| out = append(out, line) | ||
| } else { | ||
| if strings.HasPrefix(trim, "//") { | ||
| body := strings.TrimSpace(strings.TrimPrefix(trim, "//")) | ||
| if body == "}" { | ||
| inIf = false | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| slash := strings.Index(line, "//") | ||
| if slash >= 0 { | ||
| body := strings.TrimLeft(line[slash+2:], " \t") | ||
| out = append(out, commentPrefix+body) | ||
| } else { | ||
| out = append(out, line) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return strings.Join(out, "\n") | ||
| } | ||
|
Collaborator
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. I'm worried about a badly indented comment. I'm unsure how to handle this correctly. |
||
|
|
||
| // 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.
Oops, something went wrong.
This file contains hidden or 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
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.
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