Skip to content
Open
Show file tree
Hide file tree
Changes from all 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",
}
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