-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathflags_test.go
153 lines (140 loc) · 4.31 KB
/
flags_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
153
/*
*
* Copyright 2019 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package flags
import (
"flag"
"reflect"
"testing"
"time"
"google.golang.org/grpc/internal/grpctest"
)
type s struct {
grpctest.Tester
}
func Test(t *testing.T) {
grpctest.RunSubTests(t, s{})
}
func (s) TestStringWithAllowedValues(t *testing.T) {
const defaultVal = "default"
tests := []struct {
args string
allowed []string
wantVal string
wantErr bool
}{
{"-workloads=all", []string{"unary", "streaming", "all"}, "all", false},
{"-workloads=disallowed", []string{"unary", "streaming", "all"}, defaultVal, true},
}
for _, test := range tests {
flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
var w = StringWithAllowedValues("workloads", defaultVal, "usage", test.allowed)
err := flag.CommandLine.Parse([]string{test.args})
switch {
case !test.wantErr && err != nil:
t.Errorf("failed to parse command line args {%v}: %v", test.args, err)
case test.wantErr && err == nil:
t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args)
default:
if *w != test.wantVal {
t.Errorf("flag value is %v, want %v", *w, test.wantVal)
}
}
}
}
func (s) TestDurationSlice(t *testing.T) {
defaultVal := []time.Duration{time.Second, time.Nanosecond}
tests := []struct {
args string
wantVal []time.Duration
wantErr bool
}{
{"-latencies=1s", []time.Duration{time.Second}, false},
{"-latencies=1s,2s,3s", []time.Duration{time.Second, 2 * time.Second, 3 * time.Second}, false},
{"-latencies=bad", defaultVal, true},
}
for _, test := range tests {
flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
var w = DurationSlice("latencies", defaultVal, "usage")
err := flag.CommandLine.Parse([]string{test.args})
switch {
case !test.wantErr && err != nil:
t.Errorf("failed to parse command line args {%v}: %v", test.args, err)
case test.wantErr && err == nil:
t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args)
default:
if !reflect.DeepEqual(*w, test.wantVal) {
t.Errorf("flag value is %v, want %v", *w, test.wantVal)
}
}
}
}
func (s) TestIntSlice(t *testing.T) {
defaultVal := []int{1, 1024}
tests := []struct {
args string
wantVal []int
wantErr bool
}{
{"-kbps=1", []int{1}, false},
{"-kbps=1,2,3", []int{1, 2, 3}, false},
{"-kbps=20e4", defaultVal, true},
}
for _, test := range tests {
flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
var w = IntSlice("kbps", defaultVal, "usage")
err := flag.CommandLine.Parse([]string{test.args})
switch {
case !test.wantErr && err != nil:
t.Errorf("failed to parse command line args {%v}: %v", test.args, err)
case test.wantErr && err == nil:
t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args)
default:
if !reflect.DeepEqual(*w, test.wantVal) {
t.Errorf("flag value is %v, want %v", *w, test.wantVal)
}
}
}
}
func (s) TestStringSlice(t *testing.T) {
defaultVal := []string{"bar", "baz"}
tests := []struct {
args string
wantVal []string
wantErr bool
}{
{"-name=foobar", []string{"foobar"}, false},
{"-name=foo,bar", []string{"foo", "bar"}, false},
{`-name="foo,bar",baz`, []string{"foo,bar", "baz"}, false},
{`-name="foo,bar""",baz`, []string{`foo,bar"`, "baz"}, false},
}
for _, test := range tests {
flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
var w = StringSlice("name", defaultVal, "usage")
err := flag.CommandLine.Parse([]string{test.args})
switch {
case !test.wantErr && err != nil:
t.Errorf("failed to parse command line args {%v}: %v", test.args, err)
case test.wantErr && err == nil:
t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args)
default:
if !reflect.DeepEqual(*w, test.wantVal) {
t.Errorf("flag value is %v, want %v", *w, test.wantVal)
}
}
}
}