diff --git a/clap/clap_test.go b/clap/clap_test.go index 3820585..f6bceee 100644 --- a/clap/clap_test.go +++ b/clap/clap_test.go @@ -3,7 +3,6 @@ package clap_test import ( "errors" "reflect" - "slices" "testing" "github.com/fred1268/go-clap/clap" @@ -98,7 +97,8 @@ func TestMultipleMandatoryNotFound(t *testing.T) { return } got := results.Mandatory - if len(got) != 2 || !slices.Contains(got, wanted[0]) || !slices.Contains(got, wanted[1]) { + if len(got) != 2 || (got[0] != wanted[0] && got[0] != wanted[1]) || + (got[1] != wanted[0] && got[1] != wanted[1]) { t.Errorf("wanted: '%v', got '%v'", wanted, got) return } @@ -190,6 +190,9 @@ func TestLongName(t *testing.T) { if !results.HasWarnings() || len(results.Ignored) != 4 { t.Errorf("wrong error number / type") } + if results.HasErrors() { + t.Errorf("unexpected errors") + } } func TestShortAndLong(t *testing.T) { @@ -453,7 +456,7 @@ func TestMissingSliceArgument(t *testing.T) { t.Logf("t: %v\n", results) } -func TestUintParsing(t *testing.T) { +func TestUintParsingError(t *testing.T) { t.Parallel() type config struct { Uint uint `clap:"--uint"` @@ -467,7 +470,7 @@ func TestUintParsing(t *testing.T) { t.Logf("t: %v\n", results) } -func TestFloatParsing(t *testing.T) { +func TestFloatParsingError(t *testing.T) { t.Parallel() type config struct { Float64 float64 `clap:"--float64"` diff --git a/clap/tag.go b/clap/tag.go index 827e598..a11e483 100644 --- a/clap/tag.go +++ b/clap/tag.go @@ -20,7 +20,7 @@ func getTrailingFieldDescription(tags []string, field reflect.StructField) (*fie return nil, fmt.Errorf("field '%s': %w (got '%s', expected 'trailing')", field.Name, ErrInvalidTag, field.Tag.Get("clap")) } - if fieldDesc.Type.Kind() != reflect.String && fieldDesc.Type.Elem().Kind() != reflect.String { + if fieldDesc.Type.Kind() != reflect.Slice || fieldDesc.Type.Elem().Kind() != reflect.String { return nil, fmt.Errorf("field '%s' should be a []string: %w", field.Name, ErrInvalidTag) } return fieldDesc, nil diff --git a/clap/tag_test.go b/clap/tag_test.go new file mode 100644 index 0000000..cbeace8 --- /dev/null +++ b/clap/tag_test.go @@ -0,0 +1,79 @@ +package clap_test + +import ( + "testing" + + "github.com/fred1268/go-clap/clap" +) + +func TestNonEmptyTrailing(t *testing.T) { + t.Parallel() + type config struct { + Field string `clap:"--string"` + Trailing []string `clap:"trailing,-t"` + } + cfg := &config{} + var err error + var results *clap.Results + if results, err = clap.Parse([]string{"--string", "hello"}, cfg); err == nil { + t.Errorf("unexpected trailing: %s", err) + } + t.Logf("t: %v\n", results) +} + +func TestNonStringTrailing(t *testing.T) { + t.Parallel() + type config struct { + Field string `clap:"--string"` + Trailing int `clap:"trailing"` + } + cfg := &config{} + var err error + var results *clap.Results + if results, err = clap.Parse([]string{"--string", "hello"}, cfg); err == nil { + t.Errorf("unexpected trailing: %s", err) + } + t.Logf("t: %v\n", results) +} + +func TestInvalidShortName(t *testing.T) { + t.Parallel() + type config struct { + Field string `clap:",-s,foo,bar"` + } + cfg := &config{} + var err error + var results *clap.Results + if results, err = clap.Parse([]string{"-s", "hello"}, cfg); err == nil { + t.Errorf("unexpected valid shortname: %s", err) + } + t.Logf("t: %v\n", results) +} + +func TestNoShortName(t *testing.T) { + t.Parallel() + type config struct { + Field string `clap:",-"` + } + cfg := &config{} + var err error + var results *clap.Results + if results, err = clap.Parse([]string{"-s", "hello"}, cfg); err == nil { + t.Errorf("unexpected valid shortname: %s", err) + } + t.Logf("t: %v\n", results) +} + +func TestInvalidShortNameParameter(t *testing.T) { + t.Parallel() + type config struct { + Field string `clap:",-s,unexpected"` + } + cfg := &config{} + var err error + var results *clap.Results + if results, err = clap.Parse([]string{"-s", "hello"}, cfg); err == nil { + t.Errorf("unexpected valid shortname: %s", err) + } + t.Logf("t: %v\n", results) +}