Skip to content

Commit

Permalink
tests: inner option support
Browse files Browse the repository at this point in the history
  • Loading branch information
bonofiglio committed Sep 21, 2023
1 parent dc2d6c4 commit 7498682
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
93 changes: 93 additions & 0 deletions garde/tests/rules/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,96 @@ fn alphanumeric_invalid() {
&()
)
}

#[derive(Debug, garde::Validate)]
struct NotNestedOption<'a> {
#[garde(inner(alphanumeric))]
inner: Option<&'a str>,
}

#[derive(Debug, garde::Validate)]
struct NestedSliceInsideOption<'a> {
#[garde(inner(inner(alphanumeric)))]
inner: Option<&'a [&'a str]>,
}

#[derive(Debug, garde::Validate)]
struct DoubleNestedSliceInsideOption<'a> {
#[garde(inner(inner(inner(alphanumeric))))]
inner: Option<&'a [&'a [&'a str]]>,
}

#[derive(Debug, garde::Validate)]
struct OptionInsideSlice<'a> {
#[garde(inner(inner(alphanumeric)))]
inner: &'a [Option<&'a str>],
}

#[test]
fn alphanumeric_some_valid() {
util::check_ok(
&[NotNestedOption {
inner: Some("abcd0123"),
}],
&(),
);
util::check_ok(
&[NestedSliceInsideOption {
inner: Some(&["abcd0123"]),
}],
&(),
);
util::check_ok(
&[DoubleNestedSliceInsideOption {
inner: Some(&[&["abcd0123"]]),
}],
&(),
);
util::check_ok(
&[OptionInsideSlice {
inner: &[Some("abcd0123")],
}],
&(),
)
}

#[test]
fn alphanumeric_some_invalid() {
util::check_fail!(
&[NotNestedOption {
inner: Some("!!!!"),
}],
&(),
);
util::check_fail!(
&[NestedSliceInsideOption {
inner: Some(&["!!!!"]),
}],
&(),
);
util::check_fail!(
&[DoubleNestedSliceInsideOption {
inner: Some(&[&["!!!!"]]),
}],
&(),
);
util::check_fail!(
&[OptionInsideSlice {
inner: &[Some("!!!!")],
}],
&(),
)
}

#[test]
fn alphanumeric_none_valid() {
util::check_ok(&[NotNestedOption { inner: None }], &());
util::check_ok(&[NestedSliceInsideOption { inner: None }], &());
util::check_ok(&[DoubleNestedSliceInsideOption { inner: None }], &());
util::check_ok(
&[OptionInsideSlice {
inner: &[None, None],
}],
&(),
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: garde/tests/./rules/inner.rs
assertion_line: 90
expression: snapshot
---
NestedSliceInsideOption {
inner: Some(
[
"!!!!",
],
),
}
inner[0]: not alphanumeric


Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: garde/tests/./rules/inner.rs
assertion_line: 96
expression: snapshot
---
DoubleNestedSliceInsideOption {
inner: Some(
[
[
"!!!!",
],
],
),
}
inner[0][0]: not alphanumeric


Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: garde/tests/./rules/inner.rs
assertion_line: 102
expression: snapshot
---
OptionInsideSlice {
inner: [
Some(
"!!!!",
),
],
}
inner[0]: not alphanumeric


Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: garde/tests/./rules/inner.rs
assertion_line: 84
expression: snapshot
---
NotNestedOption {
inner: Some(
"!!!!",
),
}
inner: not alphanumeric


0 comments on commit 7498682

Please sign in to comment.