/
sidekick.go
58 lines (46 loc) · 1.18 KB
/
sidekick.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
// Package sidekick implements some Go test helper flags and functions.
package sidekick
import (
"flag"
"fmt"
"log"
"strings"
)
type stringSlice []string
func (ss stringSlice) String() string { return strings.Join(ss, ",") }
func (ss *stringSlice) Set(s string) error { *ss = append(*ss, s); return nil }
var (
// Debug is similar to testing.Verbose. Set to true when using flag
// -debug.
Debug bool
skips stringSlice
cases stringSlice
)
func init() {
flag.Var(&skips, "skip", "skip test case")
flag.Var(&cases, "case", "run test case")
flag.BoolVar(&Debug, "debug", false, "enter debug mode")
log.SetFlags(log.Lshortfile | log.LstdFlags)
}
// SkipCase checks if a test case should be run based on flags -skip or -case.
// SkipCase compares values in string form, you can pass in value has
// fmt.Stringer or any other interfaces that package fmt has supported.
func SkipCase(c interface{}) bool {
for _, item := range skips {
if item == fmt.Sprint(c) {
return true
}
}
for _, item := range cases {
if item == fmt.Sprint(c) {
return false
}
}
if len(cases) > 0 {
return true
}
return false
}
// TODO
// type Skiper interface{}
// type Caser interface{}