Skip to content

Commit

Permalink
up test coverage to 100% (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
fred1268 authored Mar 16, 2024
1 parent c79337f commit ed51277
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
11 changes: 7 additions & 4 deletions clap/clap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package clap_test
import (
"errors"
"reflect"
"slices"
"testing"

"github.com/fred1268/go-clap/clap"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion clap/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
79 changes: 79 additions & 0 deletions clap/tag_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit ed51277

Please sign in to comment.