diff --git a/_codegen/main.go b/_codegen/main.go index 574985181..deb6b768a 100644 --- a/_codegen/main.go +++ b/_codegen/main.go @@ -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 } @@ -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", + } + 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. diff --git a/require/require.go b/require/require.go index 23a3be780..b57d6f8bc 100644 --- a/require/require.go +++ b/require/require.go @@ -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") @@ -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") diff --git a/require/require.go.tmpl b/require/require.go.tmpl index 8b3283685..d4c236c5c 100644 --- a/require/require.go.tmpl +++ b/require/require.go.tmpl @@ -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 }