Skip to content

Commit

Permalink
Sarthak | Adds documentation for SomePredicateAssertion, OkPredicateA…
Browse files Browse the repository at this point in the history
…ssertion and their matchers
  • Loading branch information
SarthakMakhija committed Jan 18, 2024
1 parent efc14a9 commit b76bff9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/assertions/option/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,30 @@ use crate::matchers::{Should, ShouldNot};
use crate::matchers::option::be_some;
use crate::matchers::option::predicate::satisfy;

/// SomePredicateAssertion enables assertions about whether the Option value is both Some and that the contained value meets or doesn't meet certain conditions defined by a predicate.
pub trait SomePredicateAssertion<T> {
/// - Asserts that the Option value is Some and satisfies the given predicate.
/// - Returns a reference to self for fluent chaining.
/// - Panics if the assertion fails.
/// # Example
/// ```
/// use clearcheck::assertions::option::predicate::SomePredicateAssertion;
///
/// let option = Some(100);
/// option.should_be_some_and_satisfy(|value| value > &&50);
/// ```
fn should_be_some_and_satisfy<F: Fn(&&T) -> bool>(&self, predicate: F) -> &Self;

/// - Asserts that the Option value is Some and does not satisfy the given predicate.
/// - Returns a reference to self for fluent chaining.
/// - Panics if the assertion fails.
/// # Example
/// ```
/// use clearcheck::assertions::option::predicate::SomePredicateAssertion;
///
/// let option = Some(100);
/// option.should_be_some_and_not_satisfy(|value| value > &&500);
/// ```
fn should_be_some_and_not_satisfy<F: Fn(&&T) -> bool>(&self, predicate: F) -> &Self;
}

Expand Down
21 changes: 21 additions & 0 deletions src/assertions/result/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,30 @@ use crate::matchers::{Should, ShouldNot};
use crate::matchers::result::predicate::satisfy;
use crate::matchers::result::be_ok;

/// OkPredicateAssertion enables assertions about whether the Result value is both Ok and that the contained value meets or doesn't meet certain conditions defined by a predicate.
pub trait OkPredicateAssertion<T> {
/// - Asserts that the Result value is Ok and satisfies the given predicate.
/// - Returns a reference to self for fluent chaining.
/// - Panics if the assertion fails.
/// # Example
/// ```
/// use clearcheck::assertions::result::predicate::OkPredicateAssertion;
///
/// let value: Result<i32, &str> = Ok(1000);
/// value.should_be_ok_and_satisfy(|value| value > &50);
/// ```
fn should_be_ok_and_satisfy<F: Fn(&T) -> bool>(&self, predicate: F) -> &Self;

/// - Asserts that the Result value is Ok and does not satisfy the given predicate.
/// - Returns a reference to self for fluent chaining.
/// - Panics if the assertion fails.
/// # Example
/// ```
/// use clearcheck::assertions::result::predicate::OkPredicateAssertion;
///
/// let value: Result<i32, &str> = Ok(100);
/// value.should_be_ok_and_not_satisfy(|value| value > &500);
/// ```
fn should_be_ok_and_not_satisfy<F: Fn(&T) -> bool>(&self, predicate: F) -> &Self;
}

Expand Down
11 changes: 11 additions & 0 deletions src/matchers/option/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ use std::marker::PhantomData;

use crate::matchers::{Matcher, MatcherResult};

/// SomePredicateMatcher offers a flexible way to assert whether the Option value is both Some and that the contained value meets certain conditions defined by the predicate.
///
/// # Example
///```
/// use clearcheck::matchers::Matcher;
/// use clearcheck::matchers::option::predicate::satisfy;
///
/// let matcher = satisfy(|value| value > &&400);
/// assert!(matcher.test(&Some(1000)).passed());
/// ```
pub struct SomePredicateMatcher<F, T>
where F: Fn(&&T) -> bool
{
Expand All @@ -21,6 +31,7 @@ impl<F, T> Matcher<Option<T>> for SomePredicateMatcher<F, T>
}
}

/// Creates a SomePredicateMatcher that asserts whether the Option value is both Some and that the contained value meets certain conditions defined by the predicate.
pub fn satisfy<F, T>(predicate: F) -> SomePredicateMatcher<F, T>
where F: Fn(&&T) -> bool
{
Expand Down
13 changes: 13 additions & 0 deletions src/matchers/result/predicate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
use std::marker::PhantomData;
use crate::matchers::{Matcher, MatcherResult};

/// OkPredicateMatcher offers a flexible way to assert whether the Result value is both Ok and that the contained value meets certain conditions defined by the predicate.
///
/// # Example
///```
/// use clearcheck::matchers::Matcher;
/// use clearcheck::matchers::result::predicate::satisfy;
///
/// let matcher = satisfy(|value| value > &400);
/// let value: Result<i32, &str> = Ok(1000);
///
/// assert!(matcher.test(&value).passed());
/// ```
pub struct OkPredicateMatcher<F, T>
where F: Fn(&T) -> bool
{
Expand All @@ -20,6 +32,7 @@ impl<F, T, E> Matcher<Result<T, E>> for OkPredicateMatcher<F, T>
}
}

/// Creates an OkPredicateMatcher that asserts whether the Result value is both Ok and that the contained value meets certain conditions defined by the predicate.
pub fn satisfy<F, T>(predicate: F) -> OkPredicateMatcher<F, T>
where F: Fn(&T) -> bool
{
Expand Down

0 comments on commit b76bff9

Please sign in to comment.