diff options
| author | Alex Flint <[email protected]> | 2021-04-19 21:27:53 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-04-19 21:27:53 -0700 |
| commit | a0937d1b588cbb39c0fb9c250ebf21dce42b7dab (patch) | |
| tree | bb658d951719fb3d0b4ed90df9aded48d4bbaeb1 /subcommand_test.go | |
| parent | 6a01a15f75472271568c732c1191e9d33a5fc54c (diff) | |
| parent | 01a9fab8d768fbcb11e51d4df3c8fda6230462da (diff) | |
Merge pull request #150 from alexflint/push-coverage-up-more
Push test coverage up to 100%
Diffstat (limited to 'subcommand_test.go')
| -rw-r--r-- | subcommand_test.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/subcommand_test.go b/subcommand_test.go index c34ab01..2c61dd3 100644 --- a/subcommand_test.go +++ b/subcommand_test.go @@ -1,6 +1,7 @@ package arg import ( + "reflect" "testing" "github.com/stretchr/testify/assert" @@ -48,6 +49,17 @@ func TestMinimalSubcommand(t *testing.T) { assert.Equal(t, []string{"list"}, p.SubcommandNames()) } +func TestSubcommandNamesBeforeParsing(t *testing.T) { + type listCmd struct{} + var args struct { + List *listCmd `arg:"subcommand"` + } + p, err := NewParser(Config{}, &args) + require.NoError(t, err) + assert.Nil(t, p.Subcommand()) + assert.Nil(t, p.SubcommandNames()) +} + func TestNoSuchSubcommand(t *testing.T) { type listCmd struct { } @@ -179,6 +191,36 @@ func TestSubcommandsWithOptions(t *testing.T) { } } +func TestSubcommandsWithEnvVars(t *testing.T) { + type getCmd struct { + Name string `arg:"env"` + } + type listCmd struct { + Limit int `arg:"env"` + } + type cmd struct { + Verbose bool + Get *getCmd `arg:"subcommand"` + List *listCmd `arg:"subcommand"` + } + + { + var args cmd + setenv(t, "LIMIT", "123") + err := parse("list", &args) + require.NoError(t, err) + require.NotNil(t, args.List) + assert.Equal(t, 123, args.List.Limit) + } + + { + var args cmd + setenv(t, "LIMIT", "not_an_integer") + err := parse("list", &args) + assert.Error(t, err) + } +} + func TestNestedSubcommands(t *testing.T) { type child struct{} type parent struct { @@ -353,3 +395,19 @@ func TestSubcommandsWithMultiplePositionals(t *testing.T) { assert.Equal(t, 5, args.Limit) } } + +func TestValForNilStruct(t *testing.T) { + type subcmd struct{} + var cmd struct { + Sub *subcmd `arg:"subcommand"` + } + + p, err := NewParser(Config{}, &cmd) + require.NoError(t, err) + + typ := reflect.TypeOf(cmd) + subField, _ := typ.FieldByName("Sub") + + v := p.val(path{fields: []reflect.StructField{subField, subField}}) + assert.False(t, v.IsValid()) +} |
