From 1fe590925bc4e1afd94d56a1f0ca69303704806f Mon Sep 17 00:00:00 2001 From: projectmoon Date: Wed, 27 Mar 2024 12:36:42 +0100 Subject: [PATCH] More tests --- src/commands/sync.rs | 17 ++++--- src/gemfeed.rs | 104 +++++++++++++++++++++++++++++++++++++++++++ src/wf.rs | 21 +++++++++ 3 files changed, 136 insertions(+), 6 deletions(-) diff --git a/src/commands/sync.rs b/src/commands/sync.rs index fc24eb0..cb7e383 100644 --- a/src/commands/sync.rs +++ b/src/commands/sync.rs @@ -94,13 +94,18 @@ async fn sync_gemlog( let mut count = 0; for entry in gemlogs_to_post { - let post = wf.create_post(entry).await?; + let result = wf.create_post(entry).await; count += 1; - println!( - "Created post: {} [title={}]", - post.id, - post.title.unwrap_or_default() - ); + + if let Ok(post) = result { + println!( + "Created post: {} [title={}]", + post.id, + post.title.unwrap_or_default() + ); + } else { + println!("Error creating post: {} ", result.unwrap_err()); + } } println!("Post synchronization complete [posts synced={}]", count); diff --git a/src/gemfeed.rs b/src/gemfeed.rs index 355c93e..681d1ee 100644 --- a/src/gemfeed.rs +++ b/src/gemfeed.rs @@ -224,8 +224,31 @@ pub struct GemfeedEntry { body: OnceCell, } +impl Default for GemfeedEntry { + fn default() -> Self { + GemfeedEntry { + body: OnceCell::default(), + title: String::default(), + slug: String::default(), + url: Url::parse("gemini://example.com").unwrap(), + published: Option::default(), + } + } +} + #[allow(dead_code)] impl GemfeedEntry { + /// Consumes self to forcibly set body to the given string. + pub fn with_body(self, body: String) -> GemfeedEntry { + GemfeedEntry { + title: self.title, + slug: self.slug, + published: self.published, + url: self.url, + body: OnceCell::from(body) + } + } + pub fn from_gemtext(base_url: &Url, node: &GemtextNode) -> Result { let link = GemfeedLink::try_from(node)?; // Gemfeeds have only the date--according to spec, it should @@ -405,6 +428,63 @@ impl TryFrom<&AtomEntry> for GemfeedLink { } } +#[cfg(test)] +mod gemfeed_entry_tests { + use super::*; + + #[test] + fn parse_markdown_with_gt_lt_title() -> Result<()> { + let gemtext: String = r#" + # This is gemtext + + With a > in it. + "# + .lines() + .map(|line| line.trim_start()) + .map(|line| format!("{}\n", line)) + .collect(); + + let entry = GemfeedEntry { + published: None, + slug: "".to_string(), + title: "".to_string(), + url: Url::parse("gemini://example.com")?, + body: OnceCell::from(gemtext), + }; + + let result = entry.body_as_markdown(); + assert!(result.is_ok()); + + Ok(()) + } + + #[test] + fn parse_markdown_with_gt_lt() -> Result<()> { + let gemtext: String = r#" + # This is gemtext + + With a < in > it. + "# + .lines() + .map(|line| line.trim_start()) + .map(|line| format!("{}\n", line)) + .collect(); + + let entry = GemfeedEntry { + published: None, + slug: "".to_string(), + title: "".to_string(), + url: Url::parse("gemini://example.com")?, + body: OnceCell::from(gemtext), + }; + + let result = entry.body_as_markdown(); + assert!(result.is_ok()); + + Ok(()) + } +} + #[cfg(test)] mod gemfeed_tests { use super::*; @@ -585,6 +665,30 @@ mod gemfeed_tests { Ok(()) } + #[test] + fn parse_gemfeed_handles_gt_lt() -> Result<()> { + let gemfeed: String = r#" + # My Gemfeed + + This is a gemfeed. + + ## Posts + + => post2.gmi 2023-03-05 Post 2 + => post1.gmi 2023-02-01 Post 1 > + "# + .lines() + .map(|line| line.trim_start()) + .map(|line| format!("{}\n", line)) + .collect(); + + let base_url = Url::parse("gemini://example.com/posts")?; + let ast = GemtextAst::from_string(gemfeed); + let results = parse_gemfeed(&base_url, &ast)?; + assert_eq!(results.len(), 2); + Ok(()) + } + #[test] fn convert_gemfeed_links_success() -> Result<()> { let gemfeed_links: String = r#" diff --git a/src/wf.rs b/src/wf.rs index 5c18251..250824d 100644 --- a/src/wf.rs +++ b/src/wf.rs @@ -99,3 +99,24 @@ impl TryFrom<&GemfeedEntry> for PostCreateRequest { Ok(req) } } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn tryfrom_to_request_handles_gt_lt() { + let gemtext: String = r#" + # This is gemtext + + With a > in it. + "# + .lines() + .map(|line| line.trim_start()) + .map(|line| format!("{}\n", line)) + .collect(); + + let entry = GemfeedEntry::default().with_body(gemtext); + let result = PostCreateRequest::try_from(entry); + assert!(result.is_ok()); + } +}