| 1 | #![cfg(test)] |
| 2 | #![allow(dead_code)] |
| 3 | |
| 4 | use crate::{core::Tz, RRule, RRuleError, RRuleSet, Unvalidated}; |
| 5 | use chrono::{DateTime, TimeZone}; |
| 6 | use std::fmt::Debug; |
| 7 | |
| 8 | pub fn ymd_hms( |
| 9 | year: i32, |
| 10 | month: u32, |
| 11 | day: u32, |
| 12 | hour: u32, |
| 13 | minute: u32, |
| 14 | second: u32, |
| 15 | ) -> DateTime<Tz> { |
| 16 | Tz::UTC |
| 17 | .with_ymd_and_hms(year, month, day, hour, minute, second) |
| 18 | .unwrap() |
| 19 | } |
| 20 | |
| 21 | pub fn test_recurring_rrule( |
| 22 | rrule: RRule<Unvalidated>, |
| 23 | limited: bool, |
| 24 | dt_start: DateTime<Tz>, |
| 25 | expected_dates: &[DateTime<Tz>], |
| 26 | ) { |
| 27 | let rrule_set = rrule |
| 28 | .build(dt_start) |
| 29 | .map_err(|e| match e { |
| 30 | RRuleError::ParserError(e) => e.to_string(), |
| 31 | RRuleError::ValidationError(e) => e.to_string(), |
| 32 | RRuleError::IterError(e) => e, |
| 33 | }) |
| 34 | .unwrap(); |
| 35 | let res = if limited { |
| 36 | rrule_set.all(u16::MAX).dates |
| 37 | } else { |
| 38 | rrule_set.all_unchecked() |
| 39 | }; |
| 40 | |
| 41 | println!("Actual: {:?}", res); |
| 42 | println!("Expected: {:?}", expected_dates); |
| 43 | assert_eq!( |
| 44 | res.len(), |
| 45 | expected_dates.len(), |
| 46 | "Expected number of returned dates to be equal to the expected" |
| 47 | ); |
| 48 | |
| 49 | for (actual, expected) in res.iter().zip(expected_dates) { |
| 50 | assert_eq!(actual, expected); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | #[allow(clippy::needless_pass_by_value)] |
| 55 | pub fn test_recurring_rrule_set(rrule_set: RRuleSet, expected_dates: &[DateTime<Tz>]) { |
| 56 | let res = rrule_set.all(u16::MAX).dates; |
| 57 | |
| 58 | println!("Actual: {:?}", res); |
| 59 | println!("Expected: {:?}", expected_dates); |
| 60 | assert_eq!( |
| 61 | res.len(), |
| 62 | expected_dates.len(), |
| 63 | "Expected number of returned dates to be equal to the expected" |
| 64 | ); |
| 65 | |
| 66 | for (actual, expected) in res.iter().zip(expected_dates) { |
| 67 | assert_eq!(actual, expected); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /// Print and compare 2 lists of dates and panic it they are not the same. |
| 72 | pub fn check_occurrences<S: AsRef<str> + Debug>(occurrences: &[DateTime<Tz>], expected: &[S]) { |
| 73 | let formatter = |dt: &DateTime<Tz>| -> String { format!(" \"{}\",\n", dt.to_rfc3339()) }; |
| 74 | println!( |
| 75 | "Given: [\n{}]\nExpected: {:#?}", |
| 76 | occurrences.iter().map(formatter).collect::<String>(), |
| 77 | expected |
| 78 | ); |
| 79 | assert_eq!(occurrences.len(), expected.len(), "List sizes don't match"); |
| 80 | for (given, expected) in occurrences.iter().zip(expected.iter()) { |
| 81 | let exp_datetime = DateTime::parse_from_rfc3339(expected.as_ref()).unwrap(); |
| 82 | // Compare items and check if in the same offset/timezone |
| 83 | assert_eq!( |
| 84 | given.to_rfc3339(), |
| 85 | exp_datetime.to_rfc3339(), |
| 86 | "Dates not in same timezone" |
| 87 | ); |
| 88 | } |
| 89 | } |