/
skip.go
89 lines (80 loc) · 2.32 KB
/
skip.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
/*Package skip provides functions for skipping a test and printing the source code
of the condition used to skip the test.
*/
package skip // import "gotest.tools/v3/skip"
import (
"fmt"
"path"
"reflect"
"runtime"
"strings"
"gotest.tools/v3/internal/format"
"gotest.tools/v3/internal/source"
)
type skipT interface {
Skip(args ...interface{})
Log(args ...interface{})
}
// Result of skip function
type Result interface {
Skip() bool
Message() string
}
type helperT interface {
Helper()
}
// BoolOrCheckFunc can be a bool, func() bool, or func() Result. Other types will panic
type BoolOrCheckFunc interface{}
// If the condition expression evaluates to true, skip the test.
//
// The condition argument may be one of three types: bool, func() bool, or
// func() SkipResult.
// When called with a bool, the test will be skip if the condition evaluates to true.
// When called with a func() bool, the test will be skip if the function returns true.
// When called with a func() Result, the test will be skip if the Skip method
// of the result returns true.
// The skip message will contain the source code of the expression.
// Extra message text can be passed as a format string with args.
func If(t skipT, condition BoolOrCheckFunc, msgAndArgs ...interface{}) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
switch check := condition.(type) {
case bool:
ifCondition(t, check, msgAndArgs...)
case func() bool:
if check() {
t.Skip(format.WithCustomMessage(getFunctionName(check), msgAndArgs...))
}
case func() Result:
result := check()
if result.Skip() {
msg := getFunctionName(check) + ": " + result.Message()
t.Skip(format.WithCustomMessage(msg, msgAndArgs...))
}
default:
panic(fmt.Sprintf("invalid type for condition arg: %T", check))
}
}
func getFunctionName(function interface{}) string {
funcPath := runtime.FuncForPC(reflect.ValueOf(function).Pointer()).Name()
return strings.SplitN(path.Base(funcPath), ".", 2)[1]
}
func ifCondition(t skipT, condition bool, msgAndArgs ...interface{}) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if !condition {
return
}
const (
stackIndex = 2
argPos = 1
)
source, err := source.FormattedCallExprArg(stackIndex, argPos)
if err != nil {
t.Log(err.Error())
t.Skip(format.Message(msgAndArgs...))
}
t.Skip(format.WithCustomMessage(source, msgAndArgs...))
}