Skip to content

Commit

Permalink
Fixes #339 - Add assertionf assertions like Errorf and Equalf
Browse files Browse the repository at this point in the history
  • Loading branch information
ernesto-jimenez committed May 29, 2017
1 parent dd232a2 commit 509ffd4
Show file tree
Hide file tree
Showing 11 changed files with 1,635 additions and 103 deletions.
23 changes: 23 additions & 0 deletions _codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"log"
"os"
"path"
"regexp"
"strings"
"text/template"

Expand All @@ -28,6 +29,7 @@ import (

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

// Skip functions ending with f
if strings.HasSuffix(fdocs.Name, "f") && !*includeF {
continue
}

funcs = append(funcs, testFunc{*outputPkg, fdocs, fn})
importer.AddImportsFrom(sig.Params())
}
Expand Down Expand Up @@ -264,10 +271,26 @@ func (f *testFunc) ForwardedParams() string {
return p
}

func (f *testFunc) ParamsFormat() string {
return strings.Replace(f.Params(), "msgAndArgs", "msg string, args", 1)
}

func (f *testFunc) ForwardedParamsFormat() string {
return strings.Replace(f.ForwardedParams(), "msgAndArgs", "append([]interface{}{msg}, args...)", 1)
}

func (f *testFunc) Comment() string {
return "// " + strings.Replace(strings.TrimSpace(f.DocInfo.Doc), "\n", "\n// ", -1)
}

func (f *testFunc) CommentFormat() string {
search := fmt.Sprintf("%s", f.DocInfo.Name)
replace := fmt.Sprintf("%sf", f.DocInfo.Name)
comment := strings.Replace(f.Comment(), search, replace, -1)
exp := regexp.MustCompile(replace + `\(((\(\)|[^)])+)\)`)
return exp.ReplaceAllString(comment, replace+`($1, "error message %s", "formatted")`)
}

func (f *testFunc) CommentWithoutT(receiver string) string {
search := fmt.Sprintf("assert.%s(t, ", f.DocInfo.Name)
replace := fmt.Sprintf("%s.%s(", receiver, f.DocInfo.Name)
Expand Down
368 changes: 368 additions & 0 deletions assert/assertion_format.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions assert/assertion_format.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{.CommentFormat}}
func {{.DocInfo.Name}}f(t TestingT, {{.ParamsFormat}}) bool {
return {{.DocInfo.Name}}(t, {{.ForwardedParamsFormat}})
}
Loading

0 comments on commit 509ffd4

Please sign in to comment.