gounit

package module
v0.0.0-...-f1874d3 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2018 License: Apache-2.0 Imports: 15 Imported by: 2

README

GoUnit License Build Status Coverage Status Go Report Card GoDoc

GoUnit is a commandline tool that generates tests stubs based on source function or method signature.

There are plugins for

Demo

demo

Installation

go get -u github.com/hexdigest/gounit/cmd/gounit

Usage of GoUnit

This will generate test stubs for all functions and methods in file.go

  $ gounit gen -i file.go 

Run gounit help for more options

Custom test templates

If you're not satisfied with the code produced by the default GoUnit test template you can always write your own! You can use minimock template as an example. Here is how to add and switch to the custom template:

  $ curl https://raw.githubusercontent.com/hexdigest/gounit/master/templates/minimock > minimock
  $ gounit template add minimock
  $ gounit template list

    gounit templates installed

       * default
         minimock

  $ gounit template use minimock

Minimock template produces test stubs that are aware of the mocks generated by the minimock mock generator. By using both of these tools you can automate the process of writing tests and focus on your test cases rather than routine operations.

Integration with editors and IDEs

To ease an integration of GoUnit with IDEs "gen" subcommand has a "-json" flag. When -json flag is passed GoUnit reads JSON requests from Stdin in a loop and produces JSON responses with generated test(s) that are written to Stdout. Using this mode you can generate as many tests as you want by running GoUnit executable only once.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrGenerateHeader        = GenericError("failed to write header: %v")
	ErrGenerateTest          = GenericError("failed to write test: %v")
	ErrFuncNotFound          = GenericError("unable to find a function declaration")
	ErrFailedToParseInFile   = GenericError("failed to parse input file: %v")
	ErrFailedToParseOutFile  = GenericError("failed to parse output file: %v")
	ErrFailedToOpenInFile    = GenericError("failed to open input file: %v")
	ErrFailedToOpenOutFile   = GenericError("failed to open output file: %v")
	ErrFailedToCreateOutFile = GenericError("failed to create output file: %v")
	ErrInputFileDoesNotExist = GenericError("input file does not exist")
	ErrSeekFailed            = GenericError("failed to seek: %v")
	ErrFixImports            = GenericError("failed to fix imports: %v")
	ErrWriteTest             = GenericError("failed to write generated test: %v")
	ErrInvalidTestTemplate   = GenericError("invalid test template: %v")
)

Functions

func RegisterCommand

func RegisterCommand(name string, cmd Command)

func Usage

func Usage(w io.Writer)

Types

type Command

type Command interface {
	//FlagSet returns command specific flag set. If command doesn't have any flags nil should be returned.
	FlagSet() *flag.FlagSet

	//Run runs command
	Run(args []string, stdout, stderr io.Writer) error

	//Description returns short description of a command that is shown in the help message
	Description() string

	//Usage line
	Usage() string
}

func GetCommand

func GetCommand(name string) Command

type CommandLineError

type CommandLineError string

func (CommandLineError) Error

func (e CommandLineError) Error() string

type Func

type Func struct {
	Signature *ast.FuncDecl
}

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

func NewFunc

func NewFunc(sig *ast.FuncDecl) *Func

NewFunc returns pointer to the Func struct

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) 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, src, testSrc io.Reader) (*Generator, error)

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 GenericError

type GenericError string

func (GenericError) Error

func (e GenericError) Error() string

func (GenericError) Format

func (e GenericError) Format(args ...interface{}) GenericError

type Options

type Options struct {
	Lines        []int
	Functions    []string
	InputFile    string
	OutputFile   string
	Comment      string
	Template     string
	TemplateName string
	All          bool
	UseJSON      bool
	UseStdin     bool
	UseStdout    bool
}

type Request

type Request struct {
	InputFilePath  string `json:"inputFilePath"`
	OutputFilePath string `json:"outputFilePath"`
	InputFile      string `json:"inputFile"`
	OutputFile     string `json:"outputFile"`
	TemplateName   string `json:"templateName"`
	Comment        string `json:"comment"`
	Lines          []int  `json:"lines"`
}

Request is a JSON object that is read from Stdin in JSON mode (see -json command line flag)

type Response

type Response struct {
	GeneratedCode string `json:"generatedCode"`
}

Response is a JSON object that is written to Stdout in JSON mode

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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