This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder_test.go
136 lines (118 loc) · 2.78 KB
/
builder_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
package configoration
import (
"os"
"testing"
"github.com/zekroTJA/configoration/providers"
)
func TestNewBuilder(t *testing.T) {
b := NewBuilder()
if b == nil {
t.Error("new builder instance was nil")
}
if b.provider == nil {
t.Error("new builder provider array was nil")
}
}
func TestSetBasePath(t *testing.T) {
const testPath = "testpath"
b := NewBuilder().
SetBasePath(testPath)
if b == nil {
t.Error("returned builder instance was nil")
}
if b.basePath != testPath {
t.Errorf("base path (%+v) was not set like expected (%+v)", b.basePath, testPath)
}
}
func TestAddJsonFile(t *testing.T) {
b := NewBuilder().
AddJsonFile("file.json", false)
if b == nil {
t.Error("returned builder instance was nil")
}
if len(b.provider) != 1 {
t.Error("providers array is empty")
}
v, ok := b.provider[0].(*providers.JsonProvider)
if !ok || v == nil {
t.Error("added provider is no JsonProvider")
}
}
func TestAddYamlFile(t *testing.T) {
b := NewBuilder().
AddYamlFile("file.yaml", false)
if b == nil {
t.Error("returned builder instance was nil")
}
if len(b.provider) != 1 || b.provider[0] == nil {
t.Error("providers array is empty")
}
v, ok := b.provider[0].(*providers.YamlProvider)
if !ok || v == nil {
t.Error("added provider is no YamlProvider")
}
}
func TestAddEnvironmentVariables(t *testing.T) {
b := NewBuilder().
AddEnvironmentVariables("TEST_", false)
if b == nil {
t.Error("returned builder instance was nil")
}
if len(b.provider) != 1 || b.provider[0] == nil {
t.Error("providers array is empty")
}
v, ok := b.provider[0].(*providers.EnvProvider)
if !ok || v == nil {
t.Error("added provider is no EnvProvider")
}
}
func TestBuild(t *testing.T) {
os.Setenv("TEST_b__f", "2")
sec, err := NewBuilder().
SetBasePath("./testdata").
AddJsonFile("test1.json", false).
AddJsonFile("test2.json", false).
AddYamlFile("test3.yaml", false).
AddEnvironmentVariables("TEST_", false).
Build()
if err != nil {
t.Errorf("build failed: %s", err.Error())
}
{
v, err := sec.GetString("a")
assertVal(t, v, err, "test2")
}
{
v, err := sec.GetInt("b:b")
assertVal(t, v, err, 1)
}
{
v, err := sec.GetInt("b:c")
assertVal(t, v, err, 11112)
}
{
v, err := sec.GetInt("b:e")
assertVal(t, v, err, 3)
}
{
v, err := sec.GetInt("b:f")
assertVal(t, v, err, 2)
}
{
v, err := sec.GetBool("g:e:f")
assertVal(t, v, err, true)
}
{
v, err := sec.GetInt("y:e")
assertVal(t, v, err, 1123)
}
}
// --------------------------------------------------------------------------
// --- HELPERS
func assertVal(t *testing.T, val interface{}, err error, expected interface{}) {
if err != nil {
t.Errorf("get value errored: %s", err.Error())
} else if val != expected {
t.Errorf("value (%+v) was not like expected (%+v)", val, expected)
}
}