-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigparser_test.go
147 lines (122 loc) · 3.1 KB
/
configparser_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
package configparser
import (
"os"
"testing"
)
type Address struct {
Street string `json:"street" env:"USER_ADDRESS_STREET"`
City string `json:"city"`
}
type User struct {
Name string `json:"name" env:"USER_NAME"`
Age int `json:"age" env:"USER_AGE"`
Species string `json:"species" env:"USER_SPECIES"`
Street string `json:"street" env:"USER_STREET"`
NoEnv string `json:"noenv"`
Address Address `json:"address"`
Limit int `json:"limit"`
CanTalk bool `json:"cantalk" env:"USER_TALK"`
CanWalk bool `json:"canwalk"`
Score float64 `json:"score"`
}
func TestParseYaml(t *testing.T) {
var user User
err := ParseYaml("test.yaml", &user)
if err != nil {
t.Errorf("%s", err.Error())
}
if user.Species != "unknown" {
t.Errorf("user.species is not unknown.")
}
}
func TestParseJSON(t *testing.T) {
var user User
err := ParseJSON("test.json", &user)
if err != nil {
t.Errorf("%s", err.Error())
}
if user.Species != "unknown" {
t.Errorf("user.species is not unknown.")
}
}
func TestSetEnvTag(t *testing.T) {
var user User
err := ParseYaml("test.yaml", &user)
if err != nil {
t.Errorf("%s", err.Error())
}
os.Setenv("USER_NAME", "thanos")
os.Setenv("USER_ADDRESS_STREET", "newstreet")
os.Setenv("USER_AGE", "999")
os.Setenv("USER_TALK", "true")
SetValuesFromEnvironmentTag(&user)
if user.Name != "thanos" {
t.Errorf("Name was not set from env var.")
}
if user.Address.Street != "newstreet" {
t.Errorf("Street was not set in Address.")
}
if user.Age != 999 {
t.Errorf("Age was not set from env var.")
}
if !user.CanTalk {
t.Errorf("user cannot talk")
}
os.Setenv("USER_NAME", "")
os.Setenv("USER_ADDRESS_STREET", "")
os.Setenv("USER_AGE", "")
os.Setenv("USER_TALK", "")
}
func TestSetEnvDynamic(t *testing.T) {
var user User
err := ParseYaml("test.yaml", &user)
if err != nil {
t.Errorf("%s", err.Error())
}
os.Setenv("USER_AGE", "50")
os.Setenv("USER_ADDRESS_CITY", "New Castle")
os.Setenv("USER_LIMIT", "99")
os.Setenv("USER_CANWALK", "true")
os.Setenv("USER_SCORE", "5.4242")
SetValuesFromEnvironment("USER", &user)
if user.Age != 50 {
t.Errorf("Age was not set from env var.")
}
if user.Address.City != "New Castle" {
t.Errorf("City was not set in Address.")
}
if user.Limit != 99 {
t.Errorf("Limit has not been set from env var.")
}
if !user.CanWalk {
t.Errorf("User cannot walk")
}
os.Setenv("USER_AGE", "")
os.Setenv("USER_ADDRESS_CITY", "")
os.Setenv("USER_LIMIT", "")
os.Setenv("USER_CANWALK", "")
os.Setenv("USER_SCORE", "")
}
func TestSetEnvDynamicNoPrefix(t *testing.T) {
var user User
err := ParseYaml("test.yaml", &user)
if err != nil {
t.Errorf("%s", err.Error())
}
os.Setenv("AGE", "50")
os.Setenv("ADDRESS_CITY", "New Castle")
os.Setenv("USER_LIMIT", "99")
SetValuesFromEnvironment("", &user)
if user.Age != 50 {
t.Errorf("Age was not set from env var.")
}
if user.Address.City != "New Castle" {
t.Errorf("City was not set in Address.")
}
if user.Limit != 10 {
t.Errorf("Limit has not been set from env var.")
}
os.Setenv("AGE", "")
os.Setenv("ADDRESS_CITY", "")
os.Setenv("USER_LIMIT", "")
}