Skip to content

Commit

Permalink
refactor!: use a trait-based approach for defining boolean assertions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: changing to the new approach means that users need to update their imports to also include the BooleanAssertionTrait
  • Loading branch information
open-schnick committed Dec 15, 2024
1 parent 2529179 commit bc5db26
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
34 changes: 22 additions & 12 deletions src/assertions/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::{implementation, AssertionConnector, BasicAsserter};

impl<AssertedType> BasicAsserter<AssertedType>
/// Specifies various assertions on values that can be converted to a boolean. Implemented on [`BasicAsserter`]
pub trait BooleanAssertion<IntoBoolean>
where
AssertedType: Into<bool>,
IntoBoolean: Into<bool>,
{
/// Convenience method for asserting that a value is true
///
Expand All @@ -21,15 +22,9 @@ where
///
/// # Panics
/// When the value is false
#[allow(clippy::wrong_self_convention)]
#[track_caller]
pub fn is_true(self) -> AssertionConnector<bool> {
let actual = self.value.into();

implementation::assert(actual, "Value is true", actual);

AssertionConnector { value: actual }
}
#[allow(clippy::wrong_self_convention)]
fn is_true(self) -> AssertionConnector<bool>;

/// Convenience method for asserting that a value is false
///
Expand All @@ -48,9 +43,24 @@ where
///
/// # Panics
/// When the value is true
#[allow(clippy::wrong_self_convention)]
#[track_caller]
pub fn is_false(self) -> AssertionConnector<bool> {
#[allow(clippy::wrong_self_convention)]
fn is_false(self) -> AssertionConnector<bool>;
}

impl<IntoBoolean> BooleanAssertion<IntoBoolean> for BasicAsserter<IntoBoolean>
where
IntoBoolean: Into<bool>,
{
fn is_true(self) -> AssertionConnector<bool> {
let actual = self.value.into();

implementation::assert(actual, "Value is true", actual);

AssertionConnector { value: actual }
}

fn is_false(self) -> AssertionConnector<bool> {
let actual = self.value.into();

implementation::assert(!actual, "Value is false", actual);
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ mod connector;
mod implementation;

pub use assertions::{
boolean::BooleanAssertion,
equality::EqualityAssertion,
iter::IteratorAssertion,
option::{OptionAssertion, SomeAsserter},
Expand All @@ -271,8 +272,8 @@ pub use connector::AssertionConnector;
/// The prelude for smoothy. Contains the most important structs, traits and functions but not all
pub mod prelude {
pub use crate::{
assert_that, BasicAsserter, EqualityAssertion, IteratorAssertion, OptionAssertion,
ResultAssertion, StringAssertion,
assert_that, BasicAsserter, BooleanAssertion, EqualityAssertion, IteratorAssertion,
OptionAssertion, ResultAssertion, StringAssertion,
};
}

Expand Down
2 changes: 1 addition & 1 deletion tests/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use smoothy::assert_that;
use smoothy::{assert_that, BooleanAssertion};

#[cfg(test)]
mod is_true {
Expand Down

0 comments on commit bc5db26

Please sign in to comment.