Fake is used to generate a Fake implementation of an interface.
type Fake struct { Packages []*packages.Package Package *packages.Package Target *types.TypeName Mode FakeMode DestinationPackage string Name string TargetAlias string TargetName string TargetPackage string Imports Imports Methods []Method Function Method }
func NewFake(fakeMode FakeMode, targetName string, packagePath string, fakeName string, destinationPackage string, workingDir string) (*Fake, error)
NewFake returns a Fake that loads the package and finds the interface or the function.
func (f *Fake) Generate(runImports bool) ([]byte, error)
Generate uses the Fake to generate an implementation, optionally running goimports on the output.
func (f *Fake) IsFunction() bool
IsFunction indicates whether the fake is for a function..
func (f *Fake) IsInterface() bool
IsInterface indicates whether the fake is for an interface.
FakeMode indicates the type of Fake to generate.
type FakeMode int
FakeMode can be Interface, Function, or Package.
const ( InterfaceOrFunction FakeMode = iota Package )
Import is a package import with the associated alias for that package.
type Import struct { Alias string PkgPath string }
func (i Import) String() string
String returns a string that may be used as an import line in a go source file. Imports with aliases that match the package basename are printed without an alias.
Imports indexes imports by package path and alias so that all imports have a unique alias, and no package is included twice.
type Imports struct { ByAlias map[string]Import ByPkgPath map[string]Import }
func (i *Imports) Add(alias string, path string) Import
Add creates an import with the given alias and path, and adds it to Fake.Imports.
func (i *Imports) AliasForPackage(p *types.Package) string
AliasForPackage returns a package alias for the package.
Method is a method of the interface.
type Method struct { Name string Params Params Returns Returns }
Param is an argument to a function.
type Param struct { Name string Type string IsVariadic bool IsSlice bool }
Params is a slice of Param.
type Params []Param
func (p Params) AsArgs() string
AsArgs builds a string that represents the parameters to a function as arguments to a function invocation.
func (p Params) AsNamedArgs() string
AsNamedArgs builds a string that represents parameters as named arguments.
func (p Params) AsNamedArgsForInvocation() string
AsNamedArgsForInvocation builds a string that represents a function's arguments as required for invocation of the function.
func (p Params) AsNamedArgsWithTypes() string
AsNamedArgsWithTypes builds a string that represents parameters as named arugments to a function, with associated types.
func (p Params) AsReturnSignature() string
AsReturnSignature builds a string representing signature for the params of a function.
func (p Params) HasLength() bool
HasLength returns true if there are params. It returns false if there are no params.
func (p Params) Slices() Params
Slices returns those params that are a slice.
func (p Params) WithPrefix(prefix string) string
WithPrefix builds a string representing a functions parameters, and adds a prefix to each.
Return is the result of a method's invocation.
type Return struct { Name string Type string }
Returns is a slice of Return.
type Returns []Return
func (r Returns) AsArgs() string
AsArgs builds a string representing the arguments passed to a function.
func (r Returns) AsNamedArgs() string
AsNamedArgs builds a string representing a function's named arguments.
func (r Returns) AsNamedArgsWithTypes() string
AsNamedArgsWithTypes builds a string representing a function's named arguments, with associated types.
func (r Returns) AsReturnSignature() string
AsReturnSignature builds a string representing signature for the returns of a function.
func (r Returns) HasLength() bool
HasLength is true if there are returns, else false.
func (r Returns) WithPrefix(p string) string
WithPrefix builds a string representing the parameters returned from a function, and adds a prefix to each.