generated from inherelab/go-pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_test.go
63 lines (54 loc) · 1.37 KB
/
encoder_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
package properties_test
import (
"fmt"
"testing"
"time"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/properties"
)
func TestEncode(t *testing.T) {
e := properties.NewEncoder()
bs, err := e.Marshal(map[string]any{
"name": "inhere",
"age": 234,
"str1": "a string",
"str2": "a multi \nline string",
"top": map[string]any{
"sub0": "val0",
"sub1": []string{"val1-0", "val1-1"},
},
})
str := string(bs)
fmt.Println(str)
assert.NoErr(t, err)
assert.NotEmpty(t, bs)
assert.StrContains(t, str, "name=inhere")
assert.StrContains(t, str, "top.sub1[0]=val1-0")
assert.StrContains(t, str, "str2=a multi \\\nline string")
bs, err = properties.Marshal(nil)
assert.NoErr(t, err)
assert.Nil(t, bs)
}
func TestEncode_struct(t *testing.T) {
type MyConf struct {
Name string `properties:"name"`
Age int `properties:"age"`
Expire time.Duration `properties:"expire"`
}
myc := &MyConf{
Name: "inhere",
Age: 234,
Expire: time.Second * 3,
}
bs, err := properties.Encode(myc)
assert.NoErr(t, err)
str := string(bs)
assert.StrContains(t, str, "name=inhere")
assert.StrContains(t, str, "age=234")
assert.StrContains(t, str, "expire=3000000000")
}
func TestEncode_error(t *testing.T) {
bs, err := properties.Encode([]int{12, 34})
assert.Nil(t, bs)
assert.ErrMsg(t, err, "only allow encode map and struct data")
}