generator

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package generator deals with code generation.

Index

Constants

View Source
const ErrorMockNotFound errors.Const = "mock was not found"

ErrorMockNotFound may be returned if no mock was found.

Variables

This section is empty.

Functions

func GenerateForFunction

func GenerateForFunction(
	pkg, fn string,
	mockLookup MockLookup,
	msgsRenderer LoggingRenderer,
	opts ...Option,
) error

GenerateForFunction generate table tests for a function.

func GenerateForMethod

func GenerateForMethod(
	pkg, typ,
	method string,
	mockLookup MockLookup,
	msgsRenderer LoggingRenderer,
	opts ...Option,
) error

GenerateForMethod generates table test template and helpers for a method of a type.

func IsErrorPackageNotFound

func IsErrorPackageNotFound(err error) bool

IsErrorPackageNotFound tests an error to be ErrorPackageNotFound.

func WithMockContext

func WithMockContext(g *Generator, _ optionRestriction) error

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

func (g *Generator) LocalPackage(pkg string) (*types.Package, error)

LocalPackage to implement PackageProvider.

func (*Generator) Package

func (g *Generator) Package(pkg string) (*types.Package, error)

Package to implement PackageProvider.

type LoggingRenderer added in v0.0.2

type LoggingRenderer 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)
}

LoggingRenderer renders error messages. These variables:

t *testing.T
err error

are available in the generation scope to use.

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

type Option func(g *Generator, _ optionRestriction) error

Option generator option definition.

func WithCtxInit added in v0.0.2

func WithCtxInit(ctxinit func(r *gogh.GoRenderer[*gogh.Imports])) Option

WithCtxInit overrides default context.Context init rendering.

func WithMockerNames added in v0.0.2

func WithMockerNames(n func(tn *types.TypeName) (fileName string, typeName string)) Option

WithMockerNames lets to set a file and type names for a mocker of a given type.

func WithNoMock

func WithNoMock(pkg, name string) Option

WithNoMock adds a type which will not be mocked in tests.

func WithPreTest added in v0.0.2

func WithPreTest(pretest func(r *gogh.GoRenderer[*gogh.Imports])) Option

WithPreTest overrides default pretest renderer.

type PackageProvider

type PackageProvider interface {
	LocalPackage(path string) (*types.Package, error)
	Package(path string) (*types.Package, error)
}

PackageProvider this defines a source of packages for mock lookup.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL