Skip to content

Commit

Permalink
test(errors): Snapshot rendered errors
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 17, 2024
1 parent 596e08c commit 39fbe1a
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 47 deletions.
111 changes: 111 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ glob = "0.3"
notify = "7.0"
temp-env = "0.3"
log = { version = "0.4", features = ["serde"] }
snapbox = "0.6.21"

[[example]]
name = "async_source"
Expand Down
52 changes: 25 additions & 27 deletions tests/testsuite/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use config::{Config, ConfigError, File, FileFormat, Map, Value};
use serde_derive::Deserialize;
use snapbox::{assert_data_eq, str};

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

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

assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.to_string(), "trailing comma at line 4 column 1");
assert_data_eq!(
res.unwrap_err().to_string(),
str!["trailing comma at line 4 column 1"]
);
}

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

assert!(res.is_err());
assert_eq!(
assert_data_eq!(
res.unwrap_err().to_string(),
format!("invalid type: string \"fals\", expected a boolean for key `boolean_s_parse`",)
str![[r#"invalid type: string "fals", expected a boolean for key `boolean_s_parse`"#]]
);
}

Expand Down Expand Up @@ -73,10 +77,10 @@ fn test_error_deser_whole() {
.build()
.unwrap();

let err = c.try_deserialize::<Output>().unwrap_err().to_string();
assert_eq!(
err,
"invalid type: string \"Torre di Pisa\", expected an integer for key `place.name`",
let res = c.try_deserialize::<Output>();
assert_data_eq!(
res.unwrap_err().to_string(),
str![[r#"invalid type: string "Torre di Pisa", expected an integer for key `place.name`"#]]
);
}

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

assert!(res.is_err());
assert_eq!(
assert_data_eq!(
res.unwrap_err().to_string(),
"invalid type: string \"fals\", expected a boolean".to_owned()
str![[r#"invalid type: string "fals", expected a boolean"#]]
);
}

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

assert!(res.is_err());
assert_eq!(
assert_data_eq!(
res.unwrap_err().to_string(),
format!("invalid type: string \"fals\", expected a boolean for key `boolean_s_parse`",)
str![[r#"invalid type: string "fals", expected a boolean for key `boolean_s_parse`"#]]
);
}

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

assert!(res.is_err());
assert_eq!(
assert_data_eq!(
res.unwrap_err().to_string(),
format!("invalid type: boolean `true`, expected a map for key `debug`",)
str!["invalid type: boolean `true`, expected a map for key `debug`"]
);
}

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

assert!(res.is_err());
assert_eq!(
assert_data_eq!(
res.unwrap_err().to_string(),
format!("invalid type: boolean `true`, expected an array for key `debug`",)
str!["invalid type: boolean `true`, expected an array for key `debug`"]
);
}

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

let on_v: Value = "on".into();
let on_d = on_v.try_deserialize::<Diode>();
assert_eq!(
assert_data_eq!(
on_d.unwrap_err().to_string(),
"enum Diode does not have variant constructor on".to_owned()
str!["enum Diode does not have variant constructor on"]
);

let array_v: Value = vec![100, 100].into();
let array_d = array_v.try_deserialize::<Diode>();
assert_eq!(
array_d.unwrap_err().to_string(),
"value of enum Diode should be represented by either string or table with exactly one key"
);
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"]);

let confused_v: Value = [
("Brightness".to_owned(), 100.into()),
Expand All @@ -210,10 +211,7 @@ fn test_error_enum_de() {
.collect::<Map<String, Value>>()
.into();
let confused_d = confused_v.try_deserialize::<Diode>();
assert_eq!(
confused_d.unwrap_err().to_string(),
"value of enum Diode should be represented by either string or table with exactly one key"
);
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"]);
}

#[test]
Expand Down
10 changes: 6 additions & 4 deletions tests/testsuite/file.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use snapbox::{assert_data_eq, str};

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

#[test]
Expand All @@ -21,9 +23,9 @@ fn test_file_required_not_found() {
.build();

assert!(res.is_err());
assert_eq!(
assert_data_eq!(
res.unwrap_err().to_string(),
"configuration file \"tests/testsuite/file-nonexistent\" not found".to_owned()
str![[r#"configuration file "tests/testsuite/file-nonexistent" not found"#]]
);
}

Expand All @@ -47,9 +49,9 @@ fn test_file_auto_not_found() {
.build();

assert!(res.is_err());
assert_eq!(
assert_data_eq!(
res.unwrap_err().to_string(),
"configuration file \"tests/testsuite/file-nonexistent\" not found".to_owned()
str![[r#"configuration file "tests/testsuite/file-nonexistent" not found"#]]
);
}

Expand Down
5 changes: 3 additions & 2 deletions tests/testsuite/file_ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use chrono::{DateTime, TimeZone, Utc};
use serde_derive::Deserialize;
use snapbox::{assert_data_eq, str};

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

Expand Down Expand Up @@ -72,9 +73,9 @@ error
.build();

assert!(res.is_err());
assert_eq!(
assert_data_eq!(
res.unwrap_err().to_string(),
format!(r#"4:1 expecting "[Some('='), Some(':')]" but found EOF."#,)
str![[r#"4:1 expecting "[Some('='), Some(':')]" but found EOF."#]]
);
}

Expand Down
5 changes: 3 additions & 2 deletions tests/testsuite/file_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use chrono::{DateTime, TimeZone, Utc};
use float_cmp::ApproxEqUlps;
use serde_derive::Deserialize;
use snapbox::{assert_data_eq, str};

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

Expand Down Expand Up @@ -108,9 +109,9 @@ fn test_error_parse() {
.build();

assert!(res.is_err());
assert_eq!(
assert_data_eq!(
res.unwrap_err().to_string(),
format!("expected `:` at line 5 column 1",)
str!["expected `:` at line 5 column 1"]
);
}

Expand Down
Loading

0 comments on commit 39fbe1a

Please sign in to comment.