Documentation
¶
Overview ¶
Package generator deals with code generation.
Index ¶
- Constants
- func GenerateForFunction(pkg, fn string, mockLookup MockLookup, opts ...Option) error
- func GenerateForMethod(pkg, typ, method string, mockLookup MockLookup, opts ...Option) error
- func IsErrorPackageNotFound(err error) bool
- func WithMockContext(g *Generator, _ optionRestriction) error
- type ErrorPackageNotFound
- type Generator
- type MessagesRenderer
- type MockLookup
- type MockLookupResult
- type Option
- type PackageProvider
Constants ¶
const ErrorMockNotFound errors.Const = "mock was not found"
ErrorMockNotFound may be returned if no mock was found.
const ( PackageLoadMode = packages.NeedImports | packages.NeedTypes | packages.NeedName | packages.NeedDeps | packages.NeedSyntax | packages.NeedFiles | packages.NeedModule | packages.NeedSyntax )
Variables ¶
This section is empty.
Functions ¶
func GenerateForFunction ¶
func GenerateForFunction(pkg, fn string, mockLookup MockLookup, opts ...Option) error
GenerateForFunction generate table tests for a function.
func GenerateForMethod ¶
func GenerateForMethod(pkg, typ, method string, mockLookup MockLookup, opts ...Option) error
GenerateForMethod generates table test template and helpers for a method of a type.
func IsErrorPackageNotFound ¶
IsErrorPackageNotFound tests an error to be ErrorPackageNotFound.
func WithMockContext ¶
WithMockContext context.Context is not required to have a mock by default. This enables context mock requirement.
Types ¶
type ErrorPackageNotFound ¶
type ErrorPackageNotFound struct {
// contains filtered or unexported fields
}
ErrorPackageNotFound package not found in the current project.
func (ErrorPackageNotFound) Error ¶
func (e ErrorPackageNotFound) Error() string
func (ErrorPackageNotFound) Is ¶
func (e ErrorPackageNotFound) Is(err error) bool
Is to support custom handling for errors.Is.
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator a facility for code processing and generation.
func (*Generator) LocalPackage ¶
LocalPackage to implement PackageProvider.
type MessagesRenderer ¶
type MessagesRenderer interface {
// ExpectedError is used to print expected error err.
ExpectedError(r *gogh.GoRenderer[*gogh.Imports])
// UnexpectedError used when error err was not expected.
UnexpectedError(r *gogh.GoRenderer[*gogh.Imports])
// ErrorWasExpected prints a message about missing error what was expected.
ErrorWasExpected(r *gogh.GoRenderer[*gogh.Imports])
// InvalidError prints a message about an invalid error value.
InvalidError(r *gogh.GoRenderer[*gogh.Imports], errvar string)
}
MessagesRenderer this prints error processing messages.
type MockLookup ¶
type MockLookup func(p PackageProvider, t *types.Named) (MockLookupResult, error)
MockLookup is a definition of mock lookup function provided by the user.
func StdMockLookup ¶
func StdMockLookup(altPaths []string, template string, custom map[string]string) MockLookup
StdMockLookup is a lookup function that should work for Google's mockgen and pamgen mock generators.
- altPaths is a list of package paths to look in if no mock was found in object's own package.
- template is a template for mock type name based on the type name. Will look be "Mock${type}" for mockgen and "${type|P}Mock" for pamgen. P is the formatting option to translate original type name into the public one, `pamgen` always translates mock names into public form.
- custom map can specify mock type names for certain types.
It looks for a mock type in the given type's package first, then move to altPaths provided if no match was found. These criteria must be satisfied:
- The mock type name must be equal to template with type name applied to it.
- The mock type must implement the given type (it is an interface).
- There should be a function NewXXX(*gomock.Controller) *XXX in the package, where XXX is a mock type name.
type MockLookupResult ¶
type MockLookupResult struct {
Name string
Named *types.Named
Type *types.Struct
Constructor *types.Func
}
MockLookupResult a result of mock lookup.
type Option ¶
Option generator option definition.
func WithMockerFileName ¶
WithMockerFileName lets to set a file name for a mocker of a given type.
func WithNoMock ¶
WithNoMock adds a type which will not be mocked in tests.
func WithRenderers ¶
func WithRenderers( preTest func(r *gogh.GoRenderer[*gogh.Imports]), ctxInit func(r *gogh.GoRenderer[*gogh.Imports]), messager MessagesRenderer, ) Option
WithRenderers defines custom renderers for test processing logic.