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 to extracting an interface for usage #46665

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

x/tools/gopls: code action to extracting an interface for usage #46665

js-sketch opened this issue Jun 9, 2021 · 1 comment
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.

Comments

@js-sketch
Copy link

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 Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Jun 9, 2021
@gopherbot gopherbot added this to the Unreleased milestone Jun 9, 2021
@findleyr
Copy link
Contributor

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 modified the milestones: Unreleased, gopls/unplanned Jun 9, 2021
@adonovan adonovan added the Refactoring Issues related to refactoring tools label Apr 24, 2023
@findleyr findleyr changed the title x/tools/gopls: code action for extracting into interface x/tools/gopls: code action to extracting an interface for usage Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants