diff options
Diffstat (limited to 'templates/src/lib.rs')
-rw-r--r-- | templates/src/lib.rs | 81 |
1 files changed, 38 insertions, 43 deletions
diff --git a/templates/src/lib.rs b/templates/src/lib.rs index dd263e9..b7b9a24 100644 --- a/templates/src/lib.rs +++ b/templates/src/lib.rs @@ -56,9 +56,8 @@ mod tests { use faker_rand::en_us::internet::Domain; use faker_rand::lorem::Word; use microformats::types::{Document, Item, PropertyValue, Url}; + use rand::distributions::Distribution; use serde_json::json; - use std::cell::RefCell; - use std::rc::Rc; enum PostType { Note, @@ -99,7 +98,17 @@ mod tests { rand::random::<Word>(), rand::random::<Word>() ); - let dt = chrono::offset::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true); + let dt = time::OffsetDateTime::now_utc() + .to_offset( + time::UtcOffset::from_hms( + rand::distributions::Uniform::new(-11, 12) + .sample(&mut rand::thread_rng()), + if rand::random::<bool>() { 0 } else { 30 }, + 0 + ).unwrap() + ) + .format(&time::format_description::well_known::Rfc3339) + .unwrap(); match kind { PostType::Note => { @@ -189,46 +198,38 @@ mod tests { } } - fn check_dt_published(mf2: &serde_json::Value, item: &Rc<RefCell<Item>>) { + fn check_dt_published(mf2: &serde_json::Value, item: &Item) { use microformats::types::temporal::Value as TemporalValue; - let _item = item.borrow(); - let props = _item.properties.borrow(); - assert!(props.contains_key("published")); + assert!(item.properties.contains_key("published")); if let Some(PropertyValue::Temporal(TemporalValue::Timestamp(item))) = - props.get("published").and_then(|v| v.first()) + item.properties.get("published").and_then(|v| v.first()) { - use chrono::{DateTime, FixedOffset, NaiveDateTime}; - // Faithfully reconstruct the original datetime // I wonder why not just have an Enum that would // get you either date, time or a datetime, // potentially with an offset? let offset = item.as_offset().unwrap().data; - let ndt: NaiveDateTime = item.as_date().unwrap().data - .and_time(item.as_time().unwrap().data) - // subtract the offset here, since we will add it back - - offset; - let dt = DateTime::<FixedOffset>::from_utc(ndt, offset); + let date = item.as_date().unwrap().data; + let time = item.as_time().unwrap().data; - let expected: DateTime<FixedOffset> = chrono::DateTime::parse_from_rfc3339( + let dt = date.with_time(time).assume_offset(offset); + let expected = time::OffsetDateTime::parse( mf2["properties"]["published"][0].as_str().unwrap(), - ) - .unwrap(); - + &time::format_description::well_known::Rfc3339 + ).unwrap(); + assert_eq!(dt, expected); } else { unreachable!() } } - fn check_e_content(mf2: &serde_json::Value, item: &Rc<RefCell<Item>>) { - let _item = item.borrow(); - let props = _item.properties.borrow(); - assert!(props.contains_key("content")); + fn check_e_content(mf2: &serde_json::Value, item: &Item) { + assert!(item.properties.contains_key("content")); - if let Some(PropertyValue::Fragment(content)) = props.get("content").and_then(|v| v.first()) + if let Some(PropertyValue::Fragment(content)) = item.properties.get("content").and_then(|v| v.first()) { assert_eq!( content.html, @@ -240,7 +241,6 @@ mod tests { } #[test] - #[ignore = "see https://gitlab.com/maxburon/microformats-parser/-/issues/7"] fn test_note() { let mf2 = gen_random_post(&rand::random::<Domain>().to_string(), PostType::Note); @@ -253,9 +253,8 @@ mod tests { .unwrap(); let parsed: Document = microformats::from_html(&html, url.clone()).unwrap(); - if let Some(PropertyValue::Item(item)) = parsed.get_item_by_url(&url) { - let _item = item.borrow(); - let props = _item.properties.borrow(); + if let Some(item) = parsed.into_iter().find(|i| i.properties.get("url").unwrap().contains(&PropertyValue::Url(url.clone()))) { + let props = &item.properties; check_e_content(&mf2, &item); check_dt_published(&mf2, &item); @@ -284,21 +283,20 @@ mod tests { .unwrap(); let parsed: Document = microformats::from_html(&html, url.clone()).unwrap(); - if let Some(PropertyValue::Item(item)) = parsed.get_item_by_url(&url) { - let _item = item.borrow(); - let props = _item.properties.borrow(); + if let Some(item) = parsed.into_iter().find(|i| i.properties.get("url").unwrap().contains(&PropertyValue::Url(url.clone()))) { check_e_content(&mf2, &item); check_dt_published(&mf2, &item); - assert!(props.contains_key("uid")); - assert!(props.contains_key("url")); - assert!(props + assert!(item.properties.contains_key("uid")); + assert!(item.properties.contains_key("url")); + assert!(item + .properties .get("url") .unwrap() .iter() - .any(|i| i == props.get("uid").and_then(|v| v.first()).unwrap())); - assert!(props.contains_key("name")); - if let Some(PropertyValue::Plain(name)) = props.get("name").and_then(|v| v.first()) { + .any(|i| i == item.properties.get("uid").and_then(|v| v.first()).unwrap())); + assert!(item.properties.contains_key("name")); + if let Some(PropertyValue::Plain(name)) = item.properties.get("name").and_then(|v| v.first()) { assert_eq!( name, mf2.pointer("/properties/name/0") @@ -337,13 +335,10 @@ mod tests { let html = crate::mf2::Entry { post: &mf2 }.to_string(); let parsed: Document = microformats::from_html(&html, url.clone()).unwrap(); - if let Some(item) = parsed.items.get(0) { - let _item = item.borrow(); - let props = _item.properties.borrow(); - + if let Some(item) = parsed.items.first() { check_dt_published(&mf2, item); - assert!(props.contains_key("like-of")); - match props.get("like-of").and_then(|v| v.first()) { + assert!(item.properties.contains_key("like-of")); + match item.properties.get("like-of").and_then(|v| v.first()) { Some(PropertyValue::Url(url)) => { assert_eq!( url, |