More tests
continuous-integration/drone/push Build is passing Details

This commit is contained in:
projectmoon 2024-03-27 12:36:42 +01:00
parent 3a4f7ba3a4
commit 1fe590925b
3 changed files with 136 additions and 6 deletions

View File

@ -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;
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);

View File

@ -224,8 +224,31 @@ pub struct GemfeedEntry {
body: OnceCell<String>,
}
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<GemfeedEntry> {
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 <dyn>
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 <dyn>
=> 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#"

View File

@ -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 <dyn>
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());
}
}