use std::time::{Duration, SystemTime};
use crate::core::Matcher;
use crate::matchers::time::ApproxEqTimeMatcher;
use super::MismatchFormat;
pub fn approx_eq_time<'a>(
expected: SystemTime,
threshold: Duration,
) -> Matcher<'a, SystemTime, SystemTime> {
Matcher::new(
ApproxEqTimeMatcher::new(expected, threshold),
MismatchFormat::new("to approximately equal", "to not approximately equal"),
)
}
#[cfg(test)]
mod tests {
use crate::{approx_eq_time, expect};
use std::time::{Duration, SystemTime};
fn threshold() -> Duration {
Duration::from_millis(1)
}
fn actual() -> SystemTime {
SystemTime::UNIX_EPOCH
}
fn expected() -> SystemTime {
SystemTime::UNIX_EPOCH + threshold()
}
fn not_expected() -> SystemTime {
SystemTime::UNIX_EPOCH + (threshold() * 2)
}
#[test]
fn succeeds_when_approx_eq() {
expect!(actual()).to(approx_eq_time(expected(), threshold()));
}
#[test]
fn succeeds_when_not_approx_eq() {
expect!(actual()).to_not(approx_eq_time(not_expected(), threshold()));
}
#[test]
#[should_panic]
fn fails_when_approx_eq() {
expect!(actual()).to_not(approx_eq_time(expected(), threshold()));
}
#[test]
#[should_panic]
fn fails_when_not_approx_eq() {
expect!(actual()).to(approx_eq_time(not_expected(), threshold()));
}
}