-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestUtil.go
193 lines (163 loc) · 6.52 KB
/
testUtil.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package unittester
import (
"encoding/json"
"fmt"
"reflect"
"runtime"
"strconv"
"strings"
)
func UnitTest(function interface{}, expected []interface{}, params []interface{}, checkOutputTypeOnly bool, detailedPassLog bool) bool {
fnType := reflect.TypeOf(function)
if fnType.Kind() != reflect.Func {
fmt.Println("\033[31mFAIL:", function, "is not a function.\033[0m")
return false
}
args := []reflect.Value{}
for _, param := range params {
args = append(args, reflect.ValueOf(param))
}
functionName := runtime.FuncForPC(reflect.ValueOf(function).Pointer()).Name()
inputLength := fnType.NumIn()
if len(params) != inputLength {
fmt.Println("\033[31mFAIL: The amount of given inputs is", len(params), "which doesn't match the amount of input of function", functionName, "which is", inputLength, "\033[0m")
return false
}
for i := 0; i < inputLength; i++ {
realInputType := fnType.In(i)
inputType := reflect.TypeOf(params[i])
if realInputType != inputType {
fmt.Println("\033[31mFAIL:", functionName, "input type index of", i, "is", realInputType, "but the input that has been given is", inputType, "\033[0m")
return false
}
}
f := reflect.ValueOf(function)
results := f.Call(args)
outputLength := len(results)
if len(expected) != outputLength {
fmt.Println("\033[31mFAIL: The amount of outputs of function", functionName, "is", outputLength, "which doesn't match the amount of expected output which is", len(expected), "\033[0m")
return false
}
if outputLength >= 1 {
lastOutput := results[outputLength-1]
if lastOutput.Interface() != nil && reflect.TypeOf(lastOutput.Interface()).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
fmt.Println("\033[31mFAIL:", functionName, "returned an error:", lastOutput.Interface(), "\033[0m")
return false
}
}
for i, value := range results {
if reflect.TypeOf(value.Interface()) != reflect.TypeOf(expected[i]) {
fmt.Println("\033[31mFAIL:", functionName, "output index of", i, "return type is", reflect.TypeOf(value.Interface()), "but expected type of", reflect.TypeOf(expected[i]), "\033[0m")
return false
}
if checkOutputTypeOnly {
continue
}
if value.Interface() != expected[i] {
fmt.Println("\033[31mFAIL:", functionName, "output index of", i, "returned", value.Interface(), "but expected", expected[i], "\033[0m")
return false
}
}
if detailedPassLog {
fmt.Println("\033[32mPASS:", functionName, "function outputs "+formatValuesAsStr(results)+" with "+formatValuesAsStr(args)+" as an arguments and run successfully.\033[0m")
} else {
fmt.Println("\033[32mPASS:", functionName, "run successfully.\033[0m")
}
return true
}
func UnitTestWithMultipleOutputCase(function interface{}, expectedOutputs [][]interface{}, params []interface{}, checkOutputTypeOnly bool, detailedPassLog bool) bool {
fnType := reflect.TypeOf(function)
if fnType.Kind() != reflect.Func {
fmt.Println("\033[31mFAIL:", function, "is not a function.\033[0m")
return false
}
args := []reflect.Value{}
for _, param := range params {
args = append(args, reflect.ValueOf(param))
}
functionName := runtime.FuncForPC(reflect.ValueOf(function).Pointer()).Name()
inputLength := fnType.NumIn()
if len(params) != inputLength {
fmt.Println("\033[31mFAIL: The amount of given inputs is", len(params), "which doesn't match the amount of input of function", functionName, "which is", inputLength, "\033[0m")
return false
}
for i := 0; i < inputLength; i++ {
realInputType := fnType.In(i)
inputType := reflect.TypeOf(params[i])
if realInputType != inputType {
fmt.Println("\033[31mFAIL:", functionName, "input type index of", i, "is", realInputType, "but the input that has been given is", inputType, "\033[0m")
return false
}
}
f := reflect.ValueOf(function)
results := f.Call(args)
numCase := len(expectedOutputs)
outputLength := len(results)
isSuccessful := false
errorMessages := [][]interface{}{}
out:
for i := 0; i < numCase; i++ {
expected := expectedOutputs[i]
caseN := strconv.Itoa(i+1) + ":"
if len(expected) != outputLength {
errorMessages = append(errorMessages, []interface{}{"\t\033[31mOutput case", caseN, "The amount of outputs of function", functionName, "is", outputLength, "which doesn't match the amount of expected output which is", len(expected), "\033[0m"})
continue out
}
if outputLength >= 1 {
lastOutput := results[outputLength-1]
if lastOutput.Interface() != nil && reflect.TypeOf(lastOutput.Interface()).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
errorMessages = append(errorMessages, []interface{}{"\t\033[31mOutput case", caseN, functionName, "returned an error:", lastOutput.Interface(), "\033[0m"})
continue out
}
}
for i, value := range results {
if reflect.TypeOf(value.Interface()) != reflect.TypeOf(expected[i]) {
fmt.Println("\t\033[31mOutput case", functionName, "output index of", i, "return type is", reflect.TypeOf(value.Interface()), "but expected type of", reflect.TypeOf(expected[i]), "\033[0m")
return false
}
if checkOutputTypeOnly {
continue
}
if value.Interface() != expected[i] {
errorMessages = append(errorMessages, []interface{}{"\t\033[31mOutput case", caseN, functionName, "output index of", i, "returned", value.Interface(), "but expected", expected[i], "\033[0m"})
continue out
}
}
isSuccessful = true
}
if isSuccessful {
if detailedPassLog {
fmt.Println("\033[32mPASS:", functionName, "function outputs "+formatValuesAsStr(results)+" with "+formatValuesAsStr(args)+" as an arguments and run successfully.\033[0m")
} else {
fmt.Println("\033[32mPASS:", functionName, "run successfully.\033[0m")
}
} else {
fmt.Println("\033[31mFAIL:", functionName, "error logs: \n[\033[0m")
printFn := reflect.ValueOf(fmt.Println)
for _, msg := range errorMessages {
reflectValues := []reflect.Value{}
for _, a := range msg {
reflectValues = append(reflectValues, reflect.ValueOf(a))
}
printFn.Call(reflectValues)
}
fmt.Println("\033[31m]\033[0m")
}
return isSuccessful
}
func formatValuesAsStr(values []reflect.Value) string {
if len(values) <= 0 {
return "nothing"
}
var sb strings.Builder
maxIndex := len(values) - 1
for i, value := range values {
jsonBytes, _ := json.Marshal(value.Interface())
str := string(jsonBytes)
sb.WriteString(str)
if i < maxIndex && maxIndex != 0 {
sb.WriteString(", ")
}
}
return sb.String()
}