Comments (7)
I like this idea a lot, it basically takes a single function and creates the interface from it. Certainly would save one from the trouble of declaring an interface with an associated struct when they really just care about a stateless function.
Are you interested in submitting a PR for this @chendrix ?
from counterfeiter.
I tried looking at the codebase yesterday and I couldn't really make my way around it quickly.
I could certainly try putting in the effort, but it would probably take me more time than someone more familiar with the code.
from counterfeiter.
I just started looking into this and realized it needs some cleanup, some tests need to be backfilled, and there's certainly some care that could be applied to some of the existing abstractions.
I'm happy to work on this, just thought I'd ask :)
from counterfeiter.
Hey @chendrix -- I just implemented this and merged it into master. If you want to kick the tires on this, provide feedback, or identify anything else that would be nice to do here, I'd be really happy to hear your thoughts on it.
I basically turned your example code into a test case for the code generator in counterfeiter, and then verified it worked with a few simple throw-away specs. It should work, although I haven't tried using it exhaustively.
from counterfeiter.
Will check it out now, thanks Tim
from counterfeiter.
There's a bug.
Running the newest version created
// This file was generated by counterfeiter
package apifakes
import (
"net/http"
"sync"
"github.com/cloudfoundry-incubator/diego-enabler/api"
)
type FakeRequestFactory struct {
Stub func(Filter, map[string]interface{}) (*http.Request, error)
mutex sync.RWMutex
argsForCall []struct {
arg1 Filter
arg2 map[string]interface{}
}
returns struct {
result1 *http.Request
result2 error
}
}
func (fake *FakeRequestFactory) Spy(arg1 Filter, arg2 map[string]interface{}) (*http.Request, error) {
fake.mutex.Lock()
fake.argsForCall = append(fake.argsForCall, struct {
arg1 Filter
arg2 map[string]interface{}
}{arg1, arg2})
fake.mutex.Unlock()
if fake.Stub != nil {
return fake.Stub(arg1, arg2)
} else {
return fake.returns.result1, fake.returns.result2
}
}
func (fake *FakeRequestFactory) CallCount() int {
fake.mutex.RLock()
defer fake.mutex.RUnlock()
return len(fake.argsForCall)
}
func (fake *FakeRequestFactory) ArgsForCall(i int) (Filter, map[string]interface{}) {
fake.mutex.RLock()
defer fake.mutex.RUnlock()
return fake.argsForCall[i].arg1, fake.argsForCall[i].arg2
}
func (fake *FakeRequestFactory) Returns(result1 *http.Request, result2 error) {
fake.Stub = nil
fake.returns = struct {
result1 *http.Request
result2 error
}{result1, result2}
}
var _ api.RequestFactory = new(FakeRequestFactory).Spy
This actually doesn't compile, because the function definitions, e.g. func (fake *FakeRequestFactory) ArgsForCall(i int) (Filter, map[string]interface{}) {
are missing package qualification for types, i.e. it should be api.Filter
, not just Filter
, if you're not doing a dot import of the api
package.
from counterfeiter.
Okay, this only happens when the type is from the same package. Weird. Thanks for the feedback Chris. Will keep working on this.
from counterfeiter.
Related Issues (20)
- counterfeiter does not work via go generate if outside PATH HOT 10
- go list gives conflicting information for package HOT 1
- feature request: add flag to change generated fakes folder suffix HOT 1
- Cannot generate fakes for types that use aliased internal types HOT 7
- Avoid using `packages.Load` if possible
- Avoid fast-failing on unresolved imports that aren't used in the target interface
- Support overriding destination package name and type naming template in "-generate" call HOT 5
- Support for DoThingsCallsOnCall(callCount, stub ...) similar to DoThingsReturnsOnCall(callCount)
- How to use shorthand generate directive for external packages? HOT 1
- cannot find GOROOT directory HOT 1
- Go 1.18 generics HOT 8
- cannot write tests in package being tested due to import cycle
- [CVE-2020-11022] Update of counterfeiter/v6 needed HOT 2
- Homebrew: Does not complete test on Go 1.20rc1 HOT 4
- disable CircleCI HOT 2
- cannot use json.Marshal(h) (value of type []byte) as driver.Value value in return statement HOT 1
- Support for callback func stub? HOT 1
- Cannot regenerate fakes if they are relied upon and deleted - go1.19.10 HOT 1
- About the latest release HOT 6
- Allow setting destination package name independent of directory name
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from counterfeiter.