-
Notifications
You must be signed in to change notification settings - Fork 0
/
goini_test.go
326 lines (285 loc) · 6.6 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//
// Created by WestleyR <westleyr@nym.hush.com> on Dec 10, 2021
// Source code: https://github.com/WestleyR/goini
// https://github.com/WildWest-Productions/goini
//
// Copyright (c) 2021 WestleyR. All rights reserved.
// This software is licensed under a BSD 3-Clause Clear License.
// Consult the LICENSE file that came with this software regarding
// your rights to distribute this software.
//
package goini
import (
"bytes"
"fmt"
"reflect"
"testing"
)
type testStruct struct {
ConfigStr string `ini:"hello"`
ConfigInt int `ini:"bar"`
Timmer float64 `ini:"timer"`
Val string `ini:"t"`
Hello subStruct `ini:"sector"`
SystemEnabled bool `ini:"sys_enable"`
Command subStruct2 `ini:"command"`
}
type pointerStruct struct {
Hello string `ini:"hello"`
Ptr *subStruct `ini:"ptr"`
}
type subStruct struct {
Bar string `ini:"hello"`
SystemEnabled bool `ini:"sys_enable"`
}
type subStruct2 struct {
Command string `ini:"command"`
Runs int `ini:"runs"`
}
var testIni = []byte(`
hello=world
bar = 2
t=hi
sys_enable = true
# test comment 1
timer = 23.31
[command]
; another test comment
command = echo hello
runs = 42
[sector]
hello=world-world
sys_enable = yes
`)
var pointerIni = []byte(`
hello = hello world
[ptr]
hello = bar
sys_enable = y
`)
func assertNil(t *testing.T, n interface{}, m ...string) {
if n != nil {
msg := fmt.Sprintf("\nExpected nil; got: %+v\n Test failed: %s\n", n, t.Name())
if len(m) > 0 {
msg += fmt.Sprintf(" Message: %s\n", m[0])
}
t.Fatalf("%s\n", msg)
}
}
func assertNotNil(t *testing.T, n interface{}, m ...string) {
if n == nil {
msg := fmt.Sprintf("\nExpected non-nil; got: %+v\n Test failed: %s\n", n, t.Name())
if len(m) > 0 {
msg += fmt.Sprintf(" Message: %s\n", m[0])
}
t.Fatalf("%s\n", msg)
}
}
func assertTrue(t *testing.T, n bool, m ...string) {
if n != true {
msg := fmt.Sprintf("\nExpected true; got: %v\n Test failed: %s\n", n, t.Name())
if len(m) > 0 {
msg += fmt.Sprintf(" Message: %s\n", m[0])
}
t.Fatalf("%s\n", msg)
}
}
func assertEqual(t *testing.T, e, g interface{}, m ...string) {
if !reflect.DeepEqual(e, g) {
msg := fmt.Sprintf("\nExpected to be equal:\n")
msg += fmt.Sprintf(" Expected: %+v\n", e)
msg += fmt.Sprintf(" Got : %+v\n", g)
msg += fmt.Sprintf("In test : %s\n", t.Name())
if len(m) > 0 {
msg += fmt.Sprintf("Message : %s\n", m[0])
}
t.Fatalf("%s\n", msg)
}
}
func assertEqualBytes(t *testing.T, e, g []byte, m ...string) {
if !bytes.Equal(e, g) {
msg := fmt.Sprintf("\nExpected to be equal:\n")
msg += fmt.Sprintf(" Expected: %q\n", string(e))
msg += fmt.Sprintf(" Got : %q\n", string(g))
msg += fmt.Sprintf("In test : %s\n", t.Name())
if len(m) > 0 {
msg += fmt.Sprintf("Message : %s\n", m[0])
}
t.Fatalf("%s\n", msg)
}
}
func TestUnmarshalPointer(t *testing.T) {
s := &testStruct{}
err := Unmarshal(testIni, &s)
assertNil(t, err, "failed to unmarshal")
expected := &testStruct{
ConfigStr: "world",
ConfigInt: 2,
Timmer: 23.31,
Val: "hi",
Hello: subStruct{
Bar: "world-world",
SystemEnabled: true,
},
SystemEnabled: true,
Command: subStruct2{
Command: "echo hello",
Runs: 42,
},
}
assertEqual(t, expected, s, "did not get expected struct")
}
func TestUnmarshalSubPointer(t *testing.T) {
t.Skip("skipping for now... (does not work yet...)")
s := &pointerStruct{}
err := Unmarshal(pointerIni, &s)
assertNil(t, err, "failed to unmarshal")
expected := &pointerStruct{
Hello: "hello world11",
Ptr: &subStruct{
Bar: "bar",
SystemEnabled: true,
},
}
assertEqual(t, expected, s, "did not get expected struct")
t.FailNow()
}
func TestUnmarshalNonPointer(t *testing.T) {
s := testStruct{}
err := Unmarshal(testIni, &s)
assertNil(t, err, "failed to unmarshal")
expected := testStruct{
ConfigStr: "world",
ConfigInt: 2,
Timmer: 23.31,
Val: "hi",
Hello: subStruct{
Bar: "world-world",
SystemEnabled: true,
},
SystemEnabled: true,
Command: subStruct2{
Command: "echo hello",
Runs: 42,
},
}
assertEqual(t, expected, s, "did not get expected struct")
}
func TestUnmarshalNonStruct(t *testing.T) {
{
s := "string"
err := Unmarshal([]byte("foo = bar"), &s)
assertNotNil(t, err, "expected error")
}
{
s := 42
err := Unmarshal([]byte("foo = bar"), &s)
assertNotNil(t, err, "expected error")
}
{
s := true
err := Unmarshal([]byte("foo = bar"), &s)
assertNotNil(t, err, "expected error")
}
{
s := make(map[string]string)
err := Unmarshal([]byte("foo = bar"), &s)
assertNotNil(t, err, "expected error")
}
}
func TestMarshalPointer(t *testing.T) {
testStruct := &testStruct{
ConfigStr: "world",
ConfigInt: 2,
Timmer: 23.31,
Val: "hi",
Hello: subStruct{
Bar: "world-world",
SystemEnabled: true,
},
SystemEnabled: true,
Command: subStruct2{
Command: "echo hello",
Runs: 42,
},
}
b, err := Marshal(testStruct)
assertNil(t, err, "failed to marshal")
expectedIni := []byte(`hello = world
bar = 2
timer = 23.31
t = hi
sys_enable = true
[sector]
hello = world-world
sys_enable = true
[command]
command = echo hello
runs = 42
`)
assertEqualBytes(t, expectedIni, b, "did not get expected output")
}
func TestMarshalNonPointer(t *testing.T) {
testStruct := testStruct{
ConfigStr: "world",
ConfigInt: 2,
Timmer: 23.31,
Val: "hi",
Hello: subStruct{
Bar: "world-world",
SystemEnabled: true,
},
SystemEnabled: true,
Command: subStruct2{
Command: "echo hello",
Runs: 42,
},
}
b, err := Marshal(testStruct)
assertNil(t, err, "failed to marshal")
expectedIni := []byte(`hello = world
bar = 2
timer = 23.31
t = hi
sys_enable = true
[sector]
hello = world-world
sys_enable = true
[command]
command = echo hello
runs = 42
`)
assertEqualBytes(t, expectedIni, b, "did not get expected output")
}
func TestMarshalNonStruct(t *testing.T) {
{
s := "string"
b, err := Marshal(s)
assertNotNil(t, err, "expected error")
assertEqualBytes(t, []byte{}, b)
}
{
s := "string"
b, err := Marshal(&s)
assertNotNil(t, err, "expected error")
assertEqualBytes(t, []byte{}, b)
}
{
s := 42
b, err := Marshal(s)
assertNotNil(t, err, "expected error")
assertEqualBytes(t, []byte{}, b)
}
{
s := true
b, err := Marshal(s)
assertNotNil(t, err, "expected error")
assertEqualBytes(t, []byte{}, b)
}
{
s := make(map[string]string)
b, err := Marshal(s)
assertNotNil(t, err, "expected error")
assertEqualBytes(t, []byte{}, b)
}
}