testcase

package
v0.0.5-alpha Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2022 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package testcase contains functionality for generating all necessary information in order to create test cases

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BasicLitString

func BasicLitString(val string) ast.Expr

BasicLitString creates an ast string expression from value

func CreatePrintfStmt

func CreatePrintfStmt(args []ast.Expr) ast.Stmt

CreatePrintfStmt creates sprintf statement from a list of expressions

func MustPrettyPrintElement

func MustPrettyPrintElement(e interface{}) string

MustPrettyPrintElement pretty prints an AST element or panics

func PrettyPrintElement

func PrettyPrintElement(e interface{}) (string, error)

PrettyPrintElement pretty prints an AST element

func Println

func Println() ast.Stmt

Println creates println statement

func PrintlnVal

func PrintlnVal(val string) ast.Stmt

PrintlnVal create println statement for input value

Types

type CycleInfo

type CycleInfo struct {
	Structs        map[string]int
	StructByStruct map[*ast.StructType]int
	Interfaces     map[*ast.InterfaceType]int
	StructMem      map[*ast.StructType]ast.Expr
	InterfaceMem   map[*ast.InterfaceType]ast.Decl
}

CycleInfo stores information about possible cycles in parsed code

func FreshCycleInfo

func FreshCycleInfo() CycleInfo

FreshCycleInfo creates a fresh cycle info struct

type DuplMapChecker

type DuplMapChecker struct {
	StructTypeMap    map[ast.StructType]bool
	IdentMap         map[ast.Ident]bool
	BasicLitMap      map[ast.BasicLit]bool
	StarExprMap      map[ast.StarExpr]bool
	ArrayTypeMap     map[ast.ArrayType]bool
	MapTypeMap       map[ast.MapType]bool
	ChanTypeMap      map[ast.ChanType]bool
	FuncTypeMap      map[ast.FuncType]bool
	InterfaceTypeMap map[ast.InterfaceType]bool
	SelectorExprMap  map[ast.SelectorExpr]bool
	EllipsisMap      map[ast.Ellipsis]bool
	FuncLitMap       map[ast.FuncLit]bool
}

DuplMapChecker checker which identifies duplicate map keys

func NewDuplMapChecker

func NewDuplMapChecker() *DuplMapChecker

NewDuplMapChecker creates a new dupl map checker

func (*DuplMapChecker) IsDuplExpr

func (d *DuplMapChecker) IsDuplExpr(e ast.Expr) bool

IsDuplExpr checks if we are dealing with a duplicate map key expression

type Dynamic

type Dynamic struct {
	CanGenInterface map[string]bool
}

Dynamic struct for performance improvements using dynamic programming

type FieldToAssignRes

type FieldToAssignRes struct {
	Idents       []*ast.Ident
	Statements   []ast.Stmt
	Declarations []ast.Decl
	// FIXME:
	ChanIdents []*ast.Ident
}

FieldToAssignRes result for converting func fields to assign stmts

func (*FieldToAssignRes) Append

func (res *FieldToAssignRes) Append(idents, chanIdents []*ast.Ident, stmts []ast.Stmt, decls []ast.Decl)

Append appends idents, statements and declarations to a fieldToAssignRes

func (*FieldToAssignRes) AppendRes

func (res *FieldToAssignRes) AppendRes(otherRes *FieldToAssignRes)

AppendRes appends other into this

type Options

type Options struct {
	ValTestCase  values.IGen
	VarTestCase  variables.IGen
	IdentGen     ident.IGen
	MaxRecursion int
}

Options test case generation options

type PrintRecursionInput

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

PrintRecursionInput input object for traversing the AST

func NewPrintRecursionInput

func NewPrintRecursionInput(valType ast.Expr, identifierName string, pointer *importer.PkgResolverPointer) *PrintRecursionInput

NewPrintRecursionInput creates new recursion input

type PrintResult

type PrintResult struct {
	Stmts []ast.Stmt
}

PrintResult result of recursion

type RecursionInput

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

RecursionInput input object for traversing the AST

func NewRecursionInput

func NewRecursionInput(valType ast.Expr, identifierName string, pointer *importer.PkgResolverPointer, ident *ast.Ident) *RecursionInput

NewRecursionInput creates new recursion input

func NewRecursionInputWithExpr

func NewRecursionInputWithExpr(e ast.Expr, input *RecursionInput) *RecursionInput

