Skip to content

Commit 5852262

Browse files
allevoepage
authored andcommitted
test: add test to show the error is without path info
1 parent f239510 commit 5852262

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

tests/testsuite/errors.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,33 @@ fn test_get_invalid_type() {
110110
);
111111
}
112112

113+
#[test]
114+
#[cfg(feature = "json")]
115+
fn test_get_missing_field() {
116+
#[derive(Debug, Deserialize)]
117+
struct InnerSettings {
118+
#[allow(dead_code)]
119+
value: u32,
120+
#[allow(dead_code)]
121+
value2: u32,
122+
}
123+
124+
let c = Config::builder()
125+
.add_source(File::from_str(
126+
r#"
127+
{
128+
"inner": { "value": 42 }
129+
}
130+
"#,
131+
FileFormat::Json,
132+
))
133+
.build()
134+
.unwrap();
135+
136+
let res = c.get::<InnerSettings>("inner");
137+
assert_data_eq!(res.unwrap_err().to_string(), str!["missing field `value2`"]);
138+
}
139+
113140
#[test]
114141
#[cfg(feature = "json")]
115142
fn test_get_bool_invalid_type() {
@@ -284,3 +311,36 @@ fn test_deserialize_invalid_type() {
284311
panic!("Wrong error {:?}", e);
285312
}
286313
}
314+
315+
#[test]
316+
#[cfg(feature = "json")]
317+
fn test_deserialize_missing_field() {
318+
#[derive(Debug, Deserialize)]
319+
struct Settings {
320+
#[allow(dead_code)]
321+
inner: InnerSettings,
322+
}
323+
324+
#[derive(Debug, Deserialize)]
325+
struct InnerSettings {
326+
#[allow(dead_code)]
327+
value: u32,
328+
#[allow(dead_code)]
329+
value2: u32,
330+
}
331+
332+
let c = Config::builder()
333+
.add_source(File::from_str(
334+
r#"
335+
{
336+
"inner": { "value": 42 }
337+
}
338+
"#,
339+
FileFormat::Json,
340+
))
341+
.build()
342+
.unwrap();
343+
344+
let res = c.try_deserialize::<Settings>();
345+
assert_data_eq!(res.unwrap_err().to_string(), str!["missing field `value2`"]);
346+
}

0 commit comments

Comments
 (0)