...

Package generator

import "github.com/maxbrunsfeld/counterfeiter/generator"
Overview
Index

Overview ▾

type Fake

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

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 (*Fake) Generate

func (f *Fake) Generate(runImports bool) ([]byte, error)

Generate uses the Fake to generate an implementation, optionally running goimports on the output.

func (*Fake) IsFunction

func (f *Fake) IsFunction() bool

IsFunction indicates whether the fake is for a function..

func (*Fake) IsInterface

func (f *Fake) IsInterface() bool

IsInterface indicates whether the fake is for an interface.

type FakeMode

FakeMode indicates the type of Fake to generate.

type FakeMode int

FakeMode can be Interface, Function, or Package.

const (
    InterfaceOrFunction FakeMode = iota
    Package
)

type Import

Import is a package import with the associated alias for that package.

type Import struct {
    Alias   string
    PkgPath string
}

func (Import) 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.

type Imports

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 (*Imports) Add

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 (*Imports) AliasForPackage

func (i *Imports) AliasForPackage(p *types.Package) string

AliasForPackage returns a package alias for the package.

type Method

Method is a method of the interface.

type Method struct {
    Name    string
    Params  Params
    Returns Returns
}

type Param

Param is an argument to a function.

type Param struct {
    Name       string
    Type       string
    IsVariadic bool
    IsSlice    bool
}

type Params

Params is a slice of Param.

type Params []Param

func (Params) AsArgs

func (p Params) AsArgs() string

AsArgs builds a string that represents the parameters to a function as arguments to a function invocation.

func (Params) AsNamedArgs

func (p Params) AsNamedArgs() string

AsNamedArgs builds a string that represents parameters as named arguments.

func (Params) AsNamedArgsForInvocation

func (p Params) AsNamedArgsForInvocation() string

AsNamedArgsForInvocation builds a string that represents a function's arguments as required for invocation of the function.

func (Params) AsNamedArgsWithTypes

func (p Params) AsNamedArgsWithTypes() string

AsNamedArgsWithTypes builds a string that represents parameters as named arugments to a function, with associated types.

func (Params) AsReturnSignature

func (p Params) AsReturnSignature() string

AsReturnSignature builds a string representing signature for the params of a function.

func (Params) HasLength

func (p Params) HasLength() bool

HasLength returns true if there are params. It returns false if there are no params.

func (Params) Slices

func (p Params) Slices() Params

Slices returns those params that are a slice.

func (Params) WithPrefix

func (p Params) WithPrefix(prefix string) string

WithPrefix builds a string representing a functions parameters, and adds a prefix to each.

type Return

Return is the result of a method's invocation.

type Return struct {
    Name string
    Type string
}

type Returns

Returns is a slice of Return.

type Returns []Return

func (Returns) AsArgs

func (r Returns) AsArgs() string

AsArgs builds a string representing the arguments passed to a function.

func (Returns) AsNamedArgs

func (r Returns) AsNamedArgs() string

AsNamedArgs builds a string representing a function's named arguments.

func (Returns) AsNamedArgsWithTypes

func (r Returns) AsNamedArgsWithTypes() string

AsNamedArgsWithTypes builds a string representing a function's named arguments, with associated types.

func (Returns) AsReturnSignature

func (r Returns) AsReturnSignature() string

AsReturnSignature builds a string representing signature for the returns of a function.

func (Returns) HasLength

func (r Returns) HasLength() bool

HasLength is true if there are returns, else false.

func (Returns) WithPrefix

func (r Returns) WithPrefix(p string) string

WithPrefix builds a string representing the parameters returned from a function, and adds a prefix to each.