Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove empty colon from display impl of Report #91

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion garde/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
24 changes: 22 additions & 2 deletions garde/tests/rules/newtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -23,7 +43,7 @@ struct Test<'a> {
}

#[test]
fn newtype_valid() {
fn newtype_fields_valid() {
util::check_ok(
&[Test {
a: NonEmptyStr_Struct { v: "test" },
Expand All @@ -34,7 +54,7 @@ fn newtype_valid() {
}

#[test]
fn newtype_invalid() {
fn newtype_fields_invalid() {
util::check_fail!(
&[Test {
a: NonEmptyStr_Struct { v: "" },
Expand Down
Original file line number Diff line number Diff line change
@@ -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


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: garde/tests/./rules/newtype.rs
expression: snapshot
---
NonEmptyStr_Struct {
v: "",
}
length is lower than 1


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: garde/tests/./rules/newtype.rs
expression: snapshot
---
NonEmptyStr_Tuple(
"",
)
length is lower than 1


8 changes: 3 additions & 5 deletions garde/tests/rules/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub fn check_ok<T: Validate + Debug>(cases: &[T], ctx: &T::Context) {
"{} input: {case:?}, errors: [{}]",
"FAIL".red(),
report
.iter()
.map(|(path, error)| format!("{path}: {error}"))
.to_string()
.split('\n')
.collect::<Vec<_>>()
.join("; ")
);
Expand All @@ -34,9 +34,7 @@ pub fn __check_fail<T: Validate + Debug>(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());
Expand Down
Loading