Skip to content

Commit

Permalink
test: add test to show the error is without path info
Browse files Browse the repository at this point in the history
  • Loading branch information
allevo authored and epage committed Jan 10, 2025
1 parent f239510 commit 5852262
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/testsuite/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<InnerSettings>("inner");
assert_data_eq!(res.unwrap_err().to_string(), str!["missing field `value2`"]);
}

#[test]
#[cfg(feature = "json")]
fn test_get_bool_invalid_type() {
Expand Down Expand Up @@ -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::<Settings>();
assert_data_eq!(res.unwrap_err().to_string(), str!["missing field `value2`"]);
}

0 comments on commit 5852262

Please sign in to comment.