From 75fe00983d10be5208c738cfe9340a216d9d3951 Mon Sep 17 00:00:00 2001 From: daladim Date: Fri, 5 Nov 2021 23:31:42 +0100 Subject: [PATCH] Added a parsing/building round-trip test --- src/ical/mod.rs | 41 +++++++++++++++++++++++ tests/assets/ical_with_unknown_fields.ics | 20 +++++++++++ 2 files changed, 61 insertions(+) create mode 100644 tests/assets/ical_with_unknown_fields.ics diff --git a/src/ical/mod.rs b/src/ical/mod.rs index a454ae1..2f5cecc 100644 --- a/src/ical/mod.rs +++ b/src/ical/mod.rs @@ -13,3 +13,44 @@ pub fn default_prod_id() -> String { format!("-//{}//{}//EN", ORG_NAME, PRODUCT_NAME) } + + +#[cfg(test)] +mod tests { + use super::*; + + use std::collections::HashSet; + use crate::item::SyncStatus; + + #[test] + fn test_ical_round_trip_serde() { + let ical_with_unknown_fields = std::fs::read_to_string("tests/assets/ical_with_unknown_fields.ics").unwrap(); + + let item_id = "http://item.id".parse().unwrap(); + let sync_status = SyncStatus::NotSynced; + let deserialized = parse(&ical_with_unknown_fields, item_id, sync_status).unwrap(); + let serialized = build_from(&deserialized).unwrap(); + assert_same_fields(&ical_with_unknown_fields, &serialized); + } + + /// Assert the properties are present (possibly in another order) + /// RFC5545 "imposes no ordering of properties within an iCalendar object." + fn assert_same_fields(left: &str, right: &str) { + let left_parts: HashSet<&str> = left.split("\r\n").collect(); + let right_parts: HashSet<&str> = right.split("\r\n").collect(); + + // Let's be more explicit than assert_eq!(left_parts, right_parts); + if left_parts != right_parts { + println!("Only in left:"); + for item in left_parts.difference(&right_parts) { + println!(" * {}", item); + } + println!("Only in right:"); + for item in right_parts.difference(&left_parts) { + println!(" * {}", item); + } + + assert_eq!(left_parts, right_parts); + } + } +} diff --git a/tests/assets/ical_with_unknown_fields.ics b/tests/assets/ical_with_unknown_fields.ics new file mode 100644 index 0000000..c1df431 --- /dev/null +++ b/tests/assets/ical_with_unknown_fields.ics @@ -0,0 +1,20 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Todo Corp LTD//Awesome Product ®//EN +BEGIN:VTODO +UID:20f57387-e116-4702-b463-d352aeaf80d0 +X_FAVOURITE_PAINT_FINISH:matte +DTSTAMP:20211103T214742 +CREATED:20211103T212345 +LAST-MODIFIED:20211103T214742 +SUMMARY:This is a task with ÜTF-8 characters +STATUS:NEEDS-ACTION +DUE:20211103T220000 +PRIORITY:6 +PERCENT-COMPLETE:48 +IMAGE;DISPLAY=BADGE;FMTTYPE=image/png;VALUE=URI:http://example.com/images/p + arty.png +CONFERENCE;FEATURE=PHONE;LABEL=Attendee dial-in;VALUE=URI:tel:+1-888-555-04 + 56,,,555123 +END:VTODO +END:VCALENDAR