Skip to content

Commit bf42f56

Browse files
committed
test(errors): Snapshot rendered errors
1 parent 726c34f commit bf42f56

File tree

11 files changed

+182
-47
lines changed

11 files changed

+182
-47
lines changed

Cargo.lock

Lines changed: 111 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ glob = "0.3"
148148
notify = "7.0"
149149
temp-env = "0.3"
150150
log = { version = "0.4", features = ["serde"] }
151+
snapbox = "0.6.21"
151152

152153
[[example]]
153154
name = "async_source"

tests/testsuite/errors.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use config::{Config, ConfigError, File, FileFormat, Map, Value};
21
use serde_derive::Deserialize;
2+
use snapbox::{assert_data_eq, str};
3+
4+
use config::{Config, ConfigError, File, FileFormat, Map, Value};
35

46
#[test]
57
#[cfg(feature = "json")]
@@ -16,8 +18,10 @@ fn test_error_parse() {
1618
.build();
1719

1820
assert!(res.is_err());
19-
let err = res.unwrap_err();
20-
assert_eq!(err.to_string(), "trailing comma at line 4 column 1");
21+
assert_data_eq!(
22+
res.unwrap_err().to_string(),
23+
str!["trailing comma at line 4 column 1"]
24+
);
2125
}
2226

