-
Notifications
You must be signed in to change notification settings - Fork 0
/
goini_test.go
193 lines (178 loc) · 6.28 KB
/
goini_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
package goini
import (
"fmt"
"os"
"testing"
)
type TestValue struct {
String string
Bool bool
Byte byte
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
UInt64 uint64
Float32 float32
Float64 float64
StringArray []string
}
var testValues = []TestValue{
{String: "test"},
{Bool: true},
{Bool: false},
{Byte: byte(255)},
{Int: 1234567},
{Int8: 123},
{Int16: 12345},
{Int32: 1234567890},
{Int64: 1234567890123456789},
{Float32: 1234567890.1234567890},
{Float64: 1234567890123456789.1234567890123456789},
{UInt64: 12345678901234567890},
{String: "https://www.jonathanhecl.com/"},
{String: "//not a comment"},
{StringArray: []string{"test", "test2"}},
}
var specialString = "it is a string=with slash // no comment"
func TestCreateNewFile(t *testing.T) {
ini := New(&TOptions{Debug: true})
for i, v := range testValues {
if len(v.StringArray) > 0 {
ini.Set("Test", fmt.Sprintf("%dstringarray", i), StringArray(v.StringArray))
} else if len(v.String) > 0 {
ini.Set("Test", fmt.Sprintf("%dstring", i), String(v.String))
} else if v.UInt64 > 0 {
ini.Set("Test", fmt.Sprintf("%duint64", i), Uint64(v.UInt64))
} else if v.Float64 > 0 {
ini.Set("Test", fmt.Sprintf("%dfloat64", i), Float64(v.Float64))
} else if v.Float32 > 0 {
ini.Set("Test", fmt.Sprintf("%dfloat32", i), Float32(v.Float32))
} else if v.Int64 > 0 {
ini.Set("Test", fmt.Sprintf("%dint64", i), Int64(v.Int64))
} else if v.Int32 > 0 {
ini.Set("Test", fmt.Sprintf("%dint32", i), Int32(v.Int32))
} else if v.Int16 > 0 {
ini.Set("Test", fmt.Sprintf("%dint16", i), Int16(v.Int16))
} else if v.Int8 > 0 {
ini.Set("Test", fmt.Sprintf("%dint8", i), Int8(v.Int8))
} else if v.Int > 0 {
ini.Set("Test", fmt.Sprintf("%dint", i), Int(v.Int))
} else if v.Byte > 0 {
ini.Set("Test", fmt.Sprintf("%dbyte", i), Byte(v.Byte))
} else {
ini.Set("Test", fmt.Sprintf("%dbool", i), Bool(v.Bool, v.Bool))
}
}
ini.Set("Test", "specialString", String(specialString))
err := ini.Save("test.ini")
if err != nil {
t.Error(err)
}
}
func TestReadFile(t *testing.T) {
ini, err := Load("test.ini", &TOptions{Debug: true})
if err != nil {
t.Error(err)
}
for i, v := range testValues {
if len(v.StringArray) > 0 {
stra := ini.Get("Test", fmt.Sprintf("%dstringarray", i)).StringArray()
if len(stra) != len(v.StringArray) {
t.Errorf("Expected %s, got %s", v.StringArray, ini.Get("Test", fmt.Sprintf("%dstringarray", i)).StringArray())
}
} else if len(v.String) > 0 {
if ini.Get("Test", fmt.Sprintf("%dstring", i)).String() != v.String {
t.Errorf("Expected %s, got %s", v.String, ini.Get("Test", fmt.Sprintf("%dstring", i)).String())
}
} else if v.UInt64 > 0 {
if ini.Get("Test", fmt.Sprintf("%duint64", i)).Uint64() != v.UInt64 {
t.Errorf("Expected %d, got %d", v.UInt64, ini.Get("Test", fmt.Sprintf("%duint64", i)).Uint64())
}
} else if v.Float64 > 0 {
if ini.Get("Test", fmt.Sprintf("%dfloat64", i)).Float64() != v.Float64 {
t.Errorf("Expected %f, got %f", v.Float64, ini.Get("Test", fmt.Sprintf("%dfloat64", i)).Float64())
}
} else if v.Float32 > 0 {
if ini.Get("Test", fmt.Sprintf("%dfloat32", i)).Float32() != v.Float32 {
t.Errorf("Expected %f, got %f", v.Float32, ini.Get("Test", fmt.Sprintf("%dfloat32", i)).Float32())
}
} else if v.Int64 > 0 {
if ini.Get("Test", fmt.Sprintf("%dint64", i)).Int64() != v.Int64 {
t.Errorf("Expected %d, got %d", v.Int64, ini.Get("Test", fmt.Sprintf("%dint64", i)).Int64())
}
} else if v.Int32 > 0 {
if ini.Get("Test", fmt.Sprintf("%dint32", i)).Int32() != v.Int32 {
t.Errorf("Expected %d, got %d", v.Int32, ini.Get("Test", fmt.Sprintf("%dint32", i)).Int32())
}
} else if v.Int16 > 0 {
if ini.Get("Test", fmt.Sprintf("%dint16", i)).Int16() != v.Int16 {
t.Errorf("Expected %d, got %d", v.Int16, ini.Get("Test", fmt.Sprintf("%dint16", i)).Int16())
}
} else if v.Int8 > 0 {
if ini.Get("Test", fmt.Sprintf("%dint8", i)).Int8() != v.Int8 {
t.Errorf("Expected %d, got %d", v.Int8, ini.Get("Test", fmt.Sprintf("%dint8", i)).Int8())
}
} else if v.Int > 0 {
if ini.Get("Test", fmt.Sprintf("%dint", i)).Int() != v.Int {
t.Errorf("Expected %d, got %d", v.Int, ini.Get("Test", fmt.Sprintf("%dint", i)).Int())
}
} else if v.Byte > 0 {
if ini.Get("Test", fmt.Sprintf("%dbyte", i)).Byte() != v.Byte {
t.Errorf("Expected %d, got %d", v.Byte, ini.Get("Test", fmt.Sprintf("%dbyte", i)).Byte())
}
} else {
if ini.Get("Test", fmt.Sprintf("%dbool", i)).Bool() != v.Bool {
t.Errorf("Expected %t, got %t", v.Bool, ini.Get("Test", fmt.Sprintf("%dbool", i)).Bool())
}
}
}
if ini.Get("Test", "specialString").String() != specialString {
t.Errorf("Expected %s, got %s", specialString, ini.Get("Test", "specialString").String())
}
}
func TestSpecial2(t *testing.T) {
content := []byte(`[Test]
change=4 ' comment
ignore=I'will ignore this
same=Never change this // comment`)
err := os.WriteFile("test2.ini", content, 0644)
if err != nil {
t.Errorf("Error creating test file: %s", err)
}
ini, err := Load("test2.ini", &TOptions{Debug: true, ForceSaveWithoutQuotes: true})
if err != nil {
t.Error(err)
}
if ini.Get("Test", "change").Int() != 4 {
t.Errorf("Expected 4, got %d", ini.Get("Test", "change").Int())
}
if ini.Get("Test", "ignore").String() != "I'will ignore this" {
t.Errorf("Expected I'will ignore this, got %s", ini.Get("Test", "ignore").String())
}
if ini.Get("Test", "same").String() != "Never change this" {
t.Errorf("Expected Never change this, got %s", ini.Get("Test", "same").String())
}
// Test save
ini.Set("Test", "change", Int(5))
ini.Set("Test", "ignore", String("I'will change this"))
ini.Set("Test", "same", String("Never change this"))
err = ini.Save("test2.ini")
if err != nil {
t.Error(err)
}
ini, err = Load("test2.ini", &TOptions{Debug: true})
if err != nil {
t.Error(err)
}
if ini.Get("Test", "change").Int() != 5 {
t.Errorf("Expected 5, got %d", ini.Get("Test", "change").Int())
}
if ini.Get("Test", "ignore").String() != "I'will change this" {
t.Errorf("Expected I'will change this, got %s", ini.Get("Test", "ignore").String())
}
if ini.Get("Test", "same").String() != "Never change this" {
t.Errorf("Expected Never change this, got %s", ini.Get("Test", "same").String())
}
}