github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/benchmark/flags/flags_test.go (about) 1 /* 2 * 3 * Copyright 2019 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package flags 20 21 import ( 22 "flag" 23 "reflect" 24 "testing" 25 "time" 26 27 "github.com/hxx258456/ccgo/grpc/internal/grpctest" 28 ) 29 30 type s struct { 31 grpctest.Tester 32 } 33 34 func Test(t *testing.T) { 35 grpctest.RunSubTests(t, s{}) 36 } 37 38 func (s) TestStringWithAllowedValues(t *testing.T) { 39 const defaultVal = "default" 40 tests := []struct { 41 args string 42 allowed []string 43 wantVal string 44 wantErr bool 45 }{ 46 {"-workloads=all", []string{"unary", "streaming", "all"}, "all", false}, 47 {"-workloads=disallowed", []string{"unary", "streaming", "all"}, defaultVal, true}, 48 } 49 50 for _, test := range tests { 51 flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError) 52 var w = StringWithAllowedValues("workloads", defaultVal, "usage", test.allowed) 53 err := flag.CommandLine.Parse([]string{test.args}) 54 switch { 55 case !test.wantErr && err != nil: 56 t.Errorf("failed to parse command line args {%v}: %v", test.args, err) 57 case test.wantErr && err == nil: 58 t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args) 59 default: 60 if *w != test.wantVal { 61 t.Errorf("flag value is %v, want %v", *w, test.wantVal) 62 } 63 } 64 } 65 } 66 67 func (s) TestDurationSlice(t *testing.T) { 68 defaultVal := []time.Duration{time.Second, time.Nanosecond} 69 tests := []struct { 70 args string 71 wantVal []time.Duration 72 wantErr bool 73 }{ 74 {"-latencies=1s", []time.Duration{time.Second}, false}, 75 {"-latencies=1s,2s,3s", []time.Duration{time.Second, 2 * time.Second, 3 * time.Second}, false}, 76 {"-latencies=bad", defaultVal, true}, 77 } 78 79 for _, test := range tests { 80 flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError) 81 var w = DurationSlice("latencies", defaultVal, "usage") 82 err := flag.CommandLine.Parse([]string{test.args}) 83 switch { 84 case !test.wantErr && err != nil: 85 t.Errorf("failed to parse command line args {%v}: %v", test.args, err) 86 case test.wantErr && err == nil: 87 t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args) 88 default: 89 if !reflect.DeepEqual(*w, test.wantVal) { 90 t.Errorf("flag value is %v, want %v", *w, test.wantVal) 91 } 92 } 93 } 94 } 95 96 func (s) TestIntSlice(t *testing.T) { 97 defaultVal := []int{1, 1024} 98 tests := []struct { 99 args string 100 wantVal []int 101 wantErr bool 102 }{ 103 {"-kbps=1", []int{1}, false}, 104 {"-kbps=1,2,3", []int{1, 2, 3}, false}, 105 {"-kbps=20e4", defaultVal, true}, 106 } 107 108 for _, test := range tests { 109 flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError) 110 var w = IntSlice("kbps", defaultVal, "usage") 111 err := flag.CommandLine.Parse([]string{test.args}) 112 switch { 113 case !test.wantErr && err != nil: 114 t.Errorf("failed to parse command line args {%v}: %v", test.args, err) 115 case test.wantErr && err == nil: 116 t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args) 117 default: 118 if !reflect.DeepEqual(*w, test.wantVal) { 119 t.Errorf("flag value is %v, want %v", *w, test.wantVal) 120 } 121 } 122 } 123 } 124 125 func (s) TestStringSlice(t *testing.T) { 126 defaultVal := []string{"bar", "baz"} 127 tests := []struct { 128 args string 129 wantVal []string 130 wantErr bool 131 }{ 132 {"-name=foobar", []string{"foobar"}, false}, 133 {"-name=foo,bar", []string{"foo", "bar"}, false}, 134 {`-name="foo,bar",baz`, []string{"foo,bar", "baz"}, false}, 135 {`-name="foo,bar""",baz`, []string{`foo,bar"`, "baz"}, false}, 136 } 137 138 for _, test := range tests { 139 flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError) 140 var w = StringSlice("name", defaultVal, "usage") 141 err := flag.CommandLine.Parse([]string{test.args}) 142 switch { 143 case !test.wantErr && err != nil: 144 t.Errorf("failed to parse command line args {%v}: %v", test.args, err) 145 case test.wantErr && err == nil: 146 t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args) 147 default: 148 if !reflect.DeepEqual(*w, test.wantVal) { 149 t.Errorf("flag value is %v, want %v", *w, test.wantVal) 150 } 151 } 152 } 153 }