Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,37 @@ being the type of the expected value.
| is_in_range | verify that the subject is in the expected range (closed range) |
| is_not_in_range | verify that the subject is not in the specified range (closed range) |

### Float
### Integer and Float

for integer numbers of type `i8`, `i16`, `i32`, `i64`, `i128` and `isize` as well as <br/>
floating point numbers of type `f32` and `f64`:

| assertion | description |
|-----------------|------------------------------------------------------|
| is_negative | verify that the subject is a negative number |
| is_not_negative | verify that the subject is a positive number or zero |
| is_positive | verify that the subject is a positive number |
| is_not_positive | verify that the subject is a finite number |

for integer numbers of type `i8`, `i16`, `i32`, `i64`, `i128`, `isize`, `u8`, `u16`, `u32`, `u64`,
`u128` and `usize` as well as <br/>
floating point numbers of type `f32` and `f64`:

| assertion | description |
|-----------|--------------------------------------------------------------|
| is_zero | verify that the subject is the additive identity (zero) |
| is_one | verify that the subject is the multiplicative identity (one) |

for floating point numbers of type `f32` and `f64`:

| assertion | description |
|-----------------|-----------------------------------------------|
| is_infinite | verify that the subject is an infinite number |
| is_finite | verify that the subject is a finite number |
| is_not_a_number | verify that the subject is not a number |
| is_a_number | verify that the subject is a number |

### Float comparison

for floating point numbers of type `f32` and `f64`.

Expand Down
126 changes: 126 additions & 0 deletions src/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,132 @@ pub trait AssertInRange<E> {
fn is_not_in_range(self, range: RangeInclusive<E>) -> Self;
}

/// Assert whether a numeric value is negative or positive.
///
/// # Examples
///
/// ```
/// use asserting::prelude::*;
///
/// assert_that!(-42).is_negative();
/// assert_that!(42).is_positive();
/// assert_that!(0).is_not_negative();
/// assert_that!(1).is_not_negative();
/// assert_that!(0).is_not_positive();
/// assert_that!(-1).is_not_positive();
///
/// assert_that!(-0.1).is_negative();
/// assert_that!(0.1).is_positive();
/// assert_that!(0.0).is_not_negative();
/// assert_that!(0.1).is_not_negative();
/// assert_that!(0.0).is_not_positive();
/// assert_that!(-0.1).is_not_positive();
/// ```
pub trait AssertSignum {
/// Verifies that the subject is a negative number.
///
/// This is equivalent to asserting that a number is less than 0.
#[track_caller]
fn is_negative(self) -> Self;

/// Verifies that the subject is a non-negative number.
///
/// This is equivalent to asserting that a number is greater than or equal
/// to 0.
#[track_caller]
fn is_not_negative(self) -> Self;

/// Verifies that the subject is a positive number.
///
/// This is equivalent to asserting that a number is greater than 0.
#[track_caller]
fn is_positive(self) -> Self;

/// Verifies that the subject is a non-positive number.
///
/// This is equivalent to asserting that a number is less than or equal to
/// 0.
#[track_caller]
fn is_not_positive(self) -> Self;
}

/// Assert the additive and multiplicative identity of a number.
///
/// # Examples
///
/// ```
/// use asserting::prelude::*;
///
/// assert_that!(0).is_zero();
/// assert_that!(1).is_one();
/// assert_that!(0.0).is_zero();
/// assert_that!(1.0).is_one();
/// ```
pub trait AssertNumericIdentity {
/// Verifies whether the subject is the additive identity (zero).
#[track_caller]
fn is_zero(self) -> Self;

/// Verifies whether the subject is the multiplicative identity (one).
#[track_caller]
fn is_one(self) -> Self;
}

/// Assert whether a numeric value is infinite or finite.
///
/// # Examples
///
/// ```
/// use asserting::prelude::*;
///
/// assert_that!(0.1).is_finite();
/// assert_that!(0.0).is_finite();
/// assert_that!(f32::INFINITY).is_infinite();
/// assert_that!(f32::NEG_INFINITY).is_infinite();
/// assert_that!(f64::INFINITY).is_infinite();
/// assert_that!(f64::NEG_INFINITY).is_infinite();
/// ```
///
/// Assert negative and positive infinity:
///
/// ```
/// use asserting::prelude::*;
///
/// assert_that!(f64::INFINITY).is_positive().is_infinite();
/// assert_that!(f64::NEG_INFINITY).is_negative().is_infinite();
/// ```
pub trait AssertInfinity {
/// Verifies that the subject is an infinite number.
#[track_caller]
fn is_infinite(self) -> Self;

/// Verifies that the subject is a finite number.
#[track_caller]
fn is_finite(self) -> Self;
}

/// Assert whether a numeric value is not a number.
///
/// # Examples
///
/// ```
/// use asserting::prelude::*;
///
/// assert_that!(0.1).is_a_number();
/// assert_that!(0.0).is_a_number();
/// assert_that!(f32::NAN).is_not_a_number();
/// assert_that!(f64::NAN).is_not_a_number();
/// ```
pub trait AssertNotANumber {
/// Verifies that the subject is not a number.
#[track_caller]
fn is_not_a_number(self) -> Self;

/// Verifies that the subject is a number.
#[track_caller]
fn is_a_number(self) -> Self;
}

/// Assert whether some value or expression is true or false.
///
/// # Examples
Expand Down
30 changes: 30 additions & 0 deletions src/expectations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,36 @@ pub struct IsNotInRange<E> {
pub expected_range: RangeInclusive<E>,
}

#[must_use]
pub struct IsNegative;

#[must_use]
pub struct IsNotNegative;

#[must_use]
pub struct IsPositive;

#[must_use]
pub struct IsNotPositive;

#[must_use]
pub struct IsZero;

#[must_use]
pub struct IsOne;

#[must_use]
pub struct IsFinite;

#[must_use]
pub struct IsInfinite;

#[must_use]
pub struct IsNotANumber;

#[must_use]
pub struct IsANumber;

#[must_use]
pub struct IsSome;

Expand Down
Loading
Loading