Some `options` fixes

Add tests for bool type.
Fix subtle bug in TestOptionsApplyInvalid.
Fix options list formatting.
This commit is contained in:
DRON-666 2020-12-23 23:26:04 +03:00
parent cb6b0f6255
commit 332b1896d1
2 changed files with 30 additions and 8 deletions

View File

@ -23,8 +23,14 @@ Exit status is 0 if the command was successful, and non-zero if there was any er
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("All Extended Options:\n")
var maxLen int
for _, opt := range options.List() {
fmt.Printf(" %-15s %s\n", opt.Namespace+"."+opt.Name, opt.Text)
if l := len(opt.Namespace + "." + opt.Name); l > maxLen {
maxLen = l
}
}
for _, opt := range options.List() {
fmt.Printf(" %*s %s\n", -maxLen, opt.Namespace+"."+opt.Name, opt.Text)
}
},
}

View File

@ -125,6 +125,7 @@ type Target struct {
Name string `option:"name"`
ID int `option:"id"`
Timeout time.Duration `option:"timeout"`
Switch bool `option:"switch"`
Other string
}
@ -155,7 +156,15 @@ var setTests = []struct {
"timeout": "10m3s",
},
Target{
Timeout: time.Duration(10*time.Minute + 3*time.Second),
Timeout: 10*time.Minute + 3*time.Second,
},
},
{
Options{
"switch": "true",
},
Target{
Switch: true,
},
},
}
@ -202,6 +211,13 @@ var invalidSetTests = []struct {
"ns",
`time: missing unit in duration "?2134"?`,
},
{
Options{
"switch": "yes",
},
"ns",
`strconv.ParseBool: parsing "yes": invalid syntax`,
},
}
func TestOptionsApplyInvalid(t *testing.T) {
@ -213,9 +229,9 @@ func TestOptionsApplyInvalid(t *testing.T) {
t.Fatalf("expected error %v not found", test.err)
}
matched, err := regexp.MatchString(test.err, err.Error())
if err != nil {
t.Fatal(err)
matched, e := regexp.MatchString(test.err, err.Error())
if e != nil {
t.Fatal(e)
}
if !matched {
@ -226,11 +242,11 @@ func TestOptionsApplyInvalid(t *testing.T) {
}
func TestListOptions(t *testing.T) {
var teststruct = struct {
teststruct := struct {
Foo string `option:"foo" help:"bar text help"`
}{}
var tests = []struct {
tests := []struct {
cfg interface{}
opts []Help
}{
@ -281,7 +297,7 @@ func TestListOptions(t *testing.T) {
}
func TestAppendAllOptions(t *testing.T) {
var tests = []struct {
tests := []struct {
cfgs map[string]interface{}
opts []Help
}{