mock

package
v0.7.1-pre2 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2024 License: Unlicense Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnexported = errors.New("must be exported")

Functions

This section is empty.

Types

type Import

type Import struct {
	Name string
	Path string
}

type Method

type Method struct {
	// contains filtered or unexported fields
}

Method represents a method that is being mocked.

func MethodFor

func MethodFor(ctx context.Context, receiver Mock, name string, typ *ast.FuncType) Method

MethodFor returns a Method representing typ, using receiver as the Method's receiver type and name as the method name.

func (Method) Ast

func (m Method) Ast(ctx context.Context) *ast.FuncDecl

Ast returns the ast representation of m.

func (Method) Field added in v0.7.0

func (m Method) Field(ctx context.Context) *ast.Field

Field returns the field that needs to be a part of the method struct for recording calls to this method.

func (Method) FieldInit added in v0.7.0

func (m Method) FieldInit(ctx context.Context, buffer int) *ast.AssignStmt

func (Method) In added in v0.7.0

func (m Method) In(ctx context.Context) *ast.TypeSpec

In returns the input struct for reporting method calls to m.

func (Method) Out added in v0.7.0

func (m Method) Out(ctx context.Context) *ast.TypeSpec

Out returns the output struct for enqueueing method returns to m.

func (Method) PrependLocalPackage

func (m Method) PrependLocalPackage(ctx context.Context, name string) (bool, error)

PrependLocalPackage prepends name as the package name for local types in m's signature. This is most often used when mocking types that are imported by the local package.

func (Method) Wrapper added in v0.7.0

func (m Method) Wrapper(ctx context.Context) *ast.TypeSpec

Wrapper returns the spec to wrap vegr.Method[inType, outType] as a local type.

type Mock

type Mock struct {
	// contains filtered or unexported fields
}

Mock is a mock of an interface type.

func For

func For(ctx context.Context, name string, params []*ast.Field, typ *ast.InterfaceType) (Mock, error)

For returns a Mock representing typ. An error will be returned if a mock cannot be created from typ.

func (Mock) Ast

func (m Mock) Ast(ctx context.Context, chanSize int) []ast.Decl

Ast returns all declaration AST for m.

func (Mock) Constructor

func (m Mock) Constructor(ctx context.Context, chanSize int) *ast.FuncDecl

Constructor returns a function AST to construct m. chanSize will be the buffer size for all channels initialized in the constructor.

func (Mock) Methods

func (m Mock) Methods(ctx context.Context) (methods []Method)

Methods returns the methods that need to be created with m as a receiver.

func (Mock) Name

func (m Mock) Name(ctx context.Context) string

Name returns the type name for m.

func (Mock) PrependLocalPackage

func (m Mock) PrependLocalPackage(ctx context.Context, name string) (bool, error)

PrependLocalPackage prepends name as the package name for local types in m's signature. This is most often used when mocking types that are imported by the local package.

If PrependLocalPackage returns true, then the package selector was required by one of the mock methods, meaning it must be imported in the resulting mock code.

func (Mock) StructDecls added in v0.7.0

func (m Mock) StructDecls(ctx context.Context) []ast.Decl

Decl returns the declaration AST for m.

type Mocks

type Mocks struct {
	// contains filtered or unexported fields
}

Mocks is a set of mocks generated from a go package.

func Generate

func Generate(ctx context.Context, finder TypeFinder, opts ...Opt) (*Mocks, error)

Generate generates a Mocks value for all interface types that are required, according to finder.

func (*Mocks) Imports

func (m *Mocks) Imports(ctx context.Context) []Import

func (*Mocks) Index

func (m *Mocks) Index(ctx context.Context, i int) Mock

func (*Mocks) Len

func (m *Mocks) Len(ctx context.Context) int

func (*Mocks) Output

func (m *Mocks) Output(ctx context.Context, pkg, dir string, chanSize int, dest io.Writer) error

Output writes the go code representing m to dest. pkg will be the package name; dir is the destination directory (needed for formatting the file); chanSize is the buffer size of any channels created in constructors.

func (*Mocks) PrependLocalPackage

func (m *Mocks) PrependLocalPackage(ctx context.Context, name, import_ string)

PrependLocalPackage prepends name as the package name for local types in m's signature. This is most often used when mocking types that are imported by the local package.

type Opt

type Opt func(genPrefs) genPrefs

Opt is an option function that can apply preferences for generating mocks.

func ForConcreteDependencies

func ForConcreteDependencies() Opt

ForConcreteDependencies tells Generate to generate mocks for any interface types that are depended on by functions, methods, or struct fields in exported code.

If none of the For* options are included, the default will be to generate mocks for all strategies.

func ForLocalInterfaces

func ForLocalInterfaces() Opt

ForLocalInterfaces tells Generate to generate mocks for local interface types.

If none of the For* options are included, the default will be to generate mocks for all strategies.

func WithLocalPkg

func WithLocalPkg(name, path string) Opt

WithLocalPkg tells Generate to use the provided package name and import path to import local types.

type TypeFinder

type TypeFinder interface {
	Decls(context.Context) []ast.Decl
	Dependencies(ctx context.Context, name string) []*dep.Dependency
}

TypeFinder represents a type which knows about types and dependencies.

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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