-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_test.go
80 lines (65 loc) · 1.76 KB
/
set_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
package mengpo_test
import (
"testing"
"time"
"github.com/goexl/gox"
"github.com/goexl/mengpo"
)
type (
user struct {
Username string `json:"username"`
Password string `json:"password"`
}
ptr struct {
True bool `default:"true"`
False *bool `default:"false"`
Nil *bool
Timeout time.Duration `default:"1h"`
Bytes gox.Bytes `default:"1g"`
User user `default:"{'username': 'storezhang', 'password': 'test'}"`
Users []*user `default:"[{'username': 'storezhang', 'password': 'test'}]"`
}
envPtr struct {
Env string `default:"${TEST_ENV}"`
}
)
func TestSetByPtr(t *testing.T) {
_ptr := new(ptr)
if err := mengpo.New().Build().Set(_ptr); nil != err {
t.Fatal(err)
}
if true != _ptr.True {
t.Fatalf(`期望:true,实际:%v`, _ptr.True)
}
if false != *_ptr.False {
t.Fatalf(`期望:false,实际:%v`, *_ptr.False)
}
if nil != _ptr.Nil {
t.Fatalf(`期望:nil,实际:%v`, _ptr.Nil)
}
if time.Hour != _ptr.Timeout {
t.Fatalf(`期望:%d,实际:%v`, time.Hour, _ptr.Timeout)
}
if gox.BytesGB != _ptr.Bytes {
t.Fatalf(`期望:%d,实际:%v`, gox.BytesGB, _ptr.Bytes)
}
if `storezhang` != _ptr.User.Username || `test` != _ptr.User.Password {
t.Fatalf(`期望:{"username": "storezhang", "password": "test"},实际:%v`, _ptr.User)
}
if 1 != len(_ptr.Users) {
t.Fatalf(`期望:[{"username": "storezhang", "password": "test"}],实际:%v`, _ptr.Users)
}
}
type getter struct{}
func (g *getter) Get(key string) string {
return key
}
func TestEnvGetter(t *testing.T) {
_ptr := new(envPtr)
if err := mengpo.New().Getter(new(getter)).Build().Set(_ptr); nil != err {
t.Fatal(err)
}
if "TEST_ENV" != _ptr.Env {
t.Fatalf("期望:TEST_ENV,实际:%v", _ptr.Env)
}
}