2327
#[test]
@@ -38,9 +42,9 @@ fn test_error_type() {
3842
let res = c.get::<bool>("boolean_s_parse");
3943

4044
assert!(res.is_err());
41-
assert_eq!(
45+
assert_data_eq!(
4246
res.unwrap_err().to_string(),
43-
format!("invalid type: string \"fals\", expected a boolean for key `boolean_s_parse`",)
47+
str![[r#"invalid type: string "fals", expected a boolean for key `boolean_s_parse`"#]]
4448
);
4549
}
4650

@@ -73,10 +77,10 @@ fn test_error_deser_whole() {
7377
.build()
7478
.unwrap();
7579

76-
let err = c.try_deserialize::<Output>().unwrap_err().to_string();
77-
assert_eq!(
78-
err,
79-
"invalid type: string \"Torre di Pisa\", expected an integer for key `place.name`",
80+
let res = c.try_deserialize::<Output>();
81+
assert_data_eq!(
82+
res.unwrap_err().to_string(),
83+
str![[r#"invalid type: string "Torre di Pisa", expected an integer for key `place.name`"#]]
8084
);
8185
}
8286

@@ -99,9 +103,9 @@ fn test_error_type_detached() {
99103
let res = value.try_deserialize::<bool>();
100104

101105
assert!(res.is_err());
102-
assert_eq!(
106+
assert_data_eq!(
103107
res.unwrap_err().to_string(),
104-
"invalid type: string \"fals\", expected a boolean".to_owned()
108+
str![[r#"invalid type: string "fals", expected a boolean"#]]
105109
);
106110
}
107111

@@ -123,9 +127,9 @@ fn test_error_type_get_bool() {
123127
let res = c.get_bool("boolean_s_parse");
124128

125129
assert!(res.is_err());
126-
assert_eq!(
130+
assert_data_eq!(
127131
res.unwrap_err().to_string(),
128-
format!("invalid type: string \"fals\", expected a boolean for key `boolean_s_parse`",)
132+
str![[r#"invalid type: string "fals", expected a boolean for key `boolean_s_parse`"#]]
129133
);
130134
}
131135

@@ -147,9 +151,9 @@ fn test_error_type_get_table() {
147151
let res = c.get_table("debug");
148152

149153
assert!(res.is_err());
150-
assert_eq!(
154+
assert_data_eq!(
151155
res.unwrap_err().to_string(),
152-
format!("invalid type: boolean `true`, expected a map for key `debug`",)
156+
str!["invalid type: boolean `true`, expected a map for key `debug`"]
153157
);
154158
}
155159

@@ -171,9 +175,9 @@ fn test_error_type_get_array() {
171175
let res = c.get_array("debug");
172176

173177
assert!(res.is_err());
174-
assert_eq!(
178+
assert_data_eq!(
175179
res.unwrap_err().to_string(),
176-
format!("invalid type: boolean `true`, expected an array for key `debug`",)
180+
str!["invalid type: boolean `true`, expected an array for key `debug`"]
177181
);
178182
}
179183

@@ -189,17 +193,14 @@ fn test_error_enum_de() {
189193

190194
let on_v: Value = "on".into();
191195
let on_d = on_v.try_deserialize::<Diode>();
192-
assert_eq!(
196+
assert_data_eq!(
193197
on_d.unwrap_err().to_string(),
194-
"enum Diode does not have variant constructor on".to_owned()
198+
str!["enum Diode does not have variant constructor on"]
195199
);
196200

197201
let array_v: Value = vec![100, 100].into();
198202
let array_d = array_v.try_deserialize::<Diode>();
199-
assert_eq!(
200-
array_d.unwrap_err().to_string(),
201-
"value of enum Diode should be represented by either string or table with exactly one key"
202-
);
203+
assert_data_eq!(array_d.unwrap_err().to_string(), str!["value of enum Diode should be represented by either string or table with exactly one key"]);
203204

204205
let confused_v: Value = [
205206
("Brightness".to_owned(), 100.into()),
@@ -210,10 +211,7 @@ fn test_error_enum_de() {
210211
.collect::<Map<String, Value>>()
211212
.into();
212213
let confused_d = confused_v.try_deserialize::<Diode>();
213-
assert_eq!(
214-
confused_d.unwrap_err().to_string(),
215-
"value of enum Diode should be represented by either string or table with exactly one key"
216-
);
214+
assert_data_eq!(confused_d.unwrap_err().to_string(), str!["value of enum Diode should be represented by either string or table with exactly one key"]);
217215
}
218216

219217
#[test]

tests/testsuite/file.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use snapbox::{assert_data_eq, str};
2+
13
use config::{Config, File, FileFormat};
24

35
#[test]
@@ -21,9 +23,9 @@ fn test_file_required_not_found() {
2123
.build();
2224

2325
assert!(res.is_err());
24-
assert_eq!(
26+
assert_data_eq!(
2527
res.unwrap_err().to_string(),
26-
"configuration file \"tests/testsuite/file-nonexistent\" not found".to_owned()
28+
str![[r#"configuration file "tests/testsuite/file-nonexistent" not found"#]]
2729
);
2830
}
2931

@@ -47,9 +49,9 @@ fn test_file_auto_not_found() {
4749
.build();
4850

4951
assert!(res.is_err());
50-
assert_eq!(
52+
assert_data_eq!(
5153
res.unwrap_err().to_string(),
52-
"configuration file \"tests/testsuite/file-nonexistent\" not found".to_owned()
54+
str![[r#"configuration file "tests/testsuite/file-nonexistent" not found"#]]
5355
);
5456
}
5557

tests/testsuite/file_ini.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use chrono::{DateTime, TimeZone, Utc};
44
use serde_derive::Deserialize;
5+
use snapbox::{assert_data_eq, str};
56

67
use config::{Config, File, FileFormat};
78

@@ -72,9 +73,9 @@ error
7273
.build();
7374

7475
assert!(res.is_err());
75-
assert_eq!(
76+
assert_data_eq!(
7677
res.unwrap_err().to_string(),
77-
format!(r#"4:1 expecting "[Some('='), Some(':')]" but found EOF."#,)
78+
str![[r#"4:1 expecting "[Some('='), Some(':')]" but found EOF."#]]
7879
);
7980
}
8081

tests/testsuite/file_json.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use chrono::{DateTime, TimeZone, Utc};
44
use float_cmp::ApproxEqUlps;
55
use serde_derive::Deserialize;
6+
use snapbox::{assert_data_eq, str};
67

78
use config::{Config, File, FileFormat, Map, Value};
89

@@ -108,9 +109,9 @@ fn test_error_parse() {
108109
.build();
109110

110111
assert!(res.is_err());
111-
assert_eq!(
112+
assert_data_eq!(
112113
res.unwrap_err().to_string(),
113-
format!("expected `:` at line 5 column 1",)
114+
str!["expected `:` at line 5 column 1"]
114115
);
115116
}
116117

0 commit comments

Comments
 (0)