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

x/tools/gopls: code action for extracting into interface #46665

Open
js-sketch opened this issue Jun 9, 2021 · 1 comment
Open

x/tools/gopls: code action for extracting into interface #46665

js-sketch opened this issue Jun 9, 2021 · 1 comment

Comments

@js-sketch
Copy link

@js-sketch js-sketch commented Jun 9, 2021

Consider the following program:

type bar struct {}
func (b *bar) x() {}
func (b *bar) y() {}
type Foo struct {
    bar *bar
}
func (f *Foo) baz() {
    f.b.y()
}

Now assume we would like to test foo and for this we'd want to mock bar. The recommended way of doing this is to extract the way that bar is used into an interface and use that in lieu of bar in foo.

type bar struct {}
func (b *bar) x() {}
func (b *bar) y() {}

type barI interface {
    y()
}
type Foo struct {
    bar barI
}
func (f *Foo) baz() {
    f.b.y()
}

Note that only y() was extracted.
Supporting such a code action in gopls would make life a lot easier for devs who write unit tests.
This is at least viable to implement for unexported members, as no new methods can be added in other packages which access this member.

@gopherbot gopherbot added this to the Unreleased milestone Jun 9, 2021
@findleyr
Copy link
Contributor

@findleyr findleyr commented Jun 9, 2021

Thanks for the feature request. I agree that this could be a useful refactoring operation, and is certainly doable. It's also useful for more than just testing.

@findleyr findleyr removed this from the Unreleased milestone Jun 9, 2021
@findleyr findleyr added this to the gopls/unplanned milestone Jun 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants