diff --git a/README.md b/README.md index 92bc540..f4216ba 100644 --- a/README.md +++ b/README.md @@ -30,20 +30,20 @@ Available options: ``` -all generate go tests for all functions and methods - - -excl regexp. generate go tests for functions and methods that don't + + -excl regexp. generate go tests for functions and methods that don't match. Takes precedence over -only, -exported, and -all - - -exported generate go tests for exported functions and methods. Takes + + -exported generate go tests for exported functions and methods. Takes precedence over -only and -all -i print test inputs in error messages - + -only regexp. generate go tests for functions and methods that match only. Takes precedence over -all - + -w write output to (test) files instead of stdout - + -nosubtests disable subtest generation. Only available for Go 1.7+ -template_dir optional. Path to a directory containing custom test code templates diff --git a/gotests_test.go b/gotests_test.go index a429ac6..9236296 100644 --- a/gotests_test.go +++ b/gotests_test.go @@ -559,6 +559,22 @@ func TestGenerateTests(t *testing.T) { }, want: mustReadAndFormatGoFile(t, "testdata/goldens/existing_test_file_with_package_level_mixed_types_comments.go"), }, + { + name: "Naked function with subtests", + args: args{ + srcPath: "testdata/naked_function_with_subtests.go", + subtests: true, + }, + want: mustReadAndFormatGoFile(t, "testdata/goldens/naked_function_with_subtests.go"), + }, + { + name: "Naked function without subtests", + args: args{ + srcPath: "testdata/naked_function_without_subtests.go", + subtests: false, + }, + want: mustReadAndFormatGoFile(t, "testdata/goldens/naked_function_without_subtests.go"), + }, { name: "Test non existing template path", args: args{ diff --git a/internal/render/bindata/esc.go b/internal/render/bindata/esc.go index 8e54710..78e11b3 100644 --- a/internal/render/bindata/esc.go +++ b/internal/render/bindata/esc.go @@ -6,6 +6,8 @@ import ( "bytes" "compress/gzip" "encoding/base64" + "fmt" + "io" "io/ioutil" "net/http" "os" @@ -100,7 +102,24 @@ func (f *_escFile) Close() error { } func (f *_escFile) Readdir(count int) ([]os.FileInfo, error) { - return nil, nil + if !f.isDir { + return nil, fmt.Errorf(" escFile.Readdir: '%s' is not directory", f.name) + } + + fis, ok := _escDirs[f.local] + if !ok { + return nil, fmt.Errorf(" escFile.Readdir: '%s' is directory, but we have no info about content of this dir, local=%s", f.name, f.local) + } + limit := count + if count <= 0 || limit > len(fis) { + limit = len(fis) + } + + if len(fis) == 0 && count > 0 { + return nil, io.EOF + } + + return []os.FileInfo(fis[0:limit]), nil } func (f *_escFile) Stat() (os.FileInfo, error) { @@ -191,9 +210,10 @@ func FSMustString(useLocal bool, name string) string { var _escData = map[string]*_escFile{ "/templates/call.tmpl": { + name: "call.tmpl", local: "templates/call.tmpl", size: 241, - modtime: 1532208007, + modtime: 1540446832, compressed: ` H4sIAAAAAAAC/0SOQWrDQAxFryKMFy0YHaDQA3hTSlvatRjLrsCeFo2SEITuHsY4mdWHP2/el/vEs2SG LtG6dhHuF7FfwA9OLGfW2sgM+c8Ax/JpekoWYYbunKf6eicBI1qLb7RxxJO7Ul4Yehmg5xVeXgHfSWlj @@ -202,30 +222,32 @@ Yy2HvZeIAR5/296PitUbzJB0KU2/K+riTuPX9Z9xLN+kQpOkCMTG7vF85C0AAP//ZQi8iPEAAAA= }, "/templates/function.tmpl": { + name: "function.tmpl", local: "templates/function.tmpl", - size: 2375, - modtime: 1532208007, + size: 2392, + modtime: 1540483824, compressed: ` -H4sIAAAAAAAC/7RWTU/jOhRdJ7/iUgFqn4rZF3XxELynt3h0VNCwGI1GJr3pWOM6HfsGVFn+7yM7zmdT -hs2wIMn1xzk+9xyDtRvMhUKY5KXKSBRq4lxq7RWc57BYAnMuTf0QWMue0NAD36FzU4K/CA0JtWVPM7Bp -4pe8CfoObI0ZilfUzqVJKIsc2H/mkXSZUSg21X8Eyo2pagkd9gh5qIAJk/2+cbbmaouDBYm14duTDPQO -e4xDfgmqTfxqMOtS533w6ln5Y37imu+QUAewQI3rbY9Yh9bxigAYSkfsOoh9fC+o8aJ/+dqBUXyHHlao -bVw8InPNnatNq/VAriht9WgUkabVrN7yWNAT4r0jWZIEvfyvkTUd3dZoSkmmxnnmit6TrIFcI5VamXut -i6jBG1d0rzW8FIUc6OyFvL6Gp9XdagF/bzbgtYaMGzQstCEvNFgrclAFeQUf+A/cOAff5kDke2Jt2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kPA83Qn9MRw2aunfsW +qUeF2e9c1uicbVNcmLbEWtZM/wqIWOMjNpjBZZ+gn71kZiDPPiLezAi1Zb5oQV31o9FareHb64nQsLu6 +LFHbzwDGUWnau1HyNHRTfh7fKAwq5dAxIzwcJSeEhW4cvICrMvi2Xym4lE34EosZGyeijF2bEnMOUOum +q3Mgt50pM7/vyxqUkLl/ErF2OmKbiYWUZbYY5jqgMXyPsRT0O2ANX9+W0B7/+rZYjuCFOtZd8aj1cgCW +945ob4nRuIe1yZjoULC1zc1UVIqEqjEWNuewDy11DnnJU0H1nyvqB6fzGHsK12+W3w62NKoOL6zed9Jg +xLjjRhSDP0xdc6/KOX/5oRxxGOoshcJpoz/N5zvhf9FYSiyI3SMeH/6tucy6DMsxoXzIqOveZ3zYEo5k +f60liaMckY18eq/+j1Evkrz8D8PEp+ALGl7XLnVp2vr0vwAAAP//X+Qs81gJAAA= `, }, "/templates/header.tmpl": { + name: "header.tmpl", local: "templates/header.tmpl", size: 142, - modtime: 1532208053, + modtime: 1540481163, compressed: ` H4sIAAAAAAAC/0TMMQ7CMAyF4d2nsDrBQC7BxIK4gkUebYXiViGb9e6OlAi6/bL1voiM1+rQaYFl1ImU iGo+Q9N1KwXePmRE6g941gspuz3fNkMj0mMkKbKWfatNT4dw65cB3K2AHJO2/DhSzv/6BgAA///GzMM9 @@ -234,9 +256,10 @@ jgAAAA== }, "/templates/inline.tmpl": { + name: "inline.tmpl", local: "templates/inline.tmpl", size: 49, - modtime: 1532202343, + modtime: 1540446006, compressed: ` H4sIAAAAAAAC/6quTklNy8xLVVDKzMvJzEtVqq1VqK4uSc0tyEksSVVQSk7MyVFS0AOLpual1NYCAgAA //+q60H/MQAAAA== @@ -244,9 +267,10 @@ H4sIAAAAAAAC/6quTklNy8xLVVDKzMvJzEtVqq1VqK4uSc0tyEksSVVQSk7MyVFS0AOLpual1NYCAgAA }, "/templates/inputs.tmpl": { + name: "inputs.tmpl", local: "templates/inputs.tmpl", size: 152, - modtime: 1532208007, + modtime: 1540446821, compressed: ` H4sIAAAAAAAC/0yNMQoCQQxFrxKWLSUHEDyAneAJIptZptgomWz1yd1lRoupEh7/vw9sWqopLdU+Z7Ql E1gLXW/E/a2F7B3Ez/MV2qJlRrDJoRcC1LZ/Zi388GpxH5IOXWzXwcXl0FD/dcX3xsCgfWLyzOcbAAD/ @@ -255,9 +279,10 @@ E1gLXW/E/a2F7B3Ez/MV2qJlRrDJoRcC1LZ/Zi388GpxH5IOXWzXwcXl0FD/dcX3xsCgfWLyzOcbAAD/ }, "/templates/message.tmpl": { + name: "message.tmpl", local: "templates/message.tmpl", size: 201, - modtime: 1532208007, + modtime: 1540446006, compressed: ` H4sIAAAAAAAC/zyN4WqDQBCE//sUiyi0oPsAhT5A/xRpS/9f4mgW9GLuTkNY9t2DB/HXDDPDN6o9BvGg ckaMbkRJrVmhKgP5ayL+XU8JMUWz+sakCt+bqd4lXYh/cIZsCHvCf48F/O+mFWZ8DPnbzTB7y0Tugvj0 @@ -266,9 +291,10 @@ ckaMbkRJrVmhKgP5ayL+XU8JMUWz+sakCt+bqd4lXYh/cIZsCHvCf48F/O+mFWZ8DPnbzTB7y0Tugvj0 }, "/templates/results.tmpl": { + name: "results.tmpl", local: "templates/results.tmpl", size: 168, - modtime: 1532208007, + modtime: 1540446006, compressed: ` H4sIAAAAAAAC/1yNTQrCQAyFr/Iosyw9gOBS3HsDoRkJlAy8ma5C7i6pRcFVfr4vee6rVDXBROn7NvoU AXc+7SUoOqPIhssVy+ODI9y1omjEDHexNTf3NrBkc85a82DstH4jG1MW8uQ4hMbv0385A3/uUd8BAAD/ @@ -276,13 +302,22 @@ AXc+7SUoOqPIhssVy+ODI9y1omjEDHexNTf3NrBkc85a82DstH4jG1MW8uQ4hMbv0385A3/uUd8BAAD/ `, }, - "/": { + "/templates": { + name: "templates", + local: `templates`, isDir: true, - local: "", }, +} - "/templates": { - isDir: true, - local: "templates", +var _escDirs = map[string][]os.FileInfo{ + + "templates": { + _escData["/templates/call.tmpl"], + _escData["/templates/function.tmpl"], + _escData["/templates/header.tmpl"], + _escData["/templates/inline.tmpl"], + _escData["/templates/inputs.tmpl"], + _escData["/templates/message.tmpl"], + _escData["/templates/results.tmpl"], }, } diff --git a/internal/render/templates/function.tmpl b/internal/render/templates/function.tmpl index fa1e59c..f16cf48 100644 --- a/internal/render/templates/function.tmpl +++ b/internal/render/templates/function.tmpl @@ -41,7 +41,7 @@ func {{.TestName}}(t *testing.T) { }{ // TODO: Add test cases. } - for {{if not .IsNaked}} _, tt := {{end}} range tests { + for {{if (or .Subtests (not .IsNaked))}} _, tt := {{end}} range tests { {{- if .Subtests }}t.Run(tt.name, func(t *testing.T) { {{- end -}} {{- with .Receiver}} {{- if .IsStruct}} diff --git a/testdata/bad_customtemplates/function.tmpl b/testdata/bad_customtemplates/function.tmpl index 062b265..1fe3a85 100644 --- a/testdata/bad_customtemplates/function.tmpl +++ b/testdata/bad_customtemplates/function.tmpl @@ -41,7 +41,7 @@ func {{.TestName}}(t *testing.T) { }{ // TODO: Add test cases. } - for {{if not .IsNaked}} _, tt := {{end}} range testCases { + for {{if (or .Subtests (not .IsNaked))}} _, tt := {{end}} range testCases { {{- if .Subtests }}t.Run(tt.name, func(t *testing.T) { {{- end -}} {{- with .Receiver}} {{- if .IsStruct}} diff --git a/testdata/customtemplates/function.tmpl b/testdata/customtemplates/function.tmpl index 062b265..1fe3a85 100644 --- a/testdata/customtemplates/function.tmpl +++ b/testdata/customtemplates/function.tmpl @@ -41,7 +41,7 @@ func {{.TestName}}(t *testing.T) { }{ // TODO: Add test cases. } - for {{if not .IsNaked}} _, tt := {{end}} range testCases { + for {{if (or .Subtests (not .IsNaked))}} _, tt := {{end}} range testCases { {{- if .Subtests }}t.Run(tt.name, func(t *testing.T) { {{- end -}} {{- with .Receiver}} {{- if .IsStruct}} diff --git a/testdata/goldens/naked_function_with_subtests.go b/testdata/goldens/naked_function_with_subtests.go new file mode 100644 index 0000000..ea4cb3b --- /dev/null +++ b/testdata/goldens/naked_function_with_subtests.go @@ -0,0 +1,29 @@ +package testdata + +import "testing" + +func Test_main(t *testing.T) { + tests := []struct { + name string + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + main() + }) + } +} + +func Test_do(t *testing.T) { + tests := []struct { + name string + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + do() + }) + } +} diff --git a/testdata/goldens/naked_function_without_subtests.go b/testdata/goldens/naked_function_without_subtests.go new file mode 100644 index 0000000..d2c829a --- /dev/null +++ b/testdata/goldens/naked_function_without_subtests.go @@ -0,0 +1,25 @@ +package testdata + +import "testing" + +func Test_main(t *testing.T) { + tests := []struct { + name string + }{ + // TODO: Add test cases. + } + for range tests { + main() + } +} + +func Test_do(t *testing.T) { + tests := []struct { + name string + }{ + // TODO: Add test cases. + } + for range tests { + do() + } +} diff --git a/testdata/naked_function_with_subtests.go b/testdata/naked_function_with_subtests.go new file mode 100644 index 0000000..3915821 --- /dev/null +++ b/testdata/naked_function_with_subtests.go @@ -0,0 +1,9 @@ +package testdata + +func main() { + do() +} + +func do() { + +} diff --git a/testdata/naked_function_without_subtests.go b/testdata/naked_function_without_subtests.go new file mode 100644 index 0000000..3915821 --- /dev/null +++ b/testdata/naked_function_without_subtests.go @@ -0,0 +1,9 @@ +package testdata + +func main() { + do() +} + +func do() { + +}