From 9545d88f026d793a74b3b26c5ec7c085311eeb61 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 18 Dec 2018 14:21:06 +0100 Subject: Move common test code into function. --- src/main.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0a52dc3..f6aec5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,20 +76,31 @@ fn match_event(event: &ical::parser::ical::component::IcalEvent, pattern: &str) } } +#[cfg(test)] +fn test_match_event_helper(pattern: &str) -> Vec { + let cal = read_icalendar_from_file("test/fixtures/events.ics").unwrap(); + cal[0].events.iter() + .filter_map(|event| { + if match_event(&event, pattern) { + Some(event.clone()) + } + else { + None + } + }) + .collect() +} + #[test] fn match_all_events() { - let cal = read_icalendar_from_file("test/fixtures/events.ics").unwrap(); - let num_events = cal[0].events.len(); - let events:Vec<_> = cal[0].events.iter().filter(|ref event| match_event(&event, "*")).collect(); - assert_eq!(num_events, events.len()); + let events:Vec<_> = test_match_event_helper("*"); + assert_eq!(3, events.len()); } #[test] fn match_events_by_summary() { - let cal = read_icalendar_from_file("test/fixtures/events.ics").unwrap(); - let num_events = cal[0].events.len(); - let events:Vec<_> = cal[0].events.iter().filter(|ref event| match_event(&event, "summary:All")).collect(); - assert_eq!(num_events - 1, events.len()); + let events:Vec<_> = test_match_event_helper("summary:All"); + assert_eq!(2, events.len()); } fn main() { -- cgit v1.2.3