Skip to content

Commit b087856

Browse files
committed
Add additional test to fix codecov
1 parent 4f9b8e4 commit b087856

File tree

1 file changed

+223
-0
lines changed

1 file changed

+223
-0
lines changed

flag_test.go

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,47 @@ func TestIntSliceFlagApply_SetsAllNames(t *testing.T) {
10581058
expect(t, err, nil)
10591059
}
10601060

1061+
func TestIntSliceFlagApply_UsesEnvValues_noDefault(t *testing.T) {
1062+
defer resetEnv(os.Environ())
1063+
os.Clearenv()
1064+
_ = os.Setenv("MY_GOAT", "1 , 2")
1065+
var val IntSlice
1066+
fl := IntSliceFlag{Name: "goat", EnvVars: []string{"MY_GOAT"}, Value: &val}
1067+
set := flag.NewFlagSet("test", 0)
1068+
_ = fl.Apply(set)
1069+
1070+
err := set.Parse(nil)
1071+
expect(t, err, nil)
1072+
expect(t, val.Value(), []int(nil))
1073+
expect(t, set.Lookup("goat").Value.(*IntSlice).Value(), []int{1, 2})
1074+
}
1075+
1076+
func TestIntSliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
1077+
defer resetEnv(os.Environ())
1078+
os.Clearenv()
1079+
_ = os.Setenv("MY_GOAT", "1 , 2")
1080+
val := NewIntSlice(3, 4)
1081+
fl := IntSliceFlag{Name: "goat", EnvVars: []string{"MY_GOAT"}, Value: val}
1082+
set := flag.NewFlagSet("test", 0)
1083+
_ = fl.Apply(set)
1084+
err := set.Parse(nil)
1085+
expect(t, err, nil)
1086+
expect(t, val.Value(), []int{3, 4})
1087+
expect(t, set.Lookup("goat").Value.(*IntSlice).Value(), []int{1, 2})
1088+
}
1089+
1090+
func TestIntSliceFlagApply_DefaultValueWithDestination(t *testing.T) {
1091+
defValue := []int{1, 2}
1092+
1093+
fl := IntSliceFlag{Name: "country", Value: NewIntSlice(defValue...), Destination: NewIntSlice(3)}
1094+
set := flag.NewFlagSet("test", 0)
1095+
_ = fl.Apply(set)
1096+
1097+
err := set.Parse([]string{})
1098+
expect(t, err, nil)
1099+
expect(t, defValue, fl.Destination.Value())
1100+
}
1101+
10611102
func TestIntSliceFlagApply_ParentContext(t *testing.T) {
10621103
_ = (&App{
10631104
Flags: []Flag{
@@ -1147,6 +1188,56 @@ func TestInt64SliceFlagWithEnvVarHelpOutput(t *testing.T) {
11471188
}
11481189
}
11491190

1191+
func TestInt64SliceFlagApply_SetsAllNames(t *testing.T) {
1192+
fl := Int64SliceFlag{Name: "bits", Aliases: []string{"B", "bips"}}
1193+
set := flag.NewFlagSet("test", 0)
1194+
_ = fl.Apply(set)
1195+
1196+
err := set.Parse([]string{"--bits", "23", "-B", "3", "--bips", "99"})
1197+
expect(t, err, nil)
1198+
}
1199+
1200+
func TestInt64SliceFlagApply_UsesEnvValues_noDefault(t *testing.T) {
1201+
defer resetEnv(os.Environ())
1202+
os.Clearenv()
1203+
_ = os.Setenv("MY_GOAT", "1 , 2")
1204+
var val Int64Slice
1205+
fl := Int64SliceFlag{Name: "goat", EnvVars: []string{"MY_GOAT"}, Value: &val}
1206+
set := flag.NewFlagSet("test", 0)
1207+
_ = fl.Apply(set)
1208+
1209+
err := set.Parse(nil)
1210+
expect(t, err, nil)
1211+
expect(t, val.Value(), []int64(nil))
1212+
expect(t, set.Lookup("goat").Value.(*Int64Slice).Value(), []int64{1, 2})
1213+
}
1214+
1215+
func TestInt64SliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
1216+
defer resetEnv(os.Environ())
1217+
os.Clearenv()
1218+
_ = os.Setenv("MY_GOAT", "1 , 2")
1219+
val := NewInt64Slice(3, 4)
1220+
fl := Int64SliceFlag{Name: "goat", EnvVars: []string{"MY_GOAT"}, Value: val}
1221+
set := flag.NewFlagSet("test", 0)
1222+
_ = fl.Apply(set)
1223+
err := set.Parse(nil)
1224+
expect(t, err, nil)
1225+
expect(t, val.Value(), []int64{3, 4})
1226+
expect(t, set.Lookup("goat").Value.(*Int64Slice).Value(), []int64{1, 2})
1227+
}
1228+
1229+
func TestInt64SliceFlagApply_DefaultValueWithDestination(t *testing.T) {
1230+
defValue := []int64{1, 2}
1231+
1232+
fl := Int64SliceFlag{Name: "country", Value: NewInt64Slice(defValue...), Destination: NewInt64Slice(3)}
1233+
set := flag.NewFlagSet("test", 0)
1234+
_ = fl.Apply(set)
1235+
1236+
err := set.Parse([]string{})
1237+
expect(t, err, nil)
1238+
expect(t, defValue, fl.Destination.Value())
1239+
}
1240+
11501241
func TestInt64SliceFlagApply_ParentContext(t *testing.T) {
11511242
_ = (&App{
11521243
Flags: []Flag{
@@ -1251,6 +1342,56 @@ func TestUintSliceFlagWithEnvVarHelpOutput(t *testing.T) {
12511342
}
12521343
}
12531344

1345+
func TestUintSliceFlagApply_SetsAllNames(t *testing.T) {
1346+
fl := UintSliceFlag{Name: "bits", Aliases: []string{"B", "bips"}}
1347+
set := flag.NewFlagSet("test", 0)
1348+
_ = fl.Apply(set)
1349+
1350+
err := set.Parse([]string{"--bits", "23", "-B", "3", "--bips", "99"})
1351+
expect(t, err, nil)
1352+
}
1353+
1354+
func TestUintSliceFlagApply_UsesEnvValues_noDefault(t *testing.T) {
1355+
defer resetEnv(os.Environ())
1356+
os.Clearenv()
1357+
_ = os.Setenv("MY_GOAT", "1 , 2")
1358+
var val UintSlice
1359+
fl := UintSliceFlag{Name: "goat", EnvVars: []string{"MY_GOAT"}, Value: &val}
1360+
set := flag.NewFlagSet("test", 0)
1361+
_ = fl.Apply(set)
1362+
1363+
err := set.Parse(nil)
1364+
expect(t, err, nil)
1365+
expect(t, val.Value(), []uint(nil))
1366+
expect(t, set.Lookup("goat").Value.(*UintSlice).Value(), []uint{1, 2})
1367+
}
1368+
1369+
func TestUintSliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
1370+
defer resetEnv(os.Environ())
1371+
os.Clearenv()
1372+
_ = os.Setenv("MY_GOAT", "1 , 2")
1373+
val := NewUintSlice(3, 4)
1374+
fl := UintSliceFlag{Name: "goat", EnvVars: []string{"MY_GOAT"}, Value: val}
1375+
set := flag.NewFlagSet("test", 0)
1376+
_ = fl.Apply(set)
1377+
err := set.Parse(nil)
1378+
expect(t, err, nil)
1379+
expect(t, val.Value(), []uint{3, 4})
1380+
expect(t, set.Lookup("goat").Value.(*UintSlice).Value(), []uint{1, 2})
1381+
}
1382+
1383+
func TestUintSliceFlagApply_DefaultValueWithDestination(t *testing.T) {
1384+
defValue := []uint{1, 2}
1385+
1386+
fl := UintSliceFlag{Name: "country", Value: NewUintSlice(defValue...), Destination: NewUintSlice(3)}
1387+
set := flag.NewFlagSet("test", 0)
1388+
_ = fl.Apply(set)
1389+
1390+
err := set.Parse([]string{})
1391+
expect(t, err, nil)
1392+
expect(t, defValue, fl.Destination.Value())
1393+
}
1394+
12541395
func TestUintSliceFlagApply_ParentContext(t *testing.T) {
12551396
_ = (&App{
12561397
Flags: []Flag{
@@ -1347,6 +1488,56 @@ func TestUint64SliceFlagWithEnvVarHelpOutput(t *testing.T) {
13471488
}
13481489
}
13491490

1491+
func TestUint64SliceFlagApply_SetsAllNames(t *testing.T) {
1492+
fl := Uint64SliceFlag{Name: "bits", Aliases: []string{"B", "bips"}}
1493+
set := flag.NewFlagSet("test", 0)
1494+
_ = fl.Apply(set)
1495+
1496+
err := set.Parse([]string{"--bits", "23", "-B", "3", "--bips", "99"})
1497+
expect(t, err, nil)
1498+
}
1499+
1500+
func TestUint64SliceFlagApply_UsesEnvValues_noDefault(t *testing.T) {
1501+
defer resetEnv(os.Environ())
1502+
os.Clearenv()
1503+
_ = os.Setenv("MY_GOAT", "1 , 2")
1504+
var val Uint64Slice
1505+
fl := Uint64SliceFlag{Name: "goat", EnvVars: []string{"MY_GOAT"}, Value: &val}
1506+
set := flag.NewFlagSet("test", 0)
1507+
_ = fl.Apply(set)
1508+
1509+
err := set.Parse(nil)
1510+
expect(t, err, nil)
1511+
expect(t, val.Value(), []uint64(nil))
1512+
expect(t, set.Lookup("goat").Value.(*Uint64Slice).Value(), []uint64{1, 2})
1513+
}
1514+
1515+
func TestUint64SliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
1516+
defer resetEnv(os.Environ())
1517+
os.Clearenv()
1518+
_ = os.Setenv("MY_GOAT", "1 , 2")
1519+
val := NewUint64Slice(3, 4)
1520+
fl := Uint64SliceFlag{Name: "goat", EnvVars: []string{"MY_GOAT"}, Value: val}
1521+
set := flag.NewFlagSet("test", 0)
1522+
_ = fl.Apply(set)
1523+
err := set.Parse(nil)
1524+
expect(t, err, nil)
1525+
expect(t, val.Value(), []uint64{3, 4})
1526+
expect(t, set.Lookup("goat").Value.(*Uint64Slice).Value(), []uint64{1, 2})
1527+
}
1528+
1529+
func TestUint64SliceFlagApply_DefaultValueWithDestination(t *testing.T) {
1530+
defValue := []uint64{1, 2}
1531+
1532+
fl := Uint64SliceFlag{Name: "country", Value: NewUint64Slice(defValue...), Destination: NewUint64Slice(3)}
1533+
set := flag.NewFlagSet("test", 0)
1534+
_ = fl.Apply(set)
1535+
1536+
err := set.Parse([]string{})
1537+
expect(t, err, nil)
1538+
expect(t, defValue, fl.Destination.Value())
1539+
}
1540+
13501541
func TestUint64SliceFlagApply_ParentContext(t *testing.T) {
13511542
_ = (&App{
13521543
Flags: []Flag{
@@ -2591,6 +2782,38 @@ func TestInt64Slice_Serialized_Set(t *testing.T) {
25912782
}
25922783
}
25932784

2785+
func TestUintSlice_Serialized_Set(t *testing.T) {
2786+
sl0 := NewUintSlice(1, 2)
2787+
ser0 := sl0.Serialize()
2788+
2789+
if len(ser0) < len(slPfx) {
2790+
t.Fatalf("serialized shorter than expected: %q", ser0)
2791+
}
2792+
2793+
sl1 := NewUintSlice(3, 4)
2794+
_ = sl1.Set(ser0)
2795+
2796+
if sl0.String() != sl1.String() {
2797+
t.Fatalf("pre and post serialization do not match: %v != %v", sl0, sl1)
2798+
}
2799+
}
2800+
2801+
func TestUint64Slice_Serialized_Set(t *testing.T) {
2802+
sl0 := NewUint64Slice(1, 2)
2803+
ser0 := sl0.Serialize()
2804+
2805+
if len(ser0) < len(slPfx) {
2806+
t.Fatalf("serialized shorter than expected: %q", ser0)
2807+
}
2808+
2809+
sl1 := NewUint64Slice(3, 4)
2810+
_ = sl1.Set(ser0)
2811+
2812+
if sl0.String() != sl1.String() {
2813+
t.Fatalf("pre and post serialization do not match: %v != %v", sl0, sl1)
2814+
}
2815+
}
2816+
25942817
func TestTimestamp_set(t *testing.T) {
25952818
ts := Timestamp{
25962819
timestamp: nil,

0 commit comments

Comments
 (0)