-
Notifications
You must be signed in to change notification settings - Fork 4
/
wrap_test.go
152 lines (136 loc) · 2.89 KB
/
wrap_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package flagvar_test
import (
"flag"
"fmt"
"reflect"
"sort"
"strings"
"testing"
"github.com/sgreben/flagvar"
)
func TestWrapPointer(t *testing.T) {
sv := flagvar.Strings{}
var p flag.Value = &sv
fv := flagvar.WrapPointer{
Value: &p,
}
var fs flag.FlagSet
fs.Var(&fv, "wrap-pointer", "")
err := fs.Parse([]string{"-wrap-pointer", "abc"})
if err != nil {
t.Fail()
}
if !reflect.DeepEqual(sv.Values, []string{"abc"}) {
t.Fail()
}
}
func TestWrapFunc(t *testing.T) {
sv := &flagvar.Strings{}
fv := flagvar.WrapFunc(func() flag.Value { return sv })
var fs flag.FlagSet
fs.Var(&fv, "wrap-func", "")
err := fs.Parse([]string{"-wrap-func", "abc"})
if err != nil {
t.Fail()
}
if !reflect.DeepEqual(sv.Values, []string{"abc"}) {
t.Fail()
}
}
func TestWrap(t *testing.T) {
updated := 0
sv := &flagvar.Strings{}
fv := flagvar.Wrap{
Value: sv,
Updated: func() {
updated++
},
}
var fs flag.FlagSet
fs.Var(&fv, "wrap", "")
err := fs.Parse([]string{"-wrap", "abc", "-wrap", "def"})
if err != nil {
t.Fail()
}
if !reflect.DeepEqual(sv.Values, []string{"abc", "def"}) {
t.Fail()
}
if updated != 2 {
t.Fail()
}
}
func TestWrapCSV(t *testing.T) {
sv := &flagvar.Strings{}
fv := flagvar.WrapCSV{Value: sv}
var fs flag.FlagSet
fs.Var(&fv, "wrap-csv", "")
err := fs.Parse([]string{"-wrap-csv", "abc,def", "-wrap-csv", "xyz"})
if err != nil {
t.Fail()
}
if !reflect.DeepEqual(sv.Values, []string{"abc", "def", "xyz"}) {
t.Fail()
}
}
func TestWrapCSVFail(t *testing.T) {
sv := &flagvar.IP{}
fv := flagvar.WrapCSV{Value: sv}
var fs flag.FlagSet
fs.Var(&fv, "wrap-csv", "")
err := fs.Parse([]string{"-wrap-csv", "127.0.0.1,def"})
if err == nil {
t.Fail()
}
}
func TestWrapCSVSeparator(t *testing.T) {
sv := &flagvar.Strings{}
fv := flagvar.WrapCSV{
Value: sv,
Separator: ";",
}
var fs flag.FlagSet
fs.Var(&fv, "wrap-csv", "")
err := fs.Parse([]string{"-wrap-csv", "abc;def", "-wrap-csv", "xyz"})
if err != nil {
t.Fail()
}
if !reflect.DeepEqual(sv.Values, []string{"abc", "def", "xyz"}) {
t.Fail()
}
}
func TestWrapCSVUpdated(t *testing.T) {
sv := &flagvar.Assignment{}
current := map[string]string{}
final := map[string]string{}
fv := flagvar.WrapCSV{
Value: sv,
UpdatedOne: func() {
current[sv.Value.Key] = sv.Value.Value
},
UpdatedAll: func() {
final = current
current = map[string]string{}
},
StringFunc: func() string {
var out []string
for k, v := range final {
out = append(out, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(out)
return strings.Join(out, ",")
},
}
var fs flag.FlagSet
fs.Var(&fv, "wrap-csv", "")
err := fs.Parse([]string{"-wrap-csv", "xyz=abc", "-wrap-csv", "abc=def,def=xyz"})
if err != nil {
t.Fail()
}
fmt.Println(final)
if !reflect.DeepEqual(final, map[string]string{"abc": "def", "def": "xyz"}) {
t.Fail()
}
if !reflect.DeepEqual(fv.String(), `abc=def,def=xyz`) {
t.Fail()
}
}