Skip to content

Commit 5053534

Browse files
committed
refactor(codegen): generalize comment replacement logic
Replace the specific RequireComment method with a more flexible Replace method that allows arbitrary string replacements in comments, while preserving "*assert.CollectT". Update template usage to leverage the new Replace method for generating require comments. Signed-off-by: a2not <[email protected]>
1 parent c52016b commit 5053534

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

_codegen/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,15 @@ func (f *testFunc) CommentWithoutT(receiver string) string {
296296
return strings.Replace(f.Comment(), search, replace, -1)
297297
}
298298

299-
func (f *testFunc) RequireComment(comment string) string {
300-
// replace "assert." with "require.", but preserve "*assert.CollectT"
299+
func (f *testFunc) Replace(comment, search, replace string) string {
300+
// replace strings, but preserve "*assert.CollectT"
301301
const (
302302
assertCollectT = "*assert.CollectT"
303303
assertCollectTPlaceholder = "__COLLECT_T_PLACEHOLDER__"
304304
)
305305

306306
protected := strings.ReplaceAll(comment, assertCollectT, assertCollectTPlaceholder)
307-
result := strings.ReplaceAll(protected, "assert.", "require.")
307+
result := strings.ReplaceAll(protected, search, replace)
308308
return strings.ReplaceAll(result, assertCollectTPlaceholder, assertCollectT)
309309
}
310310

require/require.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{ .RequireComment .Comment }}
1+
{{ .Replace .Comment "assert." "require." }}
22
func {{.DocInfo.Name}}(t TestingT, {{.Params}}) {
33
if h, ok := t.(tHelper); ok { h.Helper() }
44
if assert.{{.DocInfo.Name}}(t, {{.ForwardedParams}}) { return }

0 commit comments

Comments
 (0)