Skip to content

Commit f32afca

Browse files
committed
Support naked functions.
1 parent d0c92db commit f32afca

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

gotests_test.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,23 @@ func TestGenerateTests(t *testing.T) {
5656
srcPath: `testdata/test000.go`,
5757
wantNoTests: true,
5858
}, {
59-
name: "Function w/ neither receiver, parameters, nor results",
60-
srcPath: `testdata/test001.go`,
61-
wantNoTests: true,
59+
name: "Function w/ neither receiver, parameters, nor results",
60+
srcPath: `testdata/test001.go`,
61+
want: `package testdata
62+
63+
import "testing"
64+
65+
func TestFoo1(t *testing.T) {
66+
tests := []struct {
67+
name string
68+
}{
69+
// TODO: Add test cases.
70+
}
71+
for range tests {
72+
Foo1()
73+
}
74+
}
75+
`,
6276
}, {
6377
name: "Function w/ anonymous arguments",
6478
srcPath: `testdata/test002.go`,
@@ -2133,11 +2147,11 @@ func TestUndefinedDo(t *testing.T) {
21332147
t.Errorf("%q. GenerateTests(%v) error = %v, wantErr %v", tt.name, tt.srcPath, err, tt.wantErr)
21342148
continue
21352149
}
2136-
if len(gts) == 0 && !tt.wantNoTests {
2150+
if (len(gts) == 0) != tt.wantNoTests {
21372151
t.Errorf("%q. GenerateTests(%v) returned no tests", tt.name, tt.srcPath)
21382152
continue
21392153
}
2140-
if len(gts) > 1 && !tt.wantMultipleTests {
2154+
if (len(gts) > 1) != tt.wantMultipleTests {
21412155
t.Errorf("%q. GenerateTests(%v) returned too many tests", tt.name, tt.srcPath)
21422156
continue
21432157
}

internal/models/models.go

+4
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ func (f *Function) TestName() string {
131131
return "Test" + strings.Title(r) + strings.Title(f.Name)
132132
}
133133

134+
func (f *Function) IsNaked() bool {
135+
return f.Receiver == nil && len(f.Parameters) == 0 && len(f.Results) == 0
136+
}
137+
134138
type Import struct {
135139
Name, Path string
136140
}

internal/render/bindata/bindata.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/render/templates/function.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func {{.TestName}}(t *testing.T) {
1111
}{
1212
// TODO: Add test cases.
1313
}
14-
for _, tt := range tests { {{with .Receiver}}{{if .IsStruct}}
14+
for {{if not .IsNaked}} _, tt := {{end}} range tests { {{with .Receiver}}{{if .IsStruct}}
1515
{{Receiver .}} := {{if .Type.IsStar}}&{{end}}{{.Type.Value}}{ {{range .Fields}}
1616
{{.Name}}: tt.{{Field .}}, {{end}}
1717
} {{end}} {{end}} {{range .Parameters}}{{if .IsWriter}}

0 commit comments

Comments
 (0)