Documentation
¶
Index ¶
- func GetGoroutineId() int64
- func GetMethod(instance interface{}, methodName string) interface{}
- func GetNestedMethod(instance interface{}, methodName string) interface{}
- func GetPrivateMethod(instance interface{}, methodName string) interface{}
- func OptGeneric(o *option)
- func OptUnsafe(o *option)
- func PatchConvey(items ...interface{})
- func Sequence(value ...interface{}) sequenceOpt
- func UnPatchAll()
- type FilterGoroutineType
- type GenericInfo
- type MockBuilder
- func (builder *MockBuilder) Build() *Mocker
- func (builder *MockBuilder) ExcludeCurrentGoRoutine() *MockBuilder
- func (builder *MockBuilder) FilterGoRoutine(filter FilterGoroutineType, gId int64) *MockBuilder
- func (builder *MockBuilder) IncludeCurrentGoRoutine() *MockBuilder
- func (builder *MockBuilder) Origin(funcPtr interface{}) *MockBuilder
- func (builder *MockBuilder) Return(results ...interface{}) *MockBuilder
- func (builder *MockBuilder) To(hook interface{}) *MockBuilder
- func (builder *MockBuilder) When(when interface{}) *MockBuilder
- type Mocker
- func (mocker *Mocker) ExcludeCurrentGoRoutine() *Mocker
- func (mocker *Mocker) FilterGoRoutine(filter FilterGoroutineType, gId int64) *Mocker
- func (mocker *Mocker) IncludeCurrentGoRoutine() *Mocker
- func (mocker *Mocker) MockTimes() int
- func (mocker *Mocker) Origin(funcPtr interface{}) *Mocker
- func (mocker *Mocker) Patch() *Mocker
- func (mocker *Mocker) Release() *MockBuilder
- func (mocker *Mocker) Return(results ...interface{}) *Mocker
- func (mocker *Mocker) Times() int
- func (mocker *Mocker) To(to interface{}) *Mocker
- func (mocker *Mocker) UnPatch() *Mocker
- func (mocker *Mocker) When(when interface{}) *Mocker
- type MockerVar
- type MockeyPrivate
- type Private
- type SequenceOpt
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetMethod ¶
func GetMethod(instance interface{}, methodName string) interface{}
GetMethod resolve a certain public method from an instance.
func GetNestedMethod ¶
func GetNestedMethod(instance interface{}, methodName string) interface{}
GetNestedMethod resolves a certain public method in anonymous structs, it will look for the specific method in every anonymous struct field recursively. Deprecated, this is an old API in mockito. Please use GetMethod instead.
func GetPrivateMethod ¶
func GetPrivateMethod(instance interface{}, methodName string) interface{}
GetPrivateMethod resolve a certain public method from an instance. Deprecated, this is an old API in mockito. Please use GetMethod instead.
func OptGeneric ¶ added in v1.2.5
func OptGeneric(o *option)
func PatchConvey ¶
func PatchConvey(items ...interface{})
func UnPatchAll ¶ added in v1.2.7
func UnPatchAll()
Unpatch all mocks in current 'PatchConvey' context
If the caller is out of 'PatchConvey', it will unpatch all mocks
For example:
Test1(t) { Mock(a).Build() Mock(b).Build() // a and b will be unpatched UnpatchAll() } }) Test2(t) { Mock(a).Build() PatchConvey(t,func(){ Mock(b).Build() // only b will be unpatched UnpatchAll() } })
Types ¶
type FilterGoroutineType ¶
type FilterGoroutineType int64
const ( Disable FilterGoroutineType = 0 Include FilterGoroutineType = 1 Exclude FilterGoroutineType = 2 )
type GenericInfo ¶ added in v1.2.8
type GenericInfo uintptr
func (GenericInfo) Equal ¶ added in v1.2.8
func (g GenericInfo) Equal(other GenericInfo) bool
func (GenericInfo) UsedParamType ¶ added in v1.2.8
func (g GenericInfo) UsedParamType(n uintptr) reflect.Type
UsedParamType get the type of used parameter in generic function/struct
For example: assume we have generic function "f[int, float64](x int, y T1) T2" and derived type f[int, float64]:
UsedParamType(0) == reflect.TypeOf(int(0)) UsedParamType(1) == reflect.TypeOf(float64(0))
If index n is out of range, or the derived types have more complex structure(for example: define an generic struct in a generic function using generic types, unused parameterized type etc.), this function may return unexpected value or cause unrecoverable runtime error . So it is NOT RECOMMENDED to use this function unless you actually knows what you are doing.
type MockBuilder ¶
type MockBuilder struct {
// contains filtered or unexported fields
}
func Mock ¶
func Mock(target interface{}, opt ...optionFn) *MockBuilder
Mock mocks target function
If target is a generic method or method of generic types, you need add a genericOpt, like this:
func f[int, float64](x int, y T1) T2 Mock(f[int, float64], OptGeneric)
func MockGeneric ¶ added in v1.2.5
func MockGeneric(target interface{}) *MockBuilder
MockGeneric mocks generic function
Target must be generic method or method of generic types
func MockUnsafe ¶ added in v1.1.0
func MockUnsafe(target interface{}) *MockBuilder
MockUnsafe has the full ability of the Mock function and removes some security restrictions. This is an alternative when the Mock function fails. It may cause some unknown problems, so we recommend using Mock under normal conditions.
func (*MockBuilder) Build ¶
func (builder *MockBuilder) Build() *Mocker
func (*MockBuilder) ExcludeCurrentGoRoutine ¶
func (builder *MockBuilder) ExcludeCurrentGoRoutine() *MockBuilder
func (*MockBuilder) FilterGoRoutine ¶
func (builder *MockBuilder) FilterGoRoutine(filter FilterGoroutineType, gId int64) *MockBuilder
func (*MockBuilder) IncludeCurrentGoRoutine ¶
func (builder *MockBuilder) IncludeCurrentGoRoutine() *MockBuilder
func (*MockBuilder) Origin ¶
func (builder *MockBuilder) Origin(funcPtr interface{}) *MockBuilder
Origin add an origin hook which can be used to call un-mocked origin function
For example:
origin := Fun // only need the same type mock := func(p string) string { return origin(p + "mocked") } mock2 := Mock(Fun).To(mock).Origin(&origin).Build()
Origin only works when call origin hook directly, target will still be mocked in recursive call
func (*MockBuilder) Return ¶
func (builder *MockBuilder) Return(results ...interface{}) *MockBuilder
func (*MockBuilder) To ¶
func (builder *MockBuilder) To(hook interface{}) *MockBuilder
To declares the hook function that's called to replace the target function.
The hook function must have the same signature as the target function.
The following example would make Fun always return true
func Fun(input string) bool { return input == "fun" } Mock(Fun).To(func(_ string) bool {return true}).Build()
Note that if the target function is a struct method, you may optionally include the receiver as the first argument of the hook function. For example,
type Foo struct { Name string } func (f *Foo) Bar(other string) bool { return other == f.Name } Mock((*Foo).Bar).To(func(f *Foo, other string) bool {return true}).Build()
func (*MockBuilder) When ¶
func (builder *MockBuilder) When(when interface{}) *MockBuilder
When declares the condition hook that's called to determine whether the mock should be executed.
The condition hook function must have the same parameters as the target function.
The following example would execute the mock when input int is negative
func Fun(input int) string { return strconv.Itoa(input) } Mock(Fun).When(func(input int) bool { return input < 0 }).Return("0").Build()
Note that if the target function is a struct method, you may optionally include the receiver as the first argument of the condition hook function. For example,
type Foo struct { Age int } func (f *Foo) GetAge(younger int) string { return strconv.Itoa(f.Age - younger) } Mock((*Foo).GetAge).When(func(f *Foo, younger int) bool { return younger < 0 }).Return("0").Build()
type Mocker ¶
type Mocker struct {
// contains filtered or unexported fields
}
func (*Mocker) ExcludeCurrentGoRoutine ¶
func (*Mocker) FilterGoRoutine ¶
func (mocker *Mocker) FilterGoRoutine(filter FilterGoroutineType, gId int64) *Mocker
func (*Mocker) IncludeCurrentGoRoutine ¶
func (*Mocker) Release ¶ added in v1.2.3
func (mocker *Mocker) Release() *MockBuilder
type MockeyPrivate ¶ added in v1.2.4
type MockeyPrivate struct{}
type Private ¶ added in v1.2.4
type Private interface {
// contains filtered or unexported methods
}
type SequenceOpt ¶ added in v1.2.4
type SequenceOpt interface { // Private make sure it is mockey private interface Private // GetNext is used by mockey, don't use it if you don't know what it does GetNext() []interface{} }