use recurring_event::{DailyEvent, MonthlyEvent, MonthlyWeekdayOrdinal, Ordinal, Recurrent, WeeklyEvent, YearlyEvent, error::Error, time::{Month, OffsetDateTime, Weekday, macros::datetime}}; /* Daily */ #[test] fn daily_next_date() { let expected = vec![datetime!(2022-12-26 00:00 UTC), datetime!(2022-12-27 00:00 UTC), datetime!(2022-12-28 00:00 UTC), datetime!(2022-12-29 00:00 UTC), datetime!(2022-12-30 00:00 UTC), datetime!(2022-12-31 00:00 UTC), datetime!(2023-01-01 00:00 UTC), datetime!(2023-01-02 00:00 UTC), datetime!(2023-01-03 00:00 UTC), datetime!(2023-01-04 00:00 UTC)]; let date = datetime!(2022-12-25 00:00 UTC); let event = DailyEvent::new(date); let actual: Vec = event.take(10).collect(); assert_eq!(expected, actual, "actual: {:?}", actual) } #[test] fn daily_next_date_frequenced() { let expected = vec![datetime!(2022-12-29 00:00 UTC), datetime!(2023-01-02 00:00 UTC), datetime!(2023-01-06 00:00 UTC), datetime!(2023-01-10 00:00 UTC), datetime!(2023-01-14 00:00 UTC), datetime!(2023-01-18 00:00 UTC), datetime!(2023-01-22 00:00 UTC), datetime!(2023-01-26 00:00 UTC), datetime!(2023-01-30 00:00 UTC), datetime!(2023-02-03 00:00 UTC)]; let date = datetime!(2022-12-25 00:00 UTC); let mut event = DailyEvent::new(date); event.update_frequency(4); let actual: Vec = event.take(10).collect(); assert_eq!(expected, actual, "actual: {:?}", actual) } #[test] fn daily_next_date_from_overflow() { let expected = Error::DateOverflow; let mut event = DailyEvent::new(datetime!(9999-12-31 00:00 UTC)); let actual = event.next_date().unwrap_err(); assert_eq!(expected, actual, "actual: {:?}", actual); } /* Weekly */ #[test] fn weekly_next_date_no_recurrences() { let expected = Error::NoRecurrencesDefined; let date = datetime!(2022-08-12 00:00 UTC); let mut event = WeeklyEvent::new(date); let actual = event.next_date().unwrap_err(); assert_eq!(expected, actual, "actual: {:?}", actual); } #[test] fn weekly_next_date() { let expected = vec![datetime!(2022-12-26 00:00 UTC), datetime!(2022-12-29 00:00 UTC), datetime!(2023-01-02 00:00 UTC), datetime!(2023-01-05 00:00 UTC), datetime!(2023-01-09 00:00 UTC), datetime!(2023-01-12 00:00 UTC), datetime!(2023-01-16 00:00 UTC), datetime!(2023-01-19 00:00 UTC), datetime!(2023-01-23 00:00 UTC), datetime!(2023-01-26 00:00 UTC)]; let date = datetime!(2022-12-25 00:00 UTC); let mut event = WeeklyEvent::new(date); event.insert_weekday(Weekday::Monday); event.insert_weekday(Weekday::Thursday); let actual: Vec = event.take(10).collect(); assert_eq!(expected, actual, "actual: {:?}", actual) } #[test] fn weekly_next_date_frequenced() { let expected = vec![datetime!(2023-01-16 00:00 UTC), datetime!(2023-01-19 00:00 UTC), datetime!(2023-02-13 00:00 UTC), datetime!(2023-02-16 00:00 UTC), datetime!(2023-03-13 00:00 UTC), datetime!(2023-03-16 00:00 UTC), datetime!(2023-04-10 00:00 UTC), datetime!(2023-04-13 00:00 UTC), datetime!(2023-05-08 00:00 UTC), datetime!(2023-05-11 00:00 UTC)]; let date = datetime!(2022-12-25 00:00 UTC); let mut event = WeeklyEvent::new(date); event.update_frequency(4); event.insert_weekday(Weekday::Monday); event.insert_weekday(Weekday::Thursday); let actual: Vec = event.take(10).collect(); assert_eq!(expected, actual, "actual: {:?}", actual) } #[test] fn weekly_next_date_frequenced_last_day_of_week() { let expected = datetime!(2023-01-16 00:00 UTC); let date = datetime!(2022-12-25 00:00 UTC); let mut event = WeeklyEvent::new(date); event.update_frequency(4); event.insert_weekday(Weekday::Monday); event.insert_weekday(Weekday::Thursday); let actual = event.next_date().unwrap(); assert_eq!(expected, actual, "actual: {}", actual.date()); } /* Monthly */ #[test] fn monthly_next_date_no_recurrences() { let expected = Error::NoRecurrencesDefined; let date = datetime!(2022-08-12 00:00 UTC); let mut event = MonthlyEvent::new(date); let actual = event.next_date().unwrap_err(); assert_eq!(expected, actual, "actual: {:?}", actual); } #[test] fn monthly_next_date() { let expected = vec![datetime!(2022-12-26 00:00 UTC), datetime!(2022-12-29 00:00 UTC), datetime!(2023-01-02 00:00 UTC), datetime!(2023-01-05 00:00 UTC), datetime!(2023-01-07 00:00 UTC), datetime!(2023-01-09 00:00 UTC), datetime!(2023-01-10 00:00 UTC), datetime!(2023-01-12 00:00 UTC), datetime!(2023-01-16 00:00 UTC), datetime!(2023-01-19 00:00 UTC), datetime!(2023-01-22 00:00 UTC), datetime!(2023-01-23 00:00 UTC), datetime!(2023-01-25 00:00 UTC), datetime!(2023-01-26 00:00 UTC), datetime!(2023-01-30 00:00 UTC), datetime!(2023-02-02 00:00 UTC), datetime!(2023-02-06 00:00 UTC), datetime!(2023-02-07 00:00 UTC), datetime!(2023-02-09 00:00 UTC), datetime!(2023-02-13 00:00 UTC)]; let date = datetime!(2022-12-25 00:00 UTC); let mut event = MonthlyEvent::new(date); event.insert_weekday(Weekday::Monday); event.insert_weekday(Weekday::Thursday); event.insert_monthly_day_ordinal(7); event.insert_monthly_day_ordinal(25); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Second, Weekday::Tuesday)); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Fourth, Weekday::Sunday)); let actual: Vec = event.take(20).collect(); assert_eq!(expected, actual, "actual: {:?}", actual) } #[test] fn monthly_next_date_frequenced() { let expected = vec![datetime!(2022-12-26 00:00 UTC), datetime!(2022-12-29 00:00 UTC), datetime!(2023-04-03 00:00 UTC), datetime!(2023-04-06 00:00 UTC), datetime!(2023-04-07 00:00 UTC), datetime!(2023-04-10 00:00 UTC), datetime!(2023-04-11 00:00 UTC), datetime!(2023-04-13 00:00 UTC), datetime!(2023-04-17 00:00 UTC), datetime!(2023-04-20 00:00 UTC), datetime!(2023-04-23 00:00 UTC), datetime!(2023-04-24 00:00 UTC), datetime!(2023-04-25 00:00 UTC), datetime!(2023-04-27 00:00 UTC), datetime!(2023-08-03 00:00 UTC), datetime!(2023-08-07 00:00 UTC), datetime!(2023-08-08 00:00 UTC), datetime!(2023-08-10 00:00 UTC), datetime!(2023-08-14 00:00 UTC), datetime!(2023-08-17 00:00 UTC)]; let date = datetime!(2022-12-25 00:00 UTC); let mut event = MonthlyEvent::new(date); event.update_frequency(4); event.insert_weekday(Weekday::Monday); event.insert_weekday(Weekday::Thursday); event.insert_monthly_day_ordinal(7); event.insert_monthly_day_ordinal(25); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Second, Weekday::Tuesday)); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Fourth, Weekday::Sunday)); let actual: Vec = event.take(20).collect(); assert_eq!(expected, actual, "actual: {:?}", actual) } #[test] fn monthly_next_date_frequenced_last_day_of_month() { let expected = datetime!(2023-04-03 00:00 UTC); let date = datetime!(2022-12-31 00:00 UTC); let mut event = MonthlyEvent::new(date); event.update_frequency(4); event.insert_weekday(Weekday::Monday); event.insert_weekday(Weekday::Thursday); event.insert_monthly_day_ordinal(7); event.insert_monthly_day_ordinal(25); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Second, Weekday::Tuesday)); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Fourth, Weekday::Sunday)); let actual = event.next_date().unwrap(); assert_eq!(expected, actual, "actual: {}", actual.date()) } #[test] fn monthly_next_date_frequenced_last() { let expected = vec![datetime!(2022-12-27 00:00 UTC), datetime!(2022-12-29 00:00 UTC), datetime!(2022-12-31 00:00 UTC), datetime!(2023-04-23 00:00 UTC), datetime!(2023-04-25 00:00 UTC), datetime!(2023-04-27 00:00 UTC), datetime!(2023-04-29 00:00 UTC), datetime!(2023-08-27 00:00 UTC), datetime!(2023-08-29 00:00 UTC), datetime!(2023-08-31 00:00 UTC), datetime!(2023-12-24 00:00 UTC), datetime!(2023-12-26 00:00 UTC), datetime!(2023-12-28 00:00 UTC), datetime!(2023-12-30 00:00 UTC), datetime!(2024-04-25 00:00 UTC), datetime!(2024-04-28 00:00 UTC), datetime!(2024-04-30 00:00 UTC), datetime!(2024-08-25 00:00 UTC), datetime!(2024-08-27 00:00 UTC), datetime!(2024-08-29 00:00 UTC), datetime!(2024-08-31 00:00 UTC)]; let date = datetime!(2022-12-25 00:00 UTC); let mut event = MonthlyEvent::new(date); event.update_frequency(4); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Fourth, Weekday::Sunday)); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Fifth, Weekday::Saturday)); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Last, Weekday::Tuesday)); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Last, Weekday::Thursday)); let actual: Vec = event.take(21).collect(); assert_eq!(expected, actual, "actual: {:?}", actual) } /* Yearly */ #[test] fn yearly_next_date_no_months() { let expected = Error::NoRecurrencesDefined; let date = datetime!(2022-08-12 00:00 UTC); let mut event = YearlyEvent::new(date); let actual = event.next_date().unwrap_err(); assert_eq!(expected, actual, "actual: {:?}", actual); } #[test] fn yearly_next_date_no_recurrences() { let expected = Error::NoRecurrencesDefined; let date = datetime!(2022-08-12 00:00 UTC); let mut event = YearlyEvent::new(date); event.insert_weekday(Weekday::Tuesday, Month::February); event.remove_weekday(&Weekday::Tuesday, Month::February); event.insert_monthly_day_ordinal(7, Month::June); event.remove_monthly_day_ordinal(&7, Month::June); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Second, Weekday::Tuesday), Month::October); event.remove_monthly_weekday_ordinal(&MonthlyWeekdayOrdinal::new(Ordinal::Second, Weekday::Tuesday), Month::October); let actual = event.next_date().unwrap_err(); assert_eq!(expected, actual, "actual: {:?}", actual); } #[test] fn yearly_next_date() { let expected = vec![datetime!(2022-02-17 00:00 UTC), datetime!(2022-02-21 00:00 UTC), datetime!(2022-02-24 00:00 UTC), datetime!(2022-02-25 00:00 UTC), datetime!(2022-02-27 00:00 UTC), datetime!(2022-02-28 00:00 UTC), datetime!(2022-08-02 00:00 UTC), datetime!(2022-08-03 00:00 UTC), datetime!(2022-08-06 00:00 UTC), datetime!(2022-08-09 00:00 UTC), datetime!(2022-08-13 00:00 UTC), datetime!(2022-08-16 00:00 UTC), datetime!(2022-08-19 00:00 UTC), datetime!(2022-08-20 00:00 UTC), datetime!(2022-08-23 00:00 UTC), datetime!(2022-08-27 00:00 UTC), datetime!(2022-08-29 00:00 UTC), datetime!(2022-08-30 00:00 UTC), datetime!(2023-02-02 00:00 UTC), datetime!(2023-02-06 00:00 UTC), datetime!(2023-02-07 00:00 UTC), datetime!(2023-02-09 00:00 UTC), datetime!(2023-02-13 00:00 UTC), datetime!(2023-02-14 00:00 UTC), datetime!(2023-02-16 00:00 UTC), datetime!(2023-02-20 00:00 UTC), datetime!(2023-02-23 00:00 UTC), datetime!(2023-02-25 00:00 UTC), datetime!(2023-02-26 00:00 UTC), datetime!(2023-02-27 00:00 UTC), datetime!(2023-08-01 00:00 UTC), datetime!(2023-08-02 00:00 UTC), datetime!(2023-08-05 00:00 UTC), datetime!(2023-08-08 00:00 UTC), datetime!(2023-08-12 00:00 UTC), datetime!(2023-08-13 00:00 UTC), datetime!(2023-08-15 00:00 UTC), datetime!(2023-08-18 00:00 UTC), datetime!(2023-08-19 00:00 UTC), datetime!(2023-08-22 00:00 UTC)]; let date = datetime!(2022-02-14 00:00 UTC); let mut event = YearlyEvent::new(date); event.insert_weekday(Weekday::Monday, Month::February); event.insert_weekday(Weekday::Thursday, Month::February); event.insert_monthly_day_ordinal(7, Month::February); event.insert_monthly_day_ordinal(25, Month::February); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Second, Weekday::Tuesday), Month::February); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Fourth, Weekday::Sunday), Month::February); event.insert_weekday(Weekday::Tuesday, Month::August); event.insert_weekday(Weekday::Saturday, Month::August); event.insert_monthly_day_ordinal(13, Month::August); event.insert_monthly_day_ordinal(29, Month::August); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::First, Weekday::Wednesday), Month::August); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Third, Weekday::Friday), Month::August); let actual: Vec = event.take(40).collect(); assert_eq!(expected, actual, "actual: {:?}", actual) } #[test] fn yearly_next_date_no_occurrence_first_year() { let expected = vec![datetime!(2022-02-03 00:00 UTC), datetime!(2022-02-07 00:00 UTC), datetime!(2022-02-08 00:00 UTC), datetime!(2022-02-10 00:00 UTC), datetime!(2022-02-14 00:00 UTC), datetime!(2022-02-17 00:00 UTC), datetime!(2022-02-21 00:00 UTC), datetime!(2022-02-24 00:00 UTC), datetime!(2022-02-25 00:00 UTC), datetime!(2022-02-27 00:00 UTC), datetime!(2022-02-28 00:00 UTC), datetime!(2022-08-02 00:00 UTC), datetime!(2022-08-03 00:00 UTC), datetime!(2022-08-06 00:00 UTC), datetime!(2022-08-09 00:00 UTC), datetime!(2022-08-13 00:00 UTC), datetime!(2022-08-16 00:00 UTC), datetime!(2022-08-19 00:00 UTC), datetime!(2022-08-20 00:00 UTC), datetime!(2022-08-23 00:00 UTC), datetime!(2022-08-27 00:00 UTC), datetime!(2022-08-29 00:00 UTC), datetime!(2022-08-30 00:00 UTC), datetime!(2023-02-02 00:00 UTC), datetime!(2023-02-06 00:00 UTC), datetime!(2023-02-07 00:00 UTC), datetime!(2023-02-09 00:00 UTC), datetime!(2023-02-13 00:00 UTC), datetime!(2023-02-14 00:00 UTC), datetime!(2023-02-16 00:00 UTC), datetime!(2023-02-20 00:00 UTC), datetime!(2023-02-23 00:00 UTC), datetime!(2023-02-25 00:00 UTC), datetime!(2023-02-26 00:00 UTC), datetime!(2023-02-27 00:00 UTC), datetime!(2023-08-01 00:00 UTC), datetime!(2023-08-02 00:00 UTC), datetime!(2023-08-05 00:00 UTC), datetime!(2023-08-08 00:00 UTC), datetime!(2023-08-12 00:00 UTC)]; let date = datetime!(2021-11-14 00:00 UTC); let mut event = YearlyEvent::new(date); event.insert_weekday(Weekday::Monday, Month::February); event.insert_weekday(Weekday::Thursday, Month::February); event.insert_monthly_day_ordinal(7, Month::February); event.insert_monthly_day_ordinal(25, Month::February); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Second, Weekday::Tuesday), Month::February); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Fourth, Weekday::Sunday), Month::February); event.insert_weekday(Weekday::Tuesday, Month::August); event.insert_weekday(Weekday::Saturday, Month::August); event.insert_monthly_day_ordinal(13, Month::August); event.insert_monthly_day_ordinal(29, Month::August); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::First, Weekday::Wednesday), Month::August); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Third, Weekday::Friday), Month::August); let actual: Vec = event.take(40).collect(); assert_eq!(expected, actual, "actual: {:?}", actual) } #[test] fn yearly_next_date_frequenced_last() { let expected = vec![datetime!(2022-08-26 00:00 UTC), datetime!(2022-08-31 00:00 UTC), datetime!(2026-02-22 00:00 UTC), datetime!(2026-02-24 00:00 UTC), datetime!(2026-08-26 00:00 UTC), datetime!(2026-08-28 00:00 UTC), datetime!(2030-02-24 00:00 UTC), datetime!(2030-02-26 00:00 UTC), datetime!(2030-08-28 00:00 UTC), datetime!(2030-08-30 00:00 UTC), datetime!(2034-02-26 00:00 UTC), datetime!(2034-02-28 00:00 UTC), datetime!(2034-08-25 00:00 UTC), datetime!(2034-08-30 00:00 UTC), datetime!(2038-02-23 00:00 UTC), datetime!(2038-02-28 00:00 UTC), datetime!(2038-08-25 00:00 UTC), datetime!(2038-08-27 00:00 UTC), datetime!(2042-02-23 00:00 UTC), datetime!(2042-02-25 00:00 UTC)]; let date = datetime!(2022-06-14 00:00 UTC); let mut event = YearlyEvent::new(date); event.update_frequency(4); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Last, Weekday::Tuesday), Month::February); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Last, Weekday::Sunday), Month::February); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Last, Weekday::Wednesday), Month::August); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Last, Weekday::Friday), Month::August); let actual: Vec = event.take(20).collect(); assert_eq!(expected, actual) } #[test] fn yearly_next_date_frequenced_last_day_of_month() { let expected = datetime!(2026-02-02 00:00 UTC); let date = datetime!(2022-12-31 00:00 UTC); let mut event = YearlyEvent::new(date); event.update_frequency(4); event.insert_weekday(Weekday::Monday, Month::February); event.insert_weekday(Weekday::Thursday, Month::February); event.insert_monthly_day_ordinal(7, Month::February); event.insert_monthly_day_ordinal(25, Month::February); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Second, Weekday::Tuesday), Month::February); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Fourth, Weekday::Sunday), Month::February); event.insert_weekday(Weekday::Tuesday, Month::August); event.insert_weekday(Weekday::Saturday, Month::August); event.insert_monthly_day_ordinal(13, Month::August); event.insert_monthly_day_ordinal(29, Month::August); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::First, Weekday::Wednesday), Month::August); event.insert_monthly_weekday_ordinal(MonthlyWeekdayOrdinal::new(Ordinal::Third, Weekday::Friday), Month::August); let actual = event.next_date().unwrap(); assert_eq!(expected, actual, "actual: {}", actual.date()) }