From 3055d4b53ad87fd026a74698c364648e1193f3eb Mon Sep 17 00:00:00 2001 From: jprochazk <1665677+jprochazk@users.noreply.github.com> Date: Sat, 27 Jan 2024 12:28:50 +0100 Subject: [PATCH] remove empty colon from display impl of report --- garde/src/error.rs | 6 ++++- garde/tests/rules/newtype.rs | 24 +++++++++++++++++-- ...ules__newtype__newtype_fields_invalid.snap | 16 +++++++++++++ ...ules__newtype__newtype_struct_invalid.snap | 10 ++++++++ ...rules__newtype__newtype_tuple_invalid.snap | 10 ++++++++ garde/tests/rules/util.rs | 8 +++---- 6 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 garde/tests/rules/snapshots/rules__rules__newtype__newtype_fields_invalid.snap create mode 100644 garde/tests/rules/snapshots/rules__rules__newtype__newtype_struct_invalid.snap create mode 100644 garde/tests/rules/snapshots/rules__rules__newtype__newtype_tuple_invalid.snap diff --git a/garde/src/error.rs b/garde/src/error.rs index e775565..001703d 100644 --- a/garde/src/error.rs +++ b/garde/src/error.rs @@ -50,7 +50,11 @@ impl Report { impl std::fmt::Display for Report { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { for (path, error) in self.iter() { - writeln!(f, "{path}: {error}")?; + if path.is_empty() { + writeln!(f, "{error}")?; + } else { + writeln!(f, "{path}: {error}")?; + } } Ok(()) } diff --git a/garde/tests/rules/newtype.rs b/garde/tests/rules/newtype.rs index c2a23a2..5304a91 100644 --- a/garde/tests/rules/newtype.rs +++ b/garde/tests/rules/newtype.rs @@ -9,10 +9,30 @@ struct NonEmptyStr_Struct<'a> { v: &'a str, } +#[test] +fn newtype_struct_valid() { + util::check_ok(&[NonEmptyStr_Struct { v: "test" }], &()); +} + +#[test] +fn newtype_struct_invalid() { + util::check_fail!(&[NonEmptyStr_Struct { v: "" }], &()); +} + #[derive(Debug, garde::Validate)] #[garde(transparent)] struct NonEmptyStr_Tuple<'a>(#[garde(length(min = 1))] &'a str); +#[test] +fn newtype_tuple_valid() { + util::check_ok(&[NonEmptyStr_Tuple("test")], &()); +} + +#[test] +fn newtype_tuple_invalid() { + util::check_fail!(&[NonEmptyStr_Tuple("")], &()); +} + #[derive(Debug, garde::Validate)] struct Test<'a> { @@ -23,7 +43,7 @@ struct Test<'a> { } #[test] -fn newtype_valid() { +fn newtype_fields_valid() { util::check_ok( &[Test { a: NonEmptyStr_Struct { v: "test" }, @@ -34,7 +54,7 @@ fn newtype_valid() { } #[test] -fn newtype_invalid() { +fn newtype_fields_invalid() { util::check_fail!( &[Test { a: NonEmptyStr_Struct { v: "" }, diff --git a/garde/tests/rules/snapshots/rules__rules__newtype__newtype_fields_invalid.snap b/garde/tests/rules/snapshots/rules__rules__newtype__newtype_fields_invalid.snap new file mode 100644 index 0000000..e00267b --- /dev/null +++ b/garde/tests/rules/snapshots/rules__rules__newtype__newtype_fields_invalid.snap @@ -0,0 +1,16 @@ +--- +source: garde/tests/./rules/newtype.rs +expression: snapshot +--- +Test { + a: NonEmptyStr_Struct { + v: "", + }, + b: NonEmptyStr_Tuple( + "", + ), +} +a: length is lower than 1 +b: length is lower than 1 + + diff --git a/garde/tests/rules/snapshots/rules__rules__newtype__newtype_struct_invalid.snap b/garde/tests/rules/snapshots/rules__rules__newtype__newtype_struct_invalid.snap new file mode 100644 index 0000000..58abbec --- /dev/null +++ b/garde/tests/rules/snapshots/rules__rules__newtype__newtype_struct_invalid.snap @@ -0,0 +1,10 @@ +--- +source: garde/tests/./rules/newtype.rs +expression: snapshot +--- +NonEmptyStr_Struct { + v: "", +} +length is lower than 1 + + diff --git a/garde/tests/rules/snapshots/rules__rules__newtype__newtype_tuple_invalid.snap b/garde/tests/rules/snapshots/rules__rules__newtype__newtype_tuple_invalid.snap new file mode 100644 index 0000000..b437d3f --- /dev/null +++ b/garde/tests/rules/snapshots/rules__rules__newtype__newtype_tuple_invalid.snap @@ -0,0 +1,10 @@ +--- +source: garde/tests/./rules/newtype.rs +expression: snapshot +--- +NonEmptyStr_Tuple( + "", +) +length is lower than 1 + + diff --git a/garde/tests/rules/util.rs b/garde/tests/rules/util.rs index 7095d4e..b643b22 100644 --- a/garde/tests/rules/util.rs +++ b/garde/tests/rules/util.rs @@ -13,8 +13,8 @@ pub fn check_ok(cases: &[T], ctx: &T::Context) { "{} input: {case:?}, errors: [{}]", "FAIL".red(), report - .iter() - .map(|(path, error)| format!("{path}: {error}")) + .to_string() + .split('\n') .collect::>() .join("; ") ); @@ -34,9 +34,7 @@ pub fn __check_fail(cases: &[T], ctx: &T::Context) -> Strin for case in cases { if let Err(report) = case.validate(ctx) { writeln!(&mut snapshot, "{case:#?}").unwrap(); - for (path, error) in report.iter() { - writeln!(&mut snapshot, "{path}: {error}").unwrap(); - } + write!(&mut snapshot, "{report}").unwrap(); writeln!(&mut snapshot).unwrap(); } else { eprintln!("{} input: {case:?}", "SUCCESS".red());