Skip to content

Commit c52016b

Browse files
committed
fix(codegen): preserve *assert.CollectT in RequireComment
Update RequireComment to replace "assert." with "require." while preserving "*assert.CollectT" references. This prevents unintended replacement of pointer references to CollectT, ensuring correct code generation. Signed-off-by: a2not <[email protected]>
1 parent 2f6340f commit c52016b

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

_codegen/main.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,15 @@ func (f *testFunc) CommentWithoutT(receiver string) string {
297297
}
298298

299299
func (f *testFunc) RequireComment(comment string) string {
300-
// First, protect assert.CollectT by temporarily replacing it
301-
protected := strings.ReplaceAll(comment, "assert.CollectT", "__COLLECT_T_PLACEHOLDER__")
302-
// Then do the normal replacement
300+
// replace "assert." with "require.", but preserve "*assert.CollectT"
301+
const (
302+
assertCollectT = "*assert.CollectT"
303+
assertCollectTPlaceholder = "__COLLECT_T_PLACEHOLDER__"
304+
)
305+
306+
protected := strings.ReplaceAll(comment, assertCollectT, assertCollectTPlaceholder)
303307
result := strings.ReplaceAll(protected, "assert.", "require.")
304-
// Restore assert.CollectT
305-
return strings.ReplaceAll(result, "__COLLECT_T_PLACEHOLDER__", "assert.CollectT")
308+
return strings.ReplaceAll(result, assertCollectTPlaceholder, assertCollectT)
306309
}
307310

308311
// Standard header https://go.dev/s/generatedcode.

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+
{{ .RequireComment .Comment }}
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)