Skip to content

Commit

Permalink
docs: added rustdoc with examples and panic sections to all structs a…
Browse files Browse the repository at this point in the history
…nd functions
  • Loading branch information
IndecisiveCrab committed Nov 14, 2023
1 parent 6909566 commit 2907d87
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/Cargo.lock
.idea
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ assert_that(result)
- `is` to assert equality in a type safe way (compared to the `Into<T>` stuff)
- vec support (length, contains)
- string support (length, contains, starts_with, ends_with)
- Documentation and testing :D (especially the differnence between `is` and `equals`)
- Documentation and testing :D (especially the difference between `is` and `equals`)
16 changes: 15 additions & 1 deletion src/basic/equals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@ impl<AssertedType> AssertionBuilder<AssertedType>
where
AssertedType: PartialEq + std::fmt::Debug,
{
// FIXME: the type inference is bad as i32 does not implement Into<u16>
// FIXME: the type inference for {integers} is bad as i32 does not implement Into<u16>
/// Asserts that the assertable is equal to the expected value.
///
/// This is done by transforming the expected-value to a instance of `AssertedType` by using the [Into]-trait
/// and then comparing both values with [`PartialEq`]
///
/// # Examples
/// ```
/// # use smoothy::assert_that;
/// #
/// assert_that(String::from("Hello World!")).equals("Hello World!");
/// ```
///
/// # Panics
/// When the values are not matching according to [`PartialEq`]
pub fn equals(self, expected: impl Into<AssertedType>) {
let expected: AssertedType = expected.into();
implementation::assert_equals(self.value, expected);
Expand Down
16 changes: 15 additions & 1 deletion src/basic/not_equals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@ impl<AssertedType> AssertionBuilder<AssertedType>
where
AssertedType: PartialEq + std::fmt::Debug,
{
// FIXME: the type inference is bad as i32 does not implement Into<u16>
// FIXME: the type inference for {integers} is bad as i32 does not implement Into<u16>
/// Asserts that the assertable is *not* equal to the expected value.
///
/// This is done by transforming the expected-value to a instance of `AssertedType` by using the [Into]-trait
/// and then comparing both values with [`PartialEq`]
///
/// # Examples
/// ```
/// # use smoothy::assert_that;
/// #
/// assert_that(String::from("Hello World!")).not_equals("Hello There!");
/// ```
///
/// # Panics
/// When the values are matching according to [`PartialEq`]
pub fn not_equals(self, expected: impl Into<AssertedType>) {
let expected: AssertedType = expected.into();
implementation::assert_not_equals(self.value, expected);
Expand Down
15 changes: 15 additions & 0 deletions src/basic/try_equals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,26 @@ impl<AssertedType> AssertionBuilder<AssertedType>
where
AssertedType: PartialEq + std::fmt::Debug,
{
/// Asserts that the assertable is equal to the expected value.
///
/// This is done by transforming the expected-value to a instance of `AssertedType` by using the [`TryInto`]-trait
/// and then comparing both values with [`PartialEq`]
///
/// # Examples
/// ```
/// # use smoothy::assert_that;
/// #
/// assert_that(42u8).try_into_equals(42i8);
/// ```
///
/// # Panics
/// When the transformation fails or the values are not matching according to [`PartialEq`]
pub fn try_into_equals<T>(self, expected: T)
where
T: TryInto<AssertedType>,
<T as TryInto<AssertedType>>::Error: std::fmt::Debug,
{
// TODO: improve assertion here
let expected: AssertedType = expected.try_into().unwrap();
implementation::assert_equals(self.value, expected);
}
Expand Down
15 changes: 15 additions & 0 deletions src/basic/try_not_equals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,26 @@ impl<AssertedType> AssertionBuilder<AssertedType>
where
AssertedType: PartialEq + std::fmt::Debug,
{
/// Asserts that the assertable is *not* equal to the expected value.
///
/// This is done by transforming the expected-value to a instance of `AssertedType` by using the [`TryInto`]-trait
/// and then comparing both values with [`PartialEq`]
///
/// # Examples
/// ```
/// # use smoothy::assert_that;
/// #
/// assert_that(42u8).try_into_not_equals(100i8);
/// ```
///
/// # Panics
/// When the transformation fails or the values are matching according to [`PartialEq`]
pub fn try_into_not_equals<T>(self, expected: T)
where
T: TryInto<AssertedType>,
<T as TryInto<AssertedType>>::Error: std::fmt::Debug,
{
// TODO: improve assertion here
let expected: AssertedType = expected.try_into().unwrap();
implementation::assert_not_equals(self.value, expected);
}
Expand Down
24 changes: 19 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
//! ## Results
//!
//! Results can be asserted by calling [is_err](struct.AssertionBuilder.html#method.is_err) or [is_ok](struct.AssertionBuilder.html#method.is_ok).

Check failure on line 43 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

item in documentation is missing backticks

error: item in documentation is missing backticks --> src/lib.rs:43:97 | 43 | //! Results can be asserted by calling [is_err](struct.AssertionBuilder.html#method.is_err) or [is_ok](struct.AssertionBuilder.html#metho... | ^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown help: try | 43 | //! Results can be asserted by calling [is_err](struct.AssertionBuilder.html#method.is_err) or [`is_ok`](struct.AssertionBuilder.html#method.is_ok). | ~~~~~~~

Check failure on line 43 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

item in documentation is missing backticks

error: item in documentation is missing backticks --> src/lib.rs:43:41 | 43 | //! Results can be asserted by calling [is_err](struct.AssertionBuilder.html#method.is_err) or [is_ok](struct.AssertionBuilder.html#metho... | ^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown help: try | 43 | //! Results can be asserted by calling [`is_err`](struct.AssertionBuilder.html#method.is_err) or [is_ok](struct.AssertionBuilder.html#method.is_ok). | ~~~~~~~~
//! Futhermore their actual content can be asserted aswell.
//! Furthermore their actual content can be asserted as well.
//!
//! ### Ok
//!
Expand Down Expand Up @@ -82,7 +82,7 @@
//! assert_that(result).is_err().and_error_equals(CustomError(String::from("Oh no!")));
//! ```
//!
//! Alternativly one can assert the error message (given the error implements [Display](std::fmt::Display)):
//! Alternatively one can assert the error message (given the error implements [Display](std::fmt::Display)):
//!
//! ```
//! # use smoothy::assert_that;
Expand All @@ -106,7 +106,7 @@
// enable more lint groups
#![deny(clippy::pedantic, clippy::nursery)]
// check documentation
// #![deny(missing_docs, rustdoc::broken_intra_doc_links)]
#![deny(missing_docs, rustdoc::broken_intra_doc_links)]
// enable extra restriction lints
#![deny(
clippy::as_conversions,
Expand Down Expand Up @@ -159,16 +159,30 @@
clippy::needless_pass_by_value
)]

pub use result::*;

mod basic;
mod implementation;
mod result;

pub use result::*;

/// Main struct to enable various assertions on `AssertedType`
pub struct AssertionBuilder<AssertedType> {
pub(crate) value: AssertedType,
}

/// Entrypoint for all assertions
///
/// Enables various assertions on variable based on its type
///
/// # Examples
/// ```
/// # use smoothy::{assert_that, AssertionBuilder};
/// #
/// let assertable = String::from("Hello World!");
/// let assertion: AssertionBuilder<String> = assert_that(assertable);
/// // add your assertions here
/// ```
#[must_use]
pub const fn assert_that<AssertedType>(value: AssertedType) -> AssertionBuilder<AssertedType> {
AssertionBuilder { value }
}
100 changes: 96 additions & 4 deletions src/result/is_err.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
use crate::{implementation, AssertionBuilder};
use std::fmt::{Debug, Display};
use std::fmt::Debug;

impl<OkValue, ErrValue> AssertionBuilder<Result<OkValue, ErrValue>>
where
OkValue: Debug,
ErrValue: Debug,
{
/// Asserts that the [Result] is an [Err].
///
/// Allows the usage of chained assertions on an error-type (see [`ErrAsserter`]).
///
/// # Examples
/// ```
/// # use smoothy::assert_that;
/// #
/// let result: Result<(), String> = Err(String::new());
///
/// assert_that(result).is_err();
/// ```
///
/// # Panics
/// When the [Result] is an [Ok]
pub fn is_err(self) -> ErrAsserter<ErrValue> {
implementation::assert(self.value.is_err(), "Result to be an error", &self.value);

Expand All @@ -15,26 +30,103 @@ where
}
}

/// Enables various assertions on [Err]-values
pub struct ErrAsserter<ErrValue> {
value: ErrValue,
}

// In case the error does implement PartialEq
impl<ErrValue> ErrAsserter<ErrValue>
where
ErrValue: Debug + PartialEq,
{
/// Asserts that the asserted error is equal to the expected error.
///
/// This is done by transforming the expected error to a instance of `ErrValue` by using the [Into]-trait
/// and then comparing both values with [`PartialEq`].
///
/// # Examples
/// ```
/// # use smoothy::assert_that;
/// # use std::fmt::Display;
/// #
/// #[derive(Debug, PartialEq)]
/// pub struct ComparableError(String);
///
/// let result: Result<(), ComparableError> = Err(ComparableError(String::from("Hello There")));
///
/// assert_that(result)
/// .is_err()
/// .and_error_equals(ComparableError(String::from("Hello There")));
/// ```
///
/// When the asserted error cannot be build but converted one can also use any type that implements [Into] the asserted error
///
/// ```
/// # use smoothy::assert_that;
/// #
/// // The error type we want to assert
/// #[derive(Debug, PartialEq)]
/// pub struct NotConstructableError(String);
///
/// // The error we can construct
/// pub struct ConvertableError(pub String);
///
/// /* From-implementation */
/// # impl From<ConvertableError> for NotConstructableError {
/// # fn from(error: ConvertableError) -> Self {
/// # NotConstructableError(error.0)
/// # }
/// # }
///
/// let result: Result<(), NotConstructableError> = Err(NotConstructableError(String::from("Hello There")));
///
/// assert_that(result)
/// .is_err()
/// .and_error_equals(ConvertableError(String::from("Hello There")));
/// ```
///
/// # Panics
/// When the errors are not matching according to [`PartialEq`].
pub fn and_error_equals(self, expected: impl Into<ErrValue>) {
let expected: ErrValue = expected.into();
implementation::assert_equals(self.value, expected);
}
}

// In case the error does not implement PartialEq
impl<ErrValue> ErrAsserter<ErrValue>
where
ErrValue: Debug + Display,
ErrValue: Debug + ToString,
{
/// Asserts that the asserted error message is equal to the expected string.
///
/// This assertion can be used when the error does not implement [`PartialEq`] or the error cannot be constructed.
///
/// The asserted error message is created by calling [`to_string`](ToString::to_string) on the error
///
/// # Examples
/// ```
/// # use smoothy::assert_that;
/// # use std::fmt::Display;
/// #
/// #[derive(Debug)]
/// pub struct NonComparableError(String);
/// #
/// # impl Display for NonComparableError {
/// # fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// # write!(f, "{}", self.0)
/// # }
/// # }
///
/// let result: Result<(), NonComparableError> =
/// Err(NonComparableError(String::from("Hello There")));
///
/// assert_that(result)
/// .is_err()
/// .and_error_to_string_equals(String::from("Hello There"));
/// ```
///
/// # Panics
/// When the error message is not equal to the expected error message.
pub fn and_error_to_string_equals(self, expected: impl AsRef<str>) {
implementation::assert_equals(self.value.to_string().as_ref(), expected.as_ref());
}
Expand Down
39 changes: 36 additions & 3 deletions src/result/is_ok.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
use crate::{implementation, AssertionBuilder};
use std::fmt::Debug;

impl<OkValue, ErrValue> AssertionBuilder<Result<OkValue, ErrValue>>
where
OkValue: std::fmt::Debug + PartialEq,
ErrValue: std::fmt::Debug + PartialEq,
OkValue: Debug + PartialEq,
ErrValue: Debug + PartialEq,
{
/// Asserts that the [Result] is an [Ok].
///
/// Allows the usage of chained assertions on an ok-value (see [`OkAsserter`]).
///
/// # Examples
/// ```
/// # use smoothy::assert_that;
/// #
/// let result: Result<String, ()> = Ok(String::new());
///
/// assert_that(result).is_ok();
/// ```
///
/// # Panics
/// When the [Result] is an [Err]
pub fn is_ok(self) -> OkAsserter<OkValue> {
implementation::assert(self.value.is_ok(), "Result to be Ok", &self.value);

Expand All @@ -14,14 +30,31 @@ where
}
}

/// Enables various assertions on [Ok]-values
pub struct OkAsserter<OkValue> {
value: OkValue,
}

impl<OkValue> OkAsserter<OkValue>
where
OkValue: std::fmt::Debug + PartialEq,
OkValue: Debug + PartialEq,
{
/// Asserts that the value of the [Ok] is equal to the expected value
///
/// This is done by transforming the expected-value to a instance of `OkValue` by using the [Into]-trait
/// and then comparing both values with [`PartialEq`]
///
/// # Examples
/// ```
/// # use smoothy::assert_that;
/// #
/// let result: Result<String, ()> = Ok(String::from("Hello There"));
///
/// assert_that(result).is_ok().and_value_equals("Hello There");
/// ```
///
/// # Panics
/// When the values are not matching according to [`PartialEq`]
pub fn and_value_equals(self, expected: impl Into<OkValue>) {
let expected: OkValue = expected.into();
implementation::assert_equals(self.value, expected);
Expand Down

0 comments on commit 2907d87

Please sign in to comment.