-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
158 lines (122 loc) · 3.64 KB
/
parser_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
package dynflags_test
import (
"testing"
"time"
"github.com/containeroo/dynflags"
"github.com/stretchr/testify/assert"
)
func TestDynFlagsParse(t *testing.T) {
t.Parallel()
t.Run("Parse valid arguments", func(t *testing.T) {
t.Parallel()
df := dynflags.New(dynflags.ContinueOnError)
group := df.Group("http")
group.String("method", "GET", "HTTP method to use")
group.String("url", "", "Target URL")
args := []string{
"--http.identifier1.method", "POST",
"--http.identifier1.url=https://example.com",
}
err := df.Parse(args)
assert.NoError(t, err)
parsedGroups := df.Parsed()
httpGroup := parsedGroups.Lookup("http")
assert.NotNil(t, httpGroup)
identifier1 := httpGroup.Lookup("identifier1")
assert.NotNil(t, identifier1)
assert.Equal(t, "POST", identifier1.Lookup("method"))
assert.Equal(t, "https://example.com", identifier1.Lookup("url"))
})
t.Run("Exit on missing key", func(t *testing.T) {
df := dynflags.New(dynflags.ExitOnError)
group := df.Group("http")
group.String("method", "GET", "HTTP method to use")
args := []string{
"-http.identifier1", "https://example.com",
}
err := df.Parse(args)
assert.Error(t, err)
})
t.Run("Parse with missing value", func(t *testing.T) {
t.Parallel()
df := dynflags.New(dynflags.ContinueOnError)
group := df.Group("http")
group.String("method", "GET", "HTTP method to use")
args := []string{
"--http.identifier1.method",
}
err := df.Parse(args)
assert.NoError(t, err)
unparsedArgs := df.UnknownArgs()
assert.Contains(t, unparsedArgs, "--http.identifier1.method")
})
t.Run("Parse with wrong value type and continue", func(t *testing.T) {
t.Parallel()
df := dynflags.New(dynflags.ContinueOnError)
group := df.Group("http")
group.Duration("timeout", 10*time.Second, "HTTP timeout")
args := []string{
"--http.identifier1.timeout", "1",
}
err := df.Parse(args)
assert.NoError(t, err)
unparsedArgs := df.UnknownArgs()
assert.Contains(t, unparsedArgs, "--http.identifier1.timeout")
})
t.Run("Parse with invalid flag format", func(t *testing.T) {
t.Parallel()
df := dynflags.New(dynflags.ContinueOnError)
args := []string{
"-invalidFlag",
}
err := df.Parse(args)
assert.NoError(t, err)
unparsedArgs := df.UnknownArgs()
assert.Contains(t, unparsedArgs, "-invalidFlag")
})
t.Run("Parse with no identifier and exit", func(t *testing.T) {
t.Parallel()
df := dynflags.New(dynflags.ExitOnError)
group1 := df.Group("http")
group1.Duration("timeout", 10*time.Second, "HTTP timeout")
args := []string{
"--http.duration", "10s",
}
err := df.Parse(args)
assert.Error(t, err)
})
t.Run("Parse with unknown group and exit on error", func(t *testing.T) {
t.Parallel()
df := dynflags.New(dynflags.ExitOnError)
args := []string{
"--unknown.identifier1.flag1", "value1",
}
err := df.Parse(args)
assert.Error(t, err)
assert.EqualError(t, err, "unknown flag 'flag1' in group 'unknown'")
})
t.Run("Handle invalid key format", func(t *testing.T) {
t.Parallel()
df := dynflags.New(dynflags.ContinueOnError)
args := []string{
"--invalidformat",
}
err := df.Parse(args)
assert.NoError(t, err)
unparsedArgs := df.UnknownArgs()
assert.Contains(t, unparsedArgs, "--invalidformat")
})
t.Run("Handle missing flag value", func(t *testing.T) {
t.Parallel()
df := dynflags.New(dynflags.ContinueOnError)
group := df.Group("http")
group.String("method", "GET", "HTTP method to use")
args := []string{
"--http.identifier1.method",
}
err := df.Parse(args)
assert.NoError(t, err)
unparsedArgs := df.UnknownArgs()
assert.Contains(t, unparsedArgs, "--http.identifier1.method")
})
}