NewRecursionInputWithExpr helper function for creating new recursion inputs

func (*RecursionInput) Copy

func (r *RecursionInput) Copy() *RecursionInput

Copy copies recursion input with a fresh cycle info

type TestCase

type TestCase struct {
	// Properties used for generating mutations when doing crossover in evolution
	FuncDecl    *ast.FuncDecl
	Pointer     *importer.PkgResolverPointer
	PackageInfo *importer.PackageInfo
	Opts        Options
	Deco        *decorator.Deco
	Dynamic     Dynamic
	RunTimeInfo *runtime.Info

	// Properties used to create value stmts in test cases
	Decls      []string
	Stmts      []string
	FuncStmt   string
	ChanIdents []string
	// Properties used for creating assert stmts in test cases
	ResultStmts      []string
	ResultUsageStmts []string
	FuncPrintStmt    string
}

TestCase contains all information for generating a test case

func New

func New(f *ast.FuncDecl,
	pointer *importer.PkgResolverPointer,
	pkgInfo *importer.PackageInfo,
	opts Options,
	decorator *decorator.Deco) *TestCase

New creates a new test case

func (*TestCase) ArrayExprToPrintStmt

func (g *TestCase) ArrayExprToPrintStmt(t *ast.ArrayType, input *PrintRecursionInput) *PrintResult

ArrayExprToPrintStmt converts an array type to print statements

func (*TestCase) ArrayExprToValExpr

func (g *TestCase) ArrayExprToValExpr(input *RecursionInput) *TypeExprToValExprRes

ArrayExprToValExpr convert a array expression to value expression

func (*TestCase) BasicExprToPrintStmt

func (g *TestCase) BasicExprToPrintStmt(input *PrintRecursionInput, typeIdentifier, varIdentifier string) *PrintResult

BasicExprToPrintStmt converts a basic identifier to a print stmt

func (*TestCase) BasicExprToValExpr

func (g *TestCase) BasicExprToValExpr(identifier string) ast.Expr

BasicExprToValExpr converts a basic identifier to an expression

func (*TestCase) ChanTypeToValExpr

func (g *TestCase) ChanTypeToValExpr(t *ast.ChanType, input *RecursionInput) *TypeExprToValExprRes

ChanTypeToValExpr converts a chan type to a value expression

func (*TestCase) CheckIfCanGenExpr

func (g *TestCase) CheckIfCanGenExpr(input *RecursionInput) bool

CheckIfCanGenExpr recurse interface expression and discover if we find any non exported expressions

func (*TestCase) CheckIfCanGenFunc

func (g *TestCase) CheckIfCanGenFunc(f *ast.FuncType, input *RecursionInput) bool

CheckIfCanGenFunc check if we are able to gen func implementation

func (*TestCase) CheckIfCanGenInterface

func (g *TestCase) CheckIfCanGenInterface(t *ast.InterfaceType, input *RecursionInput) bool

CheckIfCanGenInterface if any non exported values are present in an interface, we need to return it as nil

func (*TestCase) CorrectTypeExpr

func (g *TestCase) CorrectTypeExpr(e ast.Expr, input *RecursionInput) ast.Expr

CorrectTypeExpr corrects type expressions for imports

func (*TestCase) Create

func (g *TestCase) Create()

Create converts a function declaration to a list of assignment statements and declaration statements

func (*TestCase) ErrExprToPrintStmt

func (g *TestCase) ErrExprToPrintStmt(input *PrintRecursionInput) *PrintResult

ErrExprToPrintStmt converts err expression to print statement

func (*TestCase) ErrExprToValExpr

func (g *TestCase) ErrExprToValExpr() *TypeExprToValExprRes

ErrExprToValExpr converts error expression to val expression

func (*TestCase) FieldToAssignStmt

func (g *TestCase) FieldToAssignStmt(p *ast.Field, funcName string, pointer *importer.PkgResolverPointer) *FieldToAssignRes

FieldToAssignStmt converts a function parameter to an assignment field

func (*TestCase) FieldToAssignStmts

func (g *TestCase) FieldToAssignStmts(params *ast.FieldList, funcName string, pointer *importer.PkgResolverPointer) *FieldToAssignRes

FieldToAssignStmts converts a parameter to assignment statements

func (*TestCase) FieldToPrintStmt

func (g *TestCase) FieldToPrintStmt(field *ast.Field, funcName string, pointer *importer.PkgResolverPointer) ([]ast.Expr, *PrintResult)

