forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
55 lines (45 loc) · 1.11 KB
/
helper.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
package assert
import (
"fmt"
"reflect"
)
func fail(t TestingT, failureMsg string, msgAndArgs ...interface{}) bool {
if th, ok := t.(helper); ok {
th.Helper()
}
msg := formatMsgAndArgs(msgAndArgs...)
if msg == "" {
t.Errorf("%s", failureMsg)
} else {
t.Errorf("%s: %s", failureMsg, msg)
}
return false
}
func formatValues(got, expected interface{}) (string, string) {
if reflect.TypeOf(got) != reflect.TypeOf(expected) {
return fmt.Sprintf("%T(%#v)", got, got), fmt.Sprintf("%T(%#v)", expected, expected)
}
return fmt.Sprintf("%#v", got), fmt.Sprintf("%#v", expected)
}
func formatMsgAndArgs(msgAndArgs ...interface{}) string {
if len(msgAndArgs) == 0 || msgAndArgs == nil {
return ""
}
if len(msgAndArgs) == 1 {
return msgAndArgs[0].(string)
}
if len(msgAndArgs) > 1 {
return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
}
return ""
}
// didPanic returns true if fn panics when called.
func didPanic(fn PanicTestFunc) (panicked bool, message interface{}) {
defer func() {
if message = recover(); message != nil {
panicked = true
}
}()
fn()
return panicked, message
}