-
Notifications
You must be signed in to change notification settings - Fork 0
/
configparser_test.go
127 lines (116 loc) · 3.12 KB
/
configparser_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 configparser
import (
"fmt"
"reflect"
"sort"
"testing"
)
func basicTest(t *testing.T, cf ConfigParser, argument Argument) {
E := []string{
"Commented Bar",
"Foo Bar",
"Internationalized Stuff",
"Long Line",
"Section\\with$weird%characters[\t",
"Spaces",
"Spacey Bar",
"Spacey Bar From The Beginning",
"Types",
}
allow_no_value := argument.AllowNoValue
if allow_no_value {
E = append(E, "NoValue")
}
sort.Strings(E)
F := [][]string{{"baz", "qwe"}, {"foo", "bar3"}}
L := cf.Sections()
sort.Strings(L)
if !reflect.DeepEqual(L, E) {
t.Errorf("%v, %v", L, E)
}
K, err := cf.OptItems("Spacey Bar From The Beginning", false, nil)
if err != nil {
return
}
sort.Strings(L)
if !reflect.DeepEqual(K, F) {
t.Errorf("%v, %v", K, F)
}
L = []string{}
for section := range cf.GetSectionMap() {
L = append(L, section)
}
sort.Strings(L)
if !reflect.DeepEqual(L, E) {
t.Errorf("%v, %v", L, E)
}
// TODO items
if s, err := cf.Gett("Foo Bar", "foo"); err != nil || s != "bar1" {
t.Errorf("err %v, %v != %v", err, s, "bar1")
}
if s, err := cf.Gett("Spacey Bar", "foo"); err != nil || s != "bar2" {
t.Errorf("err %v, %v != %v", err, s, "bar2")
}
if s, err := cf.Gett("Spacey Bar From The Beginning", "foo"); err != nil || s != "bar3" {
t.Errorf("err %v, %v != %v", err, s, "bar3")
}
if s, err := cf.Gett("Spacey Bar From The Beginning", "baz"); err != nil || s != "qwe" {
t.Errorf("err %v, %v != %v", err, s, "qwe")
}
}
func testBasic(t *testing.T, argument Argument) {
const configString = "[Foo Bar]\n" +
"foo%[1]sbar1\n" +
"[Spacey Bar]\n" +
"foo %[1]s bar2\n" +
"[Spacey Bar From The Beginning]\n" +
" foo %[1]s bar3\n" +
" baz %[1]s qwe\n" +
"[Commented Bar]\n" +
"foo%[2]s bar4 %[4]s comment\n" +
"baz%[1]sqwe %[3]sanother one\n" +
"[Long Line]\n" +
"foo%[2]s this line is much, much longer than my editor\n" +
" likes it.\n" +
"[Section\\with$weird%%characters[\t]\n" +
"[Internationalized Stuff]\n" +
"foo[bg]%[2]s Bulgarian\n" +
"foo%[1]sDefault\n" +
"foo[en]%[1]sEnglish\n" +
"foo[de]%[1]sDeutsch\n" +
"[Spaces]\n" +
"key with spaces %[2]s value\n" +
"another with spaces %[1]s splat!\n" +
"[Types]\n" +
"int %[2]s 42\n" +
"float %[1]s 0.44\n" +
"boolean %[1]s NO\n" +
"123 %[2]s strange but acceptable\n"
cfgString := fmt.Sprintf(configString, argument.Delimiters[0], argument.Delimiters[1], argument.CommentPrefixes[0], argument.CommentPrefixes[1])
if argument.AllowNoValue {
cfgString = cfgString + "[NoValue]\noption-without-value\n"
}
cf := NewRawConfigParser(argument)
if err := cf.readString(cfgString, "<string>"); err != nil {
t.Errorf("%v", err)
}
basicTest(t, cf, argument)
}
var argument = Argument{
AllowNoValue: false,
Delimiters: []string{"=", ":"},
CommentPrefixes: []string{";", "#"},
InlineCommentPrefixes: []string{";", "#"},
EmptyLinesInValues: true,
Strict: false,
DefaultSection: DefaultSect,
Interpolation: _UNSET,
}
func TestStrict(t *testing.T) {
arg := argument
arg.Strict = true
testBasic(t, arg)
}
//func TestNewRawConfigParser(t *testing.T) {
//
//}