Skip to content

Commit

Permalink
Support naked functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
cweill committed Feb 29, 2016
1 parent d0c92db commit f32afca
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
24 changes: 19 additions & 5 deletions gotests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,23 @@ func TestGenerateTests(t *testing.T) {
srcPath: `testdata/test000.go`,
wantNoTests: true,
}, {
name: "Function w/ neither receiver, parameters, nor results",
srcPath: `testdata/test001.go`,
wantNoTests: true,
name: "Function w/ neither receiver, parameters, nor results",
srcPath: `testdata/test001.go`,
want: `package testdata
import "testing"
func TestFoo1(t *testing.T) {
tests := []struct {
name string
}{
// TODO: Add test cases.
}
for range tests {
Foo1()
}
}
`,
}, {
name: "Function w/ anonymous arguments",
srcPath: `testdata/test002.go`,
Expand Down Expand Up @@ -2133,11 +2147,11 @@ func TestUndefinedDo(t *testing.T) {
t.Errorf("%q. GenerateTests(%v) error = %v, wantErr %v", tt.name, tt.srcPath, err, tt.wantErr)
continue
}
if len(gts) == 0 && !tt.wantNoTests {
if (len(gts) == 0) != tt.wantNoTests {
t.Errorf("%q. GenerateTests(%v) returned no tests", tt.name, tt.srcPath)
continue
}
if len(gts) > 1 && !tt.wantMultipleTests {
if (len(gts) > 1) != tt.wantMultipleTests {
t.Errorf("%q. GenerateTests(%v) returned too many tests", tt.name, tt.srcPath)
continue
}
Expand Down
4 changes: 4 additions & 0 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func (f *Function) TestName() string {
return "Test" + strings.Title(r) + strings.Title(f.Name)
}

func (f *Function) IsNaked() bool {
return f.Receiver == nil && len(f.Parameters) == 0 && len(f.Results) == 0
}

type Import struct {
Name, Path string
}
Expand Down
6 changes: 3 additions & 3 deletions internal/render/bindata/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/render/templates/function.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func {{.TestName}}(t *testing.T) {
}{
// TODO: Add test cases.
}
for _, tt := range tests { {{with .Receiver}}{{if .IsStruct}}
for {{if not .IsNaked}} _, tt := {{end}} range tests { {{with .Receiver}}{{if .IsStruct}}
{{Receiver .}} := {{if .Type.IsStar}}&{{end}}{{.Type.Value}}{ {{range .Fields}}
{{.Name}}: tt.{{Field .}}, {{end}}
} {{end}} {{end}} {{range .Parameters}}{{if .IsWriter}}
Expand Down

0 comments on commit f32afca

Please sign in to comment.