-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_test.go
81 lines (76 loc) · 1.99 KB
/
file_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
package ini
import (
"fmt"
"reflect"
"testing"
)
const testDir = "testdata/"
func TestLoad(t *testing.T) {
cases := []struct {
filename string
hasCfg bool
wantErr error
}{
{testDir + "my.ini", true, nil},
{testDir + "nonexist.ini", false, fmt.Errorf("open " + testDir + "nonexist.ini: no such file or directory")},
{testDir + "empty.ini", true, nil},
}
for _, c := range cases {
cfg, err := Load(c.filename)
if err == nil && c.wantErr == nil {
continue
}
if err == nil || c.wantErr == nil || err.Error() != c.wantErr.Error() {
t.Errorf("wantErr: %v, gotErr: %v", c.wantErr, err)
}
if (cfg != nil) != c.hasCfg {
t.Errorf("want hasCfg: %v, got cfg: %v", c.hasCfg, cfg)
}
}
}
func TestConfigInit(t *testing.T) {
cfg := Config{}
cfg.init()
if cfg.Sections == nil {
t.Errorf("init cfg.sections error")
}
}
func TestConfigNewSection(t *testing.T) {
cases := []struct {
name string
wantErr error
}{
{"sec1", nil},
{"", fmt.Errorf("empty section name")},
{"sec1", fmt.Errorf("section(sec1) name already exists")},
{"sec2", nil},
}
cfg := Config{}
cfg.init()
for _, c := range cases {
err := cfg.newSection(c.name)
if c.wantErr == nil && err == nil {
continue
} else if err == nil || c.wantErr == nil || err.Error() != c.wantErr.Error() {
t.Errorf("wantErr: %v, gotErr: %v", c.wantErr, err)
}
}
want := Config{
SecList: []string{"sec1", "sec2"},
Sections: map[string]*Section{
"sec1": &Section{KeyVal: map[string]string{}},
"sec2": &Section{KeyVal: map[string]string{}},
},
}
if !reflect.DeepEqual(cfg.SecList, want.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")
}
}