asserting 0.14.0

Fluent assertions for tests in Rust that are convenient to write and easy to extend.
Documentation
//! Implementation of the equality assertions.

use crate::assertions::{
    AssertEquality, AssertHasDebugString, AssertHasDisplayString, AssertSameAs,
};
use crate::colored::{mark_diff, mark_diff_str};
use crate::expectations::{
    has_debug_string, has_display_string, is_equal_to, is_same_as, not, HasDebugString,
    HasDisplayString, IsEqualTo, IsSameAs,
};
use crate::spec::{DiffFormat, Expectation, Expression, FailingStrategy, Invertible, Spec};
use crate::std::fmt::{Debug, Display};
use crate::std::format;
use crate::std::string::{String, ToString};

impl<S, E, R> AssertEquality<E> for Spec<'_, S, R>
where
    S: PartialEq<E> + Debug,
    E: Debug,
    R: FailingStrategy,
{
    fn is_equal_to(self, expected: E) -> Self {
        self.expecting(is_equal_to(expected))
    }

    fn is_not_equal_to(self, expected: E) -> Self {
        self.expecting(not(is_equal_to(expected)))
    }
}

impl<S, E> Expectation<S> for IsEqualTo<E>
where
    S: PartialEq<E> + Debug,
    E: Debug,
{
    fn test(&mut self, subject: &S) -> bool {
        subject == &self.expected
    }

    fn message(
        &self,
        expression: &Expression<'_>,
        actual: &S,
        inverted: bool,
        format: &DiffFormat,
    ) -> String {
        let not = if inverted { "not " } else { "" };
        let expected = &self.expected;
        let (marked_actual, marked_expected) = mark_diff(actual, expected, format);
        format!(
            "expected {expression} to be {not}equal to {expected:?}\n   but was: {marked_actual}\n  expected: {not}{marked_expected}",
        )
    }
}

impl<E> Invertible for IsEqualTo<E> {}

impl<S, R> AssertSameAs<S> for Spec<'_, S, R>
where
    S: PartialEq + Debug,
    R: FailingStrategy,
{
    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)))
    }
}

impl<S> Expectation<S> for IsSameAs<S>
where
    S: PartialEq + Debug,
{
    fn test(&mut self, subject: &S) -> bool {
        subject == &self.expected
    }

    fn message(
        &self,
        expression: &Expression<'_>,
        actual: &S,
        inverted: bool,
        format: &DiffFormat,
    ) -> String {
        let not = if inverted { "not " } else { "" };
        let expected = &self.expected;
        let (marked_actual, marked_expected) = mark_diff(actual, expected, format);
        format!(
            "expected {expression} to be {not}the same as {expected:?}\n   but was: {marked_actual}\n  expected: {not}{marked_expected}",
        )
    }
}

impl<E> Invertible for IsSameAs<E> {}

impl<S, E, R> AssertHasDebugString<E> for Spec<'_, S, R>
where
    S: Debug,
    E: AsRef<str>,
    R: FailingStrategy,
{
    fn has_debug_string(self, expected: E) -> Self {
        self.expecting(has_debug_string(expected))
    }

    fn does_not_have_debug_string(self, expected: E) -> Self {
        self.expecting(not(has_debug_string(expected)))
    }
}

impl<S, E> Expectation<S> for HasDebugString<E>
where
    S: Debug,
    E: AsRef<str>,
{
    fn test(&mut self, subject: &S) -> bool {
        format!("{subject:?}") == self.expected.as_ref()
    }

    fn message(
        &self,
        expression: &Expression<'_>,
        actual: &S,
        inverted: bool,
        format: &DiffFormat,
    ) -> String {
        let not = if inverted { "not " } else { "" };
        let expected = self.expected.as_ref();
        let (marked_actual, marked_expected) =
            mark_diff_str(&format!("{actual:?}"), expected, format);
        format!(
            "expected {expression} to {not}have a debug string equal to {expected:?}\n   but was: {marked_actual}\n  expected: {not}{marked_expected}",
        )
    }
}

impl<E> Invertible for HasDebugString<E> {}

impl<S, E, R> AssertHasDisplayString<E> for Spec<'_, S, R>
where
    S: Display,
    E: AsRef<str>,
    R: FailingStrategy,
{
    fn has_display_string(self, expected: E) -> Self {
        self.expecting(has_display_string(expected))
    }

    fn does_not_have_display_string(self, expected: E) -> Self {
        self.expecting(not(has_display_string(expected)))
    }
}

impl<S, E> Expectation<S> for HasDisplayString<E>
where
    S: Display,
    E: AsRef<str>,
{
    fn test(&mut self, subject: &S) -> bool {
        subject.to_string() == self.expected.as_ref()
    }

    fn message(
        &self,
        expression: &Expression<'_>,
        actual: &S,
        inverted: bool,
        format: &DiffFormat,
    ) -> String {
        let not = if inverted { "not " } else { "" };
        let expected = self.expected.as_ref();
        let (marked_actual, marked_expected) = mark_diff_str(&actual.to_string(), expected, format);
        format!(
            "expected {expression} to {not}have a display string equal to {expected:?}\n   but was: \"{marked_actual}\"\n  expected: {not}\"{marked_expected}\"",
        )
    }
}

impl<E> Invertible for HasDisplayString<E> {}