Skip to content

Commit

Permalink
Add Named option to makes tests a map (#152)
Browse files Browse the repository at this point in the history
* feature(named): Table Data as map (aka `named`)

* added `named` arguments to produce map instead of the slice with
  names as keys
* help records updated (readme and main.go)

* dev: adding tests

minor tests adjustments

* fix: attempt to fix testify imports

Resolves #153

* tests: some of the tests (named=off) removed

As this functionality most probably already covered.
  • Loading branch information
butuzov authored Mar 27, 2021
1 parent d408325 commit d503102
Show file tree
Hide file tree
Showing 22 changed files with 298 additions and 82 deletions.
3 changes: 3 additions & 0 deletions gotests.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Options struct {
PrintInputs bool // Print function parameters in error messages
Subtests bool // Print tests using Go 1.7 subtests
Parallel bool // Print tests that runs the subtests in parallel.
Named bool // Create Map instead of slice
Importer func() types.Importer // A custom importer.
Template string // Name of custom template set
TemplateDir string // Path to custom template set
Expand Down Expand Up @@ -118,10 +119,12 @@ func generateTest(src models.Path, files []models.Path, opt *Options) (*Generate
if len(funcs) == 0 {
return nil, nil
}

b, err := output.Process(h, funcs, &output.Options{
PrintInputs: opt.PrintInputs,
Subtests: opt.Subtests,
Parallel: opt.Parallel,
Named: opt.Named,
Template: opt.Template,
TemplateDir: opt.TemplateDir,
TemplateParams: opt.TemplateParams,
Expand Down
12 changes: 9 additions & 3 deletions gotests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
//
// -i print test inputs in error messages
//
// -named switch table tests from using slice to map (with test name for the key)
//
// -only regexp. generate tests for functions and methods that match only.
// Takes precedence over -all
//
// -nosubtests disable subtest generation when >= Go 1.7
// -nosubtests disable generating tests using the Go 1.7 subtests feature
//
// -parallel enable parallel subtest generation when >= Go 1.7.
// -parallel enable generating parallel subtests using the Go 1.7 feature
//
// -w write output to (test) files instead of stdout
//
Expand All @@ -37,7 +39,7 @@
//
// -template_params_file read external parameters to template by json with file
//
// -template_params read external parameters to template by json with stdin
// -template_params read external parameters to template by json with stdin
package main

import (
Expand Down Expand Up @@ -68,6 +70,9 @@ var (

// parallel is default false.
parallel bool

// use map instead of slice for table tests.
named bool
)

func main() {
Expand All @@ -82,6 +87,7 @@ func main() {
PrintInputs: *printInputs,
Subtests: !nosubtests,
Parallel: parallel,
Named: named,
WriteOutput: *writeOutput,
Template: valOrGetenv(*template, "GOTESTS_TEMPLATE"),
TemplateDir: valOrGetenv(*templateDir, "GOTESTS_TEMPLATE_DIR"),
Expand Down
2 changes: 2 additions & 0 deletions gotests/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Options struct {
PrintInputs bool // Print function parameters as part of error messages.
Subtests bool // Print tests using Go 1.7 subtests
Parallel bool // Print tests that runs the subtests in parallel.
Named bool // Create Map instead of slice
WriteOutput bool // Write output to test file(s).
Template string // Name of custom template set
TemplateDir string // Path to custom template set
Expand Down Expand Up @@ -96,6 +97,7 @@ func parseOptions(out io.Writer, opt *Options) *gotests.Options {
PrintInputs: opt.PrintInputs,
Subtests: opt.Subtests,
Parallel: opt.Parallel,
Named: opt.Named,
Template: opt.Template,
TemplateDir: opt.TemplateDir,
TemplateParams: templateParams,
Expand Down
3 changes: 2 additions & 1 deletion gotests/subtests.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import "flag"

func init() {
flag.BoolVar(&nosubtests, "nosubtests", false, "disable generating tests using the Go 1.7 subtests feature")
flag.BoolVar(&parallel, "parallel", false, "enable generating parallel subtests")
flag.BoolVar(&parallel, "parallel", false, "enable generating parallel subtests using the Go 1.7 feature")
flag.BoolVar(&named, "named", false, "switch table tests from using slice to map (with test name for the key)")
}
62 changes: 61 additions & 1 deletion gotests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestGenerateTests(t *testing.T) {
printInputs bool
subtests bool
parallel bool
named bool
importer types.Importer
templateDir string
template string
Expand Down Expand Up @@ -561,7 +562,7 @@ func TestGenerateTests(t *testing.T) {
{
name: "Entire testdata directory",
args: args{
srcPath: `testdata/`,
srcPath: `testdata/`,
template: "testify",
},
wantMultipleTests: true,
Expand Down Expand Up @@ -812,6 +813,64 @@ func TestGenerateTests(t *testing.T) {
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/function_with_return_value_template_data.go"),
},
{
name: "named_named=on",
args: args{
srcPath: "testdata/test038.go",
named: true,
},
want: mustReadAndFormatGoFile(t, "testdata/named/named_on.go"),
},
{
name: "named_named=on,template=testify",
args: args{
srcPath: "testdata/test038.go",
template: "testify",
named: true,
},
want: mustReadAndFormatGoFile(t, "testdata/named/named_on_template_testify.go"),
},

{
name: "named_named=on,subtests=on",
args: args{
srcPath: "testdata/test038.go",
subtests: true,
named: true,
},
want: mustReadAndFormatGoFile(t, "testdata/named/named_on_subtests_on.go"),
},
{
name: "named_named=on,subtests=on,template=testify",
args: args{
srcPath: "testdata/test038.go",
subtests: true,
named: true,
template: "testify",
},
want: mustReadAndFormatGoFile(t, "testdata/named/named_on_subtests_on_template_testify.go"),
},
{
name: "named_named=on,subtests=on,parallel=on",
args: args{
srcPath: "testdata/test038.go",
subtests: true,
parallel: true,
named: true,
},
want: mustReadAndFormatGoFile(t, "testdata/named/named_on_subtests_on_parallel_on.go"),
},
{
name: "named_named=on,subtests=on,parallel=on,template=testify",
args: args{
srcPath: "testdata/test038.go",
subtests: true,
parallel: true,
named: true,
template: "testify",
},
want: mustReadAndFormatGoFile(t, "testdata/named/named_on_subtests_on_parallel_on_template_testify.go"),
},
}
tmp, err := ioutil.TempDir("", "gotests_test")
if err != nil {
Expand All @@ -836,6 +895,7 @@ func TestGenerateTests(t *testing.T) {
PrintInputs: tt.args.printInputs,
Subtests: tt.args.subtests,
Parallel: tt.args.parallel,
Named: tt.args.named,
Importer: func() types.Importer { return tt.args.importer },
TemplateDir: tt.args.templateDir,
Template: tt.args.template,
Expand Down
15 changes: 14 additions & 1 deletion internal/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ import (
"github.com/cweill/gotests/internal/render"
)

// we do not need support for aliases in import for now.
var importsMap = map[string]string{
"testify": "github.com/stretchr/testify/assert",
}

type Options struct {
PrintInputs bool
Subtests bool
Parallel bool
Named bool
Template string
TemplateDir string
TemplateParams map[string]interface{}
Expand Down Expand Up @@ -68,12 +74,19 @@ func IsFileExist(path string) bool {
}

func writeTests(w io.Writer, head *models.Header, funcs []*models.Function, opt *Options) error {
if path, ok := importsMap[opt.Template]; ok {
head.Imports = append(head.Imports, &models.Import{
Path: fmt.Sprintf(`"%s"`, path),
})
}

b := bufio.NewWriter(w)
if err := render.Header(b, head); err != nil {
return fmt.Errorf("render.Header: %v", err)
}

for _, fun := range funcs {
if err := render.TestFunction(b, fun, opt.PrintInputs, opt.Subtests, opt.Parallel, opt.TemplateParams); err != nil {
if err := render.TestFunction(b, fun, opt.PrintInputs, opt.Subtests, opt.Named, opt.Parallel, opt.TemplateParams); err != nil {
return fmt.Errorf("render.TestFunction: %v", err)
}
}
Expand Down
53 changes: 27 additions & 26 deletions internal/render/bindata/esc.go

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

8 changes: 4 additions & 4 deletions internal/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ const (
nFile = 7
)

var (
tmpls *template.Template
)
var tmpls *template.Template

func init() {
Reset()
Expand Down Expand Up @@ -169,18 +167,20 @@ func Header(w io.Writer, h *models.Header) error {
return err
}

func TestFunction(w io.Writer, f *models.Function, printInputs, subtests, parallel bool, templateParams map[string]interface{}) error {
func TestFunction(w io.Writer, f *models.Function, printInputs, subtests, named, parallel bool, templateParams map[string]interface{}) error {
return tmpls.ExecuteTemplate(w, "function", struct {
*models.Function
PrintInputs bool
Subtests bool
Parallel bool
Named bool
TemplateParams map[string]interface{}
}{
Function: f,
PrintInputs: printInputs,
Subtests: subtests,
Parallel: parallel,
Named: named,
TemplateParams: templateParams,
})
}
11 changes: 6 additions & 5 deletions internal/render/templates/function.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func {{.TestName}}(t *testing.T) {
{{- end}}
}
{{- end}}
tests := []struct {
name string
tests := {{ if .Named}}map[string]struct{{else}}[]struct{{end}} {
{{ if (not .Named)}}name string{{end}}
{{- with .Receiver}}
{{- if and .IsStruct .Fields}}
fields fields
Expand All @@ -41,10 +41,11 @@ func {{.TestName}}(t *testing.T) {
}{
// TODO: Add test cases.
}
for {{if (or .Subtests (not .IsNaked))}} _, tt := {{end}} range tests {
for {{if (or .Subtests (not .IsNaked))}} {{if .Named}}name{{else}}_{{end}}, tt := {{end}} range tests {
{{- if .Subtests}}
{{- if .Parallel}}tt := tt{{end}}
t.Run(tt.name, func(t *testing.T) {
{{- if .Parallel}}tt := tt;{{end}}
{{- if and .Parallel .Named}}name := name;{{ end }}
t.Run({{if .Named}}name{{else}}tt.name{{end}}, func(t *testing.T) {
{{- if .Parallel}}t.Parallel(){{end}}
{{- end}}
{{- with .Receiver}}
Expand Down
2 changes: 1 addition & 1 deletion internal/render/templates/inputs.tmpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{define "inputs"}}{{$f := .}}{{if not .Subtests}}tt.name, {{end}}{{if $f.PrintInputs}}{{range $f.Parameters}}tt.args.{{Param .}}, {{end}}{{end}}{{end}}
{{define "inputs"}}{{$f := .}}{{if not .Subtests}}{{if not .Named}}tt.{{end}}name, {{end}}{{if $f.PrintInputs}}{{range $f.Parameters}}tt.args.{{Param .}}, {{end}}{{end}}{{end}}
Loading

0 comments on commit d503102

Please sign in to comment.