Skip to content

Commit

Permalink
refactor!: use a trait-based approach for defining option 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 OptionAssertionTrait
  • Loading branch information
open-schnick committed Dec 14, 2024
1 parent 458ed06 commit 385ce14
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 90 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,12 @@ mod string;

pub use connector::AssertionConnector;
pub use equality::EqualityAssertion;
pub use option::SomeAsserter;
pub use option::{OptionAssertion, SomeAsserter};
pub use result::{ErrAsserter, OkAsserter};

/// 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};
pub use crate::{assert_that, BasicAsserter, EqualityAssertion, OptionAssertion};
}

/// Main struct with various assertions on `AssertedType`
Expand Down
86 changes: 83 additions & 3 deletions src/option.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,84 @@
mod is_none;
mod is_some;
use crate::{implementation, BasicAsserter};
use std::fmt::Debug;

pub use is_some::SomeAsserter;
/// Specifies various assertions on [`Option`]. Implemented on [`BasicAsserter`]
pub trait OptionAssertion<AssertedValue> {
/// Asserts that the [Option] is [Some].
///
/// Allows the usage of chained assertions on an option-type (see [`SomeAsserter`]).
///
/// # Examples
/// ```
/// # use smoothy::prelude::*;
/// #
/// let option: Option<String> = Some(String::new());
///
/// assert_that(option).is_some();
/// ```
///
/// # Panics
/// When the [Option] is [None]
#[track_caller]
#[allow(clippy::wrong_self_convention)]
fn is_some(self) -> SomeAsserter<AssertedValue>;

/// Asserts that the [Option] is [None].
///
/// # Examples
/// ```
/// # use smoothy::prelude::*;
/// #
/// let option: Option<String> = None;
///
/// assert_that(option).is_none();
/// ```
///
/// # Panics
/// When the [Option] is [Some]
#[track_caller]
#[allow(clippy::wrong_self_convention)]
fn is_none(self);
}

impl<OptionValue> OptionAssertion<OptionValue> for BasicAsserter<Option<OptionValue>>
where
OptionValue: Debug,
{
fn is_some(self) -> SomeAsserter<OptionValue> {
implementation::assert(self.value.is_some(), "Option is Some", &self.value);

#[allow(clippy::unwrap_used)]
let value = self.value.unwrap();

SomeAsserter { value }
}

fn is_none(self) {
implementation::assert(self.value.is_none(), "Option is None", &self.value);
}
}

/// Enables various assertions on [Some]-values
pub struct SomeAsserter<SomeValue> {
value: SomeValue,
}

impl<SomeValue> SomeAsserter<SomeValue> {
/// Prepares the [Some] value for further assertions.
///
/// # Examples
/// ```
/// # use smoothy::prelude::*;
/// #
/// let option: Option<String> = Some(String::from("Hello World!"));
///
/// let asserter = assert_that(option).is_some().and_value();
/// // further assertions
/// asserter.equals("Hello World!");
/// ```
#[track_caller]
#[must_use = "Transforming the asserted value does not assert anything"]
pub fn and_value(self) -> BasicAsserter<SomeValue> {
BasicAsserter { value: self.value }
}
}
26 changes: 0 additions & 26 deletions src/option/is_none.rs

This file was deleted.

57 changes: 0 additions & 57 deletions src/option/is_some.rs

This file was deleted.

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

#[test]
fn succeeds() {
Expand Down
2 changes: 1 addition & 1 deletion tests/is_some.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use smoothy::{assert_that, BasicAsserter, EqualityAssertion};
use smoothy::{assert_that, BasicAsserter, EqualityAssertion, OptionAssertion};

mod assert_option {
use super::*;
Expand Down

0 comments on commit 385ce14

Please sign in to comment.