@@ -5,7 +5,7 @@ use config::{Config, ConfigError, File, FileFormat, Map, Value};
5
5
6
6
#[ test]
7
7
#[ cfg( feature = "json" ) ]
8
- fn test_error_path_index_bounds ( ) {
8
+ fn test_path_index_bounds ( ) {
9
9
let c = Config :: builder ( )
10
10
. add_source ( File :: from_str (
11
11
r#"
@@ -28,7 +28,7 @@ fn test_error_path_index_bounds() {
28
28
29
29
#[ test]
30
30
#[ cfg( feature = "json" ) ]
31
- fn test_error_path_index_negative_bounds ( ) {
31
+ fn test_path_index_negative_bounds ( ) {
32
32
let c = Config :: builder ( )
33
33
. add_source ( File :: from_str (
34
34
r#"
@@ -51,7 +51,7 @@ fn test_error_path_index_negative_bounds() {
51
51
52
52
#[ test]
53
53
#[ cfg( feature = "json" ) ]
54
- fn test_error_parse ( ) {
54
+ fn test_parse ( ) {
55
55
let res = Config :: builder ( )
56
56
. add_source ( File :: from_str (
57
57
r#"
@@ -72,67 +72,47 @@ fn test_error_parse() {
72
72
73
73
#[ test]
74
74
#[ cfg( feature = "json" ) ]
75
- fn test_error_type ( ) {
76
- let c = Config :: builder ( )
77
- . add_source ( File :: from_str (
78
- r#"
79
- {
80
- "boolean_s_parse": "fals"
81
- }
82
- "# ,
83
- FileFormat :: Json ,
84
- ) )
75
+ fn test_root_not_table ( ) {
76
+ let e = Config :: builder ( )
77
+ . add_source ( File :: from_str ( r#"false"# , FileFormat :: Json ) )
85
78
. build ( )
86
- . unwrap ( ) ;
87
-
88
- let res = c. get :: < bool > ( "boolean_s_parse" ) ;
89
-
90
- assert ! ( res. is_err( ) ) ;
91
- assert_data_eq ! (
92
- res. unwrap_err( ) . to_string( ) ,
93
- str ![ [ r#"invalid type: string "fals", expected a boolean for key `boolean_s_parse`"# ] ]
94
- ) ;
79
+ . unwrap_err ( ) ;
80
+ match e {
81
+ ConfigError :: FileParse { cause, .. } => assert_eq ! (
82
+ "invalid type: boolean `false`, expected a map" ,
83
+ format!( "{cause}" )
84
+ ) ,
85
+ _ => panic ! ( "Wrong error: {:?}" , e) ,
86
+ }
95
87
}
96
88
97
89
#[ test]
98
90
#[ cfg( feature = "json" ) ]
99
- fn test_error_deser_whole ( ) {
100
- #[ derive( Deserialize , Debug ) ]
101
- struct Place {
102
- #[ allow( dead_code) ]
103
- name : usize , // is actually s string
104
- }
105
-
106
- #[ derive( Deserialize , Debug ) ]
107
- struct Output {
108
- #[ allow( dead_code) ]
109
- place : Place ,
110
- }
111
-
91
+ fn test_get_invalid_type ( ) {
112
92
let c = Config :: builder ( )
113
93
. add_source ( File :: from_str (
114
94
r#"
115
95
{
116
- "place": {
117
- "name": "Torre di Pisa"
118
- }
96
+ "boolean_s_parse": "fals"
119
97
}
120
98
"# ,
121
99
FileFormat :: Json ,
122
100
) )
123
101
. build ( )
124
102
. unwrap ( ) ;
125
103
126
- let res = c. try_deserialize :: < Output > ( ) ;
104
+ let res = c. get :: < bool > ( "boolean_s_parse" ) ;
105
+
106
+ assert ! ( res. is_err( ) ) ;
127
107
assert_data_eq ! (
128
108
res. unwrap_err( ) . to_string( ) ,
129
- str ![ [ r#"invalid type: string "Torre di Pisa ", expected an integer for key `place.name `"# ] ]
109
+ str ![ [ r#"invalid type: string "fals ", expected a boolean for key `boolean_s_parse `"# ] ]
130
110
) ;
131
111
}
132
112
133
113
#[ test]
134
114
#[ cfg( feature = "json" ) ]
135
- fn test_error_type_detached ( ) {
115
+ fn test_get_bool_invalid_type ( ) {
136
116
let c = Config :: builder ( )
137
117
. add_source ( File :: from_str (
138
118
r#"
@@ -145,43 +125,42 @@ fn test_error_type_detached() {
145
125
. build ( )
146
126
. unwrap ( ) ;
147
127
148
- let value = c. get :: < Value > ( "boolean_s_parse" ) . unwrap ( ) ;
149
- let res = value. try_deserialize :: < bool > ( ) ;
128
+ let res = c. get_bool ( "boolean_s_parse" ) ;
150
129
151
130
assert ! ( res. is_err( ) ) ;
152
131
assert_data_eq ! (
153
132
res. unwrap_err( ) . to_string( ) ,
154
- str ![ [ r#"invalid type: string "fals", expected a boolean"# ] ]
133
+ str ![ [ r#"invalid type: string "fals", expected a boolean for key `boolean_s_parse` "# ] ]
155
134
) ;
156
135
}
157
136
158
137
#[ test]
159
138
#[ cfg( feature = "json" ) ]
160
- fn test_error_type_get_bool ( ) {
139
+ fn test_get_table_invalid_type ( ) {
161
140
let c = Config :: builder ( )
162
141
. add_source ( File :: from_str (
163
142
r#"
164
143
{
165
- "boolean_s_parse ": "fals"
144
+ "debug ": true
166
145
}
167
146
"# ,
168
147
FileFormat :: Json ,
169
148
) )
170
149
. build ( )
171
150
. unwrap ( ) ;
172
151
173
- let res = c. get_bool ( "boolean_s_parse ") ;
152
+ let res = c. get_table ( "debug ") ;
174
153
175
154
assert ! ( res. is_err( ) ) ;
176
155
assert_data_eq ! (
177
156
res. unwrap_err( ) . to_string( ) ,
178
- str ![ [ r# "invalid type: string "fals" , expected a boolean for key `boolean_s_parse`"# ] ]
157
+ str ![ "invalid type: boolean `true` , expected a map for key `debug`" ]
179
158
) ;
180
159
}
181
160
182
161
#[ test]
183
162
#[ cfg( feature = "json" ) ]
184
- fn test_error_type_get_table ( ) {
163
+ fn test_get_array_invalid_type ( ) {
185
164
let c = Config :: builder ( )
186
165
. add_source ( File :: from_str (
187
166
r#"
@@ -194,41 +173,42 @@ fn test_error_type_get_table() {
194
173
. build ( )
195
174
. unwrap ( ) ;
196
175
197
- let res = c. get_table ( "debug" ) ;
176
+ let res = c. get_array ( "debug" ) ;
198
177
199
178
assert ! ( res. is_err( ) ) ;
200
179
assert_data_eq ! (
201
180
res. unwrap_err( ) . to_string( ) ,
202
- str ![ "invalid type: boolean `true`, expected a map for key `debug`" ]
181
+ str ![ "invalid type: boolean `true`, expected an array for key `debug`" ]
203
182
) ;
204
183
}
205
184
206
185
#[ test]
207
186
#[ cfg( feature = "json" ) ]
208
- fn test_error_type_get_array ( ) {
187
+ fn test_value_deserialize_invalid_type ( ) {
209
188
let c = Config :: builder ( )
210
189
. add_source ( File :: from_str (
211
190
r#"
212
191
{
213
- "debug ": true
192
+ "boolean_s_parse ": "fals"
214
193
}
215
194
"# ,
216
195
FileFormat :: Json ,
217
196
) )
218
197
. build ( )
219
198
. unwrap ( ) ;
220
199
221
- let res = c. get_array ( "debug" ) ;
200
+ let value = c. get :: < Value > ( "boolean_s_parse" ) . unwrap ( ) ;
201
+ let res = value. try_deserialize :: < bool > ( ) ;
222
202
223
203
assert ! ( res. is_err( ) ) ;
224
204
assert_data_eq ! (
225
205
res. unwrap_err( ) . to_string( ) ,
226
- str ![ "invalid type: boolean `true` , expected an array for key `debug`" ]
206
+ str ![ [ r# "invalid type: string "fals" , expected a boolean"# ] ]
227
207
) ;
228
208
}
229
209
230
210
#[ test]
231
- fn test_error_enum_de ( ) {
211
+ fn test_value_deserialize_enum ( ) {
232
212
#[ derive( Debug , Deserialize , PartialEq , Eq ) ]
233
213
enum Diode {
234
214
Off ,
@@ -262,57 +242,45 @@ fn test_error_enum_de() {
262
242
263
243
#[ test]
264
244
#[ cfg( feature = "json" ) ]
265
- fn error_with_path ( ) {
266
- #[ derive( Debug , Deserialize ) ]
267
- struct Inner {
245
+ fn test_deserialize_invalid_type ( ) {
246
+ #[ derive( Deserialize , Debug ) ]
247
+ struct Place {
268
248
#[ allow( dead_code) ]
269
- test : i32 ,
249
+ name : usize , // is actually s string
270
250
}
271
251
272
- #[ derive( Debug , Deserialize ) ]
273
- struct Outer {
252
+ #[ derive( Deserialize , Debug ) ]
253
+ struct Output {
274
254
#[ allow( dead_code) ]
275
- inner : Inner ,
255
+ place : Place ,
276
256
}
277
- const CFG : & str = r#"
257
+
258
+ let c = Config :: builder ( )
259
+ . add_source ( File :: from_str (
260
+ r#"
278
261
{
279
- "inner ": {
280
- "test ": "ABC "
262
+ "place ": {
263
+ "name ": "Torre di Pisa "
281
264
}
282
265
}
283
- "# ;
284
-
285
- let e = Config :: builder ( )
286
- . add_source ( File :: from_str ( CFG , FileFormat :: Json ) )
266
+ "# ,
267
+ FileFormat :: Json ,
268
+ ) )
287
269
. build ( )
288
- . unwrap ( )
289
- . try_deserialize :: < Outer > ( )
290
- . unwrap_err ( ) ;
270
+ . unwrap ( ) ;
291
271
272
+ let res = c. try_deserialize :: < Output > ( ) ;
273
+ let e = res. unwrap_err ( ) ;
274
+ assert_data_eq ! (
275
+ e. to_string( ) ,
276
+ str ![ [ r#"invalid type: string "Torre di Pisa", expected an integer for key `place.name`"# ] ]
277
+ ) ;
292
278
if let ConfigError :: Type {
293
279
key : Some ( path) , ..
294
280
} = e
295
281
{
296
- assert_eq ! ( path, "inner.test " ) ;
282
+ assert_eq ! ( path, "place.name " ) ;
297
283
} else {
298
284
panic ! ( "Wrong error {:?}" , e) ;
299
285
}
300
286
}
301
-
302
- #[ test]
303
- #[ cfg( feature = "json" ) ]
304
- fn test_error_root_not_table ( ) {
305
- match Config :: builder ( )
306
- . add_source ( File :: from_str ( r#"false"# , FileFormat :: Json ) )
307
- . build ( )
308
- {
309
- Ok ( _) => panic ! ( "Should not merge if root is not a table" ) ,
310
- Err ( e) => match e {
311
- ConfigError :: FileParse { cause, .. } => assert_eq ! (
312
- "invalid type: boolean `false`, expected a map" ,
313
- format!( "{cause}" )
314
- ) ,
315
- _ => panic ! ( "Wrong error: {:?}" , e) ,
316
- } ,
317
- }
318
- }
0 commit comments