From 13425b31e402c9c61bf3469c37c49fd82c97ed72 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Fri, 26 Apr 2024 17:17:35 -0300 Subject: [PATCH] test(npf): runtime and debug_assertions to check any error as soon as possible --- Cargo.toml | 1 + src/convert/npf.rs | 102 ++++++++++++++++++++++++++++++++------------- src/utils.rs | 10 ----- 3 files changed, 74 insertions(+), 39 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f141754..46c6503 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,4 @@ url = { version = "2.5.0", features = ["serde"] } [features] default = ["uuid-link-to-mention"] uuid-link-to-mention = [] +npf-runtime-asserts = [] diff --git a/src/convert/npf.rs b/src/convert/npf.rs index 70328b3..c2d7ab0 100644 --- a/src/convert/npf.rs +++ b/src/convert/npf.rs @@ -26,6 +26,38 @@ pub enum NPFConvertError { InvalidURL { url: String, err: url::ParseError }, } +#[cfg(any(feature = "npf-runtime-asserts", test, debug_assertions))] +macro_rules! assert_npf_eq_node_text { + ($b:expr, $n:expr) => { + match ($b, $n) { + (left, right) => { + let npf_text = { + let text = RefCell::new(String::new()); + left.clone().for_each_content(|c| { + if let BlockValue::Text(t) = c { + text.borrow_mut().push_str(&t.text); + } + }); + let r = text.borrow().to_string(); + r + }; + let markdown_text = { + let text = RefCell::new(String::new()); + crate::utils::iter_nodes(right, &|node| match &node.data.borrow().value { + NodeValue::Text(t) => text.borrow_mut().push_str(&t), + NodeValue::SoftBreak => text.borrow_mut().push_str(" "), + NodeValue::LineBreak => text.borrow_mut().push_str("\n"), + _ => (), + }); + let r = text.borrow().to_string(); + r + }; + assert_eq!(npf_text, markdown_text); + } + }; + }; +} + impl<'a> TryFrom>> for objects::Post { type Error = NPFConvertError; fn try_from(mut nodes: Children<'a, RefCell>) -> Result { @@ -50,36 +82,49 @@ impl<'a> TryFrom<&'a Node<'a, RefCell>> for objects::Post { let mut post = Self::new(0); let block_text = BlockText::from(String::from(t.clone())); post.content.push(BlockValue::Text(block_text)); + assert_npf_eq_node_text!(&post, &node); Ok(post) } - NodeValue::Strong => Ok(Self::try_from(node.children())? - .fold_content() - .for_each_content(|c| { - if let BlockValue::Text(ref mut t) = c { - let format = FormatValue::Bold(FormatTypeBold::from(&t.text)); - t.push_formatting(format); - // t.text = String::from(t.text.trim()); - } - })), - NodeValue::Emph => Ok(Self::try_from(node.children())? - .fold_content() - .for_each_content(|c| { - if let BlockValue::Text(ref mut t) = c { - let format = FormatValue::Italic(FormatTypeItalic::from(&t.text)); - t.push_formatting(format); - // t.text = String::from(t.text.trim()); - } - })), - NodeValue::Strikethrough => Ok(Self::try_from(node.children())? - .fold_content() - .for_each_content(|c| { - if let BlockValue::Text(ref mut t) = c { - let format = - FormatValue::StrikeThrough(FormatTypeStrikeThrough::from(&t.text)); - t.push_formatting(format); - // t.text = String::from(t.text.trim()); - } - })), + NodeValue::Strong => { + let strong = Self::try_from(node.children())? + .fold_content() + .for_each_content(|c| { + if let BlockValue::Text(ref mut t) = c { + let format = FormatValue::Bold(FormatTypeBold::from(&t.text)); + t.push_formatting(format); + } + }); + + assert_npf_eq_node_text!(&strong, &node); + Ok(strong) + } + NodeValue::Emph => { + let italic = Self::try_from(node.children())? + .fold_content() + .for_each_content(|c| { + if let BlockValue::Text(ref mut t) = c { + let format = FormatValue::Italic(FormatTypeItalic::from(&t.text)); + t.push_formatting(format); + } + }); + + assert_npf_eq_node_text!(&italic, &node); + Ok(italic) + } + NodeValue::Strikethrough => { + let strike_through = Self::try_from(node.children())? + .fold_content() + .for_each_content(|c| { + if let BlockValue::Text(ref mut t) = c { + let format = + FormatValue::StrikeThrough(FormatTypeStrikeThrough::from(&t.text)); + t.push_formatting(format); + } + }); + + assert_npf_eq_node_text!(&strike_through, &node); + Ok(strike_through) + } NodeValue::Link(link) => { let content = Self::try_from(node.children())?.fold_content(); @@ -101,7 +146,6 @@ impl<'a> TryFrom<&'a Node<'a, RefCell>> for objects::Post { let mut format = FormatTypeLink::from(&t.text); format.url = url.clone(); t.push_formatting(FormatValue::Link(format)); - // t.text = String::from(t.text.trim()); } })), Err(err) => Err(NPFConvertError::InvalidURL { diff --git a/src/utils.rs b/src/utils.rs index 5924172..af73b15 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -74,13 +74,3 @@ where None } -pub fn extract_text<'a>(node: &'a comrak::nodes::AstNode<'a>) -> String { - let text = RefCell::new(String::new()); - iter_nodes(node, &|node| { - if let NodeValue::Text(t) = &node.data.borrow().value { - text.borrow_mut().push_str(&t); - } - }); - let r = text.borrow().to_string(); - r -}