Repository navigation
Expand file tree
/
Copy pathcompile-node_create_test.go
More file actions
181 lines (175 loc) · 5.11 KB
/
Copy pathcompile-node_create_test.go
File metadata and controls
181 lines (175 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package cli
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/echocat/cli4g/codec"
)
func TestCreateRejectsRequiredParentArguments(t *testing.T) {
type options struct {
Optional string `argument:"optional"`
Value string `argument:"value" required:"true"`
}
for _, test := range []struct {
name string
path string
create func() (Container, error)
}{
{
name: "explicit application argument",
path: "app",
create: func() (Container, error) {
return NewSimpleApplication("app").
WithArgument(
NewArgument(codec.NewString(), "optional"),
NewArgument(codec.NewString(), "value").
WithRequired(true),
).
WithCommand(NewSimpleCommand("child")).
Create()
},
},
{
name: "tagged application argument",
path: "app",
create: func() (Container, error) {
return NewApplication[*options]("app").
WithCommand(NewSimpleCommand("child")).
Create()
},
},
{
name: "explicit nested command argument",
path: "app group parent",
create: func() (Container, error) {
return NewSimpleApplication("app").
WithCommand(NewSimpleCommand("group").
WithCommand(NewSimpleCommand("parent").
WithArgument(NewArgument(codec.NewString(), "value").
WithRequired(true),
).
WithCommand(NewSimpleCommand("leaf")),
),
).
Create()
},
},
{
name: "tagged nested command argument",
path: "app group parent",
create: func() (Container, error) {
return NewSimpleApplication("app").
WithCommand(NewSimpleCommand("group").
WithCommand(NewCommand[options]("parent").
WithCommand(NewSimpleCommand("leaf")),
),
).
Create()
},
},
{
name: "required variadic argument with zero minimum arity",
path: "app",
create: func() (Container, error) {
return NewSimpleApplication("app").
WithArgument(NewArgument(codec.NewStrings(), "value").
WithRequired(true),
).
WithCommand(NewSimpleCommand("child")).
Create()
},
},
} {
t.Run(test.name, func(t *testing.T) {
app, err := test.create()
require.EqualError(t, err, test.path+`: required positional argument "value" cannot be combined with subcommands`)
assert.Nil(t, app)
})
}
}
func TestCreateAllowsOptionalParentArgumentsAndRequiredFlags(t *testing.T) {
type rootOptions struct {
Token string `flag:"token" required:"true"`
Value string `argument:"root-value"`
}
type parentOptions struct {
Values []string `argument:"parent-values"`
}
type leafOptions struct {
Value string `argument:"leaf-value" required:"true"`
}
app := NewApplication[rootOptions]("app").
WithHandler(func(context.Context, rootOptions) error { return nil }).
WithCommand(NewCommand[*parentOptions]("parent").
WithHandler(func(context.Context, *parentOptions) error { return nil }).
WithCommand(NewCommand[leafOptions]("leaf")),
).
MustCreate()
parent, found := FindCommand[*parentOptions](app, "parent")
require.True(t, found)
leaf, found := FindCommand[leafOptions](app, "parent", "leaf")
require.True(t, found)
for _, test := range []struct {
name string
args []string
names []string
rootValue string
parentData []string
leafValue string
}{
{
name: "application's own argument",
args: []string{"--token=token-value", "root-data"},
names: []string{},
rootValue: "root-data",
},
{
name: "parent's own variadic argument",
args: []string{"--token=token-value", "parent", "one", "two"},
names: []string{"parent"},
parentData: []string{"one", "two"},
},
{
name: "leaf's required argument with omitted parent arguments",
args: []string{"parent", "leaf", "--token=token-value", "leaf-data"},
names: []string{"parent", "leaf"},
leafValue: "leaf-data",
},
} {
t.Run(test.name, func(t *testing.T) {
invocation, err := app.Parse(context.Background(), nil, test.args...)
require.NoError(t, err)
defer func() { assert.NoError(t, invocation.Close()) }()
assert.Equal(t, test.names, invocation.NameStack())
_, rootData := invocation.Application()
assert.Equal(t, "token-value", rootData.Token)
assert.Equal(t, test.rootValue, rootData.Value)
if len(test.names) > 0 {
parentData, found := parent.OptionsFrom(invocation)
require.True(t, found)
assert.Equal(t, test.parentData, parentData.Values)
}
if len(test.names) > 1 {
leafData, found := leaf.OptionsFrom(invocation)
require.True(t, found)
assert.Equal(t, test.leafValue, leafData.Value)
}
})
}
}
func TestCreateAllowsRequiredApplicationArgumentsWithoutSubcommands(t *testing.T) {
app, err := NewSimpleApplication("app").
WithArgument(NewArgument(codec.NewString(), "value").
WithRequired(true),
).
Create()
require.NoError(t, err)
invocation, err := app.Parse(context.Background(), nil, "data")
require.NoError(t, err)
defer func() { assert.NoError(t, invocation.Close()) }()
argument, value := invocation.Argument(0)
require.NotNil(t, argument)
assert.Equal(t, "value", argument.Name())
assert.Equal(t, "data", value)
}