-
Notifications
You must be signed in to change notification settings - Fork 3
/
section_test.go
153 lines (126 loc) · 2.67 KB
/
section_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
package forge_test
import (
"encoding/json"
"fmt"
"testing"
"github.com/brettlangdon/forge"
)
func TestSectionKeys(t *testing.T) {
t.Parallel()
section := forge.NewSection()
section.SetString("key1", "value1")
section.SetString("key2", "value2")
section.SetString("key3", "value3")
keys := section.Keys()
if len(keys) != 3 {
t.Error("expected Section to have 3 keys")
}
if keys[0] != "key1" {
t.Error("expected 'key1' to be in the list of keys")
}
if keys[1] != "key2" {
t.Error("expected 'key2' to be in the list of keys")
}
if keys[2] != "key3" {
t.Error("expected 'key3' to be in the list of keys")
}
}
func TestMergeSection(t *testing.T) {
config1Str := `
global = "global value";
prod {
value = "string value";
integer = 500
float = 80.80
boolean = true
negative = FALSE
nothing = NULL
}
`
config2Str := `
integer = 500
float = 80.80
boolean = true
negative = FALSE
nothing = NULL
new_section {
integer = 500
float = 80.80
boolean = true
negative = FALSE
nothing = NULL
}
prod {
value = "new value";
secret = "shhh";
nothing = "value"
negative = false
boolean = false
}
`
config1, err := forge.ParseString(config1Str)
if err != nil {
t.Error(err)
t.FailNow()
}
config2, err := forge.ParseString(config2Str)
if err != nil {
t.Error(err)
t.FailNow()
}
err = config1.Merge(config2)
if err != nil {
t.Error(err)
t.FailNow()
}
// Validation
valueFloat, _ := config1.GetFloat("float")
if valueFloat != float64(80.80) {
t.Errorf("Excepted '80.80' got %v", valueFloat)
}
valueNegative, _ := config1.Resolve("new_section.negative")
if valueNegative.GetValue().(bool) {
t.Errorf("Excepted 'false' got %v", valueNegative)
}
valueString, _ := config1.Resolve("prod.value")
if valueString.GetValue().(string) == "string value" {
t.Errorf("Excepted 'new value' got %v", valueString)
}
valueBoolean, _ := config1.Resolve("prod.boolean")
if valueBoolean.GetValue().(bool) {
t.Errorf("Excepted 'false' got %v", valueBoolean)
}
bytes, _ := json.MarshalIndent(config1.ToMap(), "", " ")
fmt.Println(string(bytes))
}
func TestMergeSectionFailSectionToField(t *testing.T) {
config1Str := `
global = "global value";
prod {
value = "string value";
integer = 500
float = 80.80
boolean = true
negative = FALSE
nothing = NULL
}
`
config2Str := `
global = "global value";
prod = "I'm prod value"
`
config1, err := forge.ParseString(config1Str)
if err != nil {
t.Error(err)
t.FailNow()
}
config2, err := forge.ParseString(config2Str)
if err != nil {
t.Error(err)
t.FailNow()
}
err = config1.Merge(config2)
if err.Error() != "source (STRING) and target (SECTION) type doesn't match: prod" {
t.Error(err)
}
}