@@ -110,6 +110,28 @@ fn test_get_invalid_type() {
110
110
) ;
111
111
}
112
112
113
+ #[ test]
114
+ #[ cfg( feature = "json" ) ]
115
+ fn test_get_invalid_type_file ( ) {
116
+ let c = Config :: builder ( )
117
+ . add_source ( File :: new (
118
+ "tests/testsuite/get-invalid-type.json" ,
119
+ FileFormat :: Json ,
120
+ ) )
121
+ . build ( )
122
+ . unwrap ( ) ;
123
+
124
+ let res = c. get :: < bool > ( "boolean_s_parse" ) ;
125
+
126
+ assert ! ( res. is_err( ) ) ;
127
+ assert_data_eq ! (
128
+ res. unwrap_err( ) . to_string( ) ,
129
+ str ![ [
130
+ r#"invalid type: string "fals", expected a boolean for key `boolean_s_parse` in tests/testsuite/get-invalid-type.json"#
131
+ ] ]
132
+ ) ;
133
+ }
134
+
113
135
#[ test]
114
136
#[ cfg( feature = "json" ) ]
115
137
fn test_get_missing_field ( ) {
@@ -140,6 +162,32 @@ fn test_get_missing_field() {
140
162
) ;
141
163
}
142
164
165
+ #[ test]
166
+ #[ cfg( feature = "json" ) ]
167
+ fn test_get_missing_field_file ( ) {
168
+ #[ derive( Debug , Deserialize ) ]
169
+ struct InnerSettings {
170
+ #[ allow( dead_code) ]
171
+ value : u32 ,
172
+ #[ allow( dead_code) ]
173
+ value2 : u32 ,
174
+ }
175
+
176
+ let c = Config :: builder ( )
177
+ . add_source ( File :: new (
178
+ "tests/testsuite/get-missing-field.json" ,
179
+ FileFormat :: Json ,
180
+ ) )
181
+ . build ( )
182
+ . unwrap ( ) ;
183
+
184
+ let res = c. get :: < InnerSettings > ( "inner" ) ;
185
+ assert_data_eq ! (
186
+ res. unwrap_err( ) . to_string( ) ,
187
+ str ![ "missing field `value2` for key `inner`" ]
188
+ ) ;
189
+ }
190
+
143
191
#[ test]
144
192
#[ cfg( feature = "json" ) ]
145
193
fn test_get_bool_invalid_type ( ) {
@@ -315,6 +363,47 @@ fn test_deserialize_invalid_type() {
315
363
}
316
364
}
317
365
366
+ #[ test]
367
+ #[ cfg( feature = "json" ) ]
368
+ fn test_deserialize_invalid_type_file ( ) {
369
+ #[ derive( Deserialize , Debug ) ]
370
+ struct Place {
371
+ #[ allow( dead_code) ]
372
+ name : usize , // is actually s string
373
+ }
374
+
375
+ #[ derive( Deserialize , Debug ) ]
376
+ struct Output {
377
+ #[ allow( dead_code) ]
378
+ place : Place ,
379
+ }
380
+
381
+ let c = Config :: builder ( )
382
+ . add_source ( File :: new (
383
+ "tests/testsuite/deserialize-invalid-type.json" ,
384
+ FileFormat :: Json ,
385
+ ) )
386
+ . build ( )
387
+ . unwrap ( ) ;
388
+
389
+ let res = c. try_deserialize :: < Output > ( ) ;
390
+ let e = res. unwrap_err ( ) ;
391
+ assert_data_eq ! (
392
+ e. to_string( ) ,
393
+ str ![ [
394
+ r#"invalid type: string "Torre di Pisa", expected an integer for key `place.name` in tests/testsuite/deserialize-invalid-type.json"#
395
+ ] ]
396
+ ) ;
397
+ if let ConfigError :: Type {
398
+ key : Some ( path) , ..
399
+ } = e
400
+ {
401
+ assert_eq ! ( path, "place.name" ) ;
402
+ } else {
403
+ panic ! ( "Wrong error {:?}" , e) ;
404
+ }
405
+ }
406
+
318
407
#[ test]
319
408
#[ cfg( feature = "json" ) ]
320
409
fn test_deserialize_missing_field ( ) {
@@ -350,3 +439,35 @@ fn test_deserialize_missing_field() {
350
439
str ![ "missing field `value2` for key `inner`" ]
351
440
) ;
352
441
}
442
+
443
+ #[ test]
444
+ #[ cfg( feature = "json" ) ]
445
+ fn test_deserialize_missing_field_file ( ) {
446
+ #[ derive( Debug , Deserialize ) ]
447
+ struct Settings {
448
+ #[ allow( dead_code) ]
449
+ inner : InnerSettings ,
450
+ }
451
+
452
+ #[ derive( Debug , Deserialize ) ]
453
+ struct InnerSettings {
454
+ #[ allow( dead_code) ]
455
+ value : u32 ,
456
+ #[ allow( dead_code) ]
457
+ value2 : u32 ,
458
+ }
459
+
460
+ let c = Config :: builder ( )
461
+ . add_source ( File :: new (
462
+ "tests/testsuite/deserialize-missing-field.json" ,
463
+ FileFormat :: Json ,
464
+ ) )
465
+ . build ( )
466
+ . unwrap ( ) ;
467
+
468
+ let res = c. try_deserialize :: < Settings > ( ) ;
469
+ assert_data_eq ! (
470
+ res. unwrap_err( ) . to_string( ) ,
471
+ str ![ "missing field `value2` for key `inner`" ]
472
+ ) ;
473
+ }
0 commit comments