FieldToPrintStmt convert field to print statement

func (*TestCase) FuncDeclToExprStmt

func (g *TestCase) FuncDeclToExprStmt(f *ast.FuncDecl, recvIdent, paramIdent []*ast.Ident, printIdents []ast.Expr) (ast.Stmt, ast.Stmt)

FuncDeclToExprStmt converts func declaration to expression statement

func (*TestCase) FuncNilFunc

func (g *TestCase) FuncNilFunc(t ast.Expr, input *RecursionInput) *TypeExprToValExprRes

FuncNilFunc creates a nil func

func (*TestCase) FuncReturnListToBodyStatements

func (g *TestCase) FuncReturnListToBodyStatements(input *RecursionInput) *TypeExprToValExprRes

FuncReturnListToBodyStatements converts a return list of function to statements return values

func (*TestCase) FuncTypeToValExpr

func (g *TestCase) FuncTypeToValExpr(input *RecursionInput) *TypeExprToValExprRes

FuncTypeToValExpr converts a func type to a value expression

func (*TestCase) GetFuncReceiverStmts

func (g *TestCase) GetFuncReceiverStmts(recv *ast.FieldList, funcName string, pointer *importer.PkgResolverPointer) *FieldToAssignRes

GetFuncReceiverStmts retrieve statements for receiverr field

func (*TestCase) GetUnnamedStructIdent

func (g *TestCase) GetUnnamedStructIdent(fieldType ast.Expr, input *RecursionInput) *ast.Ident

GetUnnamedStructIdent retrieves an identifier for an unnamed struct field

func (*TestCase) HasChan

func (g *TestCase) HasChan() bool

HasChan reports if test case has a channel receiver

func (*TestCase) HasPrintStmts

func (g *TestCase) HasPrintStmts() bool

HasPrintStmts check if any print statements are generated for current test case

func (*TestCase) IdentToPrintStmt

func (g *TestCase) IdentToPrintStmt(t, oType *ast.Ident, input *PrintRecursionInput) *PrintResult

IdentToPrintStmt converts ident to print statement

func (*TestCase) IdentWithNilObjectToValExpr

func (g *TestCase) IdentWithNilObjectToValExpr(t *ast.Ident, input *RecursionInput) *TypeExprToValExprRes

IdentWithNilObjectToValExpr converts identifier with nil object to val expression

func (*TestCase) InterfaceGenDecl

func (g *TestCase) InterfaceGenDecl(input *RecursionInput, interfaceImplIdent *ast.Ident) *ast.GenDecl

InterfaceGenDecl creates interface gen decl

func (*TestCase) InterfaceNilFunc

func (g *TestCase) InterfaceNilFunc(t ast.Expr, input *RecursionInput) *TypeExprToValExprRes

InterfaceNilFunc creates a function returning nil value for expression

func (*TestCase) InterfaceTypeToFuncImpl

func (g *TestCase) InterfaceTypeToFuncImpl(input *RecursionInput, interfaceImplIdent *ast.Ident) *TypeExprToValExprRes

InterfaceTypeToFuncImpl converts interface type to function implementation declarations

func (*TestCase) InterfaceTypeToValExpr

func (g *TestCase) InterfaceTypeToValExpr(input *RecursionInput) *TypeExprToValExprRes

InterfaceTypeToValExpr converts an interface type to val expression and declarations

func (*TestCase) IsBasicExpr

func (g *TestCase) IsBasicExpr(x ast.Expr) (string, bool)

IsBasicExpr checks if expr is basic expr

func (*TestCase) IsBasicLit

func (g *TestCase) IsBasicLit(identifier string) bool

IsBasicLit reports if an idenetifier is a basic literal

func (*TestCase) IsError

func (g *TestCase) IsError(identifier string) bool

IsError checks if identifier is reserver error keyword

func (*TestCase) MapExprToPrintStmt

func (g *TestCase) MapExprToPrintStmt(t *ast.MapType, input *PrintRecursionInput) *PrintResult

MapExprToPrintStmt converts a map type to print statements

func (*TestCase) MapExprToValExpr

func (g *TestCase) MapExprToValExpr(input *RecursionInput) *TypeExprToValExprRes

MapExprToValExpr convert a map expression to value expression

func (*TestCase) ResultsToPrintStmts

