-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_test.go
164 lines (146 loc) · 3.93 KB
/
lib_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
package gostructor
import (
"os"
"testing"
"github.com/goreflect/gostructor/infra"
"github.com/goreflect/gostructor/pipeline"
"github.com/goreflect/gostructor/tags"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
type (
MyStruct struct {
Field1 []string
Field2 []int32 `cf_hocon:"field2"`
Field3 []float32
Field4 []bool `cf_hocon:"field4"`
}
NestedStruct1 struct {
Field1 string `cf_hocon:"test1"`
}
MyStruct2 struct {
NestedStruct1 NestedStruct1 `cf_hocon:"tururur"`
MyMap map[int]string `cf_hocon:"MyMap"`
}
// MyStruct3 struct {
// NestedStruct2 struct {
// Field1 string `cf_hocon:"test1"`
// } `cf_hocon:"node=planB"`
// }
// MyStruct4 struct {
// NestedStruct4 struct {
// Field1 string
// } `cf_hocon:"node=planC.tururu.tratatat.planZ"`
// }
EnvStruct struct {
Field1 int16 `cf_env:"myField1"`
Field2 string `cf_env:"myField2"`
Field3 bool `cf_env:"myField3"`
Field4 float32 `cf_env:"myField4"`
Field5 []bool `cf_env:"myField5"`
}
ManySourceStrategies struct {
Field1 int16 `cf_env:"myField1" cf_hocon:"field2" cf_default:"14"`
Field2 string `cf_hocon:"field2" cf_default:"test_tratata" cf_env:"test_tururu"`
}
)
func Test_parseHocon1(t *testing.T) {
os.Setenv(tags.HoconFile, "./test_configs/test1.hocon")
myStruct, err := ConfigureSetup(&MyStruct{}, pipeline.EmptyAdditionalPrefix, []infra.FuncType{infra.FunctionSetupHocon})
if err != nil {
t.Error("error while configurig: ", err)
return
}
assert.Equal(t, &MyStruct{
Field1: []string{"test1", "test2", "test3"},
Field2: []int32{112312323, 2, 123123123, 4},
Field3: []float32{1.2, 1.5, 1.7, 11.2},
Field4: []bool{true, false, false, true},
}, myStruct.(*MyStruct))
}
func Test_parseHocon(t *testing.T) {
os.Setenv(tags.HoconFile, "./test_configs/testmap.hocon")
myStruct, err := ConfigureSetup(&MyStruct2{}, pipeline.EmptyAdditionalPrefix, []infra.FuncType{infra.FunctionSetupHocon})
if err != nil {
t.Error("error while configuring: ", err)
return
}
assert.Equal(t, &MyStruct2{
NestedStruct1: NestedStruct1{
Field1: "testvalueInNestedStructure",
},
MyMap: map[int]string{
1: "test",
2: "test2",
3: "test3",
},
}, myStruct.(*MyStruct2))
}
func Test_getValueFromEnvironment(t *testing.T) {
os.Setenv("myField1", "12")
os.Setenv("myField2", "test")
os.Setenv("myField3", "true")
os.Setenv("myField4", "12.2")
os.Setenv("myField5", "true,false,true")
defer func() {
os.Unsetenv("myField1")
os.Unsetenv("myField2")
os.Unsetenv("myField3")
os.Unsetenv("myField4")
os.Unsetenv("myField5")
}()
myStruct, err := ConfigureSmart(&EnvStruct{})
if err != nil {
t.Error(err)
return
}
assert.Equal(t, &EnvStruct{
Field1: 12,
Field2: "test",
Field3: true,
Field4: 12.2,
Field5: []bool{true, false, true},
}, myStruct.(*EnvStruct))
}
func Test_configureEasy(t *testing.T) {
os.Setenv("myField1", "12")
os.Setenv("myField2", "test")
os.Setenv("myField3", "true")
os.Setenv("myField4", "12.2")
os.Setenv("myField5", "true,false,true")
defer func() {
os.Unsetenv("myField1")
os.Unsetenv("myField2")
os.Unsetenv("myField3")
os.Unsetenv("myField4")
os.Unsetenv("myField5")
}()
myStruct, err := ConfigureEasy(&EnvStruct{})
if err != nil {
t.Error(err)
return
}
assert.Equal(t, &EnvStruct{
Field1: 12,
Field2: "test",
Field3: true,
Field4: 12.2,
Field5: []bool{true, false, true},
}, myStruct.(*EnvStruct))
}
func TestChangeLogsParams(t *testing.T) {
ChangeLogLevel(logrus.DebugLevel)
ChangeLogFormatter(&logrus.JSONFormatter{})
}
func Test_covarianceSources(t *testing.T) {
os.Setenv("test_tururu", "tururum")
defer func() {
os.Unsetenv("test_tururu")
}()
myStruct, err := ConfigureSmart(&ManySourceStrategies{})
if err != nil {
t.Error(err)
return
}
t.Log(myStruct)
}