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