github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/pkg/flag/rktflag_test.go (about) 1 // Copyright 2015 The rkt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package flag 16 17 import ( 18 "flag" 19 "reflect" 20 "strings" 21 "testing" 22 23 "github.com/spf13/pflag" 24 ) 25 26 // Ensure all these types implement the pflag.Value and flag.Value interface 27 var _ pflag.Value = (*OptionList)(nil) 28 var _ pflag.Value = (*PairList)(nil) 29 var _ flag.Value = (*OptionList)(nil) 30 var _ flag.Value = (*PairList)(nil) 31 32 var options = []string{"zero", "one", "two"} 33 34 func TestOptionList(t *testing.T) { 35 tests := []struct { 36 opts string 37 ex string 38 err bool 39 }{ 40 { 41 opts: "zero,two", 42 ex: "zero,two", 43 err: false, 44 }, 45 { // Duplicate test 46 opts: "one,two,two", 47 ex: "", 48 err: true, 49 }, 50 { // Not permissible test 51 opts: "one,two,three", 52 ex: "", 53 err: true, 54 }, 55 { // Empty string 56 opts: "", 57 ex: "", 58 err: false, 59 }, 60 } 61 62 for i, tt := range tests { 63 // test NewOptionsList 64 if _, err := NewOptionList(options, tt.opts); (err != nil) != tt.err { 65 t.Errorf("test %d: unexpected error in NewOptionList: %v", i, err) 66 } 67 68 // test OptionList.Set() 69 ol, err := NewOptionList(options, strings.Join(options, ",")) 70 if err != nil { 71 t.Errorf("test %d: unexpected error preparing test: %v", i, err) 72 } 73 74 if err := ol.Set(tt.opts); (err != nil) != tt.err { 75 t.Errorf("test %d: could not parse options as expected: %v", i, err) 76 } 77 if tt.ex != "" && tt.ex != ol.String() { 78 t.Errorf("test %d: resulting options not as expected: %s != %s", 79 i, tt.ex, ol.String()) 80 } 81 } 82 } 83 84 var bfMap = map[string]int{ 85 options[0]: 0, 86 options[1]: 1, 87 options[2]: 1 << 1, 88 } 89 90 func TestBitFlags(t *testing.T) { 91 tests := []struct { 92 opts string 93 ex int 94 parseErr bool 95 logicErr bool 96 }{ 97 { 98 opts: "one,two", 99 ex: 3, 100 }, 101 { // Duplicate test 102 opts: "zero,two,two", 103 ex: -1, 104 parseErr: true, 105 }, 106 { // Not included test 107 opts: "zero,two,three", 108 ex: -1, 109 parseErr: true, 110 }, 111 { // Test 10 in 11 112 opts: "one,two", 113 ex: 1, 114 }, 115 { // Test 11 not in 01 116 opts: "one", 117 ex: 3, 118 logicErr: true, 119 }, 120 } 121 122 for i, tt := range tests { 123 // test NewBitFlags 124 if _, err := NewBitFlags(options, tt.opts, bfMap); (err != nil) != tt.parseErr { 125 t.Errorf("test %d: unexpected error in NewBitFlags: %v", i, err) 126 } 127 128 bf, err := NewBitFlags(options, strings.Join(options, ","), bfMap) 129 if err != nil { 130 t.Errorf("test %d: unexpected error preparing test: %v", i, err) 131 } 132 133 // test bitFlags.Set() 134 if err := bf.Set(tt.opts); (err != nil) != tt.parseErr { 135 t.Errorf("test %d: Could not parse options as expected: %v", i, err) 136 } 137 if tt.ex >= 0 && bf.HasFlag(tt.ex) == tt.logicErr { 138 t.Errorf("test %d: Result was unexpected: %d != %d", 139 i, tt.ex, bf.Flags) 140 } 141 } 142 } 143 144 type ss []string 145 type sm map[string]string 146 147 func TestPairList(t *testing.T) { 148 options := map[string][]string{ 149 "one": {"a", "b", "ll"}, 150 "two": {}, 151 "three": nil, 152 } 153 154 tests := []struct { 155 args ss 156 expected sm 157 hasError bool 158 }{ 159 { 160 args: ss{}, 161 expected: sm{}, 162 hasError: false, 163 }, { 164 args: ss{"one=a"}, 165 expected: sm{"one": "a"}, 166 hasError: false, 167 }, { 168 args: ss{"one=a", "two=bar"}, 169 expected: sm{"one": "a", "two": "bar"}, 170 hasError: false, 171 }, { 172 args: ss{"one=ll,two=bar"}, 173 expected: sm{"one": "ll", "two": "bar"}, 174 hasError: false, 175 }, { 176 args: ss{"one=bar,two=bar"}, 177 expected: sm{}, 178 hasError: true, 179 }, { 180 args: ss{"one=ll,two=bar", "one=a"}, 181 expected: sm{"one": "a", "two": "bar"}, 182 hasError: false, 183 }, 184 } 185 186 for i, tt := range tests { 187 188 pl := MustNewPairList(options, nil) 189 190 var argErr error 191 for _, arg := range tt.args { 192 err := pl.Set(arg) 193 if err != nil { 194 argErr = err 195 } 196 } 197 if argErr != nil { 198 if !tt.hasError { 199 t.Errorf("test %d: pl.Set unexpected error %v", i, argErr) 200 } else { 201 continue // no point in testing equality 202 } 203 } 204 205 if !reflect.DeepEqual(map[string]string(tt.expected), pl.Pairs) { 206 t.Errorf("test %d: pl.Pairs expected %v got %v", i, tt.expected, pl.Pairs) 207 } 208 } 209 210 pl := MustNewPairList(options, nil) 211 got := pl.PermissibleString() 212 expected := "one=[a|b|ll] three=* two=*" // order! 213 if got != expected { 214 t.Errorf("pl.PermissibleString() expected %v got %v", expected, got) 215 } 216 217 pl = MustNewPairList(options, sm{"one": "a", "two": "bar"}) 218 got = pl.String() 219 expected = "one=a two=bar" 220 if got != expected { 221 t.Errorf("pl.String expected %v got %v", expected, got) 222 } 223 224 ser := SerializePairs(pl.Pairs) 225 pl2 := MustNewPairList(options, nil) 226 pl2.Set(ser) 227 if !reflect.DeepEqual(pl.Pairs, pl2.Pairs) { 228 t.Errorf("serialize - unserialize did not match") 229 } 230 }