//! Mandatory test 2: one miss per week keeps the streak; two misses in one week end it; the total never decreases. mod common; use common::*; use liber_habit::{Mark, habit}; use liber_library::Op; use liber_library::library::{DailyKind, HabitGoal}; use proptest::prelude::*; #[test] fn one_miss_a_week_keeps_the_streak() { let mut log = Log::new(); let m = monday(); // Four weeks, each missing one day (Wednesday). for w in 0..4 { for d in 0..7 { if d != 2 { log.read_day(m.plus(w * 7 + d)); } } } let today = m.plus(4 * 7); // the next Monday, not read yet let h = habit(&log.lib(), today, 5); assert_eq!(h.streak_weeks, 5, "four weeks with one miss each, and this week so far"); assert_eq!(h.total_days, 24); assert!(!h.today_met); assert_eq!(h.calendar[0][2], Mark::Forgiven); assert_eq!(h.calendar[4][0], Mark::Today, "today is not a miss"); } #[test] fn a_second_miss_in_a_week_ends_the_streak_but_not_the_total() { let mut log = Log::new(); let m = monday(); for d in 0..14 { if d != 9 && d != 10 { log.read_day(m.plus(d)); } } let today = m.plus(14); let h = habit(&log.lib(), today, 3); assert_eq!(h.streak_weeks, 1, "only this week counts again"); assert_eq!(h.total_days, 12); assert_eq!(h.calendar[1][2], Mark::Forgiven); assert_eq!(h.calendar[1][3], Mark::Missed); } #[test] fn days_before_the_first_day_are_not_misses() { let mut log = Log::new(); let m = monday(); for d in 4..7 { log.read_day(m.plus(d)); } let h = habit(&log.lib(), m.plus(6), 1); assert_eq!(h.streak_weeks, 1); assert_eq!(h.calendar[0][0], Mark::None); assert!(h.today_met); } #[test] fn the_goal_decides_what_a_day_is() { let mut log = Log::new(); let m = monday(); log.read_day(m); // one page, 5 active minutes log.push(Op::HabitGoal { goal: HabitGoal { kind: DailyKind::Minutes, amount: 10, rollover_hour: 3 } }); assert_eq!(habit(&log.lib(), m, 1).total_days, 0, "5 minutes are not 10"); log.push(Op::HabitGoal { goal: HabitGoal { kind: DailyKind::Pages, amount: 1, rollover_hour: 3 } }); assert_eq!(habit(&log.lib(), m, 1).total_days, 1); } proptest! { #![proptest_config(ProptestConfig { cases: 300, ..ProptestConfig::default() })] /// Adding reading, or letting time pass, never lowers the total. #[test] fn the_total_never_decreases(read in prop::collection::vec(any::(), 1..120), extra in 0usize..120, later in 0i64..60) { let m = monday(); let mut log = Log::new(); for (i, r) in read.iter().enumerate() { if *r { log.read_day(m.plus(i as i64)); } } let today = m.plus(read.len() as i64); let before = habit(&log.lib(), today, 1).total_days; prop_assert!(habit(&log.lib(), today.plus(later), 1).total_days >= before); log.read_day(m.plus(extra as i64)); prop_assert!(habit(&log.lib(), today.plus(later), 1).total_days >= before); // And the streak is never more than the weeks since the first day read. let h = habit(&log.lib(), today, 1); let weeks = (today.week().0 - m.week().0) / 7 + 1; prop_assert!(h.streak_weeks as i64 <= weeks); } /// A random history with at most one miss in every week keeps the streak across all its weeks. #[test] fn one_miss_per_week_anywhere_keeps_it(misses in prop::collection::vec(0u8..8, 2..20)) { let m = monday(); let mut log = Log::new(); for (w, miss) in misses.iter().enumerate() { for d in 0..7u8 { if *miss != d { log.read_day(m.plus(w as i64 * 7 + d as i64)); } } } let today = m.plus(misses.len() as i64 * 7); prop_assert_eq!(habit(&log.lib(), today, 1).streak_weeks as usize, misses.len() + 1); } }