-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_test.go
More file actions
181 lines (161 loc) · 5.43 KB
/
Copy pathparse_test.go
File metadata and controls
181 lines (161 loc) · 5.43 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 confparse
import (
"flag"
"github.com/stretchr/testify/assert"
"os"
"runtime"
"strings"
"testing"
"time"
)
type testParseContainer struct {
Addr string `name:"addr" value:":8000" usage:"Listen and serve address"`
DatabaseUrl string `name:"databaseUrl" value:"mongodb://localhost:27017/db" usage:"Database connection url"`
Timeout time.Duration `name:"timeout" value:"200ms" usage:"Timeout value"`
OptionalVal string `name:"optional"`
ApiKey string `name:"apiKey" envVar:"API_KEY" usage:"API key"`
BatchSize int `name:"batchSize" envVar:"BATCH_SIZE" usage:"Batch size for query"`
MaxCount int64 `name:"maxCount"`
Debug bool `name:"debug" envVar:"DEBUG"`
Profit uint `name:"profit" value:"500"`
NonProfit uint `name:"nonProfit"`
MegaProfit uint64 `name:"megaProfit"`
}
func TestParse(t *testing.T) {
var testingTable = []struct {
title string
args []string
environment map[string]string
excepted *testParseContainer
}{
{
"String parse success",
[]string{"-addr", "localhost:8000"},
map[string]string{"API_KEY": "test-key"},
&testParseContainer{Addr: "localhost:8000", DatabaseUrl: "mongodb://localhost:27017/db", Timeout: 200 * time.Millisecond, ApiKey: "test-key", Profit: 500},
},
{
"Duration parse success",
[]string{"-timeout", "30s"},
map[string]string{},
&testParseContainer{Addr: ":8000", DatabaseUrl: "mongodb://localhost:27017/db", Timeout: 30 * time.Second, Profit: 500},
},
{
"Int parse success",
[]string{"-batchSize", "10000"},
map[string]string{},
&testParseContainer{Addr: ":8000", DatabaseUrl: "mongodb://localhost:27017/db", Timeout: 200 * time.Millisecond, BatchSize: 10000, Profit: 500},
},
{
"Bool parse success",
[]string{"-debug"},
map[string]string{},
&testParseContainer{Addr: ":8000", DatabaseUrl: "mongodb://localhost:27017/db", Timeout: 200 * time.Millisecond, Debug: true, Profit: 500},
},
{
"Int64 parse success",
[]string{"-maxCount", "850300"},
map[string]string{},
&testParseContainer{Addr: ":8000", DatabaseUrl: "mongodb://localhost:27017/db", Timeout: 200 * time.Millisecond, MaxCount: 850300, Profit: 500},
},
{
"Uint and Uint64 parse success",
[]string{"-profit", "100", "-megaProfit", "500000"},
map[string]string{},
&testParseContainer{Addr: ":8000", DatabaseUrl: "mongodb://localhost:27017/db", Timeout: 200 * time.Millisecond, Profit: 100, MegaProfit: 500000},
},
}
for _, tt := range testingTable {
t.Run(tt.title, func(t *testing.T) {
// clear flags
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
// clear env vars
os.Clearenv()
// set environment vars
for key, value := range tt.environment {
assert.NoError(t, os.Setenv(key, value), "must be set environment")
}
// insert arguments
os.Args = []string{"test"}
os.Args = append(os.Args, tt.args...)
// testing run
actual := &testParseContainer{}
assert.NoError(t, Parse(actual), "must be parse without errors")
assert.Equal(t, tt.excepted, actual, "must be equal parse result")
})
}
}
type testParseErrorsBoolContainer struct {
Debug bool `name:"debug" value:"foo"`
}
type testParseErrorsIntContainer struct {
Size int `name:"size" value:"abc"`
}
type testParseErrorsInt64Container struct {
Size64 int64 `name:"size64" value:"abc"`
}
type testParseErrorsUintContainer struct {
Profit uint `name:"profit" value:"-100"`
}
type testParseErrorsUint64Container struct {
Profit64 uint64 `name:"profit" value:"-1000"`
}
type testParseErrorsDurationContainer struct {
Timeout time.Duration `name:"timeout" value:"bad"`
}
func TestParseErrors(t *testing.T) {
var testingTable = []struct {
title string
actual interface{}
errorMatrix map[string]string
}{
{
"Invalid bool default value throw error",
&testParseErrorsBoolContainer{},
map[string]string{"": `strconv.ParseBool: parsing "foo": invalid syntax`},
},
{
"Invalid int default value throw error",
&testParseErrorsIntContainer{},
map[string]string{"": `strconv.Atoi: parsing "abc": invalid syntax`},
},
{
"Invalid int64 default value throw error",
&testParseErrorsInt64Container{},
map[string]string{"": `strconv.ParseInt: parsing "abc": invalid syntax`},
},
{
"Invalid uint default value throw error",
&testParseErrorsUintContainer{},
map[string]string{"": `strconv.ParseUint: parsing "-100": invalid syntax`},
},
{
"Invalid uint64 default value throw error",
&testParseErrorsUint64Container{},
map[string]string{"": `strconv.ParseUint: parsing "-1000": invalid syntax`},
},
{
"Invalid duration default value throw error",
&testParseErrorsDurationContainer{},
map[string]string{
"": `time: invalid duration "bad"`,
"go1.14": `time: invalid duration bad`,
},
},
}
for _, tt := range testingTable {
t.Run(tt.title, func(t *testing.T) {
// clear flags
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
// insert arguments
os.Args = []string{"test"}
// testing run
minorVersion := strings.Join(strings.Split(runtime.Version(), ".")[:len(strings.Split(runtime.Version(), "."))-1], ".")
exceptedError := tt.errorMatrix[minorVersion]
if len(exceptedError) == 0 {
exceptedError = tt.errorMatrix[""]
}
assert.EqualError(t, Parse(tt.actual), exceptedError, "must be parse without errors")
})
}
}