-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_test.go
More file actions
133 lines (118 loc) · 2.6 KB
/
Copy pathcommand_test.go
File metadata and controls
133 lines (118 loc) · 2.6 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
package cli
import (
"testing"
)
type TestConfig struct {
Name string `arg:"name" help:"Name"`
Port int `arg:"port" help:"Port"`
}
type RequiredConfig struct {
Name string `arg:"name" help:"Name" rules:"required"`
}
func Test_BaseCommand_Name(t *testing.T) {
tests := []struct {
name string
set string
get string
expected string
}{
{
name: "set and get",
set: "deploy",
get: "",
expected: "deploy",
},
{
name: "set returns the name",
set: "run",
get: "run",
expected: "run",
},
{
name: "empty before set",
set: "",
get: "",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := NewBaseCommand[TestConfig]()
if tt.set != "" {
cmd.Name(tt.set)
}
assertEqual(t, tt.expected, cmd.Name(tt.get))
})
}
}
func Test_BaseCommand_Add(t *testing.T) {
cmd := NewBaseCommand[TestConfig]()
assertEmpty(t, cmd.Commands())
sub := &MockCommand{BaseCommand: NewBaseCommand[MockCommandConfig]()}
cmd.Add("sub1", sub)
assertLen(t, cmd.Commands(), 1)
assertEqual(t, "sub1", cmd.Commands()[0].Name(""))
sub2 := &MockCommand{BaseCommand: NewBaseCommand[MockCommandConfig]()}
cmd.Add("sub2", sub2)
assertLen(t, cmd.Commands(), 2)
assertEqual(t, "sub2", cmd.Commands()[1].Name(""))
}
func Test_BaseCommand_Options(t *testing.T) {
tests := []struct {
name string
}{
{name: "initializes nil inputs"},
{name: "returns same pointer on second call"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := NewBaseCommand[TestConfig]()
opts := cmd.Options()
assertNotNil(t, opts)
opts2 := cmd.Options()
assertEqual(t, opts, opts2)
})
}
}
func Test_BaseCommand_Validate(t *testing.T) {
tests := []struct {
name string
vars map[string]any
wantErr bool
}{
{
name: "valid options",
vars: map[string]any{"name": "test", "port": "8080"},
wantErr: false,
},
{
name: "empty options",
vars: map[string]any{},
wantErr: false,
},
{
name: "partial options",
vars: map[string]any{"port": "8080"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := NewBaseCommand[TestConfig]()
cmd.Options()
err := cmd.Validate(tt.vars)
if tt.wantErr {
assertError(t, err)
return
}
assertNoError(t, err)
})
}
}
func Test_BaseCommand_Validate_RequiredFails(t *testing.T) {
cmd := NewBaseCommand[RequiredConfig]()
cmd.Options()
err := cmd.Validate(map[string]any{})
assertError(t, err)
assertErrorIs(t, err, ErrValidationFailed)
}