cweill / gotests

Automatically generate Go test boilerplate from your source code.
Apache License 2.0
4.92k stars 346 forks source link

[bug] Some import path may be lost when receiver and methods are defined in different file #178

Open AtwoodHuang opened 1 year ago

AtwoodHuang commented 1 year ago

I may find a bug for this code: some import paths may be lost when receiver and its methods are defined in different file.

Describe the bug

Some import paths may be lost when receiver and its methods are defined in different file.

How To Reproduce

File tree of my golang project:

|field | |field.go |method.go |receiver.go |____go.mod

receiver.go

package test

import "AtwoodHuang/test/field"

type Foo struct {
    Bar       string
    TestFiled field.Test
}

method.go

package test

func (*Foo) Foo(s string) error {
    return nil
}

field/field.go

package field

type Test struct {
}

Bugs

when generate test function for method Foo(s string) error, gotests miss import path for TestFiled in receiver Foo.(missing import path is AtwoodHuang/test/field)

package test

import "testing"

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 {
        t.Run(tt.name, func(t *testing.T) {
            f := &Foo{
                Bar:       tt.fields.Bar,
                TestFiled: tt.fields.TestFiled,
            }
            if err := f.Foo(tt.args.s); (err != nil) != tt.wantErr {
                t.Errorf("Foo.Foo() error = %v, wantErr %v", err, tt.wantErr)
            }
        })
    }
}
image

How To Fix

I will push a PR to fix this bug latter.