Documentation
¶
Index ¶
- func Generate(config Config) error
- type ArgsMethodBuilder
- type Config
- type CountMethodBuilder
- type DeclarationBuilder
- type FieldBuilder
- type FileBuilder
- type GUIDFieldBuilder
- type GeneratorModel
- type MethodArgsFieldBuilder
- type MethodBuilder
- type MethodConfig
- func (s *MethodConfig) ArgsFieldName() string
- func (s *MethodConfig) ArgsFieldSelector() *ast.SelectorExpr
- func (s *MethodConfig) ArgsForCallMethodName() string
- func (s *MethodConfig) CallCountMethodName() string
- func (s *MethodConfig) HasParams() bool
- func (s *MethodConfig) HasResults() bool
- func (s *MethodConfig) MutexFieldName() string
- func (s *MethodConfig) MutexFieldSelector() *ast.SelectorExpr
- func (s *MethodConfig) ReturnsFieldName() string
- func (s *MethodConfig) ReturnsFieldSelector() *ast.SelectorExpr
- func (s *MethodConfig) ReturnsMethodName() string
- func (s *MethodConfig) StubFieldName() string
- func (s *MethodConfig) StubFieldSelector() *ast.SelectorExpr
- type MethodMutexFieldBuilder
- type MethodStubFieldBuilder
- type MutexActionBuilder
- type Resolver
- type ReturnsFieldBuilder
- type ReturnsMethodBuilder
- type StatementBuilder
- type StructBuilder
- type StubMethodBuilder
- func (b *StubMethodBuilder) Build() ast.Decl
- func (b *StubMethodBuilder) SetArgsFieldSelector(selector *ast.SelectorExpr)
- func (b *StubMethodBuilder) SetMutexFieldSelector(selector *ast.SelectorExpr)
- func (b *StubMethodBuilder) SetParams(params []*ast.Field)
- func (b *StubMethodBuilder) SetResults(results []*ast.Field)
- func (b *StubMethodBuilder) SetReturnsFieldSelector(selector *ast.SelectorExpr)
- func (b *StubMethodBuilder) SetStubFieldSelector(selector *ast.SelectorExpr)
- type StubToInterfaceStatementBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ArgsMethodBuilder ¶
type ArgsMethodBuilder struct {
// contains filtered or unexported fields
}
ArgsMethodBuilder is responsible for creating a method on the stub structure that allows you to check what arguments were used during a specific call on the stub method.
Example:
func (stub *StubStruct) SumArgsForCall(index int) (int, int) {
// ...
}
func NewArgsMethodBuilder ¶
func NewArgsMethodBuilder(methodBuilder *MethodBuilder) *ArgsMethodBuilder
func (*ArgsMethodBuilder) Build ¶
func (b *ArgsMethodBuilder) Build() ast.Decl
func (*ArgsMethodBuilder) SetArgsFieldSelector ¶
func (b *ArgsMethodBuilder) SetArgsFieldSelector(selector *ast.SelectorExpr)
func (*ArgsMethodBuilder) SetMutexFieldSelector ¶
func (b *ArgsMethodBuilder) SetMutexFieldSelector(selector *ast.SelectorExpr)
func (*ArgsMethodBuilder) SetParams ¶
func (b *ArgsMethodBuilder) SetParams(params []*ast.Field)
SetParams specifies the parameters that the original method uses. These parameters need to have been normalized and resolved in advance.
type Config ¶
type Config struct {
// SourcePackageLocation specifies the location
// (e.g. "github.com/mokiat/gostub") where the interface
// to be stubbed is located.
SourcePackageLocation string
// SourceInterfaceName specifies the name of the interface to be stubbed
SourceInterfaceName string
// TargetFilePath specifies the file in which the stub will be saved.
TargetFilePath string
// TargetPackageName specifies the name of the package in which the
// stub will be saved. Ideally, this should equal the last segment of
// the TargetPackageLocation (e.g. "gostub_stubs")
TargetPackageName string
// TargetStructName specifies the name of the stub structure
// that will implement the interface
TargetStructName string
}
Config is used to pass a rather large configuration to the Generate method.
type CountMethodBuilder ¶
type CountMethodBuilder struct {
// contains filtered or unexported fields
}
CountMethodBuilder is responsible for creating a method on the stub structure that allows you to check how many times the stubbed method was called.
Example:
func (stub *StubStruct) SumCallCount() int {
// ...
}
func NewCountMethodBuilder ¶
func NewCountMethodBuilder(methodBuilder *MethodBuilder) *CountMethodBuilder
func (*CountMethodBuilder) Build ¶
func (b *CountMethodBuilder) Build() ast.Decl
func (*CountMethodBuilder) SetArgsFieldSelector ¶
func (b *CountMethodBuilder) SetArgsFieldSelector(selector *ast.SelectorExpr)
func (*CountMethodBuilder) SetMutexFieldSelector ¶
func (b *CountMethodBuilder) SetMutexFieldSelector(selector *ast.SelectorExpr)
type DeclarationBuilder ¶ added in v1.1.0
type FieldBuilder ¶ added in v1.1.0
type FileBuilder ¶
type FileBuilder struct {
// contains filtered or unexported fields
}
func NewFileBuilder ¶
func NewFileBuilder() *FileBuilder
func (*FileBuilder) AddDeclarationBuilder ¶ added in v1.1.0
func (m *FileBuilder) AddDeclarationBuilder(builder DeclarationBuilder)
func (*FileBuilder) AddImport ¶
func (m *FileBuilder) AddImport(pkgName, location string) string
AddImport assures that the specified package name in the specified location will be added as an import. This function returns the alias to be used in selector expressions. If the specified location is already added, then just the alias for that package is returned.
func (*FileBuilder) Build ¶
func (m *FileBuilder) Build() *ast.File
func (*FileBuilder) SetPackage ¶
func (m *FileBuilder) SetPackage(name string)
type GUIDFieldBuilder ¶ added in v1.2.0
type GUIDFieldBuilder struct {
// contains filtered or unexported fields
}
The GUIDFieldBuilder is responsible for creating a field which can be used by end-users to force two stub instances not to be equal.
Example:
type StubStruct struct {
StubGUID int
// ...
}
func NewGUIDFieldBuilder ¶ added in v1.2.0
func NewGUIDFieldBuilder() *GUIDFieldBuilder
func (*GUIDFieldBuilder) Build ¶ added in v1.2.0
func (b *GUIDFieldBuilder) Build() *ast.Field
func (*GUIDFieldBuilder) SetFieldName ¶ added in v1.2.0
func (b *GUIDFieldBuilder) SetFieldName(name string)
type GeneratorModel ¶
type GeneratorModel struct {
// contains filtered or unexported fields
}
func NewGeneratorModel ¶
func NewGeneratorModel(pkgName, stubName string) *GeneratorModel
func (*GeneratorModel) AddImport ¶
func (t *GeneratorModel) AddImport(pkgName, location string) string
AddImport assures that the specified package name in the specified location will be added as an import. This function returns the alias to be used in selector expressions. If the specified location is already added, then just the alias for that package is returned.
func (*GeneratorModel) AddMethod ¶
func (t *GeneratorModel) AddMethod(config *MethodConfig) error
func (*GeneratorModel) AddStubAssignment ¶ added in v1.1.0
func (t *GeneratorModel) AddStubAssignment(interfaceLocation, interfaceName string)
func (*GeneratorModel) Save ¶
func (t *GeneratorModel) Save(filePath string) error
type MethodArgsFieldBuilder ¶
type MethodArgsFieldBuilder struct {
// contains filtered or unexported fields
}
The MethodArgsFieldBuilder is responsible for creating the field which is internally used to track the arguments that the end-user used to call a given method.
Example:
type StubStruct struct {
// ...
sumArgsForCall []struct {
name string,
age int,
}
// ...
}
func NewMethodArgsFieldBuilder ¶
func NewMethodArgsFieldBuilder() *MethodArgsFieldBuilder
func (*MethodArgsFieldBuilder) Build ¶
func (b *MethodArgsFieldBuilder) Build() *ast.Field
func (*MethodArgsFieldBuilder) SetFieldName ¶
func (b *MethodArgsFieldBuilder) SetFieldName(name string)
func (*MethodArgsFieldBuilder) SetParams ¶
func (b *MethodArgsFieldBuilder) SetParams(params []*ast.Field)
SetParams configures the parameters that the original method has. The parameters should have been normalized and resolved beforehand.
type MethodBuilder ¶
type MethodBuilder struct {
// contains filtered or unexported fields
}
func NewMethodBuilder ¶
func NewMethodBuilder() *MethodBuilder
func (*MethodBuilder) AddStatementBuilder ¶ added in v1.1.0
func (m *MethodBuilder) AddStatementBuilder(builder StatementBuilder)
func (*MethodBuilder) Build ¶
func (m *MethodBuilder) Build() ast.Decl
func (*MethodBuilder) SetName ¶
func (m *MethodBuilder) SetName(name string)
func (*MethodBuilder) SetReceiver ¶
func (m *MethodBuilder) SetReceiver(name, recType string)
func (*MethodBuilder) SetType ¶
func (m *MethodBuilder) SetType(funcType *ast.FuncType)
type MethodConfig ¶
type MethodConfig struct {
// MethodName specifies the name of the method as seen in the
// interface it came from.
MethodName string
// MethodParams specifies all the parameters of the method.
// They should have been normalized (i.e. no type reuse and no
// anonymous parameters) and resolved (i.e. all selector expressions
// resolved against the generated stub's new namespace)
MethodParams []*ast.Field
// MethodResults specifies all the results of the method.
// They should have been normalized (i.e. no type reuse and no
// anonymous results) and resolved (i.e. all selector expressions
// resolved against the generated stub's new namespace)
MethodResults []*ast.Field
}
MethodConfig provides the needed information for the generation of a stub implementation of a given method from an interface.
func (*MethodConfig) ArgsFieldName ¶
func (s *MethodConfig) ArgsFieldName() string
func (*MethodConfig) ArgsFieldSelector ¶
func (s *MethodConfig) ArgsFieldSelector() *ast.SelectorExpr
func (*MethodConfig) ArgsForCallMethodName ¶
func (s *MethodConfig) ArgsForCallMethodName() string
func (*MethodConfig) CallCountMethodName ¶
func (s *MethodConfig) CallCountMethodName() string
func (*MethodConfig) HasParams ¶
func (s *MethodConfig) HasParams() bool
func (*MethodConfig) HasResults ¶
func (s *MethodConfig) HasResults() bool
func (*MethodConfig) MutexFieldName ¶
func (s *MethodConfig) MutexFieldName() string
func (*MethodConfig) MutexFieldSelector ¶
func (s *MethodConfig) MutexFieldSelector() *ast.SelectorExpr
func (*MethodConfig) ReturnsFieldName ¶
func (s *MethodConfig) ReturnsFieldName() string
func (*MethodConfig) ReturnsFieldSelector ¶
func (s *MethodConfig) ReturnsFieldSelector() *ast.SelectorExpr
func (*MethodConfig) ReturnsMethodName ¶
func (s *MethodConfig) ReturnsMethodName() string
func (*MethodConfig) StubFieldName ¶
func (s *MethodConfig) StubFieldName() string
func (*MethodConfig) StubFieldSelector ¶
func (s *MethodConfig) StubFieldSelector() *ast.SelectorExpr
type MethodMutexFieldBuilder ¶
type MethodMutexFieldBuilder struct {
// contains filtered or unexported fields
}
The MethodMutexFieldBuilder is responsible for creating the field which is internally used to synchronize access to data related to a given method.
Example:
type StubStruct struct {
// ...
sumMutex sync.RWMutex
// ...
}
func NewMethodMutexFieldBuilder ¶
func NewMethodMutexFieldBuilder() *MethodMutexFieldBuilder
func (*MethodMutexFieldBuilder) Build ¶
func (b *MethodMutexFieldBuilder) Build() *ast.Field
func (*MethodMutexFieldBuilder) SetFieldName ¶
func (b *MethodMutexFieldBuilder) SetFieldName(name string)
func (*MethodMutexFieldBuilder) SetMutexType ¶
func (b *MethodMutexFieldBuilder) SetMutexType(mutexType ast.Expr)
SetMutexType is a way to configure the type of mutex to be used. The type should have already been resolved.
type MethodStubFieldBuilder ¶
type MethodStubFieldBuilder struct {
// contains filtered or unexported fields
}
The MethodStubFieldBuilder is responsible for creating the field which can be used by end-users to stub the behavior of a given method.
Example:
type StubStruct struct {
// ...
SumStub func(a int, b int) (c int)
// ...
}
func NewMethodStubFieldBuilder ¶
func NewMethodStubFieldBuilder() *MethodStubFieldBuilder
func (*MethodStubFieldBuilder) Build ¶
func (b *MethodStubFieldBuilder) Build() *ast.Field
func (*MethodStubFieldBuilder) SetFieldName ¶
func (b *MethodStubFieldBuilder) SetFieldName(name string)
func (*MethodStubFieldBuilder) SetParams ¶
func (b *MethodStubFieldBuilder) SetParams(params []*ast.Field)
SetParams configures the parameters of the stub's function type. The parameters should have been normalized and resolved before being passed to this function.
func (*MethodStubFieldBuilder) SetResults ¶
func (b *MethodStubFieldBuilder) SetResults(results []*ast.Field)
SetResults configures the results of the stub's function type. The results should have been normalized and resolved before being passed to this function.
type MutexActionBuilder ¶
type MutexActionBuilder struct {
// contains filtered or unexported fields
}
MutexActionBuilder is responsible for creating a statement for a mutex of a given stub method.
Example:
func (stub *StubStruct) SumCallCount() int {
// ...
stub.sumMutex.RLock()
// ...
}
func NewMutexActionBuilder ¶
func NewMutexActionBuilder() *MutexActionBuilder
func (*MutexActionBuilder) Build ¶
func (b *MutexActionBuilder) Build() ast.Stmt
func (*MutexActionBuilder) SetAction ¶
func (b *MutexActionBuilder) SetAction(action string)
func (*MutexActionBuilder) SetDeferred ¶
func (b *MutexActionBuilder) SetDeferred(deferred bool)
func (*MutexActionBuilder) SetMutexFieldSelector ¶
func (b *MutexActionBuilder) SetMutexFieldSelector(selector *ast.SelectorExpr)
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
func NewResolver ¶
func NewResolver(model *GeneratorModel, locator *resolution.Locator) *Resolver
func (*Resolver) ResolveType ¶
func (r *Resolver) ResolveType(context *resolution.LocatorContext, astType ast.Expr) (ast.Expr, error)
type ReturnsFieldBuilder ¶
type ReturnsFieldBuilder struct {
// contains filtered or unexported fields
}
The ReturnsFieldBuilder is responsible for creating the field which is used by end-users to specify a stub method's default return values.
Example:
type StubStruct struct {
// ...
addressReturns struct {
name string,
number int,
}
// ...
}
func NewReturnsFieldBuilder ¶
func NewReturnsFieldBuilder() *ReturnsFieldBuilder
func (*ReturnsFieldBuilder) Build ¶
func (b *ReturnsFieldBuilder) Build() *ast.Field
func (*ReturnsFieldBuilder) SetFieldName ¶
func (b *ReturnsFieldBuilder) SetFieldName(name string)
func (*ReturnsFieldBuilder) SetResults ¶
func (b *ReturnsFieldBuilder) SetResults(results []*ast.Field)
SetResults configures the results that the original method has. The results should have been normalized and resolved beforehand.
type ReturnsMethodBuilder ¶
type ReturnsMethodBuilder struct {
// contains filtered or unexported fields
}
ReturnsMethodBuilder is responsible for creating a method on the stub structure that allows you to specify the results to be returned by default when the stub method is called.
Example:
func (stub *StubStruct) AddressReturns(name string, number int) {
// ...
}
func NewReturnsMethodBuilder ¶
func NewReturnsMethodBuilder(methodBuilder *MethodBuilder) *ReturnsMethodBuilder
func (*ReturnsMethodBuilder) Build ¶
func (b *ReturnsMethodBuilder) Build() ast.Decl
func (*ReturnsMethodBuilder) SetMutexFieldSelector ¶
func (b *ReturnsMethodBuilder) SetMutexFieldSelector(selector *ast.SelectorExpr)
func (*ReturnsMethodBuilder) SetResults ¶
func (b *ReturnsMethodBuilder) SetResults(results []*ast.Field)
SetResults specifies the results that the original method uses. These results need to have been normalized and resolved in advance.
func (*ReturnsMethodBuilder) SetReturnsFieldSelector ¶
func (b *ReturnsMethodBuilder) SetReturnsFieldSelector(selector *ast.SelectorExpr)
type StatementBuilder ¶ added in v1.1.0
func StatementToBuilder ¶ added in v1.1.0
func StatementToBuilder(statement ast.Stmt) StatementBuilder
type StructBuilder ¶
type StructBuilder struct {
// contains filtered or unexported fields
}
func NewStructBuilder ¶
func NewStructBuilder() *StructBuilder
func (*StructBuilder) AddFieldBuilder ¶ added in v1.1.0
func (m *StructBuilder) AddFieldBuilder(field FieldBuilder)
func (*StructBuilder) Build ¶
func (m *StructBuilder) Build() ast.Decl
func (*StructBuilder) SetName ¶
func (m *StructBuilder) SetName(name string)
type StubMethodBuilder ¶
type StubMethodBuilder struct {
// contains filtered or unexported fields
}
StubMethodBuilder is responsible for creating a method that implements the original method from the interface and does all the tracking logic used by this framework.
Example:
func (stub *StubStruct) Sum(a int, b int) int {
// ...
}
func NewStubMethodBuilder ¶
func NewStubMethodBuilder(methodBuilder *MethodBuilder) *StubMethodBuilder
func (*StubMethodBuilder) Build ¶
func (b *StubMethodBuilder) Build() ast.Decl
func (*StubMethodBuilder) SetArgsFieldSelector ¶
func (b *StubMethodBuilder) SetArgsFieldSelector(selector *ast.SelectorExpr)
func (*StubMethodBuilder) SetMutexFieldSelector ¶
func (b *StubMethodBuilder) SetMutexFieldSelector(selector *ast.SelectorExpr)
func (*StubMethodBuilder) SetParams ¶
func (b *StubMethodBuilder) SetParams(params []*ast.Field)
SetParams specifies the parameters that the original method uses. These parameters need to have been normalized and resolved in advance.
func (*StubMethodBuilder) SetResults ¶
func (b *StubMethodBuilder) SetResults(results []*ast.Field)
SetResults specifies the results that the original method returns. These results need to have been normalized and resolved in advance.
func (*StubMethodBuilder) SetReturnsFieldSelector ¶
func (b *StubMethodBuilder) SetReturnsFieldSelector(selector *ast.SelectorExpr)
func (*StubMethodBuilder) SetStubFieldSelector ¶
func (b *StubMethodBuilder) SetStubFieldSelector(selector *ast.SelectorExpr)
type StubToInterfaceStatementBuilder ¶ added in v1.1.0
type StubToInterfaceStatementBuilder struct {
// contains filtered or unexported fields
}
func NewStubToInterfaceStatementBuilder ¶ added in v1.1.0
func NewStubToInterfaceStatementBuilder() *StubToInterfaceStatementBuilder
func (*StubToInterfaceStatementBuilder) Build ¶ added in v1.1.0
func (b *StubToInterfaceStatementBuilder) Build() ast.Decl
func (*StubToInterfaceStatementBuilder) SetInterfaceSelector ¶ added in v1.1.0
func (b *StubToInterfaceStatementBuilder) SetInterfaceSelector(selector *ast.SelectorExpr)
func (*StubToInterfaceStatementBuilder) SetStubName ¶ added in v1.1.0
func (b *StubToInterfaceStatementBuilder) SetStubName(name string)
Source Files
¶
- args_field_builder.go
- args_method_builder.go
- builder.go
- count_method_builder.go
- file_builder.go
- generator.go
- guid_field_builder.go
- method_builder.go
- model.go
- mutex_action_builder.go
- mutex_field_builder.go
- resolver.go
- returns_field_builder.go
- returns_method_builder.go
- struct_builder.go
- stub_field_builder.go
- stub_method_builder.go
- stub_to_interface_statement_builder.go