-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
127 lines (120 loc) · 3.27 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
package ini
import (
"bufio"
"fmt"
"os"
"reflect"
"testing"
)
func TestParseKeyName(t *testing.T) {
cases := []struct {
line string
wantKeyName string
wantOffset int
wantErr error
}{
{"key1 = value", "key1", 6, nil},
{" = value", "", 2, nil},
{"key2 value", "", -1, fmt.Errorf("delimiter(%s) not found", KeyValueDelim)},
}
for _, c := range cases {
keyName, offset, err := parseKeyName(c.line)
if c.wantErr == nil && err == nil {
if c.wantKeyName != keyName {
t.Errorf("wantKeyName: %v, gotKeyName: %v", c.wantKeyName, keyName)
}
if offset != c.wantOffset {
t.Errorf("wantOffset: %v, gotOffset: %v", c.wantOffset, offset)
}
} else if err == nil || c.wantErr == nil || err.Error() != c.wantErr.Error() {
t.Errorf("wantErr: %v, gotErr: %v", c.wantErr, err)
}
}
}
func TestParseSecName(t *testing.T) {
cases := []struct {
line string
wantSecName string
wantErr error
}{
{"[secname]", "secname", nil},
{"[]", "", fmt.Errorf("empty section name")},
{"[uncloseSec", "", fmt.Errorf("unclosed section: %s", "[uncloseSec")},
}
for _, c := range cases {
cfg := Config{}
cfg.init()
secName, err := parseSecName([]byte(c.line), &cfg)
if c.wantErr == nil && err == nil {
if c.wantSecName != secName {
t.Errorf("wantSecName: %v, gotSecName: %v", c.wantSecName, secName)
}
} else if err == nil || c.wantErr == nil || err.Error() != c.wantErr.Error() {
t.Errorf("wantErr: %v, gotErr: %v", c.wantErr, err)
}
}
}
func TestParseValue(t *testing.T) {
cases := []struct {
line string
wantVal string
wantErr error
}{
{"value", "value", nil},
{" value ", "value", nil},
{" value " + CommentSym + "comment", "value", nil},
}
for _, c := range cases {
value, err := parseValue(c.line)
if c.wantErr == nil && err == nil {
if c.wantVal != value {
t.Errorf("wantVal: %v, gotVal: %v", c.wantVal, value)
}
} else if err == nil || c.wantErr == nil || err.Error() != c.wantErr.Error() {
t.Errorf("wantErr: %v, gotErr: %v", c.wantErr, err)
}
}
}
func TestParse(t *testing.T) {
want := Config{
SecList: []string{"DEFAULT", "paths", "server"},
Sections: map[string]*Section{
"DEFAULT": &Section{
KeyList: []string{"app_mode"},
KeyVal: map[string]string{"app_mode": "development"},
},
"paths": &Section{
KeyList: []string{"data"},
KeyVal: map[string]string{"data": "/home/git/grafana"},
},
"server": &Section{
KeyList: []string{"protocol", "http_port", "enforce_domain"},
KeyVal: map[string]string{
"protocol": "http",
"http_port": "9999",
"enforce_domain": "true"},
},
},
}
f, err := os.Open(testDir + "my.ini")
if err != nil {
t.Errorf(err.Error())
}
defer f.Close()
buf := bufio.NewReader(f)
cfg, err := parse(buf)
if err != nil {
t.Errorf("Error: %v", err)
}
if !reflect.DeepEqual(want.SecList, cfg.SecList) {
t.Errorf("wantSecList: %v, gotSecList: %v", want.SecList, cfg.SecList)
}
if !reflect.DeepEqual(want.Sections, cfg.Sections) {
for _, secName := range want.SecList {
if !reflect.DeepEqual(want.Sections[secName], cfg.Sections[secName]) {
t.Errorf("wantSection: %v, gotSection: %v", want.Sections[secName], cfg.Sections[secName])
}
}
t.Errorf("want Sections not equal to got Sections")
}
}