diff --git a/tests/testsuite/errors.rs b/tests/testsuite/errors.rs index 6cb2e01c..8647044d 100644 --- a/tests/testsuite/errors.rs +++ b/tests/testsuite/errors.rs @@ -110,6 +110,33 @@ fn test_get_invalid_type() { ); } +#[test] +#[cfg(feature = "json")] +fn test_get_missing_field() { + #[derive(Debug, Deserialize)] + struct InnerSettings { + #[allow(dead_code)] + value: u32, + #[allow(dead_code)] + value2: u32, + } + + let c = Config::builder() + .add_source(File::from_str( + r#" +{ + "inner": { "value": 42 } +} + "#, + FileFormat::Json, + )) + .build() + .unwrap(); + + let res = c.get::("inner"); + assert_data_eq!(res.unwrap_err().to_string(), str!["missing field `value2`"]); +} + #[test] #[cfg(feature = "json")] fn test_get_bool_invalid_type() { @@ -284,3 +311,36 @@ fn test_deserialize_invalid_type() { panic!("Wrong error {:?}", e); } } + +#[test] +#[cfg(feature = "json")] +fn test_deserialize_missing_field() { + #[derive(Debug, Deserialize)] + struct Settings { + #[allow(dead_code)] + inner: InnerSettings, + } + + #[derive(Debug, Deserialize)] + struct InnerSettings { + #[allow(dead_code)] + value: u32, + #[allow(dead_code)] + value2: u32, + } + + let c = Config::builder() + .add_source(File::from_str( + r#" +{ + "inner": { "value": 42 } +} + "#, + FileFormat::Json, + )) + .build() + .unwrap(); + + let res = c.try_deserialize::(); + assert_data_eq!(res.unwrap_err().to_string(), str!["missing field `value2`"]); +}