diff options
| author | Alex Flint <[email protected]> | 2019-08-06 16:58:46 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-08-06 16:58:46 -0700 |
| commit | 8baf7040d76473dd1d78c17b490d9d8cf6b1c584 (patch) | |
| tree | f3adbffc0cfcd02f559bbb1b1729e7f0a5b3ae35 /parse_test.go | |
| parent | 6de9e789a982b5708c535308315c3d1d54217612 (diff) | |
| parent | 11a27074fcbbcce6d5b2129c9d33328152cd56be (diff) | |
Merge pull request #82 from alexflint/subcommand-implv1.1.0
Add support for subcommands
Diffstat (limited to 'parse_test.go')
| -rw-r--r-- | parse_test.go | 76 |
1 files changed, 68 insertions, 8 deletions
diff --git a/parse_test.go b/parse_test.go index 9aad2e3..882564e 100644 --- a/parse_test.go +++ b/parse_test.go @@ -19,15 +19,20 @@ func setenv(t *testing.T, name, val string) { } func parse(cmdline string, dest interface{}) error { + _, err := pparse(cmdline, dest) + return err +} + +func pparse(cmdline string, dest interface{}) (*Parser, error) { p, err := NewParser(Config{}, dest) if err != nil { - return err + return nil, err } var parts []string if len(cmdline) > 0 { parts = strings.Split(cmdline, " ") } - return p.Parse(parts) + return p, p.Parse(parts) } func TestString(t *testing.T) { @@ -371,7 +376,7 @@ func TestNonsenseKey(t *testing.T) { assert.Error(t, err) } -func TestMissingValue(t *testing.T) { +func TestMissingValueAtEnd(t *testing.T) { var args struct { Foo string } @@ -379,6 +384,24 @@ func TestMissingValue(t *testing.T) { assert.Error(t, err) } +func TestMissingValueInMIddle(t *testing.T) { + var args struct { + Foo string + Bar string + } + err := parse("--foo --bar=abc", &args) + assert.Error(t, err) +} + +func TestNegativeValue(t *testing.T) { + var args struct { + Foo int + } + err := parse("--foo -123", &args) + require.NoError(t, err) + assert.Equal(t, -123, args.Foo) +} + func TestInvalidInt(t *testing.T) { var args struct { Foo int @@ -462,11 +485,10 @@ func TestPanicOnNonPointer(t *testing.T) { }) } -func TestPanicOnNonStruct(t *testing.T) { +func TestErrorOnNonStruct(t *testing.T) { var args string - assert.Panics(t, func() { - _ = parse("", &args) - }) + err := parse("", &args) + assert.Error(t, err) } func TestUnsupportedType(t *testing.T) { @@ -540,6 +562,15 @@ func TestEnvironmentVariable(t *testing.T) { assert.Equal(t, "bar", args.Foo) } +func TestEnvironmentVariableNotPresent(t *testing.T) { + var args struct { + NotPresent string `arg:"env"` + } + os.Args = []string{"example"} + MustParse(&args) + assert.Equal(t, "", args.NotPresent) +} + func TestEnvironmentVariableOverrideName(t *testing.T) { var args struct { Foo string `arg:"env:BAZ"` @@ -584,7 +615,7 @@ func TestEnvironmentVariableSliceArgumentString(t *testing.T) { var args struct { Foo []string `arg:"env"` } - setenv(t, "FOO", "bar,\"baz, qux\"") + setenv(t, "FOO", `bar,"baz, qux"`) MustParse(&args) assert.Equal(t, []string{"bar", "baz, qux"}, args.Foo) } @@ -846,6 +877,28 @@ func TestEmbedded(t *testing.T) { assert.Equal(t, true, args.Z) } +func TestEmbeddedPtr(t *testing.T) { + // embedded pointer fields are not supported so this should return an error + var args struct { + *A + } + err := parse("--x=hello", &args) + require.Error(t, err) +} + +func TestEmbeddedPtrIgnored(t *testing.T) { + // embedded pointer fields are not normally supported but here + // we explicitly exclude it so the non-nil embedded structs + // should work as expected + var args struct { + *A `arg:"-"` + B + } + err := parse("--y=321", &args) + require.NoError(t, err) + assert.Equal(t, 321, args.Y) +} + func TestEmptyArgs(t *testing.T) { origArgs := os.Args @@ -985,3 +1038,10 @@ func TestReuseParser(t *testing.T) { err = p.Parse([]string{}) assert.Error(t, err) } + +func TestVersion(t *testing.T) { + var args struct{} + err := parse("--version", &args) + assert.Equal(t, ErrVersion, err) + +} |
