From f32afca640dd234b015ba0fe26205af3c1bc5ff2 Mon Sep 17 00:00:00 2001 From: Charles Weill Date: Mon, 29 Feb 2016 00:00:36 -0500 Subject: [PATCH] Support naked functions. --- gotests_test.go | 24 +++++++++++++++++++----- internal/models/models.go | 4 ++++ internal/render/bindata/bindata.go | 6 +++--- internal/render/templates/function.tmpl | 2 +- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/gotests_test.go b/gotests_test.go index c2f7906..7718b2d 100644 --- a/gotests_test.go +++ b/gotests_test.go @@ -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`, @@ -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 } diff --git a/internal/models/models.go b/internal/models/models.go index 5d4dd76..62c9cb1 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -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 } diff --git a/internal/render/bindata/bindata.go b/internal/render/bindata/bindata.go index 92409ca..0598a0a 100644 --- a/internal/render/bindata/bindata.go +++ b/internal/render/bindata/bindata.go @@ -94,7 +94,7 @@ func templatesCallTmpl() (*asset, error) { return a, nil } -var _templatesFunctionTmpl = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\x8c\x95\xc1\x6e\xdb\x30\x0c\x86\xcf\xce\x53\xb0\x41\x5b\xd8\x83\xa1\xde\x5b\xf4\xb0\xa2\xdd\xd0\xc3\x96\x22\x0d\xd6\xc3\x30\x0c\xaa\x4d\x67\x02\x14\x39\x93\xe4\x14\x81\xe1\x77\x1f\x25\x5b\x8e\x9b\xc4\xcd\x4e\x8e\x28\xf2\x13\xc9\x9f\x52\xea\x3a\xc7\x42\x28\x84\x69\x51\xa9\xcc\x8a\x52\x4d\x9b\x06\xea\xfa\xbc\x80\xeb\x5b\x60\x4d\x33\x99\xb8\x0d\xb2\xb0\x05\x1a\xfb\x9d\xaf\xb0\x69\x62\x0b\x9f\x2c\xad\x84\x5a\xb2\x45\x02\xf5\x24\x72\x2b\xe3\x22\x7e\xfe\x32\x56\x57\x99\x85\x9a\x42\x44\x01\xaa\xb4\x70\x5e\xb0\x27\x2d\x94\x7d\x54\xeb\xca\x1a\x42\x46\x91\x22\x0e\x90\x27\x11\xc8\x0f\x55\xee\x0f\x7d\x13\xf6\x0f\xb0\x39\x66\x28\x36\xa8\xbd\x89\x10\xec\xd1\x3c\x7b\xa6\x37\x68\xae\x96\x08\xec\x8b\x40\x99\xb7\xac\xba\xf6\x0b\x97\xac\x4f\x73\xbb\x46\xff\x2b\x50\x51\x1a\xec\x1c\x03\x7a\xdc\xb7\xfb\x76\xa7\xb8\x92\x9f\xb8\xa6\x64\x2d\xea\x70\x9a\x37\x8c\x12\x06\x91\x73\x34\x95\xb4\x21\xec\x85\x2b\x3b\x1a\xe5\xca\x9c\xa3\xad\xb4\x32\x0f\x5a\x97\xda\xc7\xbc\x51\x04\xad\xe0\xb5\x2c\x65\xf0\x9d\x44\x0d\xb5\x3b\xba\xba\x82\xc5\xec\x7e\x76\x0d\x9f\xf3\x1c\x5c\xf3\x21\xe3\x06\x0d\xa3\xed\x49\x54\x94\x1a\x7e\xa7\x60\xad\x13\xa4\xcd\xa7\xd5\xa7\x3e\xd2\xe3\xfd\x16\x1f\xf6\x89\x20\xad\x93\xcb\xda\x7b\x72\x8a\xbb\xec\x12\xea\xaa\x61\x3f\xb8\xac\xa8\xa6\x7a\x44\x22\xd6\x4e\xce\x35\x65\xc5\x06\x82\xa5\xbb\xba\xa2\x0f\x74\x18\x6a\x10\x12\x7e\xd1\xc2\xa2\x3e\xd0\x84\xb2\xbd\x7c\xdd\x52\xbd\xec\xae\x2a\x0a\xd4\xf5\x31\x2c\x11\xb8\xca\x21\x76\xd3\xc9\x66\x4a\x6e\x87\xbd\x4f\x0e\xed\x33\x85\xbe\xbe\x04\xba\xf3\x2c\xae\xd6\x92\x5b\xba\x36\xba\x15\x79\x4a\x53\xee\xd1\xbb\x9d\x8c\x4b\xd9\x9b\x3f\x14\x9a\x8c\xed\xce\x7e\x2a\x14\x82\x34\x00\x5e\x81\x63\xe0\x9b\x9e\x1c\x3b\xbf\xb3\x5b\x50\x42\x26\xee\x4b\x6d\x0e\xe3\xe3\xe6\x25\xb2\xcc\x03\x8b\x78\x3a\x24\xad\xd0\x18\xbe\xc4\x2e\x4b\x74\x1e\x70\x0b\x17\x9b\x14\x42\xf0\xc5\x66\x9a\xbe\x3b\x5c\xf8\x4b\xbc\x8b\x48\x07\x47\x25\x61\x50\xf6\x86\x3f\xca\x4a\x45\x8f\x45\x85\x23\x6a\x1f\xbb\x33\x70\x44\x67\xdf\xa7\xaf\xa5\xdd\x8d\x65\xaf\x3b\x7b\xf6\x8f\x49\x9c\xdc\x0c\x5c\xda\x3e\x0c\xaf\x5e\xf7\x1c\x40\x8b\xbe\xe3\x46\x64\xed\x5d\x0c\xf4\x61\xa1\x92\xde\xc5\x5e\xc0\x93\xcc\x93\x8c\x33\x8d\x85\xc4\xcc\xb2\x7b\xc4\xf5\xc3\xdf\x8a\xcb\xb8\xc7\xa6\xef\xa1\x49\x4b\xed\x5a\xf5\x5f\xe2\xf9\x76\xd1\x4b\xdb\x8d\xcf\x37\xea\xa2\x58\x4b\x77\x23\xfb\xd4\x3b\xe2\x4e\xe0\x13\xea\x8e\x66\x37\x94\xcf\xbd\x38\xf4\x27\x11\x56\xff\x02\x00\x00\xff\xff\xdf\x98\x64\x59\x50\x06\x00\x00") +var _templatesFunctionTmpl = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\x8c\x55\x51\x4f\xdb\x30\x10\x7e\x4e\x7f\xc5\x51\x01\x4a\xa6\xc8\xbc\x83\x78\x18\x82\x4d\x3c\x8c\x22\x40\xe3\x61\x9a\x26\x93\x5c\x98\x35\xd7\xe9\x6c\xa7\xa8\x8a\xf2\xdf\x77\x76\xe2\x24\xb4\x0d\xdd\x53\xea\xf3\xdd\x77\x77\xdf\x77\xe7\xd6\x75\x8e\x85\x50\x08\xf3\xa2\x52\x99\x15\xa5\x9a\x37\x0d\xd4\xf5\x71\x01\xe7\x97\xc0\x9a\x66\x36\x73\x17\x64\x61\x4f\x68\xec\x1d\x5f\x62\xd3\xc4\x16\x3e\x59\x3a\x09\xf5\xca\x9e\x12\xa8\x67\x91\x3b\x19\x17\xf1\xe3\xa7\xb1\xba\xca\x2c\xd4\x14\x22\x0a\x50\xa5\x85\xe3\x82\xdd\x6b\xa1\xec\xad\x5a\x55\xd6\x10\x64\x14\x29\xc2\x01\xf2\x24\x04\xf2\x43\x95\xfb\xa4\x6f\xc2\xfe\x06\xf6\x80\x19\x8a\x35\x6a\x6f\x22\x08\x76\x6b\x1e\x3d\xa6\x37\x68\xae\x5e\x11\xd8\x17\x81\x32\x6f\xb1\xea\xda\x1f\x5c\xb1\xbe\xcc\xcd\x0a\xfd\xaf\x80\x8a\xd2\x60\xe7\x18\xa0\xa7\x7d\xbb\x6f\x97\xc5\xb5\x7c\xcf\x35\x15\x6b\x51\x87\x6c\xde\x30\x89\x30\x8a\x7c\x40\x53\x49\x1b\xc2\x9e\xb9\xb2\x93\x51\xae\xcd\x07\xb4\x95\x56\xe6\x46\xeb\x52\xfb\x98\x37\x8a\xa0\x13\xbc\x94\xa5\x0c\xbe\xb3\xa8\x21\xba\xa3\xb3\x33\x78\x5a\x5c\x2f\xce\xe1\x73\x9e\x83\x23\x1f\x32\x6e\xd0\x30\xba\x9e\x45\x45\xa9\x07\xf2\x89\xbd\x3b\xfe\x07\x5d\x9a\x5f\x29\x58\xeb\x44\x0a\x79\xdb\x5a\x5b\xed\xea\x3d\xfc\x6f\xd3\xbf\xcb\xa1\x07\x73\x4e\xae\x23\xef\xc9\x29\xee\xb4\x4b\xd0\x75\xca\xbe\x73\x59\x51\xbf\xf5\x84\x7c\xac\x9d\xaa\x73\xaa\x8e\x8d\xc4\x4c\x87\x9e\xa3\x0f\x34\x1a\xeb\x13\x0a\x7e\xd6\xc2\xa2\xde\xd1\x8b\xaa\x3d\x7d\xd9\x50\xbf\xec\xaa\x2a\x0a\xd4\xf5\x3e\x58\x42\xe0\x2a\x87\xd8\x93\xb7\x50\x72\x33\xd6\x25\xd9\xb5\x2f\x14\xfa\xfe\x12\xe8\xf2\x59\x5c\xae\x24\xb7\xb4\x52\xba\x1d\x80\x39\x6d\x80\x87\x1e\x6e\x32\x2e\x65\x6f\xfe\x70\x08\xc8\xd8\xde\x6c\x97\x42\x21\x48\xc3\xe1\x15\xd8\x07\x7c\xd1\x23\xc7\xce\xef\xe8\x12\x94\x90\x89\xfb\x12\xcd\x61\xb4\xdc\x2c\x45\x96\x79\xc0\x22\x9e\x8f\x91\x96\x68\x0c\x7f\xc5\xae\x4a\x74\x1e\x70\x09\x27\xeb\x14\x42\xf0\xc9\x7a\x9e\xbe\x4b\x2e\xfc\x82\x0f\x11\xe9\x28\x55\x12\x06\x65\x6b\x31\xa2\xac\x54\xf4\x90\x54\x38\xa1\xf6\xbe\x7d\x82\x3d\x3a\x7b\x9e\xbe\x96\x76\x18\xcb\x5e\x77\xf6\xe8\x1f\x9a\x38\xb9\x18\xb9\xb4\x3c\x8c\xd7\xb2\x7b\x2a\xa0\x85\xbe\xe2\x46\x64\xed\x9e\x06\xf4\x71\xa3\x92\xde\xcc\x5e\xc0\x83\x98\x07\x31\x8e\x34\x16\x12\x33\xcb\xae\x11\x57\x37\x7f\x2b\x2e\xe3\x1e\x36\x7d\x0f\x9a\xb4\xa8\x1d\x55\xff\x25\x9e\xa7\x8b\x5e\xe1\x6e\x7c\xbe\x11\x8b\x62\x25\xdd\x46\xf6\xa5\x77\x88\x83\xc0\x07\xd4\x9d\xac\x6e\x2c\x9f\x7b\x8d\xe8\x0f\x24\x9c\xfe\x05\x00\x00\xff\xff\x25\xe7\x70\x97\x6c\x06\x00\x00") func templatesFunctionTmplBytes() ([]byte, error) { return bindataRead( @@ -109,7 +109,7 @@ func templatesFunctionTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "templates/function.tmpl", size: 1616, mode: os.FileMode(420), modTime: time.Unix(1455478039, 0)} + info := bindataFileInfo{name: "templates/function.tmpl", size: 1644, mode: os.FileMode(420), modTime: time.Unix(1456721917, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -129,7 +129,7 @@ func templatesHeaderTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "templates/header.tmpl", size: 107, mode: os.FileMode(420), modTime: time.Unix(1454818382, 0)} + info := bindataFileInfo{name: "templates/header.tmpl", size: 107, mode: os.FileMode(420), modTime: time.Unix(1456559458, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/internal/render/templates/function.tmpl b/internal/render/templates/function.tmpl index d0108ac..67abe82 100644 --- a/internal/render/templates/function.tmpl +++ b/internal/render/templates/function.tmpl @@ -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}}