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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[features]
default = ["std", "colored", "float-cmp", "panic", "recursive", "regex"]
bigdecimal = ["dep:bigdecimal", "dep:once_cell"]
chrono = ["dep:chrono"]
colored = ["dep:sdiff"]
float-cmp = ["dep:float-cmp"]
num-bigint = ["dep:num-bigint", "dep:once_cell"]
Expand All @@ -29,6 +30,7 @@ panic = ["std"]
regex = ["dep:regex"]
std = [
"bigdecimal?/std",
"chrono?/std",
"float-cmp?/std",
"indexmap?/std",
"num-bigint?/std",
Expand All @@ -45,6 +47,7 @@ hashbrown = "0.17"

# optional
bigdecimal = { version = "0.4", optional = true, default-features = false }
chrono = { version = "0.4", optional = true, default-features = false, features = ["alloc"] }
float-cmp = { version = "0.10", optional = true }
indexmap = { version = "2", optional = true, default-features = false }
num-bigint = { version = "0.5", optional = true, default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions examples/fixture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ mod dummy_extern_uses {
use anyhow as _;
#[cfg(feature = "bigdecimal")]
use bigdecimal as _;
#[cfg(feature = "chrono")]
use chrono as _;
use fakeenv as _;
#[cfg(feature = "float-cmp")]
use float_cmp as _;
Expand Down
250 changes: 250 additions & 0 deletions src/chrono/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
use crate::assertions::AssertIsCloseToWithinMargin;
use crate::colored::mark_diff;
use crate::expectations::{IsCloseTo, is_close_to, not};
use crate::spec::{
DiffFormat, Expectation, Expecting, Expression, FailingStrategy, Invertible, Represent,
Represented, Spec,
};
use crate::temporal_margin::TemporalMargin;
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, TimeDelta, TimeZone};

impl TemporalMargin {
/// Converts this temporal margin to a [`TimeDelta`] value.
pub fn to_time_delta(self) -> TimeDelta {
match self {
Self::NanoSeconds(nanos) => TimeDelta::nanoseconds(i64::from(nanos)),
Self::MicroSeconds(micros) => TimeDelta::microseconds(i64::from(micros)),
Self::MilliSeconds(millis) => TimeDelta::milliseconds(i64::from(millis)),
Self::Seconds(seconds) => TimeDelta::seconds(i64::from(seconds)),
Self::Minutes(minutes) => TimeDelta::minutes(i64::from(minutes)),
Self::Hours(hours) => TimeDelta::hours(i64::from(hours)),
Self::Days(days) => TimeDelta::days(i64::from(days)),
Self::Weeks(weeks) => TimeDelta::weeks(i64::from(weeks)),
}
}
}

impl<D, R> AssertIsCloseToWithinMargin<NaiveTime, TemporalMargin> for Spec<'_, NaiveTime, D, R>
where
D: Represent<NaiveTime> + Represent<TemporalMargin>,
R: FailingStrategy,
{
fn is_close_to_with_margin(
self,
expected: NaiveTime,
margin: impl Into<TemporalMargin>,
) -> Self {
self.expecting(is_close_to(expected).within_margin(margin))
}

fn is_not_close_to_with_margin(
self,
expected: NaiveTime,
margin: impl Into<TemporalMargin>,
) -> Self {
self.expecting(not(is_close_to(expected).within_margin(margin)))
}
}

impl<D> Expectation<NaiveTime, D> for IsCloseTo<NaiveTime, TemporalMargin>
where
D: Represent<NaiveTime> + Represent<TemporalMargin>,
{
fn test(&mut self, subject: &NaiveTime) -> bool {
subject.signed_duration_since(self.expected).abs() <= self.margin.to_time_delta()
}

fn message(
&self,
expression: &Expression<'_>,
actual: &NaiveTime,
inverted: bool,
representation: &D,
format: &DiffFormat,
) -> String {
let not = if inverted { "not " } else { "" };
let (marked_actual, marked_expected) =
mark_diff(actual, &self.expected, representation, format);
let represented_expected = Represented::from((&self.expected, representation));
let represented_margin = Represented::from((&self.margin, representation));
format!(
r"expected {expression} to be {not}close to {represented_expected:?} within {represented_margin}
but was: {marked_actual}
expected: {marked_expected}"
)
}
}

impl Invertible for IsCloseTo<NaiveTime, TemporalMargin> {}

impl<D, R> AssertIsCloseToWithinMargin<NaiveDate, TemporalMargin> for Spec<'_, NaiveDate, D, R>
where
D: Represent<NaiveDate> + Represent<TemporalMargin>,
R: FailingStrategy,
{
fn is_close_to_with_margin(
self,
expected: NaiveDate,
margin: impl Into<TemporalMargin>,
) -> Self {
self.expecting(is_close_to(expected).within_margin(margin))
}

fn is_not_close_to_with_margin(
self,
expected: NaiveDate,
margin: impl Into<TemporalMargin>,
) -> Self {
self.expecting(not(is_close_to(expected).within_margin(margin)))
}
}

impl<D> Expectation<NaiveDate, D> for IsCloseTo<NaiveDate, TemporalMargin>
where
D: Represent<NaiveDate> + Represent<TemporalMargin>,
{
fn test(&mut self, subject: &NaiveDate) -> bool {
subject.signed_duration_since(self.expected).abs() <= self.margin.to_time_delta()
}

fn message(
&self,
expression: &Expression<'_>,
actual: &NaiveDate,
inverted: bool,
representation: &D,
format: &DiffFormat,
) -> String {
let not = if inverted { "not " } else { "" };
let (marked_actual, marked_expected) =
mark_diff(actual, &self.expected, representation, format);
let represented_expected = Represented::from((&self.expected, representation));
let represented_margin = Represented::from((&self.margin, representation));
format!(
r"expected {expression} to be {not}close to {represented_expected:?} within {represented_margin}
but was: {marked_actual}
expected: {marked_expected}"
)
}
}

impl Invertible for IsCloseTo<NaiveDate, TemporalMargin> {}

impl<D, R> AssertIsCloseToWithinMargin<NaiveDateTime, TemporalMargin>
for Spec<'_, NaiveDateTime, D, R>
where
D: Represent<NaiveDateTime> + Represent<TemporalMargin>,
R: FailingStrategy,
{
fn is_close_to_with_margin(
self,
expected: NaiveDateTime,
margin: impl Into<TemporalMargin>,
) -> Self {
self.expecting(is_close_to(expected).within_margin(margin))
}

fn is_not_close_to_with_margin(
self,
expected: NaiveDateTime,
margin: impl Into<TemporalMargin>,
) -> Self {
self.expecting(not(is_close_to(expected).within_margin(margin)))
}
}

impl<D> Expectation<NaiveDateTime, D> for IsCloseTo<NaiveDateTime, TemporalMargin>
where
D: Represent<NaiveDateTime> + Represent<TemporalMargin>,
{
fn test(&mut self, subject: &NaiveDateTime) -> bool {
subject.signed_duration_since(self.expected).abs() <= self.margin.to_time_delta()
}

fn message(
&self,
expression: &Expression<'_>,
actual: &NaiveDateTime,
inverted: bool,
representation: &D,
format: &DiffFormat,
) -> String {
let not = if inverted { "not " } else { "" };
let (marked_actual, marked_expected) =
mark_diff(actual, &self.expected, representation, format);
let represented_expected = Represented::from((&self.expected, representation));
let represented_margin = Represented::from((&self.margin, representation));
format!(
r"expected {expression} to be {not}close to {represented_expected:?} within {represented_margin}
but was: {marked_actual}
expected: {marked_expected}"
)
}
}

impl Invertible for IsCloseTo<NaiveDateTime, TemporalMargin> {}

impl<TZ1, TZ2, D, R> AssertIsCloseToWithinMargin<DateTime<TZ2>, TemporalMargin>
for Spec<'_, DateTime<TZ1>, D, R>
where
TZ1: TimeZone,
TZ2: TimeZone,
D: Represent<DateTime<TZ1>> + Represent<DateTime<TZ2>> + Represent<TemporalMargin>,
R: FailingStrategy,
{
fn is_close_to_with_margin(
self,
expected: DateTime<TZ2>,
margin: impl Into<TemporalMargin>,
) -> Self {
self.expecting(is_close_to(expected).within_margin(margin))
}

fn is_not_close_to_with_margin(
self,
expected: DateTime<TZ2>,
margin: impl Into<TemporalMargin>,
) -> Self {
self.expecting(not(is_close_to(expected).within_margin(margin)))
}
}

impl<TZ1, TZ2, D> Expectation<DateTime<TZ1>, D> for IsCloseTo<DateTime<TZ2>, TemporalMargin>
where
TZ1: TimeZone,
TZ2: TimeZone,
D: Represent<DateTime<TZ1>> + Represent<DateTime<TZ2>> + Represent<TemporalMargin>,
{
fn test(&mut self, subject: &DateTime<TZ1>) -> bool {
subject
.naive_utc()
.signed_duration_since(self.expected.naive_utc())
.abs()
<= self.margin.to_time_delta()
}

fn message(
&self,
expression: &Expression<'_>,
actual: &DateTime<TZ1>,
inverted: bool,
representation: &D,
format: &DiffFormat,
) -> String {
let not = if inverted { "not " } else { "" };
let (marked_actual, marked_expected) =
mark_diff(actual, &self.expected, representation, format);
let represented_expected = Represented::from((&self.expected, representation));
let represented_margin = Represented::from((&self.margin, representation));
format!(
r"expected {expression} to be {not}close to {represented_expected:?} within {represented_margin}
but was: {marked_actual}
expected: {marked_expected}"
)
}
}

impl<TZ> Invertible for IsCloseTo<DateTime<TZ>, TemporalMargin> where TZ: TimeZone {}

#[cfg(test)]
mod tests;
Loading
Loading