Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions _codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ func parseTemplates() (*template.Template, *template.Template, error) {
}
funcTemplate = string(f)
}
tmpl, err := template.New("function").Funcs(template.FuncMap{
"replace": strings.ReplaceAll,
}).Parse(funcTemplate)
tmpl, err := template.New("function").Parse(funcTemplate)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -298,6 +296,31 @@ func (f *testFunc) CommentWithoutT(receiver string) string {
return strings.Replace(f.Comment(), search, replace, -1)
}

func (f *testFunc) Replace(comment, search, replace string) string {
// replace strings, while preserving some identifiers
identifiersToBePreserved := []string{
"*assert.CollectT",
}
Comment on lines 301 to 303
Copy link
Author

@a2not a2not Nov 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be over-generalizing things, since so far we only want to preserve one keyword.
Let me know if I need to simplify this so that it only cares about assert.CollectT like a39aa2d#diff-4b4cb0af6b5935c8adcb9f00b426367349e989296edf13eee95831c4bb93c831R299-R310.

placeholderFromIdentifier := func(ident string) string {
// assuming none of the identifiers to be replaced is ALL CAPS
return fmt.Sprintf("__%s_PLACEHOLDER___", strings.ToUpper(strings.ReplaceAll(ident, ".", "_")))
}

for _, ident := range identifiersToBePreserved {
placeholder := placeholderFromIdentifier(ident)
comment = strings.ReplaceAll(comment, ident, placeholder)
}

comment = strings.ReplaceAll(comment, search, replace)

for _, ident := range identifiersToBePreserved {
placeholder := placeholderFromIdentifier(ident)
comment = strings.ReplaceAll(comment, placeholder, ident)
}

return comment
}

// Standard header https://go.dev/s/generatedcode.
var headerTemplate = `// Code generated with github.com/stretchr/testify/_codegen; DO NOT EDIT.

Expand Down
4 changes: 2 additions & 2 deletions require/require.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick t
// time.Sleep(8*time.Second)
// externalValue = true
// }()
// require.EventuallyWithT(t, func(c *require.CollectT) {
// require.EventuallyWithT(t, func(c *assert.CollectT) {
// // add assertions as needed; any assertion failure will fail the current tick
// require.True(c, externalValue, "expected 'externalValue' to be true")
// }, 10*time.Second, 1*time.Second, "external state has not changed to 'true'; still false")
Expand Down Expand Up @@ -457,7 +457,7 @@ func EventuallyWithT(t TestingT, condition func(collect *assert.CollectT), waitF
// time.Sleep(8*time.Second)
// externalValue = true
// }()
// require.EventuallyWithTf(t, func(c *require.CollectT, "error message %s", "formatted") {
// require.EventuallyWithTf(t, func(c *assert.CollectT, "error message %s", "formatted") {
// // add assertions as needed; any assertion failure will fail the current tick
// require.True(c, externalValue, "expected 'externalValue' to be true")
// }, 10*time.Second, 1*time.Second, "external state has not changed to 'true'; still false")
Expand Down
2 changes: 1 addition & 1 deletion require/require.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ replace .Comment "assert." "require."}}
{{ .Replace .Comment "assert." "require."}}
func {{.DocInfo.Name}}(t TestingT, {{.Params}}) {
if h, ok := t.(tHelper); ok { h.Helper() }
if assert.{{.DocInfo.Name}}(t, {{.ForwardedParams}}) { return }
Expand Down