-
Notifications
You must be signed in to change notification settings - Fork 1
/
flagx_test.go
61 lines (53 loc) · 1.37 KB
/
flagx_test.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
package flagx
import (
"bytes"
"flag"
"os"
"reflect"
"testing"
"time"
)
func TestFlagSet(t *testing.T) {
var d time.Duration
var ids []int
wantIDs := []int{1, 2, 3}
var offsets map[float64]struct{}
wantOffsets := map[float64]struct{}{1: {}, 2: {}, 3: {}}
fset := NewFlagSet("testing", os.Stderr)
fset.Duration(&d, "timeout", "t", 10*time.Second, "just a timeout")
fset.IntSlice(&ids, "ids", "", wantIDs, ",", "just a timeout")
fset.Float64Set(&offsets, "offsets", "", wantOffsets, "just a timeout")
names := map[string]struct{}{}
fs := fset.AsStdlib()
fs.VisitAll(func(f *flag.Flag) {
names[f.Name] = struct{}{}
})
want := map[string]struct{}{
"timeout": {}, "t": {}, "ids": {}, "offsets": {},
}
mustEqual(t, names, want)
mustEqual(t, ids, wantIDs)
mustEqual(t, offsets, wantOffsets)
}
func TestFlagSet_PrintDefaults(t *testing.T) {
const usage = ` -timeout (-t) duration
just a timeout (default 10s)
`
var buf bytes.Buffer
fset := NewFlagSet("testing", &buf)
fset.Duration(new(time.Duration), "timeout", "t", 10*time.Second, "just a timeout")
fset.PrintDefaults()
mustEqual(t, buf.String(), usage)
}
func failIfErr(tb testing.TB, err error) {
tb.Helper()
if err != nil {
tb.Fatal(err)
}
}
func mustEqual(tb testing.TB, have, want interface{}) {
tb.Helper()
if !reflect.DeepEqual(have, want) {
tb.Fatalf("\nhave: %v\nwant: %v", have, want)
}
}