-
Notifications
You must be signed in to change notification settings - Fork 0
/
apis_test.go
123 lines (96 loc) · 2.53 KB
/
apis_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
111
112
113
114
115
116
117
118
119
120
121
122
123
package goconf_test
import (
"bytes"
"io"
"testing"
"github.com/ah-its-andy/goconf"
physicalfile "github.com/ah-its-andy/goconf/physicalFile"
"github.com/stretchr/testify/assert"
)
func newTestReader() io.Reader {
return bytes.NewReader([]byte(
`name: "test"
age: 18
sec:
key: "value"
# comment`))
}
func initTestConfig(reader io.Reader) {
goconf.Init(func(b goconf.Builder) {
b.AddSource(physicalfile.YamlReader(reader))
})
}
func TestGetSectionShouldPassed(t *testing.T) {
initTestConfig(newTestReader())
sec := goconf.GetSection("sec")
assert.NotNil(t, sec)
v, ok := sec.GetString("key")
assert.True(t, ok)
assert.Equal(t, "value", v)
}
func TestSectionBindMapShouldPassed(t *testing.T) {
initTestConfig(newTestReader())
sec := goconf.GetSection("sec")
assert.NotNil(t, sec)
var recv map[string]interface{}
err := sec.Bind(&recv)
assert.Nil(t, err)
assert.Equal(t, "value", recv["key"])
}
type bindTestStrcut struct {
Key string `json:"key"`
}
func TestSectionBindStructShouldPassed(t *testing.T) {
initTestConfig(newTestReader())
sec := goconf.GetSection("sec")
assert.NotNil(t, sec)
var recv bindTestStrcut
err := sec.Bind(&recv)
assert.Nil(t, err)
assert.Equal(t, "value", recv.Key)
}
func TestGetStringShouldPassed(t *testing.T) {
initTestConfig(newTestReader())
v, ok := goconf.GetString("name")
assert.True(t, ok)
assert.Equal(t, "test", v)
v, ok = goconf.GetString("age")
assert.True(t, ok)
assert.Equal(t, "18", v)
}
func TestGetStringNotExistsShouldPassed(t *testing.T) {
initTestConfig(newTestReader())
v, ok := goconf.GetString("notexists")
assert.False(t, ok)
assert.Equal(t, "", v)
}
func TestGetDefaultShouldPassed(t *testing.T) {
initTestConfig(newTestReader())
v := goconf.GetStringOrDefault("notexists", "default")
assert.Equal(t, "default", v)
}
func TestCastToIntShouldPassed(t *testing.T) {
initTestConfig(newTestReader())
v, ok := goconf.Cast("age", goconf.IntConversion)
assert.True(t, ok)
assert.Equal(t, 18, v)
}
func TestCastOrDefaultShouldPassed(t *testing.T) {
initTestConfig(newTestReader())
v := goconf.CastOrDefault("notexists", 18, goconf.IntConversion)
assert.Equal(t, 18, v)
}
func TestCastNotExists(t *testing.T) {
initTestConfig(newTestReader())
v, ok := goconf.Cast("notexists", goconf.IntConversion)
assert.False(t, ok)
assert.Equal(t, nil, v)
}
func TestCastShouldPanicBeforeInitialized(t *testing.T) {
defer func() {
if r := recover(); r != nil {
assert.Equal(t, "configuration is not initialized", r)
}
}()
goconf.Cast("name", goconf.IntConversion)
}