-
Notifications
You must be signed in to change notification settings - Fork 0
/
xconfig_test.go
227 lines (185 loc) · 5.85 KB
/
xconfig_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package xconfig
import (
"fmt"
"io/ioutil"
"testing"
)
func TestLoads(t *testing.T) {
// Test 1: assign a simple parameter string with some comments
conf0 := New()
content, err := ioutil.ReadFile("testunit/a.conf")
if err != nil {
t.Errorf("Error loading a.conf")
return
}
conf0.LoadString(string(content))
conf0p := New()
conf0p.MergeString(string(content))
conf1 := New()
conf1.LoadFile("testunit/a.conf")
conf2 := New()
conf2.MergeFile("testunit/a.conf") // load is same as merge on first time
conf3 := New()
conf3.LoadXConfig(conf1) // load is same as merge on first time
conf4 := New()
conf4.MergeXConfig(conf1) // load is same as merge on first time
// print what we got
s0 := fmt.Sprint(conf0)
s0p := fmt.Sprint(conf0p)
s1 := fmt.Sprint(conf1)
s2 := fmt.Sprint(conf2)
s3 := fmt.Sprint(conf3)
s4 := fmt.Sprint(conf4)
/*
fmt.Println(s0)
fmt.Println(s0p)
fmt.Println(s1)
fmt.Println(s2)
fmt.Println(s3)
fmt.Println(s4)
*/
if s1 != s2 || s1 != s3 || s1 != s4 || s1 != s0 || s1 != s0p {
t.Errorf("error loading and merging natural files")
}
}
func TestOneStringParam(t *testing.T) {
// Test 1: assign a simple parameter string with some comments
conf := New()
conf.LoadString("#First test\nparam1=value1\n\n;End of test 1\n")
// print what we got
// fmt.Println(conf)
// direct access
if (*conf).Parameters["param1"].Value != "value1" {
t.Errorf("The parameter param1 is not correctly set")
}
// Get
if v, _ := conf.Get("param1"); v != "value1" {
t.Errorf("The parameter param1 is not correctly passed")
}
}
func TestStringParam(t *testing.T) {
// Test 2: assign 3 different parameters string
conf := New()
conf.LoadString("param1=value1\nparam2=value2\nparam3=value3\nparam4=\"123\nparam5=\"on")
// print what we got
// fmt.Println(conf)
// direct access
if (*conf).Parameters["param1"].Value != "value1" || (*conf).Parameters["param2"].Value != "value2" || (*conf).Parameters["param3"].Value != "value3" {
t.Errorf("The parameters are not correctly set")
}
if (*conf).Parameters["param4"].Value != "123" || (*conf).Parameters["param5"].Value != "on" {
t.Errorf("The parameters are not correctly set")
}
// Get
v1, _ := conf.Get("param1")
v2, _ := conf.Get("param2")
v3, _ := conf.Get("param3")
v4, _ := conf.Get("param4")
v5, _ := conf.Get("param5")
if v1 != "value1" || v2 != "value2" || v3 != "value3" || v4 != "123" || v5 != "on" {
t.Errorf("The parameters are not correctly passed")
}
}
func TestBoolParam(t *testing.T) {
// Test 3: assign a simple bool
conf := New()
conf.LoadString("param1=yes\nparam2=true\nparam3=on\nparam4=no\nparam5=none\nparam6=false\nparam7=off")
// fmt.Println(conf)
if (*conf).Parameters["param1"].Value != true || (*conf).Parameters["param2"].Value != true || (*conf).Parameters["param3"].Value != true || (*conf).Parameters["param4"].Value != false || (*conf).Parameters["param5"].Value != false || (*conf).Parameters["param6"].Value != false || (*conf).Parameters["param7"].Value != false {
t.Errorf("The boolean parameters are not correctly set")
}
}
func TestIntegerParam(t *testing.T) {
// Test 4:
conf := New()
conf.LoadString("param1=0\nparam2=-1\nparam3=1234567890")
// fmt.Println(conf)
if (*conf).Parameters["param1"].Value != 0 || (*conf).Parameters["param2"].Value != -1 || (*conf).Parameters["param3"].Value != 1234567890 {
t.Errorf("The integer parameters are not correctly set")
}
}
func TestFloatParam(t *testing.T) {
// Test 4:
conf := New()
conf.LoadString("param1=0.123\nparam2=12e7\nparam3=-76364.2")
// fmt.Println(conf)
if (*conf).Parameters["param1"].Value != 0.123 || (*conf).Parameters["param2"].Value != 12e7 || (*conf).Parameters["param3"].Value != -76364.2 {
t.Errorf("The float parameters are not correctly set")
}
}
func TestArrayParam(t *testing.T) {
// Test 5:
conf := New()
conf.LoadString("param1=value1\nparam1=value2\nparam1=value3\nparam2=123\nparam2=-1\nparam2=1234567890\nparam3=0.1\nparam3=-123.567\nparam3=12e7\nparam4=true\nparam4=off\nparam4=on")
// fmt.Println(conf)
arr, ext := conf.GetStringCollection("param1")
if !ext {
t.Errorf("The array parameter is not correctly set")
return
}
if arr[0] != "value1" || arr[1] != "value2" || arr[2] != "value3" {
t.Errorf("The array parameter is not correctly set")
}
}
func TestClone(t *testing.T) {
// Test 1: assign a simple parameter string with some comments
conf := New()
conf.LoadString("#First test\nparam1=value1\n\n;End of test 1\n")
conf2 := conf.Clone()
if fmt.Sprint(conf) != fmt.Sprint(conf2) {
t.Errorf("Error cloning the xconfig")
}
// print what we got
// fmt.Println("ANTES DE CLONE", conf)
// fmt.Println("OBJETO CLONED", conf2)
conf.Set("param10", "value10")
if fmt.Sprint(conf) == fmt.Sprint(conf2) {
t.Errorf("Error cloning the xconfig")
}
}
func TestStructure(t *testing.T) {
conf := New()
err := conf.LoadFile("testunit/example.conf")
if err != nil {
t.Error(err)
return
}
s0 := conf.Marshal()
r0 := `# this file is named myconfig.conf, used in following examples
# the # denotes a comment.
; is also a comment
parameter1=value1
parameter2=value2
parameter2=value3
# global config:
ip=127.0.0.1
port=80
domain=test.com
# Some list of values, they will result into an array
country=MX
country=US
country=FR
country=JP
country=ES
# some subsets
language.en.welcome=Welcome to the XConfig examples
language.en.ack=OK
language.en.cancel=Cancel
language.es.welcome=Bienvenido a los ejemplos de XConfig
language.es.ack=Perfecto
language.es.cancel=Cancelar
# spanish
`
if s0 != r0 {
t.Errorf("Error marshelling file, considering pushing the comments into an array of values")
}
}
func TestDel(t *testing.T) {
conf := New()
conf.LoadString("param1=0.123\nparam2=12e7\nparam3=-76364.2")
conf.Del("param1")
s0 := fmt.Sprint(conf)
if s0 != "XConfig[\nparam2:1.2e+08\nparam3:-76364.2\n]\n" {
t.Errorf("The parameter has not been correctly deleted")
}
}