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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features "colored, float-cmp, num-bigint"
args: --no-default-features --features "colored, float-cmp, num-bigint, rust-decimal"

msrv:
name: Build with MSRV
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ default = ["std", "colored", "float-cmp", "panic", "regex"]
colored = ["dep:sdiff"]
float-cmp = ["dep:float-cmp"]
num-bigint = ["dep:num-bigint", "dep:lazy_static"]
rust-decimal = ["dep:rust_decimal"]
panic = ["std"]
regex = ["dep:regex"]
std = []
Expand All @@ -34,6 +35,7 @@ hashbrown = "0.15"
float-cmp = { version = "0.10", optional = true }
lazy_static = { version = "1", optional = true }
num-bigint = { version = "0.4", default-features = false, optional = true }
rust_decimal = { version = "1", default-features = false, optional = true }
regex = { version = "1", optional = true }
sdiff = { version = "0.1", optional = true }

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ for numbers of types
and `usize`
* floating point numbers: `f32` and `f64`
* `num_bigint::BigInt` and `num_bigint::BigUint` (requires crate feature `num-bigint`)
* `rust_decimal::Decimal` (requires crate feature `rust-decimal`)

| assertion | description |
|-----------|--------------------------------------------------------------|
Expand Down
2 changes: 2 additions & 0 deletions examples/fixture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mod dummy_extern_uses {
use proptest as _;
#[cfg(feature = "regex")]
use regex as _;
#[cfg(feature = "rust-decimal")]
use rust_decimal as _;
#[cfg(feature = "colored")]
use sdiff as _;
use serial_test as _;
Expand Down
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ lint-all-features:

# linting code using Clippy for no-std environment
lint-no-std:
cargo clippy --all-targets --no-default-features --features "colored, float-cmp, num-bigint"
cargo clippy --all-targets --no-default-features --features "colored, float-cmp, num-bigint, rust-decimal"

# linting code using Clippy with no features enabled
lint-no-features:
Expand All @@ -55,7 +55,7 @@ test-all-features:

# run tests for no-std environment
test-no-std:
cargo test --no-default-features --features "colored, float-cmp, num-bigint"
cargo test --no-default-features --features "colored, float-cmp, num-bigint, rust-decimal"

# run tests with no features enabled
test-no-features:
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,8 @@ mod panic;
mod predicate;
mod range;
mod result;
#[cfg(feature = "rust-decimal")]
mod rust_decimal;
mod slice;
mod string;
mod vec;
Expand Down
3 changes: 1 addition & 2 deletions src/num_bigint/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::prelude::{AdditiveIdentityProperty, MultiplicativeIdentityProperty};
use crate::properties::SignumProperty;
use crate::properties::{AdditiveIdentityProperty, MultiplicativeIdentityProperty, SignumProperty};
use crate::std::vec;
use lazy_static::lazy_static;
use num_bigint::{BigInt, BigUint, Sign};
Expand Down
39 changes: 39 additions & 0 deletions src/rust_decimal/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::properties::{AdditiveIdentityProperty, MultiplicativeIdentityProperty, SignumProperty};
use rust_decimal::Decimal;

impl SignumProperty for Decimal {
fn is_negative_property(&self) -> bool {
self.is_sign_negative()
}

fn is_positive_property(&self) -> bool {
self.is_sign_positive() && !self.is_zero()
}
}

impl AdditiveIdentityProperty for Decimal {
fn additive_identity() -> Self {
Self::ZERO
}
}

impl AdditiveIdentityProperty for &Decimal {
fn additive_identity() -> Self {
&Decimal::ZERO
}
}

impl MultiplicativeIdentityProperty for Decimal {
fn multiplicative_identity() -> Self {
Self::ONE
}
}

impl MultiplicativeIdentityProperty for &Decimal {
fn multiplicative_identity() -> Self {
&Decimal::ONE
}
}

#[cfg(test)]
mod tests;
154 changes: 154 additions & 0 deletions src/rust_decimal/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
use crate::prelude::*;
use rust_decimal::Decimal;

#[test]
fn decimal_is_equal_to_other() {
let subject = Decimal::new(42_831, 3);

assert_that(subject).is_equal_to(Decimal::new(42_831, 3));

assert_that(Decimal::new(42_831, 3)).is_equal_to(Decimal::new(428_310, 4));
assert_that(Decimal::new(0, 0)).is_equal_to(Decimal::new(0, 2));
assert_that(Decimal::new(-0, 0)).is_equal_to(Decimal::new(0, 0));
}

#[test]
fn verify_decimal_is_equal_to_other_fails() {
let subject = Decimal::new(42_831, 3);

let failures = verify_that(subject)
.is_equal_to(Decimal::new(-42_831, 3))
.display_failures();

assert_eq!(
failures,
&[r"assertion failed: expected subject is equal to -42.831
but was: 42.831
expected: -42.831
"]
);
}

#[test]
fn decimal_is_not_equal_to_other() {
let subject = Decimal::new(42_831, 3);

assert_that(subject).is_not_equal_to(Decimal::new(42_831, 2));
}

#[test]
fn borrowed_decimal_is_equal_to_other() {
let subject = Decimal::new(-42_831, 3);

assert_that(&subject).is_equal_to(&Decimal::new(-42_831, 3));
}

#[test]
fn decimal_is_less_than_other() {
let subject = Decimal::new(42_831, 3);

assert_that(&subject).is_less_than(&Decimal::new(1_592_834, 3));
assert_that(subject).is_less_than(Decimal::new(42_832, 3));
}

#[test]
fn decimal_is_greater_than_other() {
let subject = Decimal::new(42_831, 3);

assert_that(&subject).is_greater_than(&Decimal::new(-232_199, 3));
assert_that(subject).is_greater_than(Decimal::new(42_830, 3));
}

#[test]
fn decimal_is_at_least_other() {
let subject = Decimal::new(42_831, 3);

assert_that(&subject).is_at_least(&Decimal::new(42_831, 3));
assert_that(&subject).is_at_least(&Decimal::new(42_830, 3));
assert_that(subject).is_at_least(Decimal::new(-332, 3));
}

#[test]
fn decimal_is_at_most_other() {
let subject = Decimal::new(42_831, 3);

assert_that(&subject).is_at_most(&Decimal::new(42_831, 3));
assert_that(&subject).is_at_most(&Decimal::new(42_832, 3));
assert_that(subject).is_at_most(Decimal::new(65_587_929, 3));
}

#[test]
fn decimal_is_negative() {
let subject = Decimal::new(-42_831, 3);

assert_that(&subject).is_negative();
}

#[test]
fn decimal_is_not_negative() {
assert_that(&Decimal::new(42_831, 3)).is_not_negative();
assert_that(Decimal::new(0, 0)).is_not_negative();
}

#[test]
fn decimal_is_positive() {
let subject = Decimal::new(42_831, 3);

assert_that(&subject).is_positive();
}

#[test]
fn decimal_is_not_positive() {
assert_that(&Decimal::new(-42_831, 3)).is_not_positive();
assert_that(Decimal::new(0, 0)).is_not_positive();
}

#[test]
fn decimal_signum_of_zero() {
assert_that(Decimal::new(0, 0)).is_zero();
assert_that(Decimal::new(0, 0).is_sign_positive()).is_true();
assert_that(Decimal::new(0, 0).is_sign_negative()).is_false();
assert_that(Decimal::new(-0, 0).is_sign_negative()).is_false();
}

#[test]
fn borrowed_decimal_is_negative() {
assert_that(&Decimal::new(-42_831, 3)).is_negative();
}

#[test]
fn borrowed_decimal_is_positive() {
assert_that(&Decimal::new(42_831, 3)).is_positive();
}

#[test]
fn mutable_borrowed_decimal_is_negative() {
assert_that(&mut Decimal::new(-42_831, 3)).is_negative();
}

#[test]
fn mutable_borrowed_decimal_is_positive() {
assert_that(&mut Decimal::new(42_831, 3)).is_positive();
}

#[test]
fn decimal_is_zero() {
assert_that(Decimal::new(0, 0)).is_zero();
assert_that(Decimal::new(-0, 0)).is_zero();
assert_that(Decimal::new(0, 2)).is_zero();
}

#[test]
fn decimal_is_one() {
assert_that(Decimal::new(1, 0)).is_one();
}

#[test]
fn borrowed_decimal_is_zero() {
assert_that(&Decimal::new(0, 0)).is_zero();
}

#[test]
fn borrowed_decimal_is_one() {
assert_that(&Decimal::new(1, 0)).is_one();
}
2 changes: 2 additions & 0 deletions tests/version_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ mod dummy_extern_uses {
use proptest as _;
#[cfg(feature = "regex")]
use regex as _;
#[cfg(feature = "rust-decimal")]
use rust_decimal as _;
#[cfg(feature = "colored")]
use sdiff as _;
use serial_test as _;
Expand Down
Loading