-
Notifications
You must be signed in to change notification settings - Fork 10
/
clop_subcommand_test.go
175 lines (159 loc) · 4.32 KB
/
clop_subcommand_test.go
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
package clop
import (
"bytes"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
type add struct {
All bool `clop:"-A; --all" usage:"add changes from all tracked and untracked files"`
Force bool `clop:"-f; --force" usage:"allow adding otherwise ignored files"`
Pathspec []string `clop:"args=pathspec"`
isSet bool
}
func (a *add) SubMain() {
a.isSet = true
}
type mv struct {
Force bool `clop:"-f; --force" usage:"allow adding otherwise ignored files"`
isSet bool
}
func (m *mv) SubMain() {
m.isSet = true
}
type touch struct {
Force bool `clop:"-f; --force" usage:"allow adding otherwise ignored files"`
isSet bool
}
func (t *touch) SubMain() {
t.isSet = true
}
type git struct {
Add add `clop:"subcommand=add" usage:"Add file contents to the index"`
Mv mv `clop:"subcommand=mv" usage:"Move or rename a file, a directory, or a symlink"`
Touch touch `clop:"subcommand" usage:"touch file to the index"`
}
// 方法1
// 子命令自动调用SubMain接口
func Test_API_subcommand_SubMain(t *testing.T) {
// 测试正确的情况
for _, test := range []testAPI{
{
// 测试add子命令
func() git {
g := git{}
p := New([]string{"add", "-Af", "a.txt"}).SetExit(false)
err := p.Bind(&g)
assert.NoError(t, err)
assert.True(t, g.Add.isSet)
assert.False(t, g.Mv.isSet)
return g
}(), git{Add: add{All: true, Force: true, isSet: true, Pathspec: []string{"a.txt"}}}},
{
// 测试mv子命令
func() git {
g := git{}
p := New([]string{"mv", "-f"}).SetExit(false)
err := p.Bind(&g)
assert.NoError(t, err)
return g
}(), git{Mv: mv{Force: true, isSet: true}}},
{
// 测试touch子命令
func() git {
g := git{}
p := New([]string{"touch", "-f"}).SetExit(false)
err := p.Bind(&g)
assert.NoError(t, err)
return g
}(), git{Touch: touch{Force: true, isSet: true}}},
{
// 测试-h 输出的Usage
func() git {
g := git{}
p := New([]string{"-h"}).SetExit(false)
b := &bytes.Buffer{}
p.w = b
err := p.Bind(&g)
assert.NoError(t, err)
assert.True(t, checkUsage(b))
os.Stdout.Write(b.Bytes())
return g
}(), git{Add: add{}}},
} {
assert.Equal(t, test.need, test.got)
}
}
// 方法2
func Test_API_subcommand(t *testing.T) {
type add struct {
All bool `clop:"-A; --all" usage:"add changes from all tracked and untracked files"`
Force bool `clop:"-f; --force" usage:"allow adding otherwise ignored files"`
Pathspec []string `clop:"args=pathspec"`
isSet bool
}
type mv struct {
Force bool `clop:"-f; --force" usage:"allow adding otherwise ignored files"`
isSet bool
}
type touch struct {
Force bool `clop:"-f; --force" usage:"allow adding otherwise ignored files"`
isSet bool
}
type git struct {
Add add `clop:"subcommand=add" usage:"Add file contents to the index"`
Mv mv `clop:"subcommand=mv" usage:"Move or rename a file, a directory, or a symlink"`
Touch touch `clop:"subcommand" usage:"touch file to the index"`
}
// 测试正确的情况
for _, test := range []testAPI{
{
// 测试add子命令
func() git {
g := git{}
p := New([]string{"add", "-Af", "a.txt"}).SetExit(false)
err := p.Bind(&g)
assert.NoError(t, err)
assert.True(t, p.IsSetSubcommand("add"))
assert.False(t, p.IsSetSubcommand("mv"))
return g
}(), git{Add: add{All: true, Force: true, Pathspec: []string{"a.txt"}}}},
{
// 测试mv子命令
func() git {
g := git{}
p := New([]string{"mv", "-f"}).SetExit(false)
err := p.Bind(&g)
assert.NoError(t, err)
assert.False(t, p.IsSetSubcommand("add"))
assert.True(t, p.IsSetSubcommand("mv"))
return g
}(), git{Mv: mv{Force: true}}},
{
// 测试touch子命令
func() git {
g := git{}
p := New([]string{"touch", "-f"}).SetExit(false)
err := p.Bind(&g)
assert.NoError(t, err)
assert.False(t, p.IsSetSubcommand("add"))
assert.True(t, p.IsSetSubcommand("touch"))
return g
}(), git{Touch: touch{Force: true}}},
{
// 测试-h 输出的Usage
func() git {
g := git{}
p := New([]string{"-h"}).SetExit(false)
b := &bytes.Buffer{}
p.w = b
err := p.Bind(&g)
assert.NoError(t, err)
assert.True(t, checkUsage(b))
os.Stdout.Write(b.Bytes())
return g
}(), git{Add: add{}}},
} {
assert.Equal(t, test.need, test.got)
}
}