generated from inherelab/go-pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_test.go
98 lines (80 loc) · 1.96 KB
/
options_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
package properties_test
import (
"testing"
"time"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/properties"
)
func TestOptions_basic(t *testing.T) {
opt := &properties.Options{}
fn := properties.WithTagName("config")
fn(opt)
assert.Eq(t, "config", opt.TagName)
}
func TestOptions_InlineComment(t *testing.T) {
text := `
key = value # inline comments
key2 = value2 // inline comments
key3 = value3
`
p := properties.NewParser()
err := p.Parse(text)
assert.NoErr(t, err)
assert.Eq(t, "value # inline comments", p.Str("key"))
p = properties.NewParser(func(opts *properties.Options) {
opts.InlineComment = true
})
err = p.Parse(text)
assert.NoErr(t, err)
assert.Eq(t, "value", p.Str("key"))
}
func TestParseTime(t *testing.T) {
text := `
// properties string
name = inhere
age = 200
expire = 3s
`
type MyConf struct {
Name string `properties:"name"`
Age int `properties:"age"`
Expire time.Duration `properties:"expire"`
}
p, err := properties.Parse(text, properties.ParseTime)
assert.NoErr(t, err)
cfg := &MyConf{}
err = p.MapStruct("", cfg)
assert.NoErr(t, err)
assert.Eq(t, "inhere", cfg.Name)
assert.Eq(t, 3*time.Second, cfg.Expire)
err = p.MapStruct("not-found", cfg)
assert.Err(t, err)
}
func TestOptions_BeforeCollect(t *testing.T) {
text := `
// properties string
name = inhere
age = 200
expire = 3s
`
type MyConf struct {
Name string `properties:"name"`
Age int `properties:"age"`
Expire time.Duration `properties:"expire"`
}
// tests Unmarshal, BeforeCollect
p := properties.NewParser(properties.ParseTime, func(opts *properties.Options) {
opts.BeforeCollect = func(name string, val any) any {
if name == "name" {
return strutil.Upper(val.(string))
}
return val
}
})
cfg := &MyConf{}
err := p.Unmarshal([]byte(text), cfg)
assert.NoErr(t, err)
assert.Eq(t, "INHERE", cfg.Name)
assert.Eq(t, 3*time.Second, cfg.Expire)
}