|
| 1 | +/* |
| 2 | +Copyright 2021 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package flag |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "reflect" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/spf13/pflag" |
| 26 | +) |
| 27 | + |
| 28 | +func TestStringSlice(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + args []string |
| 31 | + def []string |
| 32 | + expected []string |
| 33 | + parseError string |
| 34 | + changed bool |
| 35 | + }{ |
| 36 | + { |
| 37 | + args: []string{}, |
| 38 | + expected: nil, |
| 39 | + }, |
| 40 | + { |
| 41 | + args: []string{"a"}, |
| 42 | + expected: []string{"a"}, |
| 43 | + changed: true, |
| 44 | + }, |
| 45 | + { |
| 46 | + args: []string{"a", "b"}, |
| 47 | + expected: []string{"a", "b"}, |
| 48 | + changed: true, |
| 49 | + }, |
| 50 | + { |
| 51 | + def: []string{"a"}, |
| 52 | + args: []string{"a", "b"}, |
| 53 | + expected: []string{"a", "b"}, |
| 54 | + changed: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + def: []string{"a", "b"}, |
| 58 | + args: []string{"a", "b"}, |
| 59 | + expected: []string{"a", "b"}, |
| 60 | + changed: true, |
| 61 | + }, |
| 62 | + } |
| 63 | + for i, test := range tests { |
| 64 | + fs := pflag.NewFlagSet("testStringSlice", pflag.ContinueOnError) |
| 65 | + var s []string |
| 66 | + s = append(s, test.def...) |
| 67 | + |
| 68 | + v := NewStringSlice(&s) |
| 69 | + fs.Var(v, "slice", "usage") |
| 70 | + |
| 71 | + args := []string{} |
| 72 | + for _, a := range test.args { |
| 73 | + args = append(args, fmt.Sprintf("--slice=%s", a)) |
| 74 | + } |
| 75 | + |
| 76 | + err := fs.Parse(args) |
| 77 | + if test.parseError != "" { |
| 78 | + if err == nil { |
| 79 | + t.Errorf("%d: expected error %q, got nil", i, test.parseError) |
| 80 | + } else if !strings.Contains(err.Error(), test.parseError) { |
| 81 | + t.Errorf("%d: expected error %q, got %q", i, test.parseError, err) |
| 82 | + } |
| 83 | + } else if err != nil { |
| 84 | + t.Errorf("%d: expected nil error, got %v", i, err) |
| 85 | + } |
| 86 | + if !reflect.DeepEqual(s, test.expected) { |
| 87 | + t.Errorf("%d: expected %+v, got %+v", i, test.expected, s) |
| 88 | + } |
| 89 | + if v.changed != test.changed { |
| 90 | + t.Errorf("%d: expected %t got %t", i, test.changed, v.changed) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments