-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettmain_test.go
174 lines (143 loc) · 3.54 KB
/
settmain_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
package settman
import (
"os"
"testing"
)
var optionalUint, mandatoryUint *setting
var optionalString, mandatoryString *setting
var optionalBoolean, mandatoryBoolean *setting
func TestMain(m *testing.M) {
optionalUint = NewSetting(
"optionalUint",
Uint8,
uint8(1),
)
optionalString = NewSetting(
"optionalString",
String,
"HolaTest",
)
optionalBoolean = NewSetting(
"optionalBoolean",
Boolean,
true,
)
mandatoryUint = NewSetting(
"mandatoryUint",
Uint8,
nil,
)
mandatoryString = NewSetting(
"mandatoryString",
String,
nil,
)
mandatoryBoolean = NewSetting(
"mandatoryBoolean",
Boolean,
nil,
)
os.Exit(m.Run())
}
// Get values before parsing
func TestGetWithoutEnv(t *testing.T) {
opUint := optionalUint
opString := optionalString
opBoolean := optionalBoolean
maUint := mandatoryUint
maString := mandatoryString
maBoolean := mandatoryBoolean
//This are going to use default value
if nil != opUint.Parse() {
t.Error("Parse on opUint expected to succeed but failed")
t.Fail()
}
if nil != opString.Parse() {
t.Error("Parse on opString expected to succeed but failed")
t.Fail()
}
if nil != opBoolean.Parse() {
t.Error("Parse on opBoolean expected to succeed but failed")
t.Fail()
}
if nil == maUint.Parse() {
t.Error("Parse on maUint expected to fail but succeeded")
t.Fail()
}
if nil == maString.Parse() {
t.Error("Parse on maString expected to fail but succeeded")
t.Fail()
}
if nil == maBoolean.Parse() {
t.Error("Parse on maBoolean expected to fail but succeeded")
t.Fail()
}
//Test parse with invalid types
_ = os.Setenv("mandatoryUint", "100.0") //Expect uint but got float
_ = os.Setenv("mandatoryBoolean", "1") //Expect boolean but got a number
if nil == maUint.Parse() {
t.Error("Parse on maUint expected to fail but succeeded")
t.Fail()
}
if nil == maBoolean.Parse() {
t.Error("Parse on maBoolean expected to fail but succeeded")
t.Fail()
}
if opUint.Get().(uint8) != uint8(1) {
t.Error("Fail to get default value")
t.Fail()
}
if opString.Get().(string) != "HolaTest" {
t.Error("Fail to get default value")
t.Fail()
}
if opBoolean.Get().(bool) != true {
t.Error("Fail to get default value")
t.Fail()
}
}
// Get values after parsing
func TestGetWithEnv(t *testing.T) {
opUint := optionalUint
opString := optionalString
opBoolean := optionalBoolean
maUint := mandatoryUint
maString := mandatoryString
maBoolean := mandatoryBoolean
os.Setenv("optionalUint", "100")
os.Setenv("optionalString", "Nuevo string value")
os.Setenv("optionalBoolean", "false")
os.Setenv("mandatoryUint", "100")
os.Setenv("mandatoryString", "Nuevo string value")
os.Setenv("mandatoryBoolean", "false")
opUint.Parse()
opString.Parse()
opBoolean.Parse()
maUint.Parse()
maString.Parse()
maBoolean.Parse()
if opUint.Get().(uint8) != uint8(100) {
t.Error("Fail to get value from .env for optional setting uint")
t.Fail()
}
if opString.Get().(string) != "Nuevo string value" {
t.Error("Fail to get value from .env for optional setting string")
t.Fail()
}
if opBoolean.Get().(bool) != false {
t.Error("Fail to get value from .env for optional setting boolean")
t.Fail()
}
if maUint.Get().(uint8) != uint8(100) {
t.Error("Fail to get value from .env for mandatory setting uint")
t.Fail()
}
if maString.Get().(string) != "Nuevo string value" {
t.Error("Fail to get value from .env for mandatory setting string")
t.Fail()
}
if maBoolean.Get().(bool) != false {
t.Error("Fail to get value from .env for mandatory setting boolean")
t.Fail()
}
}