| 1 |
package gostub |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"reflect" |
| 6 |
) |
| 7 |
|
| 8 |
// Stub replaces the value stored at varToStub with stubVal. |
| 9 |
// varToStub must be a pointer to the variable. stubVal should have a type |
| 10 |
// that is assignable to the variable. |
| 11 |
func Stub(varToStub interface{}, stubVal interface{}) *Stubs { |
| 12 |
return New().Stub(varToStub, stubVal) |
| 13 |
} |
| 14 |
|
| 15 |
// StubFunc replaces a function variable with a function that returns stubVal. |
| 16 |
// funcVarToStub must be a pointer to a function variable. If the function |
| 17 |
// returns multiple values, then multiple values should be passed to stubFunc. |
| 18 |
// The values must match be assignable to the return values' types. |
| 19 |
func StubFunc(funcVarToStub interface{}, stubVal ...interface{}) *Stubs { |
| 20 |
return New().StubFunc(funcVarToStub, stubVal...) |
| 21 |
} |
| 22 |
|
| 23 |
type envVal struct { |
| 24 |
val string |
| 25 |
ok bool |
| 26 |
} |
| 27 |
|
| 28 |
// Stubs represents a set of stubbed variables that can be reset. |
| 29 |
type Stubs struct { |
| 30 |
// stubs is a map from the variable pointer (being stubbed) to the original value. |
| 31 |
stubs map[reflect.Value]reflect.Value |
| 32 |
origEnv map[string]envVal |
| 33 |
} |
| 34 |
|
| 35 |
// New returns Stubs that can be used to stub out variables. |
| 36 |
func New() *Stubs { |
| 37 |
return &Stubs{ |
| 38 |
stubs: make(map[reflect.Value]reflect.Value), |
| 39 |
origEnv: make(map[string]envVal), |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// Stub replaces the value stored at varToStub with stubVal. |
| 44 |
// varToStub must be a pointer to the variable. stubVal should have a type |
| 45 |
// that is assignable to the variable. |
| 46 |
func (s *Stubs) Stub(varToStub interface{}, stubVal interface{}) *Stubs { |
| 47 |
v := reflect.ValueOf(varToStub) |
| 48 |
stub := reflect.ValueOf(stubVal) |
| 49 |
|
| 50 |
// Ensure varToStub is a pointer to the variable. |
| 51 |
if v.Type().Kind() != reflect.Ptr { |
| 52 |
panic("variable to stub is expected to be a pointer") |
| 53 |
} |
| 54 |
|
| 55 |
if _, ok := s.stubs[v]; !ok { |
| 56 |
// Store the original value if this is the first time varPtr is being stubbed. |
| 57 |
s.stubs[v] = reflect.ValueOf(v.Elem().Interface()) |
| 58 |
} |
| 59 |
|
| 60 |
// *varToStub = stubVal |
| 61 |
v.Elem().Set(stub) |
| 62 |
return s |
| 63 |
} |
| 64 |
|
| 65 |
// StubFunc replaces a function variable with a function that returns stubVal. |
| 66 |
// funcVarToStub must be a pointer to a function variable. If the function |
| 67 |
// returns multiple values, then multiple values should be passed to stubFunc. |
| 68 |
// The values must match be assignable to the return values' types. |
| 69 |
func (s *Stubs) StubFunc(funcVarToStub interface{}, stubVal ...interface{}) *Stubs { |
| 70 |
funcPtrType := reflect.TypeOf(funcVarToStub) |
| 71 |
if funcPtrType.Kind() != reflect.Ptr || |
| 72 |
funcPtrType.Elem().Kind() != reflect.Func { |
| 73 |
panic("func variable to stub must be a pointer to a function") |
| 74 |
} |
| 75 |
funcType := funcPtrType.Elem() |
| 76 |
if funcType.NumOut() != len(stubVal) { |
| 77 |
panic(fmt.Sprintf("func type has %v return values, but only %v stub values provided", |
| 78 |
funcType.NumOut(), len(stubVal))) |
| 79 |
} |
| 80 |
|
| 81 |
return s.Stub(funcVarToStub, FuncReturning(funcPtrType.Elem(), stubVal...).Interface()) |
| 82 |
} |
| 83 |
|
| 84 |
// FuncReturning creates a new function with type funcType that returns results. |
| 85 |
func FuncReturning(funcType reflect.Type, results ...interface{}) reflect.Value { |
| 86 |
var resultValues []reflect.Value |
| 87 |
for i, r := range results { |
| 88 |
var retValue reflect.Value |
| 89 |
if r == nil { |
| 90 |
// We can't use reflect.ValueOf(nil), so we need to create the zero value. |
| 91 |
retValue = reflect.Zero(funcType.Out(i)) |
| 92 |
} else { |
| 93 |
// We cannot simply use reflect.ValueOf(r) as that does not work for |
| 94 |
// interface types, as reflect.ValueOf receives the dynamic type, which |
| 95 |
// is the underlying type. e.g. for an error, it may *errors.errorString. |
| 96 |
// Instead, we make the return type's expected interface value using |
| 97 |
// reflect.New, and set the data to the passed in value. |
| 98 |
tempV := reflect.New(funcType.Out(i)) |
| 99 |
tempV.Elem().Set(reflect.ValueOf(r)) |
| 100 |
retValue = tempV.Elem() |
| 101 |
} |
| 102 |
resultValues = append(resultValues, retValue) |
| 103 |
} |
| 104 |
return reflect.MakeFunc(funcType, func(_ []reflect.Value) []reflect.Value { |
| 105 |
return resultValues |
| 106 |
}) |
| 107 |
} |
| 108 |
|
| 109 |
// Reset resets all stubbed variables back to their original values. |
| 110 |
func (s *Stubs) Reset() { |
| 111 |
for v, originalVal := range s.stubs { |
| 112 |
v.Elem().Set(originalVal) |
| 113 |
} |
| 114 |
s.resetEnv() |
| 115 |
} |
| 116 |
|
| 117 |
// ResetSingle resets a single stubbed variable back to its original value. |
| 118 |
func (s *Stubs) ResetSingle(varToStub interface{}) { |
| 119 |
v := reflect.ValueOf(varToStub) |
| 120 |
originalVal, ok := s.stubs[v] |
| 121 |
if !ok { |
| 122 |
panic("cannot reset variable as it has not been stubbed yet") |
| 123 |
} |
| 124 |
|
| 125 |
v.Elem().Set(originalVal) |
| 126 |
} |