| 1 |
package gostub |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"reflect" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
func Stub(varToStub interface{}, stubVal interface{}) *Stubs { |
| 12 |
return New().Stub(varToStub, stubVal) |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 29 |
type Stubs struct { |
| 30 |
|
| 31 |
stubs map[reflect.Value]reflect.Value |
| 32 |
origEnv map[string]envVal |
| 33 |
} |
| 34 |
|
| 35 |
|
| 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 |
|
| 44 |
|
| 45 |
|
| 46 |
func (s *Stubs) Stub(varToStub interface{}, stubVal interface{}) *Stubs { |
| 47 |
v := reflect.ValueOf(varToStub) |
| 48 |
stub := reflect.ValueOf(stubVal) |
| 49 |
|
| 50 |
|
| 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 |
|
| 57 |
s.stubs[v] = reflect.ValueOf(v.Elem().Interface()) |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
v.Elem().Set(stub) |
| 62 |
return s |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 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 |
|
| 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 |
|
| 91 |
retValue = reflect.Zero(funcType.Out(i)) |
| 92 |
} else { |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 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 |
|
| 110 |
func (s *Stubs) Reset() { |
| 111 |
for v, originalVal := range s.stubs { |
| 112 |
v.Elem().Set(originalVal) |
| 113 |
} |
| 114 |
s.resetEnv() |
| 115 |
} |
| 116 |
|
| 117 |
|
| 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 |
} |