-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_collection_test.go
194 lines (177 loc) · 4.93 KB
/
parser_collection_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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package envconf_test
import (
"fmt"
"os"
"reflect"
"strconv"
"testing"
"github.com/antonmashko/envconf"
)
func TestParse_Array_Ok(t *testing.T) {
cfg := struct {
Field [5]int `env:"TEST_PARSE_ARRAY_OK"`
}{}
os.Setenv("TEST_PARSE_ARRAY_OK", "-2, -1,0, 1 ,2 ")
expectedResult := [5]int{-2, -1, 0, 1, 2}
if err := envconf.Parse(&cfg); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(cfg.Field, expectedResult) {
t.Fatalf("incorrect result. expected=%v actual=%v", expectedResult, cfg.Field)
}
}
func TestParse_Array_ErrOutOfRange(t *testing.T) {
cfg := struct {
Field [2]int `env:"TEST_PARSE_ARRAY_OK"`
}{}
os.Setenv("TEST_PARSE_ARRAY_OK", "-2, -1,0, 1 ,2 ")
if err := envconf.Parse(&cfg); err == nil {
t.Fatal("expected error but got nil")
}
}
func TestParse_Slice_Ok(t *testing.T) {
cfg := struct {
Field []int `env:"TEST_PARSE_SLICE_OK"`
}{}
os.Setenv("TEST_PARSE_SLICE_OK", "-2, -1,0, 1 ,2 ")
expectedResult := []int{-2, -1, 0, 1, 2}
if err := envconf.Parse(&cfg); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(cfg.Field, expectedResult) {
t.Fatalf("incorrect result. expected=%v actual=%v", expectedResult, cfg.Field)
}
}
func TestParse_EmptyInterfaceSlice_Ok(t *testing.T) {
cfg := struct {
Slice []interface{}
}{}
if err := envconf.Parse(&cfg); err != nil {
t.Fatal(err)
}
}
func TestParse_InterfaceSlice_Ok(t *testing.T) {
f1 := &struct {
Foo string `env:"TEST_PARSE_INTERFACE_SLICE_OK_FOO1"`
}{}
f2 := &struct {
Foo int `env:"TEST_PARSE_INTERFACE_SLICE_OK_FOO2"`
}{}
f3 := &struct {
Foo float64 `env:"TEST_PARSE_INTERFACE_SLICE_OK_FOO3"`
}{}
cfg := struct {
Field []interface{}
}{
Field: []interface{}{
f1,
f2,
[]interface{}{f3},
},
}
expectedF1 := "test"
expectedF2 := 432
expectedF3 := 43.2
os.Setenv("TEST_PARSE_INTERFACE_SLICE_OK_FOO1", expectedF1)
os.Setenv("TEST_PARSE_INTERFACE_SLICE_OK_FOO2", strconv.Itoa(expectedF2))
os.Setenv("TEST_PARSE_INTERFACE_SLICE_OK_FOO3", fmt.Sprint(expectedF3))
if err := envconf.Parse(&cfg); err != nil {
t.Fatal(err)
}
if f1.Foo != expectedF1 {
t.Fatalf("incorrect result. expected[0]=%v actual[0]=%v", expectedF1, f1.Foo)
}
if f2.Foo != expectedF2 {
t.Fatalf("incorrect result. expected[1]=%v actual[1]=%v", expectedF2, f2.Foo)
}
if f3.Foo != expectedF3 {
t.Fatalf("incorrect result. expected[1]=%v actual[1]=%v", expectedF3, f3.Foo)
}
}
func TestParse_Slice_ErrInvalidElement(t *testing.T) {
cfg := struct {
Field []int `env:"TEST_PARSE_SLICE_ErrInvalidElement"`
}{}
os.Setenv("TEST_PARSE_SLICE_ErrInvalidElement", "-2,-1,0,x,i")
if err := envconf.Parse(&cfg); err == nil {
t.Fatal("expected error but got nil")
}
}
func TestParse_StringByteSlice_Ok(t *testing.T) {
cfg := struct {
Field []byte `default:"abc"`
}{}
if err := envconf.Parse(&cfg); err != nil {
t.Fatal("envconf.Parse: ", err)
}
const expected = "abc"
if string(cfg.Field) != expected {
Fatal(t, expected, string(cfg.Field), "cfg.Field")
}
}
func TestParse_Map_Ok(t *testing.T) {
cfg := struct {
Field map[int]string `env:"TEST_PARSE_MAP_OK"`
}{}
os.Setenv("TEST_PARSE_MAP_OK", "0:ok,1:2,2:test")
expectedResult := map[int]string{0: "ok", 1: "2", 2: "test"}
if err := envconf.Parse(&cfg); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(cfg.Field, expectedResult) {
t.Fatalf("incorrect result. expected=%v actual=%v", expectedResult, cfg.Field)
}
}
func TestParse_MapValueInterfaceDefinedValue_Ok(t *testing.T) {
v1 := &struct {
Field1 int `default:"5"`
}{}
cfg := struct {
Field map[string]interface{}
}{
Field: map[string]interface{}{
"xyz": v1,
},
}
if err := envconf.Parse(&cfg); err != nil {
t.Fatal(err)
}
if v1.Field1 != 5 {
t.Fatalf("incorrect result. expected=%v actual=%v", 5, v1.Field1)
}
}
func TestParse_MapValueInterface_Ok(t *testing.T) {
cfg := struct {
Field1 map[string]interface{} `default:"test1:1,test2:2"`
Field2 map[int64]interface{} `default:"1:test1,2:test2"`
}{}
if err := envconf.Parse(&cfg); err != nil {
t.Fatal(err)
}
if cfg.Field1["test1"] != "1" || cfg.Field1["test2"] != "2" {
t.Fatalf("incorrect result map[string]interface{}. actual=%#v", cfg.Field1)
}
if cfg.Field2[1] != "test1" || cfg.Field2[2] != "test2" {
t.Fatalf("incorrect result map[string]interface{}. actual=%#v", cfg.Field2)
}
}
func TestParse_Map_ErrUnsupportedType(t *testing.T) {
t.Run("InvalidKey", func(t *testing.T) {
cfg := struct {
Field map[struct{ k interface{} }]interface{} `env:"TEST_PARSE_MAP_OK_ErrUnsupportedType"`
}{}
os.Setenv("TEST_PARSE_MAP_OK_ErrUnsupportedType", "1:1")
if err := envconf.Parse(&cfg); err == nil {
t.Fatal("expected error but got nil")
}
})
t.Run("InvalidKeyValue_KeyInputOnly", func(t *testing.T) {
cfg := struct {
Field map[struct{ k interface{} }]interface{} `env:"TEST_PARSE_MAP_OK_ErrUnsupportedType"`
}{}
os.Setenv("TEST_PARSE_MAP_OK_ErrUnsupportedType", "1")
if err := envconf.Parse(&cfg); err == nil {
t.Fatal("expected error but got nil")
}
})
}