Skip to content

Commit

Permalink
Add additional test to fix codecov
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Sep 10, 2022
1 parent b087856 commit d0fff2e
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions flag_test.go
Expand Up @@ -1690,6 +1690,56 @@ func TestFloat64SliceFlagWithEnvVarHelpOutput(t *testing.T) {
}
}

func TestFloat64SliceFlagApply_SetsAllNames(t *testing.T) {
fl := Float64SliceFlag{Name: "bits", Aliases: []string{"B", "bips"}}
set := flag.NewFlagSet("test", 0)
_ = fl.Apply(set)

err := set.Parse([]string{"--bits", "23", "-B", "3", "--bips", "99"})
expect(t, err, nil)
}

func TestFloat64SliceFlagApply_UsesEnvValues_noDefault(t *testing.T) {
defer resetEnv(os.Environ())
os.Clearenv()
_ = os.Setenv("MY_GOAT", "1.0 , 2.0")
var val Float64Slice
fl := Float64SliceFlag{Name: "goat", EnvVars: []string{"MY_GOAT"}, Value: &val}
set := flag.NewFlagSet("test", 0)
_ = fl.Apply(set)

err := set.Parse(nil)
expect(t, err, nil)
expect(t, val.Value(), []float64(nil))
expect(t, set.Lookup("goat").Value.(*Float64Slice).Value(), []float64{1, 2})
}

func TestFloat64SliceFlagApply_UsesEnvValues_withDefault(t *testing.T) {
defer resetEnv(os.Environ())
os.Clearenv()
_ = os.Setenv("MY_GOAT", "1.0 , 2.0")
val := NewFloat64Slice(3.0, 4.0)
fl := Float64SliceFlag{Name: "goat", EnvVars: []string{"MY_GOAT"}, Value: val}
set := flag.NewFlagSet("test", 0)
_ = fl.Apply(set)
err := set.Parse(nil)
expect(t, err, nil)
expect(t, val.Value(), []float64{3, 4})
expect(t, set.Lookup("goat").Value.(*Float64Slice).Value(), []float64{1, 2})
}

func TestFloat64SliceFlagApply_DefaultValueWithDestination(t *testing.T) {
defValue := []float64{1.0, 2.0}

fl := Float64SliceFlag{Name: "country", Value: NewFloat64Slice(defValue...), Destination: NewFloat64Slice(3)}
set := flag.NewFlagSet("test", 0)
_ = fl.Apply(set)

err := set.Parse([]string{})
expect(t, err, nil)
expect(t, defValue, fl.Destination.Value())
}

func TestFloat64SliceFlagValueFromContext(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Var(NewFloat64Slice(1.23, 4.56), "myflag", "doc")
Expand All @@ -1698,6 +1748,29 @@ func TestFloat64SliceFlagValueFromContext(t *testing.T) {
expect(t, f.Get(ctx), []float64{1.23, 4.56})
}

func TestFloat64SliceFlagApply_ParentContext(t *testing.T) {
_ = (&App{
Flags: []Flag{
&Float64SliceFlag{Name: "numbers", Aliases: []string{"n"}, Value: NewFloat64Slice(1.0, 2.0, 3.0)},
},
Commands: []*Command{
{
Name: "child",
Action: func(ctx *Context) error {
expected := []float64{1.0, 2.0, 3.0}
if !reflect.DeepEqual(ctx.Float64Slice("numbers"), expected) {
t.Errorf("child context unable to view parent flag: %v != %v", expected, ctx.Float64Slice("numbers"))
}
if !reflect.DeepEqual(ctx.Float64Slice("n"), expected) {
t.Errorf("child context unable to view parent flag: %v != %v", expected, ctx.Float64Slice("n"))
}
return nil
},
},
},
}).Run([]string{"run", "child"})
}

var genericFlagTests = []struct {
name string
value Generic
Expand Down

0 comments on commit d0fff2e

Please sign in to comment.