-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_test.go
144 lines (141 loc) · 2.73 KB
/
simple_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
package options
import (
"reflect"
"strings"
"testing"
)
func TestUnescape(t *testing.T) {
for _, tt := range []struct {
in, out string
}{
{``, ``},
{`# comment`, ``},
{`a word`, `a word`},
{` leading space`, `leading space`},
{`trailing space `, `trailing space`},
{` space `, `space`},
{`a \# pound # and comment`, `a # pound`},
{`name = "value " `, `name = "value "`},
{`\\`, `\`},
{`\#`, `#`},
{`\\\#`, `\#`},
{`\\\#\x`, `\#x`},
{`foo\`, `foo`},
} {
out := unescape([]byte(tt.in))
if out != tt.out {
t.Errorf("`%s`: got `%s`, want `%s`", tt.in, out, tt.out)
}
}
}
func TestSimpleDecoder(t *testing.T) {
for _, tt := range []struct {
name string
in string
m map[string]interface{}
err string
}{
{
name: `empty`,
m: map[string]interface{}{},
},
{
in: `# comment`,
m: map[string]interface{}{},
},
{
in: `name=value`,
m: map[string]interface{}{`name`: `value`},
},
{
in: `name = value`,
m: map[string]interface{}{`name`: `value`},
},
{
in: ` name = value `,
m: map[string]interface{}{`name`: `value`},
},
{
in: `name = "value"`,
m: map[string]interface{}{`name`: `value`},
},
{
in: `name = "value "`,
m: map[string]interface{}{`name`: `value `},
},
{
in: `name=value #comment`,
m: map[string]interface{}{`name`: `value`},
},
{
name: "missing value",
in: `name`,
err: `missing value: "name"`,
},
{
name: "space in name",
in: `a name = a value`,
err: `space in name: "a name = a value"`,
},
{
name: "missing name",
in: `=value`,
err: `missing name: "=value"`,
},
{
name: "field conflict1",
in: `
sub = other
sub.key = value
`,
err: "conflict on field sub",
},
{
name: "field conflict2",
in: `
sub.key = value
sub = other
`,
err: "conflict on field sub",
},
{
name: "complex",
in: `
# This is a multiple line test
key1=value1
key2 = "value 2" # comment
key3 = "value #" # the comment wasn't escaped
sub.key1 = subvalue1
sub.key2 = subvalue2
`,
m: map[string]interface{}{
"key1": `value1`,
"key2": `value 2`,
"key3": `"value`,
"sub": map[string]interface{}{
"key1": "subvalue1",
"key2": "subvalue2",
},
},
},
} {
if tt.name == "" {
tt.name = tt.in
}
t.Run(tt.name, func(t *testing.T) {
m, err := SimpleDecoder([]byte(tt.in))
switch {
case err == nil && tt.err == "":
case err == nil:
t.Fatalf("did not get expected error %v", tt.err)
case tt.err == "":
t.Fatalf("unexpected error %v", err)
case !strings.Contains(err.Error(), tt.err):
t.Fatalf("got error %v, want %v", err, tt.err)
}
if !reflect.DeepEqual(tt.m, m) {
t.Fatalf("got map %#v, want %#v", m, tt.m)
}
})
}
}