@@ -18,7 +18,7 @@ import (
1818
1919func TestKoanfAdapter_Route (t * gotesting.T ) {
2020 t .Parallel ()
21- ka := prepareTestSubject (t )
21+ ka := prepareJSONTestSubject (t )
2222 assert .Implements (t , MapAdapter {}, ka .Route ("foo" ))
2323 assert .Implements (t , MapAdapter {}, ka .Route ("foo" ))
2424}
@@ -30,7 +30,7 @@ func TestKoanfAdapter_race(t *gotesting.T) {
3030 }
3131 }()
3232 t .Parallel ()
33- ka := prepareTestSubject (t )
33+ ka := prepareJSONTestSubject (t )
3434 for i := 0 ; i < 100 ; i ++ {
3535 go ka .Reload ()
3636 ka .String ("string" )
@@ -81,37 +81,55 @@ func TestKoanfAdapter_Watch(t *gotesting.T) {
8181
8282func TestKoanfAdapter_Bool (t * gotesting.T ) {
8383 t .Parallel ()
84- k := prepareTestSubject (t )
84+ k := prepareJSONTestSubject (t )
8585 assert .True (t , k .Bool ("bool" ))
8686}
8787
8888func TestKoanfAdapter_String (t * gotesting.T ) {
8989 t .Parallel ()
90- k := prepareTestSubject (t )
90+ k := prepareJSONTestSubject (t )
9191 assert .Equal (t , "string" , k .String ("string" ))
9292}
9393
9494func TestKoanfAdapter_Strings (t * gotesting.T ) {
9595 t .Parallel ()
96- k := prepareTestSubject (t )
96+ k := prepareJSONTestSubject (t )
9797 assert .Equal (t , []string {"foo" , "bar" }, k .Strings ("strings" ))
9898}
9999
100100func TestKoanfAdapter_Float64 (t * gotesting.T ) {
101101 t .Parallel ()
102- k := prepareTestSubject (t )
102+ k := prepareJSONTestSubject (t )
103103 assert .Equal (t , 1.0 , k .Float64 ("float" ))
104104}
105105
106106func TestKoanfAdapter_Get (t * gotesting.T ) {
107107 t .Parallel ()
108- k := prepareTestSubject (t )
108+ k := prepareJSONTestSubject (t )
109109 assert .Equal (t , 1.0 , k .Get ("float" ))
110110}
111111
112- func TestKoanfAdapter_Unmarshal (t * gotesting.T ) {
112+ func TestKoanfAdapter_Unmarshal_Json (t * gotesting.T ) {
113113 t .Parallel ()
114- ka := prepareTestSubject (t )
114+ ka := prepareJSONTestSubject (t )
115+ var target string
116+ err := ka .Unmarshal ("foo.bar" , & target )
117+ assert .NoError (t , err )
118+ assert .Equal (t , "baz" , target )
119+
120+ var r Duration
121+ err = ka .Unmarshal ("duration_string" , & r )
122+ assert .NoError (t , err )
123+ assert .Equal (t , r , Duration {1 * time .Second })
124+
125+ err = ka .Unmarshal ("duration_number" , & r )
126+ assert .NoError (t , err )
127+ assert .Equal (t , r , Duration {1 * time .Nanosecond })
128+ }
129+
130+ func TestKoanfAdapter_Unmarshal_Yaml (t * gotesting.T ) {
131+ t .Parallel ()
132+ ka := prepareYamlTestSubject (t )
115133 var target string
116134 err := ka .Unmarshal ("foo.bar" , & target )
117135 assert .NoError (t , err )
@@ -201,11 +219,20 @@ func TestMapAdapter_Unmarshal(t *gotesting.T) {
201219 }, target )
202220}
203221
204- func prepareTestSubject (t * gotesting.T ) * KoanfAdapter {
222+ func prepareJSONTestSubject (t * gotesting.T ) * KoanfAdapter {
205223 k := koanf .New ("." )
206224 if err := k .Load (file .Provider ("testdata/mock.json" ), json .Parser ()); err != nil {
207225 t .Fatalf ("error loading config: %v" , err )
208226 }
209227 ka := KoanfAdapter {K : k }
210228 return & ka
211229}
230+
231+ func prepareYamlTestSubject (t * gotesting.T ) * KoanfAdapter {
232+ k := koanf .New ("." )
233+ if err := k .Load (file .Provider ("testdata/mock.yaml" ), yaml .Parser ()); err != nil {
234+ t .Fatalf ("error loading config: %v" , err )
235+ }
236+ ka := KoanfAdapter {K : k }
237+ return & ka
238+ }
0 commit comments