simplemock

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2020 License: MIT Imports: 17 Imported by: 0

README

simplemock

Go Report Card

This is a code generator for mock from interfaces in Go.

Installation

go get -u github.com/theoden9014/simplemock/cmd/simplemockgen

Usage

Usage: simplemockgen [options...] path1, path2, ...
  -out string
    	output file, default output to stdout
  -pkgname string
    	output package name for mock

Example

// example.go
package example

type Reader interface {
	Read(p []byte) (n int, err error)
}
$ simplemockgen ./example.go

Then the following code will be generated.

package example

type ReaderMock struct {
	ReadFunc func(p []byte) (n int, err error)
}

func (m *ReaderMock) Read(p []byte) (n int, err error) {
	if m.ReadFunc != nil {
		return m.ReadFunc(p)
	}
	return 0, nil
}

Documentation

Index

Constants

View Source
const (
	StatusOK  int = 0
	StatusErr int = -1
)

Variables

This section is empty.

Functions

func FormatDeclarativeParams

func FormatDeclarativeParams(fieldList FieldList) (output string)

func FormatDeclarativeParamsWithVariadic

func FormatDeclarativeParamsWithVariadic(fieldList FieldList) (output string)

func FormatDeclarativeResults

func FormatDeclarativeResults(fieldList FieldList) (output string)

func FormatInputParams

func FormatInputParams(fieldList FieldList) (output string)

func FormatInputParamsWithVariadic

func FormatInputParamsWithVariadic(fieldList FieldList) (output string)

func FormatReturnZeroValueResults

func FormatReturnZeroValueResults(fieldList FieldList) (output string)

func TypeString

func TypeString(t types.Type) string

TypeString convert types.Type to string

func TypeZeroValue

func TypeZeroValue(t types.Type) string

TypeZeroValue returns zero value of type.

Types

type Command

type Command struct {
	Stdout io.Writer
	Stderr io.Writer
}

func (*Command) Run

func (c *Command) Run(args ...string) int

type Field

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

func NewField

func NewField(name string, typ types.Type) *Field

func (*Field) Name

func (f *Field) Name() string

func (*Field) SetTag

func (f *Field) SetTag(tag reflect.StructTag)

func (*Field) String

func (f *Field) String() string

String is return "$var_name $type"

func (*Field) Type

func (f *Field) Type() types.Type

type FieldList

type FieldList []*Field

func NewFieldListFromType

func NewFieldListFromType(t types.Type) (FieldList, error)

func (*FieldList) Add

func (fl *FieldList) Add(field *Field)

func (FieldList) At

func (fl FieldList) At(i int) *Field

func (FieldList) Format

func (fl FieldList) Format(formatter fieldListFormatter) (output string)

Format is translates into Go code that is available to each user by formatter.

func (FieldList) Len

func (fl FieldList) Len() int

func (FieldList) Less

func (fl FieldList) Less(i, j int) bool

func (FieldList) String

func (fl FieldList) String() (output string)

func (FieldList) Swap

func (fl FieldList) Swap(i, j int)

func (FieldList) Validate

func (fl FieldList) Validate() error

type Func

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

func NewFunc

func NewFunc(name string, params FieldList, results FieldList, receiver *Struct, receiverName string, variadic bool) *Func

func (*Func) Name

func (fn *Func) Name() string

func (*Func) Params

func (fn *Func) Params() FieldList

func (*Func) Recv

func (fn *Func) Recv() *Struct

func (*Func) RecvName

func (fn *Func) RecvName() string

func (*Func) Results

func (fn *Func) Results() FieldList

func (*Func) SetBlockWriter

func (fn *Func) SetBlockWriter(f func(*Func, io.Writer) error)

func (*Func) ValueReceiver

func (fn *Func) ValueReceiver()

func (*Func) Variadic

func (fn *Func) Variadic() bool

func (*Func) WriteTo

func (fn *Func) WriteTo(w io.Writer) error

type GoFile

type GoFile struct {
	*bytes.Buffer

	Package string
	Import  *Import
}

func NewGoFile

func NewGoFile() *GoFile

func (*GoFile) Check

func (f *GoFile) Check() error

Check source code by govet

func (*GoFile) Format

func (f *GoFile) Format() error

Format source code by format and goimports

func (*GoFile) Generate

func (f *GoFile) Generate() error

type Import

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

func (*Import) Add

func (im *Import) Add(pkg string)

func (*Import) AddPackages

func (im *Import) AddPackages(imports map[string]*packages.Package)

func (*Import) At

func (im *Import) At(i int) string

func (*Import) Len

func (im *Import) Len() int

func (*Import) Less

func (im *Import) Less(i, j int) bool

func (*Import) Swap

func (im *Import) Swap(i, j int)

func (*Import) WriteTo

func (im *Import) WriteTo(w io.Writer) error

type SimpleMock

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

func NewSimpleMock

func NewSimpleMock(name string, interFace *types.Interface) (*SimpleMock, error)

func (*SimpleMock) Name

func (m *SimpleMock) Name() string

func (*SimpleMock) WriteTo

func (m *SimpleMock) WriteTo(w io.Writer) error

type Struct

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

func NewStruct

func NewStruct(name string, fields FieldList) *Struct

func (*Struct) AddField

func (s *Struct) AddField(field *Field) error

func (*Struct) FieldList

func (s *Struct) FieldList() FieldList

func (*Struct) Name

func (s *Struct) Name() string

func (*Struct) Type

func (s *Struct) Type() *types.Struct

func (*Struct) WriteTo

func (s *Struct) WriteTo(w io.Writer) error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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