Skip to content

Commit

Permalink
Create new assertion method for value of Some(value)` (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
cocuh authored Jul 20, 2024
1 parent 19f30e8 commit 89780ee
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/assertions/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use std::borrow::Borrow;
use std::fmt::Debug;

use crate::base::AssertionApi;
use crate::{AssertionResult, AssertionStrategy, Subject};
use crate::base::AssertionApi;

/// Trait for option assertion.
///
Expand All @@ -27,8 +27,9 @@ use crate::{AssertionResult, AssertionStrategy, Subject};
/// assert_that!(Option::Some(1)).has_value(1);
/// assert_that!(Option::Some(1)).is_some();
/// assert_that!(Option::<usize>::None).is_none();
/// assert_that!(Option::Some("foobar")).some().starts_with("foo");
/// ```
pub trait OptionAssertion<'a, T, R>
pub trait OptionAssertion<T, R>
where
AssertionResult: AssertionStrategy<R>,
{
Expand All @@ -47,9 +48,23 @@ where
where
B: Borrow<T>,
T: PartialEq + Debug;

/// Returns a new subject which is the value of the subject if the subject is [`Option::Some(_)`](`Option::Some`). Otherwise, it fails.
///
/// # Example
/// ```
/// use assertor::*;
///
/// let value = Option::Some("foobar");
/// assert_that!(value).some().starts_with("foo");
/// assert_that!(value).some().ends_with("bar");
/// ```
fn some(&self) -> Subject<T, (), R>
where
T: PartialEq + Debug;
}

impl<'a, T, R> OptionAssertion<'a, T, R> for Subject<'a, Option<T>, (), R>
impl<T, R> OptionAssertion<T, R> for Subject<'_, Option<T>, (), R>
where
AssertionResult: AssertionStrategy<R>,
{
Expand Down Expand Up @@ -100,6 +115,14 @@ where
.do_fail(),
}
}

fn some(&self) -> Subject<T, (), R>
where
T: PartialEq + Debug,
{
let value = self.actual().as_ref().expect("Expected Some(_) but was None");
self.new_subject(value, None, ())
}
}

#[cfg(test)]
Expand Down

0 comments on commit 89780ee

Please sign in to comment.