-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathconfig_test.go
168 lines (146 loc) · 3.95 KB
/
config_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package golf
import (
"bytes"
"testing"
)
func TestConfig(t *testing.T) {
cases := []struct {
key string
value interface{}
}{
{"foo", "bar"},
{"foo", 123},
{"foo", true},
{"foo", 56.23},
{"foo/bar", "bar"},
{"123/foo/bar", "bar"},
{"foo/bar/bar/bar", "bar"},
}
defaultValue := "None"
for _, c := range cases {
config := NewConfig()
config.Set(c.key, c.value)
value, err := config.Get(c.key, defaultValue)
if err != nil {
t.Error(err)
}
if value != c.value {
t.Errorf("Value not match: %q != %q, key: %q", value, c.value, c.key)
}
}
}
func TestConfigWithMultipleEntires(t *testing.T) {
settings := []struct {
key, value string
}{
{"foo/bar/bar", "bar"},
{"foo/bar/bar2", "bar2"},
{"foo/bar3", "bar3"},
{"foo2", "bar4"},
}
config := NewConfig()
for _, c := range settings {
config.Set(c.key, c.value)
}
for _, c := range settings {
value, err := config.Get(c.key, "None")
if err != nil {
t.Error(err)
}
if value != c.value {
t.Errorf("Value not match: %q != %q, key: %q", value, c.value, c.key)
}
}
}
func TestFromJSON(t *testing.T) {
reader := bytes.NewReader([]byte(`{"cool" : {"foo" : "bar"}}`))
config, err := ConfigFromJSON(reader)
if err != nil {
t.Error(err)
t.Fail()
}
value, err := config.GetString("cool/foo", "")
if err != nil {
t.Error(err)
t.Fail()
}
if value != "bar" {
t.Errorf("expected value to be abc but it was %v", value)
}
}
func TestGetStringException(t *testing.T) {
defaultValue := "None"
config := NewConfig()
config.Set("foo", 123)
val, err := config.GetString("foo", defaultValue)
if err == nil {
t.Errorf("Should have raised an type error when getting a non-string value by GetString.")
}
if val != defaultValue {
t.Errorf("Should have used the default value when raising an error.")
}
val, err = config.GetString("bar", defaultValue)
if err == nil {
t.Errorf("Should have raised an type error when getting a non-existed value by GetString.")
}
if val != defaultValue {
t.Errorf("Should have used the default value when raising an error.")
}
}
func TestGetIntegerException(t *testing.T) {
defaultValue := 123
config := NewConfig()
config.Set("foo", "bar")
val, err := config.GetInt("foo", defaultValue)
if err == nil {
t.Errorf("Should have raised an type error when getting a non-string value by GetString.")
}
if val != defaultValue {
t.Errorf("Should have used the default value when raising an error.")
}
val, err = config.GetInt("bar", defaultValue)
if err == nil {
t.Errorf("Should have raised an type error when getting a non-existed value by GetString.")
}
if val != defaultValue {
t.Errorf("Should have used the default value when raising an error.")
}
}
func TestGetBoolException(t *testing.T) {
defaultValue := false
config := NewConfig()
config.Set("foo", "bar")
val, err := config.GetBool("foo", defaultValue)
if err == nil {
t.Errorf("Should have raised an type error when getting a non-string value by GetString.")
}
if val != defaultValue {
t.Errorf("Should have used the default value when raising an error.")
}
val, err = config.GetBool("bar", defaultValue)
if err == nil {
t.Errorf("Should have raised an type error when getting a non-existed value by GetString.")
}
if val != defaultValue {
t.Errorf("Should have used the default value when raising an error.")
}
}
func TestGetFloat64Exception(t *testing.T) {
defaultValue := 0.5
config := NewConfig()
config.Set("foo", "bar")
val, err := config.GetFloat("foo", defaultValue)
if err == nil {
t.Errorf("Should have raised an type error when getting a non-string value by GetString.")
}
if val != defaultValue {
t.Errorf("Should have used the default value when raising an error.")
}
val, err = config.GetFloat("bar", defaultValue)
if err == nil {
t.Errorf("Should have raised an type error when getting a non-existed value by GetString.")
}
if val != defaultValue {
t.Errorf("Should have used the default value when raising an error.")
}
}