forked from admpub/confl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
181 lines (158 loc) · 3.45 KB
/
parse_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
package confl
import (
"fmt"
"reflect"
"testing"
)
// Test to make sure we get what we expect.
func test(t *testing.T, data string, ex map[string]interface{}) {
m, err := Parse(data)
if err != nil {
t.Fatalf("Received err: %v\n", err)
}
if m == nil {
t.Fatal("Received nil map")
}
if !reflect.DeepEqual(m, ex) {
t.Fatalf("Not Equal:\nReceived: '%+v'\nExpected: '%+v'\n", m, ex)
}
}
func TestParseSimpleTopLevel(t *testing.T) {
ex := map[string]interface{}{
"foo": "1",
"bar": float64(2.2),
"baz": true,
"boo": int64(22),
}
test(t, "foo='1'; bar=2.2; baz=true; boo=22", ex)
}
var sample1 = `
foo {
host {
ip = '127.0.0.1'
port = 4242
}
servers = [ "a.com", "b.com", "c.com"]
}
`
func TestParseSample1(t *testing.T) {
ex := map[string]interface{}{
"foo": map[string]interface{}{
"host": map[string]interface{}{
"ip": "127.0.0.1",
"port": int64(4242),
},
"servers": []interface{}{"a.com", "b.com", "c.com"},
},
}
test(t, sample1, ex)
}
var cluster = `
cluster {
port: 4244
authorization {
user: route_user
password: top_secret
timeout: 1
}
# Routes are actively solicited and connected to from this server.
# Other servers can connect to us if they supply the correct credentials
# in their routes definitions from above.
// Test both styles of comments
routes = [
nats-route://foo:[email protected]:4245
nats-route://foo:[email protected]:4246
]
}
`
func TestParseSample2(t *testing.T) {
ex := map[string]interface{}{
"cluster": map[string]interface{}{
"port": int64(4244),
"authorization": map[string]interface{}{
"user": "route_user",
"password": "top_secret",
"timeout": int64(1),
},
"routes": []interface{}{
"nats-route://foo:[email protected]:4245",
"nats-route://foo:[email protected]:4246",
},
},
}
test(t, cluster, ex)
}
var sample3 = `
foo {
expr = '(true == "false")'
text = 'This is a multi-line
text block.'
text2 (
hello world
this is multi line
with empty line
)
}
`
func TestParseSample3(t *testing.T) {
ex := map[string]interface{}{
"foo": map[string]interface{}{
"expr": "(true == \"false\")",
"text": "This is a multi-line\ntext block.",
"text2": "hello world\n this is multi line\n\nwith empty line",
},
}
test(t, sample3, ex)
}
var sample4 = `
array [
{ abc: 123 }
{ xyz: "word" }
]
`
func TestParseSample4(t *testing.T) {
ex := map[string]interface{}{
"array": []interface{}{
map[string]interface{}{"abc": int64(123)},
map[string]interface{}{"xyz": "word"},
},
}
test(t, sample4, ex)
}
var sample5 = `
table [
[ 1, 123 ],
[ "a", "b", "c"],
]
`
func TestParseSample5(t *testing.T) {
ex := map[string]interface{}{
"table": []interface{}{
[]interface{}{int64(1), int64(123)},
[]interface{}{"a", "b", "c"},
},
}
test(t, sample5, ex)
}
func TestBigSlices(t *testing.T) {
txt := "Hosts : ["
for i := 0; i < 100; i++ {
txt += fmt.Sprintf(`"http://192.168.1.%d:9999", `, i)
}
txt += `"http://123.123.123.123:9999"]` + "\n"
x := struct{ Hosts []string }{}
if err := Unmarshal([]byte(txt), &x); err != nil {
t.Fatalf("error unmarshaling sample: %v", err)
}
if len(x.Hosts) != 101 {
t.Fatalf("%d != 101", len(x.Hosts))
}
for i, v := range x.Hosts {
if i < 100 && v != fmt.Sprintf("http://192.168.1.%d:9999", i) {
t.Errorf("%d unexpected: %s", i, v)
}
}
if x.Hosts[100] != "http://123.123.123.123:9999" {
t.Errorf("unexpected: %s", x.Hosts[100])
}
}