Add DatedSessionIter unit tests
This commit is contained in:
parent
bc3c4dedf9
commit
6208e5d1dc
@ -172,6 +172,12 @@ pub struct Cancellation {
|
||||
pub note: Note,
|
||||
}
|
||||
|
||||
impl Cancellation {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl_opt_noted!(Cancellation);
|
||||
impl_with_note!(Cancellation);
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
use crate::day::Day;
|
||||
use crate::session::rule::SessionRuleLike;
|
||||
use crate::session::{
|
||||
Alternation, Cancellation, Dated, Except, ExtraSession, OptNoted, RegularSession, Session,
|
||||
};
|
||||
@ -28,6 +29,7 @@ impl DatedSessionIter {
|
||||
Session::Regular(session) => session
|
||||
.except
|
||||
.iter()
|
||||
.filter(|(&day, _)| session.rule.accepts(day))
|
||||
.map(|(&day, except)| match except {
|
||||
Except::Alternation(alternation) => DatedSession::Altered {
|
||||
day,
|
||||
@ -149,16 +151,14 @@ impl OptNoted for DatedSession {
|
||||
mod test {
|
||||
use crate::session::iter::{DatedSession, DatedSessionIter};
|
||||
use crate::session::rule::WeekdayOfMonth;
|
||||
use crate::session::{RegularSession, WithNote};
|
||||
use crate::session::{Alternation, Cancellation, ExtraSession, RegularSession, WithNote};
|
||||
use crate::test_util::{date, day};
|
||||
use chrono::Weekday;
|
||||
|
||||
#[test]
|
||||
fn test_regular() {
|
||||
let tue_session =
|
||||
RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue)).with_note("18:30 Uhr");
|
||||
let sun_session =
|
||||
RegularSession::from(WeekdayOfMonth::new(1, Weekday::Sun)).with_note("10:00 Uhr");
|
||||
let tue_session = RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue));
|
||||
let sun_session = RegularSession::from(WeekdayOfMonth::new(1, Weekday::Sun));
|
||||
let mut iter = DatedSessionIter::new(
|
||||
date(17, 2, 2025),
|
||||
[tue_session.clone().into(), sun_session.clone().into()],
|
||||
@ -186,4 +186,132 @@ mod test {
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_old() {
|
||||
let extra_session = ExtraSession::from(day(15, 2, 2025));
|
||||
let regular_session = RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue))
|
||||
.with_note("18:30 Uhr")
|
||||
.except(day(18, 2, 2025), Cancellation::new())
|
||||
.except(day(18, 3, 2025), Alternation::from(day(21, 1, 2025)));
|
||||
|
||||
let mut iter = DatedSessionIter::new(
|
||||
day(19, 2, 2025),
|
||||
[regular_session.clone().into(), extra_session.into()],
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
iter.next(),
|
||||
Some(DatedSession::Regular {
|
||||
day: day(15, 4, 2025),
|
||||
session: regular_session.clone(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_except() {
|
||||
let regular_session = RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue))
|
||||
.except(day(17, 2, 2025), Cancellation::new());
|
||||
|
||||
let mut iter = DatedSessionIter::new(day(17, 2, 2025), [regular_session.clone().into()]);
|
||||
|
||||
assert_eq!(
|
||||
iter.next(),
|
||||
Some(DatedSession::Regular {
|
||||
day: day(18, 2, 2025),
|
||||
session: regular_session.clone()
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extra_altered_and_canceled() {
|
||||
//an extra session on the same day as a tuesday session
|
||||
let extra_session = ExtraSession::from(day(18, 2, 2025)).with_note("morning session");
|
||||
//an alternation of a tuesday session without moving it
|
||||
let (note_alternation_day, note_alternation) = (
|
||||
day(18, 3, 2025),
|
||||
Alternation {
|
||||
note: Some("a little different today".into()),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
let tue_session = RegularSession::from(WeekdayOfMonth::new(3, Weekday::Tue))
|
||||
.except(note_alternation_day, note_alternation.clone());
|
||||
//a sunday session moved in front of a tuesday session
|
||||
let moved_sun_session_day = day(16, 2, 2025);
|
||||
let (day_alternation_day, day_alternation) =
|
||||
(day(2, 3, 2025), Alternation::from(moved_sun_session_day));
|
||||
//a canceled sunday session
|
||||
let (cancel_day, cancellation) = (day(6, 4, 2025), Cancellation::new());
|
||||
let sun_session = RegularSession::from(WeekdayOfMonth::new(1, Weekday::Sun))
|
||||
.except(day_alternation_day, day_alternation.clone())
|
||||
.except(cancel_day, cancellation.clone());
|
||||
|
||||
let mut iter = DatedSessionIter::new(
|
||||
day(15, 2, 2025),
|
||||
[
|
||||
extra_session.clone().into(),
|
||||
tue_session.clone().into(),
|
||||
sun_session.clone().into(),
|
||||
],
|
||||
);
|
||||
|
||||
//at first comes the moved sunday
|
||||
assert_eq!(
|
||||
iter.next(),
|
||||
Some(DatedSession::Altered {
|
||||
day: day_alternation_day,
|
||||
regular: sun_session.clone(),
|
||||
cause: day_alternation.clone(),
|
||||
})
|
||||
);
|
||||
//then comes the extra session on tuesday (because it was first in the list)
|
||||
assert_eq!(
|
||||
iter.next(),
|
||||
Some(DatedSession::Extra(extra_session.clone()))
|
||||
);
|
||||
//after that comes a regular tuesday session
|
||||
assert_eq!(
|
||||
iter.next(),
|
||||
Some(DatedSession::Regular {
|
||||
day: day(18, 2, 2025),
|
||||
session: tue_session.clone()
|
||||
})
|
||||
);
|
||||
//now we have an altered but not moved tuesday session
|
||||
assert_eq!(
|
||||
iter.next(),
|
||||
Some(DatedSession::Altered {
|
||||
day: note_alternation_day,
|
||||
regular: tue_session.clone(),
|
||||
cause: note_alternation.clone(),
|
||||
})
|
||||
);
|
||||
//the next sunday session was canceled
|
||||
assert_eq!(
|
||||
iter.next(),
|
||||
Some(DatedSession::Canceled {
|
||||
day: cancel_day,
|
||||
regular: sun_session.clone(),
|
||||
cause: cancellation.clone(),
|
||||
})
|
||||
);
|
||||
//and now we are back to regular sessions
|
||||
assert_eq!(
|
||||
iter.next(),
|
||||
Some(DatedSession::Regular {
|
||||
day: day(15, 4, 2025),
|
||||
session: tue_session.clone()
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
iter.next(),
|
||||
Some(DatedSession::Regular {
|
||||
day: day(4, 5, 2025),
|
||||
session: sun_session.clone()
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user