Skip to content

Commit f0b9399

Browse files
authored
Merge pull request #2236 from dearchap/improve_test_cov_112025
Improve test coverage
2 parents 987fbca + 994a960 commit f0b9399

File tree

13 files changed

+75
-50
lines changed

13 files changed

+75
-50
lines changed

args_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ import (
88
"github.com/stretchr/testify/require"
99
)
1010

11+
func TestArgNotSet(t *testing.T) {
12+
arg := &StringArg{
13+
Name: "sa",
14+
Value: "foo",
15+
}
16+
17+
require.Equal(t, "foo", arg.Get())
18+
}
19+
1120
func TestArgsFloatTypes(t *testing.T) {
1221
cmd := buildMinimalTestCommand()
1322
var fval float64

command.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,10 @@ func (cmd *Command) handleExitCoder(ctx context.Context, err error) error {
334334
}
335335

336336
func (cmd *Command) argsWithDefaultCommand(oldArgs Args) Args {
337-
if cmd.DefaultCommand != "" {
338-
rawArgs := append([]string{cmd.DefaultCommand}, oldArgs.Slice()...)
339-
newArgs := &stringSliceArgs{v: rawArgs}
337+
rawArgs := append([]string{cmd.DefaultCommand}, oldArgs.Slice()...)
338+
newArgs := &stringSliceArgs{v: rawArgs}
340339

341-
return newArgs
342-
}
343-
344-
return oldArgs
340+
return newArgs
345341
}
346342

347343
// Root returns the Command at the root of the graph

flag.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ func (f FlagsByName) Len() int {
7777
}
7878

7979
func (f FlagsByName) Less(i, j int) bool {
80-
if len(f[j].Names()) == 0 {
81-
return false
82-
} else if len(f[i].Names()) == 0 {
83-
return true
84-
}
8580
return lexicographicLess(f[i].Names()[0], f[j].Names()[0])
8681
}
8782

flag_bool.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ func (b boolValue) Create(val bool, p *bool, c BoolConfig) Value {
5050

5151
// ToString formats the bool value
5252
func (b boolValue) ToString(value bool) string {
53-
return strconv.FormatBool(value)
53+
b.destination = &value
54+
return b.String()
5455
}
5556

5657
// Below functions are to satisfy the flag.Value interface

flag_duration.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ func (d durationValue) Create(val time.Duration, p *time.Duration, c NoConfig) V
1818
}
1919

2020
func (d durationValue) ToString(val time.Duration) string {
21-
return fmt.Sprintf("%v", val)
21+
d = durationValue(val)
22+
return d.String()
2223
}
2324

2425
// Below functions are to satisfy the flag.Value interface
@@ -34,7 +35,9 @@ func (d *durationValue) Set(s string) error {
3435

3536
func (d *durationValue) Get() any { return time.Duration(*d) }
3637

37-
func (d *durationValue) String() string { return (*time.Duration)(d).String() }
38+
func (d *durationValue) String() string {
39+
return fmt.Sprintf("%v", time.Duration(*d))
40+
}
3841

3942
func (cmd *Command) Duration(name string) time.Duration {
4043
if v, ok := cmd.Value(name).(time.Duration); ok {

flag_float.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func (f floatValue[T]) Create(val T, p *T, c NoConfig) Value {
2525
}
2626

2727
func (f floatValue[T]) ToString(b T) string {
28-
return strconv.FormatFloat(float64(b), 'g', -1, int(unsafe.Sizeof(T(0))*8))
28+
f.val = &b
29+
return f.String()
2930
}
3031

3132
// Below functions are to satisfy the flag.Value interface

flag_generic.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ func (f genericValue) Create(val Value, p *Value, c NoConfig) Value {
1717
}
1818

1919
func (f genericValue) ToString(b Value) string {
20-
if b != nil {
21-
return b.String()
22-
}
23-
return ""
20+
f.val = b
21+
return f.String()
2422
}
2523

2624
// Below functions are to satisfy the flag.Value interface

flag_int.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ func (i intValue[T]) Create(val T, p *T, c IntegerConfig) Value {
3636
}
3737

3838
func (i intValue[T]) ToString(b T) string {
39-
if i.base == 0 {
40-
i.base = 10
41-
}
42-
43-
return strconv.FormatInt(int64(b), i.base)
39+
i.val = &b
40+
return i.String()
4441
}
4542

4643
// Below functions are to satisfy the flag.Value interface

flag_slice_base.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ func (i *SliceBase[T, C, VC]) Set(value string) error {
8282

8383
// String returns a readable representation of this value (for usage defaults)
8484
func (i *SliceBase[T, C, VC]) String() string {
85-
v := i.Value()
86-
var t T
87-
if reflect.TypeOf(t).Kind() == reflect.String {
88-
return fmt.Sprintf("%v", v)
85+
var defaultVals []string
86+
var v VC
87+
for _, s := range *i.slice {
88+
defaultVals = append(defaultVals, v.ToString(s))
8989
}
90-
return fmt.Sprintf("%T{%s}", v, i.ToString(v))
90+
return strings.Join(defaultVals, ", ")
9191
}
9292

9393
// Serialize allows SliceBase to fulfill Serializer
@@ -110,10 +110,6 @@ func (i *SliceBase[T, C, VC]) Get() interface{} {
110110
}
111111

112112
func (i SliceBase[T, C, VC]) ToString(t []T) string {
113-
var defaultVals []string
114-
var v VC
115-
for _, s := range t {
116-
defaultVals = append(defaultVals, v.ToString(s))
117-
}
118-
return strings.Join(defaultVals, ", ")
113+
i.slice = &t
114+
return i.String()
119115
}

flag_string.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ func (s stringValue) Create(val string, p *string, c StringConfig) Value {
3030
}
3131

3232
func (s stringValue) ToString(val string) string {
33-
if val == "" {
34-
return val
35-
}
36-
return fmt.Sprintf("%q", val)
33+
s.destination = &val
34+
return s.String()
3735
}
3836

3937
// Below functions are to satisfy the flag.Value interface
@@ -49,8 +47,8 @@ func (s *stringValue) Set(val string) error {
4947
func (s *stringValue) Get() any { return *s.destination }
5048

5149
func (s *stringValue) String() string {
52-
if s.destination != nil {
53-
return *s.destination
50+
if s.destination != nil && *s.destination != "" {
51+
return fmt.Sprintf("%q", *s.destination)
5452
}
5553
return ""
5654
}

0 commit comments

Comments
 (0)