1package pflag 2 3import ( 4 "fmt" 5 "strconv" 6 "strings" 7 "testing" 8) 9 10func setUpUISFlagSet(uisp *[]uint) *FlagSet { 11 f := NewFlagSet("test", ContinueOnError) 12 f.UintSliceVar(uisp, "uis", []uint{}, "Command separated list!") 13 return f 14} 15 16func setUpUISFlagSetWithDefault(uisp *[]uint) *FlagSet { 17 f := NewFlagSet("test", ContinueOnError) 18 f.UintSliceVar(uisp, "uis", []uint{0, 1}, "Command separated list!") 19 return f 20} 21 22func TestEmptyUIS(t *testing.T) { 23 var uis []uint 24 f := setUpUISFlagSet(&uis) 25 err := f.Parse([]string{}) 26 if err != nil { 27 t.Fatal("expected no error; got", err) 28 } 29 30 getUIS, err := f.GetUintSlice("uis") 31 if err != nil { 32 t.Fatal("got an error from GetUintSlice():", err) 33 } 34 if len(getUIS) != 0 { 35 t.Fatalf("got is %v with len=%d but expected length=0", getUIS, len(getUIS)) 36 } 37} 38 39func TestUIS(t *testing.T) { 40 var uis []uint 41 f := setUpUISFlagSet(&uis) 42 43 vals := []string{"1", "2", "4", "3"} 44 arg := fmt.Sprintf("--uis=%s", strings.Join(vals, ",")) 45 err := f.Parse([]string{arg}) 46 if err != nil { 47 t.Fatal("expected no error; got", err) 48 } 49 for i, v := range uis { 50 u, err := strconv.ParseUint(vals[i], 10, 0) 51 if err != nil { 52 t.Fatalf("got error: %v", err) 53 } 54 if uint(u) != v { 55 t.Fatalf("expected uis[%d] to be %s but got %d", i, vals[i], v) 56 } 57 } 58 getUIS, err := f.GetUintSlice("uis") 59 if err != nil { 60 t.Fatalf("got error: %v", err) 61 } 62 for i, v := range getUIS { 63 u, err := strconv.ParseUint(vals[i], 10, 0) 64 if err != nil { 65 t.Fatalf("got error: %v", err) 66 } 67 if uint(u) != v { 68 t.Fatalf("expected uis[%d] to be %s but got: %d from GetUintSlice", i, vals[i], v) 69 } 70 } 71} 72 73func TestUISDefault(t *testing.T) { 74 var uis []uint 75 f := setUpUISFlagSetWithDefault(&uis) 76 77 vals := []string{"0", "1"} 78 79 err := f.Parse([]string{}) 80 if err != nil { 81 t.Fatal("expected no error; got", err) 82 } 83 for i, v := range uis { 84 u, err := strconv.ParseUint(vals[i], 10, 0) 85 if err != nil { 86 t.Fatalf("got error: %v", err) 87 } 88 if uint(u) != v { 89 t.Fatalf("expect uis[%d] to be %d but got: %d", i, u, v) 90 } 91 } 92 93 getUIS, err := f.GetUintSlice("uis") 94 if err != nil { 95 t.Fatal("got an error from GetUintSlice():", err) 96 } 97 for i, v := range getUIS { 98 u, err := strconv.ParseUint(vals[i], 10, 0) 99 if err != nil { 100 t.Fatal("got an error from GetIntSlice():", err) 101 } 102 if uint(u) != v { 103 t.Fatalf("expected uis[%d] to be %d from GetUintSlice but got: %d", i, u, v) 104 } 105 } 106} 107 108func TestUISWithDefault(t *testing.T) { 109 var uis []uint 110 f := setUpUISFlagSetWithDefault(&uis) 111 112 vals := []string{"1", "2"} 113 arg := fmt.Sprintf("--uis=%s", strings.Join(vals, ",")) 114 err := f.Parse([]string{arg}) 115 if err != nil { 116 t.Fatal("expected no error; got", err) 117 } 118 for i, v := range uis { 119 u, err := strconv.ParseUint(vals[i], 10, 0) 120 if err != nil { 121 t.Fatalf("got error: %v", err) 122 } 123 if uint(u) != v { 124 t.Fatalf("expected uis[%d] to be %d from GetUintSlice but got: %d", i, u, v) 125 } 126 } 127 128 getUIS, err := f.GetUintSlice("uis") 129 if err != nil { 130 t.Fatal("got an error from GetUintSlice():", err) 131 } 132 for i, v := range getUIS { 133 u, err := strconv.ParseUint(vals[i], 10, 0) 134 if err != nil { 135 t.Fatalf("got error: %v", err) 136 } 137 if uint(u) != v { 138 t.Fatalf("expected uis[%d] to be %d from GetUintSlice but got: %d", i, u, v) 139 } 140 } 141} 142 143func TestUISAsSliceValue(t *testing.T) { 144 var uis []uint 145 f := setUpUISFlagSet(&uis) 146 147 in := []string{"1", "2"} 148 argfmt := "--uis=%s" 149 arg1 := fmt.Sprintf(argfmt, in[0]) 150 arg2 := fmt.Sprintf(argfmt, in[1]) 151 err := f.Parse([]string{arg1, arg2}) 152 if err != nil { 153 t.Fatal("expected no error; got", err) 154 } 155 156 f.VisitAll(func(f *Flag) { 157 if val, ok := f.Value.(SliceValue); ok { 158 _ = val.Replace([]string{"3"}) 159 } 160 }) 161 if len(uis) != 1 || uis[0] != 3 { 162 t.Fatalf("Expected ss to be overwritten with '3.1', but got: %v", uis) 163 } 164} 165 166func TestUISCalledTwice(t *testing.T) { 167 var uis []uint 168 f := setUpUISFlagSet(&uis) 169 170 in := []string{"1,2", "3"} 171 expected := []int{1, 2, 3} 172 argfmt := "--uis=%s" 173 arg1 := fmt.Sprintf(argfmt, in[0]) 174 arg2 := fmt.Sprintf(argfmt, in[1]) 175 err := f.Parse([]string{arg1, arg2}) 176 if err != nil { 177 t.Fatal("expected no error; got", err) 178 } 179 for i, v := range uis { 180 if uint(expected[i]) != v { 181 t.Fatalf("expected uis[%d] to be %d but got: %d", i, expected[i], v) 182 } 183 } 184} 185