Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing import path when receiver and methods are defined in dif… #179

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions gotests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,12 @@ func TestGenerateTests(t *testing.T) {
template: "testify",
},
want: mustReadAndFormatGoFile(t, "testdata/named/named_on_subtests_on_parallel_on_template_testify.go"),
}, {
name: "receiver and methods are defined in different file",
args: args{
srcPath: "testdata/separate_definition/method.go",
},
want: mustReadAndFormatGoFile(t, "testdata/goldens/receiver_and_methods_in_different_file.go"),
},
}
tmp, err := ioutil.TempDir("", "gotests_test")
Expand Down
8 changes: 6 additions & 2 deletions internal/goparser/goparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ func (p *Parser) Parse(srcPath string, files []models.Path) (*Result, error) {
if err != nil {
return nil, err
}
return &Result{
result := &Result{
Header: &models.Header{
Comments: parsePkgComment(f, f.Package),
Package: f.Name.String(),
Imports: parseImports(f.Imports),
Code: goCode(b, f),
},
Funcs: p.parseFunctions(fset, f, fs),
}, nil
}
for _, v := range fs {
result.Header.Imports = append(result.Header.Imports, parseImports(v.Imports)...)
}
return result, nil
}

func (p *Parser) readFile(srcPath string) ([]byte, error) {
Expand Down
34 changes: 34 additions & 0 deletions testdata/goldens/receiver_and_methods_in_different_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package separate_definition

import (
"testing"

"github.com/cweill/gotests/testdata/separate_definition/field"
)

func TestFoo_Foo(t *testing.T) {
type fields struct {
Bar string
TestFiled field.Test
}
type args struct {
s string
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
f := &Foo{
Bar: tt.fields.Bar,
TestFiled: tt.fields.TestFiled,
}
if err := f.Foo(tt.args.s); (err != nil) != tt.wantErr {
t.Errorf("%q. Foo.Foo() error = %v, wantErr %v", tt.name, err, tt.wantErr)
}
}
}
4 changes: 4 additions & 0 deletions testdata/separate_definition/field/field.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package field

type Test struct {
}
5 changes: 5 additions & 0 deletions testdata/separate_definition/method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package separate_definition

func (*Foo) Foo(s string) error {
return nil
}
8 changes: 8 additions & 0 deletions testdata/separate_definition/receiver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package separate_definition

import "github.com/cweill/gotests/testdata/separate_definition/field"

type Foo struct {
Bar string
TestFiled field.Test
}