(self, predicate: P) -> Self + where + P: Fn(&S) -> bool, + { + self.expecting(satisfies(predicate)) + } + + fn satisfies_with_message
(self, message: impl Into (self, predicate: P) -> Self
+ fn each_element(mut self, assert: A) -> Self::Output
where
- P: Fn(&S) -> bool,
- {
- self.expecting(satisfies(predicate))
- }
-
- /// Asserts whether the given predicate is meet.
- ///
- /// This method takes a predicate function and calls it as an expectation.
- /// In case the predicate function returns false, it does fail with the
- /// provided failure message and according to the current failing strategy
- /// of this `Spec`.
- ///
- /// This method can be used to do simple custom assertions without
- /// implementing an [`Expectation`] and an assertion trait.
- ///
- /// # Examples
- ///
- /// ```
- /// use asserting::prelude::*;
- ///
- /// fn is_odd(value: &i32) -> bool {
- /// value & 1 == 1
- /// }
- ///
- /// assert_that!(37).satisfies_with_message("expected my number to be odd", is_odd);
- ///
- /// let failures = verify_that!(22)
- /// .satisfies_with_message("expected my number to be odd", is_odd)
- /// .display_failures();
- ///
- /// assert_that!(failures).contains_exactly([
- /// "expected my number to be odd\n"
- /// ]);
- /// ```
- ///
- /// To assert a predicate with a generic failure message instead of
- /// providing one use the method
- /// [`satisfies`](Spec::satisfies).
- #[allow(clippy::return_self_not_must_use)]
- #[track_caller]
- pub fn satisfies_with_message (self, message: impl Into for DerivedSpec<'_, O, S>
+where
+ O: DoFail,
+{
+ fn expecting(mut self, mut expectation: impl Expectation) -> Self {
+ if !expectation.test(&self.subject) {
+ let message =
+ expectation.message(&self.expression, &self.subject, false, &self.diff_format);
+ self.do_fail_with_message(message);
+ }
+ self
+ }
+}
+
+impl for DerivedSpec<'_, O, S>
+where
+ S: PartialEq + Debug,
+ O: DoFail,
+{
+ fn is_same_as(self, expected: S) -> Self {
+ self.expecting(is_same_as(expected))
+ }
+
+ fn is_not_same_as(self, expected: S) -> Self {
+ self.expecting(not(is_same_as(expected)))
+ }
+}
+
+#[cfg(feature = "float-cmp")]
+mod float_cmp {
+ use super::DerivedSpec;
+ use crate::assertions::{AssertIsCloseToWithDefaultMargin, AssertIsCloseToWithinMargin};
+ use crate::expectations::{is_close_to, not};
+ use crate::spec::{DoFail, Expecting};
+ use float_cmp::{F32Margin, F64Margin};
+
+ impl + Debug,
+ O: DoFail,
+{
+ fn is_in_range>
+where
+ S: Debug,
+ O: DoFail,
+{
+ fn is_some(self) -> Self {
+ self.expecting(is_some())
+ }
+
+ fn is_none(self) -> Self {
+ self.expecting(is_none())
+ }
+}
+
+impl<'a, O, T> AssertOptionValue for DerivedSpec<'a, O, Option::IntoIter: DefinedOrderProperty,
+ E: IntoIterator,
+ ::Key: PartialEq::Value: Debug,
+ E: Debug,
+ O: DoFail,
+{
+ fn contains_key(self, expected_key: E) -> Self {
+ self.expecting(map_contains_key(expected_key))
+ }
+
+ fn does_not_contain_key(self, expected_key: E) -> Self {
+ self.expecting(not(map_contains_key(expected_key)))
+ }
+
+ fn contains_keys(self, expected_keys: impl IntoIterator::Key: Debug,
+ ::Value: PartialEq::IntoIter: DefinedOrderProperty,
+ T: Debug,
+ O: DoFail + GetFailures,
+{
+ type SingleElement = DerivedSpec<'a, O, T>;
+ type MultipleElements = DerivedSpec<'a, O, Vec::IntoIter: DefinedOrderProperty,
+ T: ToOwned::IntoIter: DefinedOrderProperty,
+ T: 'a + ToOwned Spec<'_, S, R> {
&self.expression
}
- /// Returns the location in source code or test code if it has been set.
- pub fn location(&self) -> Option Spec<'_, S, R>
+impl<'a, I, R> AssertElements<'a, I> for Spec<'a, I, R>
where
- R: FailingStrategy,
+ I: IntoIterator,
{
- /// Asserts the given expectation.
- ///
- /// In case the expectation is not meet, the assertion fails according to
- /// the current failing strategy of this `Spec`.
- ///
- /// This method is called from the implementations of the assertion traits
- /// defined in the [`assertions`](crate::assertions) module. Implementations
- /// of custom assertions will call this method with a proper expectation.
- ///
- /// # Examples
- ///
- /// ```
- /// use asserting::expectations::{IsEmpty, IsEqualTo};
- /// use asserting::prelude::*;
- ///
- /// assert_that!(7 * 6).expecting(IsEqualTo {expected: 42 });
- ///
- /// assert_that!("").expecting(IsEmpty);
- /// ```
- #[allow(clippy::needless_pass_by_value, clippy::return_self_not_must_use)]
- #[track_caller]
- pub fn expecting(mut self, mut expectation: impl Expectation) -> Self {
- if !expectation.test(&self.subject) {
- let message =
- expectation.message(&self.expression, &self.subject, false, &self.diff_format);
- self.do_fail_with_message(message);
- }
- self
- }
+ type Output = Spec<'a, (), R>;
- /// Asserts whether the given predicate is meet.
- ///
- /// This method takes a predicate function and calls it as an expectation.
- /// In case the predicate function returns false, it does fail with a
- /// generic failure message and according to the current failing strategy of
- /// this `Spec`.
- ///
- /// This method can be used to do simple custom assertions without
- /// implementing an [`Expectation`] and an assertion trait.
- ///
- /// # Examples
- ///
- /// ```
- /// use asserting::prelude::*;
- ///
- /// fn is_odd(value: &i32) -> bool {
- /// value & 1 == 1
- /// }
- ///
- /// assert_that!(37).satisfies(is_odd);
- ///
- /// let failures = verify_that!(22).satisfies(is_odd).display_failures();
- ///
- /// assert_that!(failures).contains_exactly([
- /// "expected 22 to satisfy the given predicate, but returned false\n"
- /// ]);
- /// ```
- ///
- /// To assert a predicate with a custom failure message instead of the
- /// generic one, use the method
- /// [`satisfies_with_message`](Spec::satisfies_with_message).
- #[allow(clippy::return_self_not_must_use)]
- #[track_caller]
- pub fn satisfies