1
|
|
|
package gonfig |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"fmt" |
5
|
|
|
"os" |
6
|
|
|
"reflect" |
7
|
|
|
"testing" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
func init() { |
11
|
|
|
if err := os.Setenv("TEST_GET_ENV_STR", "THIS IS TEST"); err != nil { |
12
|
|
|
panic(err) |
13
|
|
|
} |
14
|
|
|
if err := os.Setenv("TEST_GET_ENV_ARR_STR", "THIS;IS;TEST"); err != nil { |
15
|
|
|
panic(err) |
16
|
|
|
} |
17
|
|
|
if err := os.Setenv("TEST_GET_ENV_INT", "202"); err != nil { |
18
|
|
|
panic(err) |
19
|
|
|
} |
20
|
|
|
if err := os.Setenv("PORT", "8080"); err != nil { |
21
|
|
|
panic(err) |
22
|
|
|
} |
23
|
|
|
if err := os.Setenv("APP_MODE", "test"); err != nil { |
24
|
|
|
panic(err) |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
func TestGetEnvStr(t *testing.T) { |
29
|
|
|
type args struct { |
30
|
|
|
key string |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
tests := []struct { |
34
|
|
|
name string |
35
|
|
|
args args |
36
|
|
|
want string |
37
|
|
|
}{ |
38
|
|
|
{ |
39
|
|
|
args: args{ |
40
|
|
|
key: "TEST_GET_ENV_STR", |
41
|
|
|
}, |
42
|
|
|
want: "THIS IS TEST", |
43
|
|
|
}, |
44
|
|
|
{ |
45
|
|
|
args: args{ |
46
|
|
|
key: "", |
47
|
|
|
}, |
48
|
|
|
want: "", |
49
|
|
|
}, |
50
|
|
|
} |
51
|
|
|
for _, tt := range tests { |
52
|
|
|
t.Run(tt.name, func(t *testing.T) { |
53
|
|
|
if got := GetEnvStr(tt.args.key); got != tt.want { |
54
|
|
|
t.Errorf("GetEnvStr() = %v, want %v", got, tt.want) |
55
|
|
|
} |
56
|
|
|
}) |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
func TestGetEnvArrStr(t *testing.T) { |
61
|
|
|
type args struct { |
62
|
|
|
key string |
63
|
|
|
} |
64
|
|
|
tests := []struct { |
65
|
|
|
name string |
66
|
|
|
args args |
67
|
|
|
want []string |
68
|
|
|
}{ |
69
|
|
|
{ |
70
|
|
|
args: args{ |
71
|
|
|
key: "TEST_GET_ENV_ARR_STR", |
72
|
|
|
}, |
73
|
|
|
want: []string{"THIS", "IS", "TEST"}, |
74
|
|
|
}, |
75
|
|
|
{ |
76
|
|
|
args: args{ |
77
|
|
|
key: "", |
78
|
|
|
}, |
79
|
|
|
want: nil, |
80
|
|
|
}, |
81
|
|
|
} |
82
|
|
|
for _, tt := range tests { |
83
|
|
|
t.Run(tt.name, func(t *testing.T) { |
84
|
|
|
if got := GetEnvArrStr(tt.args.key); !reflect.DeepEqual(got, tt.want) { |
85
|
|
|
t.Errorf("GetEnvArrStr() = %v, want %v", got, tt.want) |
86
|
|
|
} |
87
|
|
|
}) |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
func TestGetListenPort(t *testing.T) { |
92
|
|
|
port := "8080" |
93
|
|
|
tests := []struct { |
94
|
|
|
name string |
95
|
|
|
want *string |
96
|
|
|
}{ |
97
|
|
|
{ |
98
|
|
|
want: &port, |
99
|
|
|
}, |
100
|
|
|
} |
101
|
|
|
for _, tt := range tests { |
102
|
|
|
t.Run(tt.name, func(t *testing.T) { |
103
|
|
|
if got := GetListenPort(); !reflect.DeepEqual(got, tt.want) { |
104
|
|
|
t.Errorf("GetListenPort() = %v, want %v", got, tt.want) |
105
|
|
|
} else { |
106
|
|
|
fmt.Println("Good") |
107
|
|
|
} |
108
|
|
|
}) |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
func TestGetApplicationMode(t *testing.T) { |
113
|
|
|
mode := "test" |
114
|
|
|
tests := []struct { |
115
|
|
|
name string |
116
|
|
|
want *string |
117
|
|
|
}{ |
118
|
|
|
{ |
119
|
|
|
want: &mode, |
120
|
|
|
}, |
121
|
|
|
} |
122
|
|
|
for _, tt := range tests { |
123
|
|
|
t.Run(tt.name, func(t *testing.T) { |
124
|
|
|
if got := GetApplicationMode(); !reflect.DeepEqual(got, tt.want) { |
125
|
|
|
t.Errorf("GetApplicationMode() = %v, want %v", got, tt.want) |
126
|
|
|
} |
127
|
|
|
}) |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
func TestGetEnvInt(t *testing.T) { |
132
|
|
|
type args struct { |
133
|
|
|
key string |
134
|
|
|
} |
135
|
|
|
tests := []struct { |
136
|
|
|
name string |
137
|
|
|
args args |
138
|
|
|
want int |
139
|
|
|
}{ |
140
|
|
|
{ |
141
|
|
|
args: args{ |
142
|
|
|
key: "TEST_GET_ENV_INT", |
143
|
|
|
}, |
144
|
|
|
want: 202, |
145
|
|
|
}, |
146
|
|
|
{ |
147
|
|
|
args: args{ |
148
|
|
|
key: "TEST_GET_ENV_STR", |
149
|
|
|
}, |
150
|
|
|
want: 0, |
151
|
|
|
}, |
152
|
|
|
} |
153
|
|
|
for _, tt := range tests { |
154
|
|
|
t.Run(tt.name, func(t *testing.T) { |
155
|
|
|
if got := GetEnvInt(tt.args.key); got != tt.want { |
156
|
|
|
t.Errorf("GetEnvInt() = %v, want %v", got, tt.want) |
157
|
|
|
} |
158
|
|
|
}) |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
func TestGetEnvIntWithDefault(t *testing.T) { |
163
|
|
|
type args struct { |
164
|
|
|
key string |
165
|
|
|
defaultValue int |
166
|
|
|
} |
167
|
|
|
tests := []struct { |
168
|
|
|
name string |
169
|
|
|
args args |
170
|
|
|
want int |
171
|
|
|
}{ |
172
|
|
|
{ |
173
|
|
|
args: args{ |
174
|
|
|
key: "TEST_GET_ENV_INT", |
175
|
|
|
defaultValue: 202, |
176
|
|
|
}, |
177
|
|
|
want: 202, |
178
|
|
|
}, |
179
|
|
|
{ |
180
|
|
|
args: args{ |
181
|
|
|
key: "TEST_GET_ENV_STR", |
182
|
|
|
defaultValue: 202, |
183
|
|
|
}, |
184
|
|
|
want: 202, |
185
|
|
|
}, |
186
|
|
|
{ |
187
|
|
|
args: args{ |
188
|
|
|
key: "TEST_GET_ENV_STR", |
189
|
|
|
}, |
190
|
|
|
want: 0, |
191
|
|
|
}, |
192
|
|
|
} |
193
|
|
|
for _, tt := range tests { |
194
|
|
|
t.Run(tt.name, func(t *testing.T) { |
195
|
|
|
if got := GetEnvIntWithDefault(tt.args.key, tt.args.defaultValue); got != tt.want { |
196
|
|
|
t.Errorf("GetEnvIntWithDefault() = %v, want %v", got, tt.want) |
197
|
|
|
} |
198
|
|
|
}) |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
func TestGetEnvStrWithDefault(t *testing.T) { |
203
|
|
|
type args struct { |
204
|
|
|
key string |
205
|
|
|
defaultValue string |
206
|
|
|
} |
207
|
|
|
tests := []struct { |
208
|
|
|
name string |
209
|
|
|
args args |
210
|
|
|
want string |
211
|
|
|
}{ |
212
|
|
|
{ |
213
|
|
|
args: args{ |
214
|
|
|
key: "TEST_GET_ENV_STR", |
215
|
|
|
}, |
216
|
|
|
want: "THIS IS TEST", |
217
|
|
|
}, |
218
|
|
|
{ |
219
|
|
|
args: args{ |
220
|
|
|
key: "DEFAULT_VALUE", |
221
|
|
|
defaultValue: "DEFAULT_VALUE", |
222
|
|
|
}, |
223
|
|
|
want: "DEFAULT_VALUE", |
224
|
|
|
}, |
225
|
|
|
} |
226
|
|
|
for _, tt := range tests { |
227
|
|
|
t.Run(tt.name, func(t *testing.T) { |
228
|
|
|
if got := GetEnvStrWithDefault(tt.args.key, tt.args.defaultValue); got != tt.want { |
229
|
|
|
t.Errorf("GetEnvStrWithDefault() = %v, want %v", got, tt.want) |
230
|
|
|
} |
231
|
|
|
}) |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|