Skip to content

Commit d24513f

Browse files
committed
add some tests
1 parent aa4fdeb commit d24513f

4 files changed

Lines changed: 266 additions & 12 deletions

File tree

pkg/cli/bind.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,6 @@ func (f *flagConfigBinder) Decode(flags *FlagSet, element interface{}) error {
3535
}
3636

3737
func (f *flagConfigBinder) setValue(ctx *bindContext) error {
38-
/*if len(ctx.paths) == 0 {
39-
v := reflect.ValueOf(ctx.value)
40-
t := ctx.element.Type()
41-
if v.Type().AssignableTo(t) {
42-
ctx.element.Set(v)
43-
return nil
44-
} else if v.Type().ConvertibleTo(t) {
45-
ctx.element.Set(v.Convert(t))
46-
return nil
47-
}
48-
}*/
49-
5038
switch ctx.element.Kind() {
5139
case reflect.Struct:
5240
if len(ctx.paths) == 0 {

pkg/cli/flag_int_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package cli
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestFlagInt(t *testing.T) {
10+
type config struct {
11+
Foo int
12+
Bar int64
13+
Any interface{}
14+
}
15+
16+
testcases := []struct {
17+
name string
18+
cmd func() *Command
19+
args []string
20+
test func(t *testing.T, cmd *Command, args []string, err error)
21+
}{
22+
{
23+
name: "int value",
24+
cmd: func() *Command {
25+
c := &Command{Name: "foo", Config: &config{}}
26+
c.Flags().Int("foo", 0, FlagDoc{})
27+
return c
28+
},
29+
args: []string{"--foo 12"},
30+
test: func(t *testing.T, cmd *Command, args []string, err error) {
31+
require.NoError(t, err)
32+
require.Equal(t, 12, cmd.Flags().GetInt("foo"))
33+
require.Equal(t, &config{Foo: 12}, cmd.Config)
34+
},
35+
},
36+
{
37+
name: "float value",
38+
cmd: func() *Command {
39+
c := &Command{Name: "foo", Config: &config{}}
40+
c.Flags().Int("foo", 0, FlagDoc{})
41+
return c
42+
},
43+
args: []string{"--foo 12.4"},
44+
test: func(t *testing.T, cmd *Command, args []string, err error) {
45+
require.EqualError(t, err, "failed to set flag foo: parsing 12.4: invalid syntax")
46+
},
47+
},
48+
{
49+
name: "bind to int64",
50+
cmd: func() *Command {
51+
c := &Command{Name: "foo", Config: &config{}}
52+
c.Flags().Int("bar", 0, FlagDoc{})
53+
return c
54+
},
55+
args: []string{"--bar 12"},
56+
test: func(t *testing.T, cmd *Command, args []string, err error) {
57+
require.NoError(t, err)
58+
require.Equal(t, 12, cmd.Flags().GetInt("bar"))
59+
require.Equal(t, &config{Bar: 12}, cmd.Config)
60+
},
61+
},
62+
{
63+
name: "bind to interface{}",
64+
cmd: func() *Command {
65+
c := &Command{Name: "foo", Config: &config{}}
66+
c.Flags().Int("any", 0, FlagDoc{})
67+
return c
68+
},
69+
args: []string{"--any 12"},
70+
test: func(t *testing.T, cmd *Command, args []string, err error) {
71+
require.NoError(t, err)
72+
require.Equal(t, 12, cmd.Flags().GetInt("any"))
73+
require.Equal(t, &config{Any: 12}, cmd.Config)
74+
},
75+
},
76+
}
77+
for _, tc := range testcases {
78+
tc := tc
79+
t.Run(tc.name, func(t *testing.T) {
80+
var cmd *Command
81+
var args []string
82+
root := tc.cmd()
83+
root.SetArgs(tc.args)
84+
root.Run = func(c *Command, a []string) error {
85+
cmd = c
86+
args = a
87+
return nil
88+
}
89+
90+
err := root.Execute()
91+
92+
tc.test(t, cmd, args, err)
93+
})
94+
}
95+
}

pkg/cli/flag_slice_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package cli
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestFlagSlice(t *testing.T) {
10+
type config struct {
11+
Foo []string
12+
Explodes []string `explode:"explode"`
13+
}
14+
15+
testcases := []struct {
16+
name string
17+
cmd func() *Command
18+
args []string
19+
test func(t *testing.T, cmd *Command, args []string, err error)
20+
}{
21+
{
22+
name: "no default",
23+
cmd: func() *Command {
24+
c := &Command{Name: "foo", Config: &config{}}
25+
c.Flags().StringSlice("foo", nil, false, FlagDoc{})
26+
return c
27+
},
28+
args: []string{"--foo a b c"},
29+
test: func(t *testing.T, cmd *Command, args []string, err error) {
30+
require.NoError(t, err)
31+
require.Equal(t, []string{"a", "b", "c"}, cmd.Flags().GetStringSlice("foo"))
32+
require.Equal(t, &config{Foo: []string{"a", "b", "c"}}, cmd.Config)
33+
},
34+
},
35+
{
36+
name: "with default",
37+
cmd: func() *Command {
38+
c := &Command{Name: "foo", Config: &config{}}
39+
c.Flags().StringSlice("foo", []string{"zzz"}, false, FlagDoc{})
40+
return c
41+
},
42+
args: []string{"--foo a b c"},
43+
test: func(t *testing.T, cmd *Command, args []string, err error) {
44+
require.NoError(t, err)
45+
require.Equal(t, []string{"a", "b", "c"}, cmd.Flags().GetStringSlice("foo"))
46+
require.Equal(t, &config{Foo: []string{"a", "b", "c"}}, cmd.Config)
47+
},
48+
},
49+
{
50+
name: "value should be split",
51+
cmd: func() *Command {
52+
c := &Command{Name: "foo", Config: &config{}}
53+
c.Flags().StringSlice("foo", []string{"zzz"}, false, FlagDoc{})
54+
return c
55+
},
56+
args: []string{"--foo", "a,b,c"},
57+
test: func(t *testing.T, cmd *Command, args []string, err error) {
58+
require.NoError(t, err)
59+
require.Equal(t, []string{"a", "b", "c"}, cmd.Flags().GetStringSlice("foo"))
60+
require.Equal(t, &config{Foo: []string{"a", "b", "c"}}, cmd.Config)
61+
},
62+
},
63+
{
64+
name: "explode",
65+
cmd: func() *Command {
66+
c := &Command{Name: "foo", Config: &config{}}
67+
c.Flags().StringSlice("foo", nil, true, FlagDoc{})
68+
return c
69+
},
70+
args: []string{"--foo", "a", "--foo", "b", "--foo", "c"},
71+
test: func(t *testing.T, cmd *Command, args []string, err error) {
72+
require.NoError(t, err)
73+
require.Equal(t, []string{"a", "b", "c"}, cmd.Flags().GetStringSlice("foo"))
74+
require.Equal(t, &config{Foo: []string{"a", "b", "c"}}, cmd.Config)
75+
},
76+
},
77+
{
78+
name: "explode tag",
79+
cmd: func() *Command {
80+
c := &Command{Name: "foo", Config: &config{}}
81+
c.Flags().StringSlice("explode", nil, true, FlagDoc{})
82+
return c
83+
},
84+
args: []string{"--explode", "a", "--explode", "b", "--explode", "c"},
85+
test: func(t *testing.T, cmd *Command, args []string, err error) {
86+
require.NoError(t, err)
87+
require.Equal(t, []string{"a", "b", "c"}, cmd.Flags().GetStringSlice("explode"))
88+
require.Equal(t, &config{Explodes: []string{"a", "b", "c"}}, cmd.Config)
89+
},
90+
},
91+
}
92+
for _, tc := range testcases {
93+
tc := tc
94+
t.Run(tc.name, func(t *testing.T) {
95+
var cmd *Command
96+
var args []string
97+
root := tc.cmd()
98+
root.SetArgs(tc.args)
99+
root.Run = func(c *Command, a []string) error {
100+
cmd = c
101+
args = a
102+
return nil
103+
}
104+
105+
err := root.Execute()
106+
107+
tc.test(t, cmd, args, err)
108+
})
109+
}
110+
}

pkg/cli/flag_string_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cli_test
2+
3+
import (
4+
"mokapi/pkg/cli"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestFlagString(t *testing.T) {
11+
type config struct {
12+
Foo string
13+
}
14+
15+
testcases := []struct {
16+
name string
17+
args []string
18+
test func(t *testing.T)
19+
}{
20+
{
21+
name: "string value",
22+
test: func(t *testing.T) {
23+
c := cli.Command{Config: &config{}, Run: func(cmd *cli.Command, args []string) error {
24+
return nil
25+
}}
26+
c.Flags().String("foo", "", cli.FlagDoc{})
27+
c.SetArgs([]string{"--foo bar"})
28+
29+
err := c.Execute()
30+
31+
require.NoError(t, err)
32+
require.Equal(t, "bar", c.Flags().GetString("foo"))
33+
require.Equal(t, &config{Foo: "bar"}, c.Config)
34+
},
35+
},
36+
{
37+
name: "file",
38+
test: func(t *testing.T) {
39+
path := createTempFile(t, "test.yml", "foobar")
40+
41+
c := cli.Command{Config: &config{}, Run: func(cmd *cli.Command, args []string) error {
42+
return nil
43+
}}
44+
c.Flags().String("foo", "", cli.FlagDoc{})
45+
c.SetArgs([]string{"--foo file:" + path})
46+
47+
err := c.Execute()
48+
49+
require.NoError(t, err)
50+
require.Equal(t, "file:"+path, c.Flags().GetString("foo"))
51+
require.Equal(t, &config{Foo: "foobar"}, c.Config)
52+
},
53+
},
54+
}
55+
for _, tc := range testcases {
56+
tc := tc
57+
t.Run(tc.name, func(t *testing.T) {
58+
tc.test(t)
59+
})
60+
}
61+
}

0 commit comments

Comments
 (0)