gomonkey

package
v1.1.15 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2024 License: Apache-2.0, MIT Imports: 5 Imported by: 0

README

gomonkey

gomonkey is a library to make monkey patching in unit tests easy, and the core idea of monkey patching comes from Bouke, you can read this blogpost for an explanation on how it works.

Features

  • support a patch for a function
  • support a patch for a public member method
  • support a patch for a private member method
  • support a patch for a interface
  • support a patch for a function variable
  • support a patch for a global variable
  • support patches of a specified sequence for a function
  • support patches of a specified sequence for a member method
  • support patches of a specified sequence for a interface
  • support patches of a specified sequence for a function variable

Notes

  • gomonkey fails to patch a function or a member method if inlining is enabled, please running your tests with inlining disabled by adding the command line argument that is -gcflags=-l(below go1.10) or -gcflags=all=-l(go1.10 and above).
  • A panic may happen when a goroutine is patching a function or a member method that is visited by another goroutine at the same time. That is to say, gomonkey is not threadsafe.

Supported Platform:

  • ARCH

    • amd64
    • arm64
    • 386
    • loong64
  • OS

    • Linux
    • MAC OS X
    • Windows

Installation

  • below v2.1.0, for example v2.0.2
$ go get github.com/agiledragon/gomonkey@v2.0.2
  • v2.1.0 and above, for example v2.2.0
$ go get github.com/agiledragon/gomonkey/v2@v2.2.0

Test Method

$ cd test 
$ go test -gcflags=all=-l

Using gomonkey

Please refer to the test cases as idioms, very complete and detailed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetResultValues

func GetResultValues(funcType reflect.Type, results ...interface{}) []reflect.Value

Types

type OutputCell

type OutputCell struct {
	Values Params
	Times  int
}

type Params

type Params []interface{}

type Patches

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

func ApplyFunc

func ApplyFunc(target, double interface{}) *Patches

func ApplyFuncReturn

func ApplyFuncReturn(target interface{}, output ...interface{}) *Patches

func ApplyFuncSeq

func ApplyFuncSeq(target interface{}, outputs []OutputCell) *Patches

func ApplyFuncVar

func ApplyFuncVar(target, double interface{}) *Patches

func ApplyFuncVarReturn

func ApplyFuncVarReturn(target interface{}, output ...interface{}) *Patches

func ApplyFuncVarSeq

func ApplyFuncVarSeq(target interface{}, outputs []OutputCell) *Patches

func ApplyGlobalVar

func ApplyGlobalVar(target, double interface{}) *Patches

func ApplyMethod

func ApplyMethod(target interface{}, methodName string, double interface{}) *Patches

func ApplyMethodFunc

func ApplyMethodFunc(target interface{}, methodName string, doubleFunc interface{}) *Patches

func ApplyMethodReturn

func ApplyMethodReturn(target interface{}, methodName string, output ...interface{}) *Patches

func ApplyMethodSeq

func ApplyMethodSeq(target interface{}, methodName string, outputs []OutputCell) *Patches

func ApplyPrivateMethod

func ApplyPrivateMethod(target interface{}, methodName string, double interface{}) *Patches

func NewPatches

func NewPatches() *Patches

func (*Patches) ApplyCore

func (this *Patches) ApplyCore(target, double reflect.Value) *Patches

func (*Patches) ApplyCoreOnlyForPrivateMethod

func (this *Patches) ApplyCoreOnlyForPrivateMethod(target unsafe.Pointer, double reflect.Value) *Patches

func (*Patches) ApplyFunc

func (this *Patches) ApplyFunc(target, double interface{}) *Patches

func (*Patches) ApplyFuncReturn

func (this *Patches) ApplyFuncReturn(target interface{}, returns ...interface{}) *Patches

func (*Patches) ApplyFuncSeq

func (this *Patches) ApplyFuncSeq(target interface{}, outputs []OutputCell) *Patches

func (*Patches) ApplyFuncVar

func (this *Patches) ApplyFuncVar(target, double interface{}) *Patches

func (*Patches) ApplyFuncVarReturn

func (this *Patches) ApplyFuncVarReturn(target interface{}, returns ...interface{}) *Patches

func (*Patches) ApplyFuncVarSeq

func (this *Patches) ApplyFuncVarSeq(target interface{}, outputs []OutputCell) *Patches

func (*Patches) ApplyGlobalVar

func (this *Patches) ApplyGlobalVar(target, double interface{}) *Patches

func (*Patches) ApplyMethod

func (this *Patches) ApplyMethod(target interface{}, methodName string, double interface{}) *Patches

func (*Patches) ApplyMethodFunc

func (this *Patches) ApplyMethodFunc(target interface{}, methodName string, doubleFunc interface{}) *Patches

func (*Patches) ApplyMethodReturn

func (this *Patches) ApplyMethodReturn(target interface{}, methodName string, returns ...interface{}) *Patches

func (*Patches) ApplyMethodSeq

func (this *Patches) ApplyMethodSeq(target interface{}, methodName string, outputs []OutputCell) *Patches

func (*Patches) ApplyPrivateMethod

func (this *Patches) ApplyPrivateMethod(target interface{}, methodName string, double interface{}) *Patches

func (*Patches) Reset

func (this *Patches) Reset()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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