From 134654ba25761984fc2c4b9e55975e2fbfca5d8e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 16 Sep 2017 12:24:01 +0200 Subject: Add test for end-of-minute --- src/timetype.rs | 220 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 220 insertions(+) diff --git a/src/timetype.rs b/src/timetype.rs index dbe7564..b0a5967 100644 --- a/src/timetype.rs +++ b/src/timetype.rs @@ -3229,3 +3229,223 @@ mod test_end_of_hour { } +#[cfg(test)] +mod test_end_of_minute { + use super::TimeType as TT; + use chrono::NaiveDate; + use chrono::Timelike; + use chrono::Datelike; + + macro_rules! generate_test_moment_operator_amount_and_end_of_minute { + { + name = $name:ident; + base = $base:expr; + amount = $amount:expr; + expected = $exp:expr; + operator = $op:expr; + } => { + #[test] + fn $name() { + let base = TT::moment($base); + let result = $op(base, $amount).end_of_minute().calculate(); + assert!(result.is_ok(), "Operation failed: {:?}", result); + let result = result.unwrap(); + let expected = $exp; + + assert_eq!(expected, *result.get_moment().unwrap()); + } + } + } + + macro_rules! generate_test_moment_plus_amount_and_end_of_minute { + { + name = $name:ident; + base = $base:expr; + amount = $amount:expr; + expected = $exp:expr; + } => { + generate_test_moment_operator_amount_and_end_of_minute! { + name = $name; + base = $base; + amount = $amount; + expected = $exp; + operator = |base, amount| base + amount; + } + } + } + + macro_rules! generate_test_moment_minus_amount_and_end_of_minute { + { + name = $name:ident; + base = $base:expr; + amount = $amount:expr; + expected = $exp:expr; + } => { + generate_test_moment_operator_amount_and_end_of_minute! { + name = $name; + base = $base; + amount = $amount; + expected = $exp; + operator = |base, amount| base - amount; + } + } + } + + // + // tests + // + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_zero_seconds; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::seconds(0); + expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_seconds; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::seconds(1); + expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_too_much_seconds; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::seconds(62); + expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 1, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_minutes; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::minutes(2); + expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 2, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_too_much_minutes; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::minutes(65); + expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(1, 5, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_minutes_in_seconds; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::seconds(62); + expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 1, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_months; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::months(14); + expected = NaiveDate::from_ymd(2001, 3, 1).and_hms(0, 0, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_years; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::years(62); + expected = NaiveDate::from_ymd(2062, 1, 1).and_hms(0, 0, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_more_than_one_year; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::years(1) + TT::months(1); + expected = NaiveDate::from_ymd(2001, 2, 1).and_hms(0, 0, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_more_than_one_month; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + + // As we calculate 1 month + 1 day first, we end up adding 31 days to the base + amount = TT::months(1) + TT::days(1); + + // and therefor this results in the date 2000-02-01 + // This is not that inuitive, of course. + expected = NaiveDate::from_ymd(2000, 2, 1).and_hms(0, 0, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_more_than_one_day; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::days(1) + TT::hours(1); + expected = NaiveDate::from_ymd(2000, 1, 2).and_hms(1, 0, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_more_than_one_hour; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::hours(1) + TT::minutes(1); + expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(1, 1, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_more_than_one_minute; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::minutes(1) + TT::seconds(1); + expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 1, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_invalid_months; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::months(13); + expected = NaiveDate::from_ymd(2001, 2, 1).and_hms(0, 0, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_invalid_days; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::days(31); + expected = NaiveDate::from_ymd(2000, 2, 1).and_hms(0, 0, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_invalid_hours; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::hours(25); + expected = NaiveDate::from_ymd(2000, 1, 2).and_hms(1, 0, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_invalid_minutes; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::minutes(61); + expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(1, 1, 59); + } + + generate_test_moment_plus_amount_and_end_of_minute! { + name = test_moment_plus_invalid_seconds; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::seconds(61); + expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 1, 59); + } + + generate_test_moment_minus_amount_and_end_of_minute! { + name = test_moment_minus_nothing; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::seconds(0); + expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 59); + } + + generate_test_moment_minus_amount_and_end_of_minute! { + name = test_moment_minus_seconds; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::seconds(1); + expected = NaiveDate::from_ymd(1999, 12, 31).and_hms(23, 59, 59); + } + + generate_test_moment_minus_amount_and_end_of_minute! { + name = test_moment_minus_months; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::months(12); + expected = NaiveDate::from_ymd(1999, 1, 1).and_hms(0, 0, 59); + } + +} + -- cgit v1.2.3