unittest

package
v0.0.0-...-272d833 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CalledExpr

type CalledExpr struct {
	*ast.CallExpr
	// contains filtered or unexported fields
}

CalledExpr is a extension of `ast.CallExpr`

func (*CalledExpr) FieldName

func (c *CalledExpr) FieldName() string

FieldName returns the filed name of the `CalledExpr` in the struct receiver

func (*CalledExpr) GetArgsReturns

func (c *CalledExpr) GetArgsReturns() (args, returns []string, err error)

GetArgsReturns return the args and result of the `CalledExpr`

func (*CalledExpr) Name

func (c *CalledExpr) Name() string

Name get the name of called

func (*CalledExpr) ParseIntfToArgsAndReturns

func (c *CalledExpr) ParseIntfToArgsAndReturns(itf *types.Interface, fnName string) (args, returns []string)

type CalledMethod

type CalledMethod struct {
	FieldName     string
	MethodName    string
	Args, Results []string
}

type Func

type Func struct {
	PkgDecls  []ast.Decl
	File      *ast.File
	Struct    *ast.StructType
	Signature *ast.FuncDecl
	PkgPath   string
}

Func is a wrapper around ast.FuncDecl containing few methods to use within a test template

func GetFunctionsFromFile

func GetFunctionsFromFile(file *ast.File, pkgPath string) []*Func

func NewFunc

func NewFunc(
	sig *ast.FuncDecl,
	str *ast.StructType,
	file *ast.File,
	pkgDecls []ast.Decl,
	pkgPath string) *Func

NewFunc returns pointer to the Func struct

func (*Func) Fields

func (f *Func) Fields(fs *token.FileSet) []string

Fields a list of the function fields with their types

func (*Func) FindCalledMethods

func (f *Func) FindCalledMethods() (fns []*CalledExpr)

FindCalledMethods find all called method inside the function body

func (*Func) HasMockableField

func (f *Func) HasMockableField(fields []ReceiverField) bool

func (*Func) HasReceiverFields

func (f *Func) HasReceiverFields() bool

func (*Func) IsMethod

func (f *Func) IsMethod() bool

IsMethod returns true if the function is a method

func (*Func) IsVariadic

func (f *Func) IsVariadic() bool

IsVariadic returns true if it's the variadic function

func (*Func) LastParam

func (f *Func) LastParam() *ast.Field

LastParam returns function's last param

func (*Func) LastResult

func (f *Func) LastResult() *ast.Field

LastResult returns function's last result

func (*Func) Name

func (f *Func) Name() string

Name returns a name of func

func (*Func) NumParams

func (f *Func) NumParams() int

NumParams returns a number of the function params

func (*Func) NumResults

func (f *Func) NumResults() int

NumResults returns a number of the function results

func (*Func) Params

func (f *Func) Params(fs *token.FileSet) []string

Params returns a list of the function params with their types

func (*Func) ParamsNames

func (f *Func) ParamsNames() []string

ParamsNames returns a list of the function params' names

func (*Func) ParseFieldsToNodeString

func (f *Func) ParseFieldsToNodeString(
	fs *token.FileSet,
	ident *ast.Ident,
	fields []*ast.Field,
	field *ast.Field,
	index int) (nodeString string)

func (*Func) ParseStarExprToReceiverField

func (f *Func) ParseStarExprToReceiverField(se *ast.StarExpr) []ReceiverField

func (*Func) ReceiverFields

func (f *Func) ReceiverFields() []ReceiverField

func (*Func) ReceiverInstance

func (f *Func) ReceiverInstance() string

ReceiverInstance returns a instance of the method receiver

func (*Func) ReceiverType

func (f *Func) ReceiverType() ast.Expr

ReceiverType returns a type of the method receiver

func (*Func) Results

func (f *Func) Results(fs *token.FileSet) []string

Results returns a list of the function results with their types if function's last param is an error it is not included in the result slice

func (*Func) ResultsNames

func (f *Func) ResultsNames() []string

ResultsNames returns a list of the function results' names if function's last result is an error the name of param is "err"

func (*Func) ReturnsError

func (f *Func) ReturnsError() bool

ReturnsError returns true if the function's last param's type is error

func (*Func) TestName

func (f *Func) TestName() string

TestName returns a name of the test

type Generator

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

Generator is used to generate a test stub for function Func

func NewGenerator

func NewGenerator(opt Options) *Generator

NewGenerator returns a pointer to Generator

func (*Generator) Source

func (g *Generator) Source() string

func (*Generator) Write

func (g *Generator) Write(w io.Writer) error

func (*Generator) WriteHeader

func (g *Generator) WriteHeader(w io.Writer) error

WriteHeader writes a package name and import specs

func (*Generator) WriteTests

func (g *Generator) WriteTests(w io.Writer) error

WriteTests writes test stubs for every function that don't have test yet

type Options

type Options struct {
	Comment        string
	Template       string
	HeaderTemplate string
	PackageName    string
	PkgPath        string

	Funcs []*Func
}

type ReceiverField

type ReceiverField struct {
	CalledMethods []*CalledExpr
	Field         *ast.Field
	PkgPath       string
	Name          string
	StructName    string
	Mockable      bool
	IsCalled      bool
}

func NewReceiverField

func NewReceiverField(
	name,
	pkgPath string,
	fd *ast.Field,
	calledMethods []*CalledExpr,
) ReceiverField

func (*ReceiverField) IsFieldMockable

func (r *ReceiverField) IsFieldMockable() bool

func (*ReceiverField) IsIdentMockable

func (r *ReceiverField) IsIdentMockable(ident *ast.Ident) bool

func (*ReceiverField) IsNameCalled

func (r *ReceiverField) IsNameCalled() bool

func (*ReceiverField) IsSelectorExprMockable

func (r *ReceiverField) IsSelectorExprMockable(expr *ast.SelectorExpr) bool

func (*ReceiverField) Set

func (r *ReceiverField) Set()

func (*ReceiverField) SetForArray

func (r *ReceiverField) SetForArray(typ ast.Expr)

type TemplateData

type TemplateData struct {
	Comment       string
	Func          *Func
	CalledMethods []CalledMethod
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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