Skip to content

Commit f239510

Browse files
committed
test(error): Organize tests
1 parent cc1ba8b commit f239510

File tree

1 file changed

+60
-92
lines changed

1 file changed

+60
-92
lines changed

tests/testsuite/errors.rs

Lines changed: 60 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use config::{Config, ConfigError, File, FileFormat, Map, Value};
55

66
#[test]
77
#[cfg(feature = "json")]
8-
fn test_error_path_index_bounds() {
8+
fn test_path_index_bounds() {
99
let c = Config::builder()
1010
.add_source(File::from_str(
1111
r#"
@@ -28,7 +28,7 @@ fn test_error_path_index_bounds() {
2828

2929
#[test]
3030
#[cfg(feature = "json")]
31-
fn test_error_path_index_negative_bounds() {
31+
fn test_path_index_negative_bounds() {
3232
let c = Config::builder()
3333
.add_source(File::from_str(
3434
r#"
@@ -51,7 +51,7 @@ fn test_error_path_index_negative_bounds() {
5151

5252
#[test]
5353
#[cfg(feature = "json")]
54-
fn test_error_parse() {
54+
fn test_parse() {
5555
let res = Config::builder()
5656
.add_source(File::from_str(
5757
r#"
@@ -72,67 +72,47 @@ fn test_error_parse() {
7272

7373
#[test]
7474
#[cfg(feature = "json")]
75-
fn test_error_type() {
76-
let c = Config::builder()
77-
.add_source(File::from_str(
78-
r#"
79-
{
80-
"boolean_s_parse": "fals"
81-
}
82-
"#,
83-
FileFormat::Json,
84-
))
75+
fn test_root_not_table() {
76+
let e = Config::builder()
77+
.add_source(File::from_str(r#"false"#, FileFormat::Json))
8578
.build()
86-
.unwrap();
87-
88-
let res = c.get::<bool>("boolean_s_parse");
89-
90-
assert!(res.is_err());
91-
assert_data_eq!(
92-
res.unwrap_err().to_string(),
93-
str![[r#"invalid type: string "fals", expected a boolean for key `boolean_s_parse`"#]]
94-
);
79+
.unwrap_err();
80+
match e {
81+
ConfigError::FileParse { cause, .. } => assert_eq!(
82+
"invalid type: boolean `false`, expected a map",
83+
format!("{cause}")
84+
),
85+
_ => panic!("Wrong error: {:?}", e),
86+
}
9587
}
9688

9789
#[test]
9890
#[cfg(feature = "json")]
99-
fn test_error_deser_whole() {
100-
#[derive(Deserialize, Debug)]
101-
struct Place {
102-
#[allow(dead_code)]
103-
name: usize, // is actually s string
104-
}
105-
106-
#[derive(Deserialize, Debug)]
107-
struct Output {
108-
#[allow(dead_code)]
109-
place: Place,
110-
}
111-
91+
fn test_get_invalid_type() {
11292
let c = Config::builder()
11393
.add_source(File::from_str(
11494
r#"
11595
{
116-
"place": {
117-
"name": "Torre di Pisa"
118-
}
96+
"boolean_s_parse": "fals"
11997
}
12098
"#,
12199
FileFormat::Json,
122100
))
123101
.build()
124102
.unwrap();
125103

126-
let res = c.try_deserialize::<Output>();
104+
let res = c.get::<bool>("boolean_s_parse");
105+
106+
assert!(res.is_err());
127107
assert_data_eq!(
128108
res.unwrap_err().to_string(),
129-
str![[r#"invalid type: string "Torre di Pisa", expected an integer for key `place.name`"#]]
109+
str![[r#"invalid type: string "fals", expected a boolean for key `boolean_s_parse`"#]]
130110
);
131111
}
132112

133113
#[test]
134114
#[cfg(feature = "json")]
135-
fn test_error_type_detached() {
115+
fn test_get_bool_invalid_type() {
136116
let c = Config::builder()
137117
.add_source(File::from_str(
138118
r#"
@@ -145,43 +125,42 @@ fn test_error_type_detached() {
145125
.build()
146126
.unwrap();
147127

148-
let value = c.get::<Value>("boolean_s_parse").unwrap();
149-
let res = value.try_deserialize::<bool>();
128+
let res = c.get_bool("boolean_s_parse");
150129

151130
assert!(res.is_err());
152131
assert_data_eq!(
153132
res.unwrap_err().to_string(),
154-
str![[r#"invalid type: string "fals", expected a boolean"#]]
133+
str![[r#"invalid type: string "fals", expected a boolean for key `boolean_s_parse`"#]]
155134
);
156135
}
157136

158137
#[test]
159138
#[cfg(feature = "json")]
160-
fn test_error_type_get_bool() {
139+
fn test_get_table_invalid_type() {
161140
let c = Config::builder()
162141
.add_source(File::from_str(
163142
r#"
164143
{
165-
"boolean_s_parse": "fals"
144+
"debug": true
166145
}
167146
"#,
168147
FileFormat::Json,
169148
))
170149
.build()
171150
.unwrap();
172151

173-
let res = c.get_bool("boolean_s_parse");
152+
let res = c.get_table("debug");
174153

175154
assert!(res.is_err());
176155
assert_data_eq!(
177156
res.unwrap_err().to_string(),
178-
str![[r#"invalid type: string "fals", expected a boolean for key `boolean_s_parse`"#]]
157+
str!["invalid type: boolean `true`, expected a map for key `debug`"]
179158
);
180159
}
181160

182161
#[test]
183162
#[cfg(feature = "json")]
184-
fn test_error_type_get_table() {
163+
fn test_get_array_invalid_type() {
185164
let c = Config::builder()
186165
.add_source(File::from_str(
187166
r#"
@@ -194,41 +173,42 @@ fn test_error_type_get_table() {
194173
.build()
195174
.unwrap();
196175

197-
let res = c.get_table("debug");
176+
let res = c.get_array("debug");
198177

199178
assert!(res.is_err());
200179
assert_data_eq!(
201180
res.unwrap_err().to_string(),
202-
str!["invalid type: boolean `true`, expected a map for key `debug`"]
181+
str!["invalid type: boolean `true`, expected an array for key `debug`"]
203182
);
204183
}
205184

206185
#[test]
207186
#[cfg(feature = "json")]
208-
fn test_error_type_get_array() {
187+
fn test_value_deserialize_invalid_type() {
209188
let c = Config::builder()
210189
.add_source(File::from_str(
211190
r#"
212191
{
213-
"debug": true
192+
"boolean_s_parse": "fals"
214193
}
215194
"#,
216195
FileFormat::Json,
217196
))
218197
.build()
219198
.unwrap();
220199

221-
let res = c.get_array("debug");
200+
let value = c.get::<Value>("boolean_s_parse").unwrap();
201+
let res = value.try_deserialize::<bool>();
222202

223203
assert!(res.is_err());
224204
assert_data_eq!(
225205
res.unwrap_err().to_string(),
226-
str!["invalid type: boolean `true`, expected an array for key `debug`"]
206+
str![[r#"invalid type: string "fals", expected a boolean"#]]
227207
);
228208
}
229209

230210
#[test]
231-
fn test_error_enum_de() {
211+
fn test_value_deserialize_enum() {
232212
#[derive(Debug, Deserialize, PartialEq, Eq)]
233213
enum Diode {
234214
Off,
@@ -262,57 +242,45 @@ fn test_error_enum_de() {
262242

263243
#[test]
264244
#[cfg(feature = "json")]
265-
fn error_with_path() {
266-
#[derive(Debug, Deserialize)]
267-
struct Inner {
245+
fn test_deserialize_invalid_type() {
246+
#[derive(Deserialize, Debug)]
247+
struct Place {
268248
#[allow(dead_code)]
269-
test: i32,
249+
name: usize, // is actually s string
270250
}
271251

272-
#[derive(Debug, Deserialize)]
273-
struct Outer {
252+
#[derive(Deserialize, Debug)]
253+
struct Output {
274254
#[allow(dead_code)]
275-
inner: Inner,
255+
place: Place,
276256
}
277-
const CFG: &str = r#"
257+
258+
let c = Config::builder()
259+
.add_source(File::from_str(
260+
r#"
278261
{
279-
"inner": {
280-
"test": "ABC"
262+
"place": {
263+
"name": "Torre di Pisa"
281264
}
282265
}
283-
"#;
284-
285-
let e = Config::builder()
286-
.add_source(File::from_str(CFG, FileFormat::Json))
266+
"#,
267+
FileFormat::Json,
268+
))
287269
.build()
288-
.unwrap()
289-
.try_deserialize::<Outer>()
290-
.unwrap_err();
270+
.unwrap();
291271

272+
let res = c.try_deserialize::<Output>();
273+
let e = res.unwrap_err();
274+
assert_data_eq!(
275+
e.to_string(),
276+
str![[r#"invalid type: string "Torre di Pisa", expected an integer for key `place.name`"#]]
277+
);
292278
if let ConfigError::Type {
293279
key: Some(path), ..
294280
} = e
295281
{
296-
assert_eq!(path, "inner.test");
282+
assert_eq!(path, "place.name");
297283
} else {
298284
panic!("Wrong error {:?}", e);
299285
}
300286
}
301-
302-
#[test]
303-
#[cfg(feature = "json")]
304-
fn test_error_root_not_table() {
305-
match Config::builder()
306-
.add_source(File::from_str(r#"false"#, FileFormat::Json))
307-
.build()
308-
{
309-
Ok(_) => panic!("Should not merge if root is not a table"),
310-
Err(e) => match e {
311-
ConfigError::FileParse { cause, .. } => assert_eq!(
312-
"invalid type: boolean `false`, expected a map",
313-
format!("{cause}")
314-
),
315-
_ => panic!("Wrong error: {:?}", e),
316-
},
317-
}
318-
}

0 commit comments

Comments
 (0)