Skip to content

Commit c7668ea

Browse files
Fixes stretchr#339 - Add assertionf assertions like Errorf and Equalf
1 parent c33f336 commit c7668ea

11 files changed

+1635
-103
lines changed

_codegen/main.go

+23
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"log"
2121
"os"
2222
"path"
23+
"regexp"
2324
"strings"
2425
"text/template"
2526

@@ -28,6 +29,7 @@ import (
2829

2930
var (
3031
pkg = flag.String("assert-path", "github.com/stretchr/testify/assert", "Path to the assert package")
32+
includeF = flag.Bool("include-format-funcs", false, "include format functions such as Errorf and Equalf")
3133
outputPkg = flag.String("output-package", "", "package for the resulting code")
3234
tmplFile = flag.String("template", "", "What file to load the function template from")
3335
out = flag.String("out", "", "What file to write the source code to")
@@ -157,6 +159,11 @@ func analyzeCode(scope *types.Scope, docs *doc.Package) (imports.Importer, []tes
157159
continue
158160
}
159161

162+
// Skip functions ending with f
163+
if strings.HasSuffix(fdocs.Name, "f") && !*includeF {
164+
continue
165+
}
166+
160167
funcs = append(funcs, testFunc{*outputPkg, fdocs, fn})
161168
importer.AddImportsFrom(sig.Params())
162169
}
@@ -264,10 +271,26 @@ func (f *testFunc) ForwardedParams() string {
264271
return p
265272
}
266273

274+
func (f *testFunc) ParamsFormat() string {
275+
return strings.Replace(f.Params(), "msgAndArgs", "msg string, args", 1)
276+
}
277+
278+
func (f *testFunc) ForwardedParamsFormat() string {
279+
return strings.Replace(f.ForwardedParams(), "msgAndArgs", "append([]interface{}{msg}, args...)", 1)
280+
}
281+
267282
func (f *testFunc) Comment() string {
268283
return "// " + strings.Replace(strings.TrimSpace(f.DocInfo.Doc), "\n", "\n// ", -1)
269284
}
270285

286+
func (f *testFunc) CommentFormat() string {
287+
search := fmt.Sprintf("%s", f.DocInfo.Name)
288+
replace := fmt.Sprintf("%sf", f.DocInfo.Name)
289+
comment := strings.Replace(f.Comment(), search, replace, -1)
290+
exp := regexp.MustCompile(replace + `\(((\(\)|[^)])+)\)`)
291+
return exp.ReplaceAllString(comment, replace+`($1, "error message %s", "formatted")`)
292+
}
293+
271294
func (f *testFunc) CommentWithoutT(receiver string) string {
272295
search := fmt.Sprintf("assert.%s(t, ", f.DocInfo.Name)
273296
replace := fmt.Sprintf("%s.%s(", receiver, f.DocInfo.Name)

assert/assertion_format.go

+368
Large diffs are not rendered by default.

assert/assertion_format.go.tmpl

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{.CommentFormat}}
2+
func {{.DocInfo.Name}}f(t TestingT, {{.ParamsFormat}}) bool {
3+
return {{.DocInfo.Name}}(t, {{.ForwardedParamsFormat}})
4+
}

0 commit comments

Comments
 (0)