Skip to content

Commit da066d2

Browse files
committed
test(error): Show lack of origin
1 parent fd55144 commit da066d2

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"place": {
3+
"name": "Torre di Pisa"
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"inner": { "value": 42 }
3+
}

tests/testsuite/errors.rs

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

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+
113135
#[test]
114136
#[cfg(feature = "json")]
115137
fn test_get_missing_field() {
@@ -140,6 +162,32 @@ fn test_get_missing_field() {
140162
);
141163
}
142164

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+
143191
#[test]
144192
#[cfg(feature = "json")]
145193
fn test_get_bool_invalid_type() {
@@ -315,6 +363,47 @@ fn test_deserialize_invalid_type() {
315363
}
316364
}
317365

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+
318407
#[test]
319408
#[cfg(feature = "json")]
320409
fn test_deserialize_missing_field() {
@@ -350,3 +439,35 @@ fn test_deserialize_missing_field() {
350439
str!["missing field `value2` for key `inner`"]
351440
);
352441
}
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+
}

tests/testsuite/get-invalid-type.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"boolean_s_parse": "fals"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"inner": { "value": 42 }
3+
}

0 commit comments

Comments
 (0)