generated from inherelab/go-pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
256 lines (214 loc) · 5.36 KB
/
parser_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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package properties_test
import (
"fmt"
"testing"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/properties"
)
func TestParser_Parse(t *testing.T) {
text := `
name = inhere
age = 345
only-key =
env-key = ${SHELL | bash}
##### comments1
top.sub.key0 = a string value
top.sub.key1 = "a quote value1"
top.sub.key2 = 'a quote value2'
/* comments 1.1 */
top.sub.key3 = 234
! inline list
top2.inline.list.ids = [234, 345, 456]
# use var refer
top2.sub.var-refer = ${top.sub.key0}
/*
multi line
comments
*/
top2.sub.key2-other = has-char
// comments 2
top.sub.key3 = false
# slice list
top.sub.key4[0] = abc
top.sub.key4[1] = def
## --- comments 3 ---
top.sub.key5[0].f1 = ab
top.sub.key5[1].f2 = de
# multi line value
top.sub2.mline1 = """multi line
value
"""
# multi line value2
top.sub2.mline2 = this is \
multi line2 \
value
`
p := properties.NewParser(
properties.WithDebug,
properties.ParseEnv,
properties.ParseInlineSlice,
func(opts *properties.Options) {
opts.TrimValue = true
},
)
err := p.Parse(text)
assert.NoErr(t, err)
fmt.Println("\ndata map:")
dump.NoLoc(p.Data)
fmt.Println("\nstring map:")
dump.NoLoc(p.SMap())
fmt.Println("\ncomments:")
dump.NoLoc(p.Comments())
assert.Eq(t, 345, p.Int("age"))
assert.Eq(t, "inhere", p.Str("name"))
assert.Eq(t, "a quote value1", p.Str("top.sub.key1"))
assert.Eq(t, []string{"234", "345", "456"}, p.Strings("top2.inline.list.ids"))
assert.Eq(t, "[234, 345, 456]", p.SMap().Get("top2.inline.list.ids"))
}
func TestParser_WithOptions_parseVar(t *testing.T) {
text := `key0 = val1
top.sub.key0 = a string value
top2.sub.var-refer = ${top.sub.key0}
`
p := properties.NewParser()
err := p.Parse(text)
assert.NoErr(t, err)
smp := p.SMap()
assert.Eq(t, "a string value", smp.Str("top.sub.key0"))
assert.Eq(t, "a string value", smp.Str("top2.sub.var-refer"))
}
func TestParser_Parse_multiLineValS(t *testing.T) {
text := `key0 = val1
top.sub2.mline1 = '''multi line
value
'''
key1 = val2
`
p := properties.NewParser()
err := p.Parse(text)
assert.NoErr(t, err)
smp := p.SMap()
assert.NotEmpty(t, smp)
assert.ContainsKeys(t, smp, []string{"key0", "key1", "top.sub2.mline1"})
assert.Eq(t, "multi line\nvalue\n", smp.Str("top.sub2.mline1"))
// start and end mark at new line
text = `key0 = val1
top.sub2.mline1 = '''
multi line
value
'''
key1 = val2
`
p = properties.NewParser()
assert.NoErr(t, p.Parse(text))
smp = p.SMap()
assert.NotEmpty(t, smp)
assert.ContainsKey(t, smp, "top.sub2.mline1")
assert.Eq(t, "\nmulti line\nvalue\n", smp.Str("top.sub2.mline1"))
// value at end line
text = `key0 = val1
top.sub2.mline1 = '''multi line
value'''
key1 = val2
`
p = properties.NewParser()
assert.NoErr(t, p.Parse(text))
smp = p.SMap()
assert.NotEmpty(t, smp)
assert.ContainsKey(t, smp, "top.sub2.mline1")
assert.Eq(t, "multi line\nvalue", smp.Str("top.sub2.mline1"))
// empty value
text = `key0 = val1
top.sub2.mline1 = '''
'''
key1 = val2
`
p = properties.NewParser()
assert.NoErr(t, p.Parse(text))
smp = p.SMap()
assert.NotEmpty(t, smp)
assert.ContainsKey(t, smp, "top.sub2.mline1")
assert.Eq(t, "\n", smp.Str("top.sub2.mline1"))
}
func TestParser_Parse_multiLineValD(t *testing.T) {
text := `key0 = val1
top.sub2.mline1 = """multi line
value
"""
key1 = val2
`
p := properties.NewParser()
err := p.Parse(text)
assert.NoErr(t, err)
smp := p.SMap()
assert.NotEmpty(t, smp)
assert.ContainsKeys(t, smp, []string{"key0", "key1", "top.sub2.mline1"})
assert.Eq(t, "multi line\nvalue\n", smp.Str("top.sub2.mline1"))
// start and end mark at new line
text = `key0 = val1
top.sub2.mline1 = """
multi line
value
"""
key1 = val2
`
p = properties.NewParser()
assert.NoErr(t, p.Parse(text))
smp = p.SMap()
assert.NotEmpty(t, smp)
assert.ContainsKey(t, smp, "top.sub2.mline1")
assert.Eq(t, "\nmulti line\nvalue\n", smp.Str("top.sub2.mline1"))
// value at end line
text = `key0 = val1
top.sub2.mline1 = """multi line
value"""
key1 = val2
`
p = properties.NewParser()
assert.NoErr(t, p.Parse(text))
smp = p.SMap()
assert.NotEmpty(t, smp)
assert.ContainsKey(t, smp, "top.sub2.mline1")
assert.Eq(t, "multi line\nvalue", smp.Str("top.sub2.mline1"))
// empty value
text = `
key0 = val1
top.sub2.mline1 = """
"""
key1 = val2
`
p = properties.NewParser()
assert.NoErr(t, p.Parse(text))
smp = p.SMap()
assert.NotEmpty(t, smp)
assert.ContainsKey(t, smp, "top.sub2.mline1")
assert.Eq(t, "\n", smp.Str("top.sub2.mline1"))
}
func TestParser_Parse_multiLineValQ(t *testing.T) {
text := `key0 = val1
top.sub2.mline1 = multi line \
value
key1 = val2
`
p := properties.NewParser()
err := p.Parse(text)
assert.NoErr(t, err)
smp := p.SMap()
assert.NotEmpty(t, smp)
assert.ContainsKeys(t, smp, []string{"key0", "key1", "top.sub2.mline1"})
assert.Eq(t, "multi line value", smp.Str("top.sub2.mline1"))
}
func TestParser_Parse_err(t *testing.T) {
p := properties.NewParser()
assert.ErrMsg(t, p.Parse(""), `cannot input empty contents to parse`)
assert.ErrMsg(t, p.ParseBytes(nil), `cannot input empty contents to parse`)
err := p.Parse("no-value")
assert.ErrMsg(t, err, `invalid syntax, no matcher available. line 1: "no-value"`)
err = p.Parse("/")
assert.ErrMsg(t, err, `invalid contents. line 1: "/"`)
err = p.Parse("=value")
assert.ErrMsg(t, err, `key cannot be empty. line 1: "=value"`)
err = p.Unmarshal(nil, nil)
assert.ErrMsg(t, err, `cannot input empty contents to parse`)
}