Skip to content

Commit

Permalink
test(error): Show lack of origin
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 10, 2025
1 parent fd55144 commit 43dc246
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
121 changes: 121 additions & 0 deletions tests/testsuite/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ fn test_get_invalid_type() {
);
}

#[test]
#[cfg(feature = "json")]
fn test_get_invalid_type_file() {
let c = Config::builder()
.add_source(File::new(
"tests/testsuite/get-invalid-type.json",
FileFormat::Json,
))
.build()
.unwrap();

let res = c.get::<bool>("boolean_s_parse");

assert!(res.is_err());
assert_data_eq!(
res.unwrap_err().to_string(),
str![[
r#"invalid type: string "fals", expected a boolean for key `boolean_s_parse` in tests/testsuite/get-invalid-type.json"#
]]
);
}

#[test]
#[cfg(feature = "json")]
fn test_get_missing_field() {
Expand Down Expand Up @@ -140,6 +162,32 @@ fn test_get_missing_field() {
);
}

#[test]
#[cfg(feature = "json")]
fn test_get_missing_field_file() {
#[derive(Debug, Deserialize)]
struct InnerSettings {
#[allow(dead_code)]
value: u32,
#[allow(dead_code)]
value2: u32,
}

let c = Config::builder()
.add_source(File::new(
"tests/testsuite/get-missing-field.json",
FileFormat::Json,
))
.build()
.unwrap();

let res = c.get::<InnerSettings>("inner");
assert_data_eq!(
res.unwrap_err().to_string(),
str!["missing field `value2` for key `inner`"]
);
}

#[test]
#[cfg(feature = "json")]
fn test_get_bool_invalid_type() {
Expand Down Expand Up @@ -315,6 +363,47 @@ fn test_deserialize_invalid_type() {
}
}

#[test]
#[cfg(feature = "json")]
fn test_deserialize_invalid_type_file() {
#[derive(Deserialize, Debug)]
struct Place {
#[allow(dead_code)]
name: usize, // is actually s string
}

#[derive(Deserialize, Debug)]
struct Output {
#[allow(dead_code)]
place: Place,
}

let c = Config::builder()
.add_source(File::new(
"tests/testsuite/deserialize-invalid-type.json",
FileFormat::Json,
))
.build()
.unwrap();

let res = c.try_deserialize::<Output>();
let e = res.unwrap_err();
assert_data_eq!(
e.to_string(),
str![[
r#"invalid type: string "Torre di Pisa", expected an integer for key `place.name` in tests/testsuite/deserialize-invalid-type.json"#
]]
);
if let ConfigError::Type {
key: Some(path), ..
} = e
{
assert_eq!(path, "place.name");
} else {
panic!("Wrong error {:?}", e);
}
}

#[test]
#[cfg(feature = "json")]
fn test_deserialize_missing_field() {
Expand Down Expand Up @@ -350,3 +439,35 @@ fn test_deserialize_missing_field() {
str!["missing field `value2` for key `inner`"]
);
}

#[test]
#[cfg(feature = "json")]
fn test_deserialize_missing_field_file() {
#[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::new(
"tests/testsuite/deserialize-missing-field.json",
FileFormat::Json,
))
.build()
.unwrap();

let res = c.try_deserialize::<Settings>();
assert_data_eq!(
res.unwrap_err().to_string(),
str!["missing field `value2` for key `inner`"]
);
}
3 changes: 3 additions & 0 deletions tests/testsuite/get-invalid-type.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"boolean_s_parse": "fals"
}
3 changes: 3 additions & 0 deletions tests/testsuite/get-missing-field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"inner": { "value": 42 }
}

0 comments on commit 43dc246

Please sign in to comment.