func (g *TestCase) ResultsToPrintStmts(results *ast.FieldList, funcName string, pointer *importer.PkgResolverPointer) ([]ast.Expr, *PrintResult, []ast.Stmt)

ResultsToPrintStmts converts a results list to print statements

func (*TestCase) ReturnFieldToStmt

func (g *TestCase) ReturnFieldToStmt(res *ast.Field, input *RecursionInput) *TypeExprToValExprRes

ReturnFieldToStmt converts return field to statement and declarations

func (*TestCase) SelectorExprToPrintStmt

func (g *TestCase) SelectorExprToPrintStmt(input *PrintRecursionInput) *PrintResult

SelectorExprToPrintStmt converts a selector expression to a print statement

func (*TestCase) SelectorExprToValExpr

func (g *TestCase) SelectorExprToValExpr(input *RecursionInput) *TypeExprToValExprRes

SelectorExprToValExpr converts a selector expression to a value expression

func (*TestCase) ShouldReturnForFunc

func (g *TestCase) ShouldReturnForFunc(expr ast.Expr, input *RecursionInput) bool

ShouldReturnForFunc checks if should return because unimplementable func type

func (*TestCase) ShouldReturnForInterface

func (g *TestCase) ShouldReturnForInterface(expr ast.Expr, input *RecursionInput) bool

ShouldReturnForInterface checks if should return because unimplementable interface

func (*TestCase) StarExprToPrintStmt

func (g *TestCase) StarExprToPrintStmt(t *ast.StarExpr, input *PrintRecursionInput) *PrintResult

StarExprToPrintStmt converts a star expression to a print statement

func (*TestCase) StarExprToValExpr

func (g *TestCase) StarExprToValExpr(input *RecursionInput) *TypeExprToValExprRes

StarExprToValExpr converts star expression to value expression

func (*TestCase) StructExprToPrintStmt

func (g *TestCase) StructExprToPrintStmt(input *PrintRecursionInput) *PrintResult

StructExprToPrintStmt converts a struct expression to print statement

func (*TestCase) StructExprToValExpr

func (g *TestCase) StructExprToValExpr(input *RecursionInput) *TypeExprToValExprRes

StructExprToValExpr create struct expression

func (*TestCase) StructFieldsToKeyValExpr

func (g *TestCase) StructFieldsToKeyValExpr(res *ast.CompositeLit, input *RecursionInput) *TypeExprToValExprRes

StructFieldsToKeyValExpr converts struct expression to key value expressions for initialising the fields of a struct

func (*TestCase) TypeExprToValExpr

func (g *TestCase) TypeExprToValExpr(input *RecursionInput) *TypeExprToValExprRes

TypeExprToValExpr converts a type expression, the type definition in a function parameter, to an expression used in an assignment statement

func (*TestCase) TypeExpressionToPrintStmt

func (g *TestCase) TypeExpressionToPrintStmt(input *PrintRecursionInput) *PrintResult

TypeExpressionToPrintStmt converts type expression of a result to print statement

func (*TestCase) TypeSpecToValExpr

func (g *TestCase) TypeSpecToValExpr(t *ast.Ident, objectDeclType *ast.TypeSpec, input *RecursionInput) *TypeExprToValExprRes

TypeSpecToValExpr converts type spec to val expression Start of type recursion

func (*TestCase) UnnamedStructToValExpr

func (g *TestCase) UnnamedStructToValExpr(t *ast.StructType, input *RecursionInput) *TypeExprToValExprRes

UnnamedStructToValExpr converts unnamed struct to val expression

type TypeExprToValExprRes

type TypeExprToValExprRes struct {
	Expr         ast.Expr
	Statements   []ast.Stmt
	Declarations []ast.Decl
	// FIXME:
	ChanIdents []*ast.Ident
	ChanStmts  []ast.Stmt
	ChanDecls  []ast.Stmt
}

TypeExprToValExprRes result for converting input to value expressions

func EmptyResult

func EmptyResult() *TypeExprToValExprRes

EmptyResult creates empty result

func (*TypeExprToValExprRes) Merge

func (t *TypeExprToValExprRes) Merge(other *TypeExprToValExprRes)

Merge merges two type exr to val expr without using the expression

type Writer

type Writer struct {
	Res []byte
}

Writer io writer implementation for gathering element formatted strings

func (*Writer) GetString

func (w *Writer) GetString() string

GetString convert byte array to string

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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