forked from nishanths/exhaustive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_test.go
130 lines (118 loc) · 3.12 KB
/
flag_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
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
package exhaustive
import (
"errors"
"reflect"
"regexp"
"testing"
)
func TestRegexpFlag(t *testing.T) {
t.Run("not set", func(t *testing.T) {
var v regexpFlag
if got := v.re; got != nil {
t.Errorf("got %+v, want nil", got)
}
if got := v.String(); got != "" {
t.Errorf("got %q, want empty string", got)
}
})
t.Run("empty input", func(t *testing.T) {
var v regexpFlag
if err := v.Set(""); err != nil {
t.Errorf("error unexpectedly non-nil: %v", err)
}
if got := v.re; got != nil {
t.Errorf("got %+v, want nil", got)
}
if got := v.String(); got != "" {
t.Errorf("got %q, want empty string", got)
}
})
t.Run("bad input", func(t *testing.T) {
var v regexpFlag
if err := v.Set("("); err == nil {
t.Errorf("error unexpectedly nil")
}
if got := v.re; got != nil {
t.Errorf("got %+v, want nil", got)
}
if got := v.String(); got != "" {
t.Errorf("got %q, want empty string", got)
}
})
t.Run("good input", func(t *testing.T) {
var v regexpFlag
if err := v.Set("^foo$"); err != nil {
t.Errorf("error unexpectedly non-nil: %v", err)
}
if v.re == nil {
t.Errorf("unexpectedly nil")
}
if !v.re.MatchString("foo") {
t.Errorf("did not match")
}
if got, want := v.String(), regexp.MustCompile("^foo$").String(); got != want {
t.Errorf("got %q, want %q", got, want)
}
})
// The flag.Value interface doc says: "The flag package may call the
// String method with a zero-valued receiver, such as a nil pointer."
t.Run("String nil receiver", func(t *testing.T) {
var v *regexpFlag
// expect no panic, and ...
if got := v.String(); got != "" {
t.Errorf("got %q, want empty string", got)
}
})
}
func TestStringsFlag(t *testing.T) {
t.Run("empty", func(t *testing.T) {
var v stringsFlag
if err := v.Set(""); err != nil {
t.Errorf("error unexpectedly non-nil: %v", err)
}
if got := len(v.elements); got != 0 {
t.Errorf("got %d, want 0 length", got)
}
if got := v.String(); got != "" {
t.Errorf("got %q, want empty string", got)
}
})
t.Run("happy path", func(t *testing.T) {
var v stringsFlag
if err := v.Set("a, b,bb, c ,d "); err != nil {
t.Errorf("error unexpectedly non-nil: %v", err)
}
if got, want := v.elements, []string{"a", "b", "bb", "c", "d"}; !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
if got, want := v.String(), "a,b,bb,c,d"; got != want {
t.Errorf("got %q, want %q", got, want)
}
})
t.Run("filter error", func(t *testing.T) {
errBoom := errors.New("boom")
var v stringsFlag
v.filter = func(e string) error {
if e == "bb" {
return errBoom
}
return nil
}
err := v.Set("a, b,bb, c ,d ")
if err == nil {
t.Errorf("error unexpectedly nil: %v", err)
}
if err != errBoom {
t.Errorf("got %v, want %v", err, errBoom)
}
})
// The flag.Value interface doc says: "The flag package may call the
// String method with a zero-valued receiver, such as a nil pointer."
t.Run("String nil receiver", func(t *testing.T) {
var v *stringsFlag
// expect no panic, and ...
if got := v.String(); got != "" {
t.Errorf("got %q, want empty string", got)
}
})
}