-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (49 loc) · 1.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"errors"
"fmt"
"reflect"
)
type stubMapping map[string]interface{}
var StrubStorage = stubMapping{}
func setup() {
StrubStorage = map[string]interface{}{
"longRunningTest1": longRunningTest1,
"longRunningTest2": longRunningTest2,
}
}
func main() {
setup()
/*
Check for the enable of the tests.
For each enable status, call the function - Open/Close design principle (not checked)
Keep a counter for enable tests
If error is found, return and cancel using Context
*/
result1, _ := Call("longRunningTest1", "max", "mustermann")
outputStr1 := result1.(string)
fmt.Println(outputStr1)
result2, _ := Call("longRunningTest2", 100)
outputStr2 := result2.(int)
fmt.Println(outputStr2)
}
func Call(funcName string, params ...interface{}) (result interface{}, err error) {
f := reflect.ValueOf(StrubStorage[funcName])
if len(params) != f.Type().NumIn() {
err = errors.New("The number of params is out of index")
return
}
in := make([]reflect.Value, len(params))
for k, param := range params {
in[k] = reflect.ValueOf(param)
}
res := f.Call(in)
result = res[0].Interface()
return
}
func longRunningTest1(a, b string) string {
return fmt.Sprintf("Done for %v, %v", a, b)
}
func longRunningTest2(val int) int {
return 10 * val
}