lib

package
v0.0.0-...-f0190a3 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2018 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Magic is the string used to identify files generated by Mockhiato.
	Magic = "// Code generated by Mockhiato. DO NOT EDIT."
	// InterfaceNameToken is a placeholder used in configs.
	InterfaceNameToken = "{interface}"
	// PackageNameToken is a placeholder used in configs.
	PackageNameToken = "{package}"
	// FuncNameToken is a placeholder used in configs.
	FuncNameToken = "{func}"
)

Variables

This section is empty.

Functions

func Err

func Err(value interface{}) *errors.Error

Err wraps value in a stack trace

func IsExternalDependency

func IsExternalDependency(project *Project, packagePath string) bool

IsExternalDependency returns true when `packagePath` is not a project package.

func Unpanic

func Unpanic() func()

Unpanic recovers from a panic.

Types

type Bufferw

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

Bufferw is a wrapper around bytes.Buffer.

func (*Bufferw) Bytes

func (r *Bufferw) Bytes() []byte

Bytes wraps bytes.Buffer.Bytse()

func (*Bufferw) WriteString

func (r *Bufferw) WriteString(s string) int

WriteString wraps bytes.Buffer.WriteString()

type Config

type Config struct {
	Verbose                      bool
	ProjectPath                  string
	MockFileName                 string
	DependentMocksPath           string
	StructNameFormat             string
	DependentPackageNameFormat   string
	MockFunctionMethodNameFormat string
}

Config configures Mockhiato behavior. See mockhiato/cmd for documentation.

type Formatter

type Formatter interface {
	GenerateMocks(project *Project)
	IsMockFile(file *os.File) bool
}

Formatter formats mock files.

type Interface

type Interface struct {
	TObject    types.Object
	TInterface *types.Interface
}

Interface contains metadata for an interface definition. Formatters rely on this to generate mocks.

type Package

type Package struct {
	// Package is the package that contains interfaces.
	TPackage *types.Package
	// Interfaces contains interface definitions found in the package.
	Interfaces []*Interface
	// Signatures contains signature definitions found in the package.
	Signatures []*Signature
}

Package contains metadata for a package discovered in the project tree. Formatters rely on this to generate mocks.

func NewPackage

func NewPackage(p *types.Package) *Package

NewPackage constructor

type PackageMapper

type PackageMapper struct {
	PkgPath     string
	PathToAlias map[string]string
	// contains filtered or unexported fields
}

PackageMapper indexes packages and generates package aliases if required.

func NewPackageMapper

func NewPackageMapper(PkgPath string) *PackageMapper

NewPackageMapper creates a new PackageMapper.

func (*PackageMapper) IndexFunc

func (r *PackageMapper) IndexFunc(tFunc *types.Func)

IndexFunc indexes tFunc's dependencies.

func (*PackageMapper) IndexImports

func (r *PackageMapper) IndexImports(tPkg *types.Package)

IndexImports indexes tPkg's dependencies.

func (*PackageMapper) IndexInterface

func (r *PackageMapper) IndexInterface(tInterface *types.Interface)

IndexInterface indexes tInterface's dependencies.

func (*PackageMapper) IndexSignature

func (r *PackageMapper) IndexSignature(tSignature *types.Signature)

IndexSignature indexes tSignature's dependencies.

func (*PackageMapper) IndexTuple

func (r *PackageMapper) IndexTuple(tTuple *types.Tuple)

IndexTuple indexes tTuple's dependencies

func (*PackageMapper) ObjectTypeString

func (r *PackageMapper) ObjectTypeString(tObject types.Object) string

ObjectTypeString returns the string representation of tObject's type.

func (*PackageMapper) RecordDependency

func (r *PackageMapper) RecordDependency(tPkg *types.Package) string

RecordDependency indexes tPkg as a dependency and returns its name.

func (*PackageMapper) TypeString

func (r *PackageMapper) TypeString(tType types.Type) string

TypeString returns the string representation of tType.

type Project

type Project struct {
	// ProjectAbsPath is the project's absolute path.
	ProjectAbsPath string
	// GoAbsPath is $GOPATH.
	GoAbsPath string
	// GoSrcAbsPath is $GOPATH/src.
	GoSrcAbsPath string
	// PackagePath is the project's package path, which should be the relative path to $GOPATH/src.
	PackagePath string
	// VendorPath is the project's vedor path, which should be PackagePath/vendor
	VendorPath string
	// DependentMocksPath is where mocks for dependent interfaces (referenced but not defined by the project) will be created.
	DependentMocksPath string

	// Program is the loaded project
	Program *loader.Program
	// Packages is a map of packages with interfaces that needs to be mocked.
	Packages map[*types.Package]*Package

	// GenAbsPaths contains a list of generated file paths
	GenAbsPaths []string
}

Project represents the project Mockhiato is operating on.

func NewProject

func NewProject() *Project

NewProject creates a new Project.

type Signature

type Signature struct {
	TObject    types.Object
	TSignature *types.Signature
}

Signature contains metadata for a typed function. Formatters rely on this to generate mocks. Given a typed function,

type Example func(int) error

Formatters should generate a struct that implements the function's signature.

type ExampleMock struct { mock.Mock }
func (r *ExampleMock) Run(int) error { ... }

Users can then use the generated struct to set up expectations and verify behavior of function parameters.

func Test(t *testing.T) {
  mock := &ExampleMock{}
  mock.On("Run", 123).Return(io.EOF)
  app.Execute(mock.Run)
}

Directories

Path Synopsis
plugin

Jump to

Keyboard shortcuts

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