generator

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2024 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Overview

Package generator generates Moqueries mocks

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidConfig is returned when configuration values are invalid or
	// conflict with each other
	ErrInvalidConfig = errors.New("invalid configuration")
	// ErrUnknownFieldType is returned when a param or result list has an
	// unknown expression
	ErrUnknownFieldType = errors.New("unknown field type")
)
View Source
var ErrNonExported = errors.New("non-exported types")

ErrNonExported is returned by Generate when ExcludeNonExported is set to true resulting in an empty mock.

Functions

func Generate

func Generate(reqs ...GenerateRequest) error

Generate generates a moq

func GenerateWithTypeCache

func GenerateWithTypeCache(cache TypeCache, req GenerateRequest) error

GenerateWithTypeCache generates a single moq using the provided type cache. This function is exposed for use in bulk operations that have already loaded a type.

Types

type Converter

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

Converter converts various interface and function types to AST structs to build a moq

func NewConverter

func NewConverter(typ Type, isExported bool, typeCache TypeCache) *Converter

NewConverter creates a new Converter

func (*Converter) AssertMethod

func (c *Converter) AssertMethod() (*dst.FuncDecl, error)

AssertMethod generates a method to assert all expectations are met

func (*Converter) BaseDecls

func (c *Converter) BaseDecls() ([]dst.Decl, error)

BaseDecls generates the base declarations used to store the moq's state and establish the type

func (*Converter) FuncClosure

func (c *Converter) FuncClosure(fn Func) (*dst.FuncDecl, error)

FuncClosure generates a mock implementation of function type wrapped in a closure

func (*Converter) IsolationAccessor

func (c *Converter) IsolationAccessor(suffix, fnName string) (*dst.FuncDecl, error)

IsolationAccessor generates a function to access an isolation interface

func (*Converter) IsolationStruct

func (c *Converter) IsolationStruct(suffix string) (*dst.GenDecl, error)

IsolationStruct generates a struct used to isolate an interface for the moq

func (*Converter) MethodStructs

func (c *Converter) MethodStructs(fn Func) ([]dst.Decl, error)

MethodStructs generates a structure for storing a set of parameters or a set of results for a method invocation of a mock

func (*Converter) MockMethod

func (c *Converter) MockMethod(fn Func) (*dst.FuncDecl, error)

MockMethod generates a mock implementation of a method

func (*Converter) NewFunc

func (c *Converter) NewFunc() (*dst.FuncDecl, error)

NewFunc generates a function for constructing a moq

func (*Converter) RecorderMethods

func (c *Converter) RecorderMethods(fn Func) ([]dst.Decl, error)

RecorderMethods generates a recorder implementation of a method and associated return method

func (*Converter) ResetMethod

func (c *Converter) ResetMethod() (*dst.FuncDecl, error)

ResetMethod generates a method to reset the moq's state

type Converterer

type Converterer interface {
	BaseDecls() (baseDecls []dst.Decl, err error)
	IsolationStruct(suffix string) (structDecl *dst.GenDecl, err error)
	MethodStructs(fn Func) (structDecls []dst.Decl, err error)
	NewFunc() (funcDecl *dst.FuncDecl, err error)
	IsolationAccessor(suffix, fnName string) (funcDecl *dst.FuncDecl, err error)
	FuncClosure(fn Func) (funcDecl *dst.FuncDecl, err error)
	MockMethod(fn Func) (funcDecl *dst.FuncDecl, err error)
	RecorderMethods(fn Func) (funcDecls []dst.Decl, err error)
	ResetMethod() (funcDecl *dst.FuncDecl, err error)
	AssertMethod() (funcDecl *dst.FuncDecl, err error)
}

Converterer is the interface used by MoqGenerator to invoke a Converter

type Func

type Func struct {
	Name     string
	FuncType *dst.FuncType
}

Func holds on to function related data

type GenerateRequest

type GenerateRequest struct {
	Types          []string `json:"types"`
	Export         bool     `json:"export"`
	Destination    string   `json:"destination"`
	DestinationDir string   `json:"destination-dir"`
	Package        string   `json:"package"`
	Import         string   `json:"import"`
	TestImport     bool     `json:"test-import"`
	// WorkingDir is the current working directory. Optional, in which case
	// os.Getwd is used. Useful in cases where a request is serialized then
	// rerun in bulk processing from a different working directory. WorkingDir
	// is used for relative-path imports and relative path destination
	// files/directories.
	WorkingDir string `json:"working-dir"`
	// ExcludeNonExported causes the generator to exclude non-exported types
	// from the generated mock. This includes possibly returning ErrNonExported
	// if after exclusion, nothing is left to be written as a mock.
	ExcludeNonExported bool `json:"exclude-non-exported"`
}

GenerateRequest contains all the parameters needed to call Generate

type GetwdFunc

type GetwdFunc func() (string, error)

GetwdFunc is the signature of os.Getwd

type MoqGenerator

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

MoqGenerator generates moqs

func New

func New(
	typeCache TypeCache,
	getwdFn GetwdFunc,
	newConverterFn NewConverterFunc,
) *MoqGenerator

New returns a new MoqGenerator

func (*MoqGenerator) Generate

func (g *MoqGenerator) Generate(req GenerateRequest) (MoqResponse, error)

Generate generates moqs for the given types in the given destination package

type MoqResponse

type MoqResponse struct {
	File       *dst.File
	DestPath   string
	OutPkgPath string
}

MoqResponse contains the results of generating a mock

type NewConverterFunc

type NewConverterFunc func(typ Type, export bool) Converterer

NewConverterFunc creates a new converter for a specific type

type Type

type Type struct {
	TypeInfo   ast.TypeInfo
	Funcs      []Func
	OutPkgPath string
	Reduced    bool
}

Type describes the type being converted into a mock

type TypeCache

type TypeCache interface {
	Type(id dst.Ident, contextPkg string, testImport bool) (ast.TypeInfo, error)
	IsComparable(expr dst.Expr) (bool, error)
	IsDefaultComparable(expr dst.Expr) (bool, error)
	FindPackage(dir string) (string, error)
}

TypeCache defines the interface to the Cache type

Directories

Path Synopsis
Package testmoqs contains multiple test mocks and adaptors for use in unit testing
Package testmoqs contains multiple test mocks and adaptors for use in unit testing

Jump to

Keyboard shortcuts

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