diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-09-16 12:16:44 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-09-16 12:16:44 +0200 |
commit | f36ded862f9b5a0c60cc057cab1a249c195832c4 (patch) | |
tree | 5771167d2c9969e5a20927194b96904c6d534ca2 /src/timetype.rs | |
parent | 4798795c5717b03bd21d0e7e50a3be06674101eb (diff) |
Add tests to test end-of-day
Diffstat (limited to 'src/timetype.rs')
-rw-r--r-- | src/timetype.rs | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/src/timetype.rs b/src/timetype.rs index fe4a100..d7585b1 100644 --- a/src/timetype.rs +++ b/src/timetype.rs @@ -2787,3 +2787,224 @@ mod test_end_of_month { } } + + +#[cfg(test)] +mod test_end_of_day { + use super::TimeType as TT; + use chrono::NaiveDate; + use chrono::Timelike; + use chrono::Datelike; + + macro_rules! generate_test_moment_operator_amount_and_end_of_day { + { + 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_day().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_day { + { + name = $name:ident; + base = $base:expr; + amount = $amount:expr; + expected = $exp:expr; + } => { + generate_test_moment_operator_amount_and_end_of_day! { + name = $name; + base = $base; + amount = $amount; + expected = $exp; + operator = |base, amount| base + amount; + } + } + } + + macro_rules! generate_test_moment_minus_amount_and_end_of_day { + { + name = $name:ident; + base = $base:expr; + amount = $amount:expr; + expected = $exp:expr; + } => { + generate_test_moment_operator_amount_and_end_of_day! { + name = $name; + base = $base; + amount = $amount; + expected = $exp; + operator = |base, amount| base - amount; + } + } + } + + // + // tests + // + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_plus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_minus_amount_and_end_of_day! { + 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(23, 59, 59); + } + + generate_test_moment_minus_amount_and_end_of_day! { + 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_day! { + 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(23, 59, 59); + } + +} |