-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from butuzov/refactor
refactoring: code refactoring
- Loading branch information
Showing
10 changed files
with
231 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package output | ||
|
||
import "os" | ||
|
||
func IsFileExist(path string) bool { | ||
_, err := os.Stat(path) | ||
return !os.IsNotExist(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package output | ||
|
||
// we do not need support for aliases in import for now. | ||
var importsMap = map[string]string{ | ||
"testify": "github.com/stretchr/testify/assert", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package output | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/cweill/gotests/internal/models" | ||
"github.com/cweill/gotests/internal/render" | ||
"golang.org/x/tools/imports" | ||
) | ||
|
||
type Options struct { | ||
PrintInputs bool | ||
Subtests bool | ||
Parallel bool | ||
Named bool | ||
Template string | ||
TemplateDir string | ||
TemplateParams map[string]interface{} | ||
TemplateData [][]byte | ||
|
||
render *render.Render | ||
} | ||
|
||
func (o *Options) Process(head *models.Header, funcs []*models.Function) ([]byte, error) { | ||
o.render = render.New() | ||
|
||
switch { | ||
case o.providesTemplateDir(): | ||
if err := o.render.LoadCustomTemplates(o.TemplateDir); err != nil { | ||
return nil, fmt.Errorf("loading custom templates: %v", err) | ||
} | ||
case o.providesTemplate(): | ||
if err := o.render.LoadCustomTemplatesName(o.Template); err != nil { | ||
return nil, fmt.Errorf("loading custom templates of name: %v", err) | ||
} | ||
case o.providesTemplateData(): | ||
o.render.LoadFromData(o.TemplateData) | ||
} | ||
|
||
// | ||
tf, err := ioutil.TempFile("", "gotests_") | ||
if err != nil { | ||
return nil, fmt.Errorf("ioutil.TempFile: %v", err) | ||
} | ||
defer tf.Close() | ||
defer os.Remove(tf.Name()) | ||
|
||
// create physical copy of test | ||
b := &bytes.Buffer{} | ||
if err := o.writeTests(b, head, funcs); err != nil { | ||
return nil, err | ||
} | ||
|
||
// format file | ||
out, err := imports.Process(tf.Name(), b.Bytes(), nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("imports.Process: %v", err) | ||
} | ||
return out, nil | ||
} | ||
|
||
func (o *Options) providesTemplateData() bool { | ||
return o != nil && len(o.TemplateData) > 0 | ||
} | ||
|
||
func (o *Options) providesTemplateDir() bool { | ||
return o != nil && o.TemplateDir != "" | ||
} | ||
|
||
func (o *Options) providesTemplate() bool { | ||
return o != nil && o.Template != "" | ||
} | ||
|
||
func (o *Options) writeTests(w io.Writer, head *models.Header, funcs []*models.Function) error { | ||
if path, ok := importsMap[o.Template]; ok { | ||
head.Imports = append(head.Imports, &models.Import{ | ||
Path: fmt.Sprintf(`"%s"`, path), | ||
}) | ||
} | ||
|
||
b := bufio.NewWriter(w) | ||
if err := o.render.Header(b, head); err != nil { | ||
return fmt.Errorf("render.Header: %v", err) | ||
} | ||
|
||
for _, fun := range funcs { | ||
err := o.render.TestFunction(b, fun, o.PrintInputs, o.Subtests, o.Named, o.Parallel, o.TemplateParams) | ||
if err != nil { | ||
return fmt.Errorf("render.TestFunction: %v", err) | ||
} | ||
} | ||
|
||
return b.Flush() | ||
} |
0
internal/output/output_test.go → internal/output/options_test.go
100644 → 100755
File renamed without changes.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package render | ||
|
||
//go:generate esc -o bindata/esc.go -pkg=bindata templates | ||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/cweill/gotests/internal/models" | ||
) | ||
|
||
const nFile = 7 // Number of files to be read from template (package) template (directory) | ||
|
||
func fieldName(f *models.Field) string { | ||
var n string | ||
if f.IsNamed() { | ||
n = f.Name | ||
} else { | ||
n = f.Type.String() | ||
} | ||
return n | ||
} | ||
|
||
func receiverName(f *models.Receiver) string { | ||
var n string | ||
if f.IsNamed() { | ||
n = f.Name | ||
} else { | ||
n = f.ShortName() | ||
} | ||
if n == "name" { | ||
// Avoid conflict with test struct's "name" field. | ||
n = "n" | ||
} else if n == "t" { | ||
// Avoid conflict with test argument. | ||
// "tr" is short for t receiver. | ||
n = "tr" | ||
} | ||
return n | ||
} | ||
|
||
func parameterName(f *models.Field) string { | ||
var n string | ||
if f.IsNamed() { | ||
n = f.Name | ||
} else { | ||
n = fmt.Sprintf("in%v", f.Index) | ||
} | ||
return n | ||
} | ||
|
||
func wantName(f *models.Field) string { | ||
var n string | ||
if f.IsNamed() { | ||
n = "want" + strings.Title(f.Name) | ||
} else if f.Index == 0 { | ||
n = "want" | ||
} else { | ||
n = fmt.Sprintf("want%v", f.Index) | ||
} | ||
return n | ||
} | ||
|
||
func gotName(f *models.Field) string { | ||
var n string | ||
if f.IsNamed() { | ||
n = "got" + strings.Title(f.Name) | ||
} else if f.Index == 0 { | ||
n = "got" | ||
} else { | ||
n = fmt.Sprintf("got%v", f.Index) | ||
} | ||
return n | ||
} |
Oops, something went wrong.
nit: WORNING -> WARNING