Skip to content

Commit

Permalink
test: verify section 3.3.11 text escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodie committed Jul 7, 2024
1 parent 38d992c commit c0ffee1
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/custom_property_parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let event = Event::new()
.summary("test event")
.append_property(
"TEST;IMPORTANCE=very;DUE=tomorrow:FOOBAR\n"
r#"TEST;IMPORTANCE=very;DUE=tomorrow:FOOBAR;COMPLEX=\n"#
.parse::<Property>()
.unwrap(),
)
Expand Down
13 changes: 8 additions & 5 deletions examples/full_circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ use icalendar::*;

fn main() {
let event = Event::new()
.summary("test event")
.description("here I have something really important to do")
.summary(";IMPORTANCE=very;test event")
.description("here; I have something; really important to do")
.starts(Utc::now())
.class(Class::Confidential)
.ends(Utc::now() + Duration::days(1))
.append_property(
Property::new("TEST", "FOOBAR")
.add_parameter("IMPORTANCE", "very")
.add_parameter("DUE", "tomorrow")
.add_parameter("COMPLEX", r#"this is code; I think"#)
.add_parameter("keyval", "color:red")
.add_parameter("CODE", "this is code; so I quote")
.done(),
)
.uid("my.own.id")
Expand All @@ -34,10 +37,10 @@ fn main() {

println!("{}", &ical); // print what we built
println!("{}", from_parsed); // print what parsed
println!("{:#?}", built_calendar); // inner representation of what we built
println!("{:#?}", from_parsed); // inner representation of what we built and then parsed
println!("built calendar:\n{:#?}", built_calendar); // inner representation of what we built
println!("from parsed:\n{:#?}", from_parsed); // inner representation of what we built and then parsed
println!(
"{:#?}",
"read_calenar:\n{:#?}",
parser::read_calendar(&parser::unfold(&ical)).unwrap()
); // inner presentation of the parser's data structure
}
109 changes: 109 additions & 0 deletions tests/full_circle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#![cfg(feature = "parser")]
use std::str::FromStr;

use chrono::*;
use icalendar::*;
// use pretty_assertions::assert_eq;

fn get_summary(calendar: &Calendar) -> &str {
calendar.components[0]
.as_event()
.unwrap()
.get_summary()
.unwrap()
}
fn get_description(calendar: &Calendar) -> &str {
calendar.components[0]
.as_event()
.unwrap()
.get_description()
.unwrap()
}

fn init_test_event() -> (&'static str, &'static str, Calendar) {
let summary = ";IMPORTANCE=very;test event";
let description = "this, contains: many escapeworthy->\n<-;characters";

let event = Event::new()
.summary(summary)
.description(description)
.starts(Utc::now())
.class(Class::Confidential)
.ends(Utc::now() + Duration::days(1))
.append_property(
Property::new("TEST", "FOOBAR")
.add_parameter("IMPORTANCE", "very")
.add_parameter("COMPLEX", r#"this is code; I think"#)
.add_parameter("keyval", "color:red")
.done(),
)
.uid("my.own.id")
.done();

let todo = Todo::new().summary("Buy some: milk").done();

let mut built_calendar = Calendar::new();
built_calendar.push(event);
built_calendar.push(todo);
(summary, description, built_calendar)
}

#[test]
#[ignore = "equality difficult"]
fn serializes_correctly() {
let (_, _, built_calendar) = init_test_event();
println!("built calendar:\n{:#?}", built_calendar); // inner representation of what we built

let serialized = built_calendar.to_string();
println!("serialized: {}", &serialized); // print what we built

let from_parsed = Calendar::from_str(&serialized).unwrap();
println!("parsed again:\n{:#?}", from_parsed); // inner representation of what we built and then parsed

assert_eq!(built_calendar, from_parsed)
}

#[test]
fn escape_late() {
let (summary, description, built_calendar) = init_test_event();
println!("built calendar:\n{:#?}", built_calendar); // inner representation of what we built

let serialized = built_calendar.to_string();
println!("serialized: {}", &serialized); // print what we built

let from_parsed = Calendar::from_str(&serialized).unwrap();
println!("parsed again:\n{:#?}", from_parsed); // inner representation of what we built and then parsed

// these should not be escaped
assert_eq!(get_summary(&built_calendar), summary);
assert_eq!(get_description(&built_calendar), description);
}

#[test]
fn unescape_text() {
let (summary, description, built_calendar) = init_test_event();
println!("built calendar:\n{:#?}", built_calendar); // inner representation of what we built

let serialized = built_calendar.to_string();
println!("serialized:\n {}", &serialized); // print what we built

let from_parsed = Calendar::from_str(&serialized).unwrap();
println!("parsed again:\n{:#?}", from_parsed); // inner representation of what we built and then parsed

assert_eq!(get_summary(&from_parsed), summary);
assert_eq!(get_description(&from_parsed), description);
}

#[test]
fn reparse_equivalence() {
let (_summary, _description, built_calendar) = init_test_event();
println!("built calendar:\n{:#?}", built_calendar); // inner representation of what we built

let serialized = built_calendar.to_string();
println!("serialized: {}", &serialized); // print what we built

let from_parsed = Calendar::from_str(&serialized).unwrap();
println!("parsed again:\n{:#?}", from_parsed); // inner representation of what we built and then parsed

assert_eq!(get_summary(&built_calendar), get_summary(&from_parsed),);
}
2 changes: 1 addition & 1 deletion tests/reserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ END:VTODO\r
BEGIN:VALARM\r
TRIGGER;RELATED=END:-P2D\r
ACTION:EMAIL\r
ATTENDEE:mailto:john_doe@example.com\r
ATTENDEE:\"mailto:john_doe@example.com\"\r
SUMMARY:*** REMINDER: SEND AGENDA FOR WEEKLY STAFF MEETING ***\r
DESCRIPTION:A draft agenda needs to be sent out to the attendees to the wee\r
kly managers meeting (MGR-LIST). Attached is a pointer the document templat\r
Expand Down

0 comments on commit c0ffee1

Please sign in to comment.