7
"github.com/gookit/ini/v2"
10
type testStruct struct {
11
A string `app_config:"PARAM_A"`
12
B int `app_config:"PARAM_B"`
16
type testInvalidStruct struct {
17
D string `app_config:"PARAM_D"`
18
E int `app_config:"PARAM_E"`
19
F float32 `app_config:"PARAM_F"`
22
type testInvalidParam struct {
23
D string `app_config:"PARAM_D"`
24
Z int `app_config:"PARAM_Z"`
27
var iniStr = `PARAM_A = test_a
34
func TestParseField(t *testing.T) {
36
fields := parseFields(reflect.TypeOf(ta))
38
if !testContainField("A", fields) {
39
t.Error("testStruct does not contain the \"A\" field")
42
if testContainField("C", fields) {
43
t.Error("testStruct contain the \"C\" field")
47
func TestSetupField(t *testing.T) {
48
ini.LoadStrings(iniStr)
51
fields := parseFields(reflect.TypeOf(ta))
53
rv := reflect.ValueOf(&ta)
54
err := setupFields(rv, fields)
60
t.Error("incorrect value for PARAM_A")
63
t.Error("incorrect value for PARAM_B")
66
t.Error("incorrect value for \"C\" field")
70
func TestSetupFieldWithInvalidStruct(t *testing.T) {
71
ini.LoadStrings(iniStr)
73
ta := testInvalidStruct{}
74
fields := parseFields(reflect.TypeOf(ta))
76
rv := reflect.ValueOf(&ta)
77
err := setupFields(rv, fields)
79
if err.Error() != "config: undefined type float32 of field F" {
80
t.Error("unknown error message for undefined type")
83
t.Error("ignoring unknown type")
87
func TestInvalidParameter(t *testing.T) {
88
ini.LoadStrings(iniStr)
90
ta := testInvalidParam{}
91
fields := parseFields(reflect.TypeOf(ta))
93
rv := reflect.ValueOf(&ta)
94
err := setupFields(rv, fields)
96
if err.Error() != "app.ini: Undefined parameter \"PARAM_Z\"" {
97
t.Error("unknown error message for undefined parameter")
100
t.Error("ignoring unknown parameter")
104
func testContainField(fieldName string, fields []structField) bool {
105
for _, el := range fields {
106
if el.name == fieldName {