-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_file_parsing_test.go
110 lines (88 loc) · 1.85 KB
/
config_file_parsing_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
package confy
import (
"reflect"
"testing"
)
type innerStructWithNest struct {
Thing string
I innerStruct
}
type innerStruct struct {
Mff string
Oorg int
}
type testStruct struct {
Thonku innerStruct `confy:"thonku_complex"`
Thing string `confy:"thing"`
I int `confy:"i_int"`
B bool `confy:"b_bool"`
Things []string `confy:"things_array"`
Ahhh []innerStruct `confy:"array_complex"`
SimplePtr *string `confy:"simple_ptr"`
VeryAhh []innerStructWithNest `confy:"array_very_complex"`
}
var dummy = testStruct{
Thonku: innerStruct{
Mff: "inner_string",
Oorg: 3,
},
Thing: "example_string",
I: 42,
B: true,
Things: []string{"string1", "string2", "string3"},
Ahhh: []innerStruct{
{
Mff: "first_inner",
},
{
Mff: "second_inner",
},
},
VeryAhh: []innerStructWithNest{
{
Thing: "test",
I: innerStruct{
Mff: "very_complex_inner",
Oorg: 2,
},
},
},
}
func TestAutoParser(t *testing.T) {
s := "present"
dummy.SimplePtr = &s
config, err := LoadConfigFileAuto[testStruct]("testdata/test.json", false)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(config, dummy) {
t.Fatalf("%+v", config)
}
config, err = LoadConfigFileAuto[testStruct]("testdata/test.toml", false)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(config, dummy) {
t.Fatalf("%+v", config)
}
config, err = LoadConfigFileAuto[testStruct]("testdata/test.yaml", false)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(config, dummy) {
t.Fatalf("%+v", config)
}
}
func TestDuplicateTagDetection(t *testing.T) {
type duplicates struct {
Test string `confy:"test"`
Toast string `confy:"test"`
}
defer func() {
if a := recover(); a == nil {
// didnt detect a duplicate tag
t.Fail()
}
}()
LoadConfigFileAuto[duplicates]("testdata/duplicates.json", false)
}