test(npf): add basic tests for the conversion
This commit is contained in:
@@ -113,6 +113,264 @@ pub fn from<'a>(node: &'a Node<'a, RefCell<Ast>>) -> Result<objects::Post, NPFCo
|
||||
objects::Post::try_from(node)
|
||||
}
|
||||
|
||||
/*
|
||||
Voluptate laboris sint cupidatat ullamco ut ea consectetur et est culpa et culpa duis
|
||||
*/
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use super::content_blocks::BlockValue;
|
||||
use crate::convert::npf;
|
||||
use crate::convert::npf::text_formatting::{
|
||||
FormatTypeBold, FormatTypeItalic, FormatTypeLink, FormatTypeStrikeThrough, FormatValue,
|
||||
};
|
||||
use crate::utils;
|
||||
use comrak::Arena;
|
||||
|
||||
macro_rules! assert_eq_text {
|
||||
($b:expr, $s:tt) => {
|
||||
match ($b, $s) {
|
||||
(left, right) => {
|
||||
if let BlockValue::Text(b) = left {
|
||||
assert_eq!(b.text, right);
|
||||
} else {
|
||||
panic!("Given block doesn't match BlockValue::Text\n{:#?}", left);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! extrac_formatting {
|
||||
($b:expr) => {{
|
||||
let block = $b;
|
||||
if let BlockValue::Text(b) = block {
|
||||
b.formatting
|
||||
.clone()
|
||||
.unwrap_or_else(|| {
|
||||
panic!("Given block doesn't have a formatting vector {:#?}", block)
|
||||
})
|
||||
.to_vec()
|
||||
} else {
|
||||
panic!("Given block doesn't match BlockValue::Text {:#?}", block);
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! assert_eq_formatting {
|
||||
($a:ident, $b:ident) => {
|
||||
let vec_a = $a;
|
||||
$b.iter().enumerate().for_each(|(i, f)| match f {
|
||||
FormatValue::Bold(f) => {
|
||||
if let FormatValue::Bold(f2) = &vec_a[i] {
|
||||
assert_eq!(f.start, f2.start);
|
||||
assert_eq!(f.end, f2.end);
|
||||
} else {
|
||||
panic!(
|
||||
"Formatting value aren't the same on {f:#?} and {:#?}",
|
||||
&vec_a[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
FormatValue::Italic(f) => {
|
||||
if let FormatValue::Italic(f2) = &vec_a[i] {
|
||||
assert_eq!(f.start, f2.start);
|
||||
assert_eq!(f.end, f2.end);
|
||||
} else {
|
||||
panic!(
|
||||
"Formatting value aren't the same on {f:#?} and {:#?}",
|
||||
&vec_a[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
FormatValue::StrikeThrough(f) => {
|
||||
if let FormatValue::StrikeThrough(f2) = &vec_a[i] {
|
||||
assert_eq!(f.start, f2.start);
|
||||
assert_eq!(f.end, f2.end);
|
||||
} else {
|
||||
panic!(
|
||||
"Formatting value aren't the same on {f:#?} and {:#?}",
|
||||
&vec_a[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
FormatValue::Small(f) => {
|
||||
if let FormatValue::Small(f2) = &vec_a[i] {
|
||||
assert_eq!(f.start, f2.start);
|
||||
assert_eq!(f.end, f2.end);
|
||||
} else {
|
||||
panic!(
|
||||
"Formatting value aren't the same on {f:#?} and {:#?}",
|
||||
&vec_a[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
FormatValue::Link(f) => {
|
||||
if let FormatValue::Link(f2) = &vec_a[i] {
|
||||
assert_eq!(f.start, f2.start);
|
||||
assert_eq!(f.end, f2.end);
|
||||
assert_eq!(f.url, f2.url);
|
||||
} else {
|
||||
panic!(
|
||||
"Formatting value aren't the same on {f:#?} and {:#?}",
|
||||
&vec_a[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
FormatValue::Mention(f) => {
|
||||
if let FormatValue::Mention(f2) = &vec_a[i] {
|
||||
assert_eq!(f.start, f2.start);
|
||||
assert_eq!(f.end, f2.end);
|
||||
assert_eq!(f.blog.uuid, f2.blog.uuid);
|
||||
assert_eq!(f.blog.name, f2.blog.name);
|
||||
assert_eq!(f.blog.url, f2.blog.url);
|
||||
} else {
|
||||
panic!(
|
||||
"Formatting value aren't the same on {f:#?} and {:#?}",
|
||||
&vec_a[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
FormatValue::Color(f) => {
|
||||
if let FormatValue::Color(f2) = &vec_a[i] {
|
||||
assert_eq!(f.start, f2.start);
|
||||
assert_eq!(f.end, f2.end);
|
||||
assert_eq!(f.hex, f2.hex);
|
||||
} else {
|
||||
panic!(
|
||||
"Formatting value aren't the same on {f:#?} and {:#?}",
|
||||
&vec_a[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_block_plain() {
|
||||
let markdown = "Hello world, this is a test of markdown.";
|
||||
let arena = Arena::new();
|
||||
let ast = comrak::parse_document(&arena, &markdown, &utils::default_options());
|
||||
|
||||
let npf = npf::from(&ast).unwrap();
|
||||
|
||||
assert_eq_text!(&npf.content[0], "Hello world, this is a test of markdown.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_block_formatting() {
|
||||
let markdown = "Hello world, **this is a test of markdown**.";
|
||||
let arena = Arena::new();
|
||||
let ast = comrak::parse_document(&arena, &markdown, &utils::default_options());
|
||||
|
||||
let npf = npf::from(&ast).unwrap();
|
||||
|
||||
let formatting = vec![FormatValue::Bold(FormatTypeBold::from(13..39))];
|
||||
let npf_formatting = extrac_formatting!(&npf.content[0]);
|
||||
|
||||
assert_eq_formatting!(formatting, npf_formatting);
|
||||
assert_eq_text!(&npf.content[0], "Hello world, this is a test of markdown.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_block_formatting_nested() {
|
||||
let markdown = "Hello world, **this [is a test of](https://guz.one) markdown**.";
|
||||
let arena = Arena::new();
|
||||
let ast = comrak::parse_document(&arena, &markdown, &utils::default_options());
|
||||
|
||||
let npf = npf::from(&ast).unwrap();
|
||||
|
||||
let formatting = vec![
|
||||
FormatValue::Link(FormatTypeLink::new(
|
||||
18..30,
|
||||
url::Url::parse("https://guz.one").unwrap(),
|
||||
)),
|
||||
FormatValue::Bold(FormatTypeBold::from(13..39)),
|
||||
];
|
||||
let npf_formatting = extrac_formatting!(&npf.content[0]);
|
||||
|
||||
assert_eq_formatting!(formatting, npf_formatting);
|
||||
assert_eq_text!(&npf.content[0], "Hello world, this is a test of markdown.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_block_formatting_broken() {
|
||||
// This isn't "valid" markdown, so the conversion should reflect that
|
||||
let markdown = "Hello [world, *this is](https://guz.one) a test of markdown*.";
|
||||
let arena = Arena::new();
|
||||
let ast = comrak::parse_document(&arena, &markdown, &utils::default_options());
|
||||
|
||||
let npf = npf::from(&ast).unwrap();
|
||||
|
||||
let formatting = vec![FormatValue::Link(FormatTypeLink::new(
|
||||
6..21,
|
||||
url::Url::parse("https://guz.one").unwrap(),
|
||||
))];
|
||||
let npf_formatting = extrac_formatting!(&npf.content[0]);
|
||||
|
||||
assert_eq_formatting!(formatting, npf_formatting);
|
||||
assert_eq_text!(
|
||||
&npf.content[0],
|
||||
"Hello world, *this is a test of markdown*."
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_block_formatting_complex() {
|
||||
let markdown = "Hello [world, *this is*](https://guz.one) ~~a test of markdown~~.";
|
||||
let arena = Arena::new();
|
||||
let ast = comrak::parse_document(&arena, &markdown, &utils::default_options());
|
||||
|
||||
let npf = npf::from(&ast).unwrap();
|
||||
|
||||
let formatting = vec![
|
||||
FormatValue::Italic(FormatTypeItalic::from(13..20)),
|
||||
FormatValue::Link(FormatTypeLink::new(
|
||||
6..20,
|
||||
url::Url::parse("https://guz.one").unwrap(),
|
||||
)),
|
||||
FormatValue::StrikeThrough(FormatTypeStrikeThrough::from(21..39)),
|
||||
];
|
||||
let npf_formatting = extrac_formatting!(&npf.content[0]);
|
||||
|
||||
assert_eq_formatting!(formatting, npf_formatting);
|
||||
assert_eq_text!(&npf.content[0], "Hello world, this is a test of markdown.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_block_paragraph() {
|
||||
let markdown = "If **you** are reading this, thanks for giving a look\n\
|
||||
and checking the ~~ugly~~ source code of this *little\n\
|
||||
**personal** project*. It is heart warming to know that *at least*\n\
|
||||
someone found this interesting and maybe useful, even knowing\n\
|
||||
how niched this whole project is.\\
|
||||
- [Gustavo \"Guz\" L. de Mello](https://guz.one), Apr 16, 12.2024";
|
||||
|
||||
let arena = Arena::new();
|
||||
let ast = comrak::parse_document(&arena, &markdown, &utils::default_options());
|
||||
|
||||
let npf = npf::from(&ast).unwrap();
|
||||
let formatting = vec![
|
||||
FormatValue::Bold(FormatTypeBold::from(3..6)),
|
||||
FormatValue::StrikeThrough(FormatTypeStrikeThrough::from(67..71)),
|
||||
FormatValue::Bold(FormatTypeBold::from(99..107)),
|
||||
FormatValue::Italic(FormatTypeItalic::from(92..115)),
|
||||
FormatValue::Italic(FormatTypeItalic::from(150..158)),
|
||||
FormatValue::Link(FormatTypeLink::new(
|
||||
257..282,
|
||||
url::Url::parse("https://guz.one").unwrap(),
|
||||
)),
|
||||
];
|
||||
|
||||
let npf_formatting = extrac_formatting!(&npf.content[0]);
|
||||
|
||||
assert_eq_formatting!(formatting, npf_formatting);
|
||||
assert_eq_text!(
|
||||
&npf.content[0],
|
||||
"If you are reading this, thanks for giving a look \
|
||||
and checking the ugly source code of this little personal project. \
|
||||
It is heart warming to know that at least someone found this \
|
||||
interesting and maybe useful, even knowing how niched this \
|
||||
whole project is.\n\
|
||||
- Gustavo \"Guz\" L. de Mello, Apr 16, 12.2024"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user