-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: use a trait-based approach for defining option assertions
BREAKING CHANGE: changing to the new approach means that users need to update their imports to also include the OptionAssertionTrait
- Loading branch information
1 parent
458ed06
commit 385ce14
Showing
6 changed files
with
87 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters