Skip to content

Commit

Permalink
Merge pull request cweill#81 from zacatac/master
Browse files Browse the repository at this point in the history
Consider `isNaked` and subtests flags in for loop of function template.
  • Loading branch information
cweill authored Oct 29, 2018
2 parents 641e349 + 813402b commit 276664f
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 37 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions gotests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
89 changes: 62 additions & 27 deletions internal/render/bindata/esc.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 @@ -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}}
Expand Down
2 changes: 1 addition & 1 deletion testdata/bad_customtemplates/function.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
2 changes: 1 addition & 1 deletion testdata/customtemplates/function.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
29 changes: 29 additions & 0 deletions testdata/goldens/naked_function_with_subtests.go
Original file line number Diff line number Diff line change
@@ -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()
})
}
}
25 changes: 25 additions & 0 deletions testdata/goldens/naked_function_without_subtests.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
9 changes: 9 additions & 0 deletions testdata/naked_function_with_subtests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package testdata

func main() {
do()
}

func do() {

}
9 changes: 9 additions & 0 deletions testdata/naked_function_without_subtests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package testdata

func main() {
do()
}

func do() {

}

0 comments on commit 276664f

Please sign in to comment.