Skip to content

Commit

Permalink
Add tests for core
Browse files Browse the repository at this point in the history
  • Loading branch information
bnchi committed Oct 2, 2024
1 parent 0785d36 commit 65751f8
Show file tree
Hide file tree
Showing 3 changed files with 323 additions and 7 deletions.
1 change: 0 additions & 1 deletion mdast_util_to_markdown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ pub fn to_markdown_with_options(tree: &Node, options: &Options) -> Result<String
result.push('\n');
}
}

Ok(result)
}
1 change: 0 additions & 1 deletion mdast_util_to_markdown/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ impl<'a> State<'a> {
return;
}
results.push_str("\n".repeat(1 + n).as_ref());
return;
} else if let Join::HTMLComment = join {
results.push_str("\n\n<!---->\n\n");
}
Expand Down
328 changes: 323 additions & 5 deletions mdast_util_to_markdown/tests/core.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,96 @@
use markdown::mdast::Definition;
use markdown::mdast::{Break, Code, Definition, Heading, List, ListItem};
use markdown::mdast::{Node, Paragraph, Root, Text, ThematicBreak};
use mdast_util_to_markdown::to_markdown_with_options as to_md_with_opts;
use mdast_util_to_markdown::{to_markdown as to, Options};
use pretty_assertions::assert_eq;

#[test]
fn core() {
assert_eq!(
to(&Node::Root(Root {
children: vec![
Node::Paragraph(Paragraph {
children: vec![Node::Text(Text {
value: String::from("a"),
position: None
})],
position: None
}),
Node::ThematicBreak(ThematicBreak { position: None }),
Node::Paragraph(Paragraph {
children: vec![Node::Text(Text {
value: String::from("b"),
position: None
})],
position: None
}),
],
position: None
}))
.unwrap(),
"a\n\n***\n\nb\n",
"should support root"
);

assert_eq!(
to(&Node::Root(Root {
children: vec![
Node::Text(Text {
value: String::from("a"),
position: None
}),
Node::Break(Break { position: None }),
Node::Text(Text {
value: String::from("b"),
position: None
}),
],
position: None
}))
.unwrap(),
"a\\\nb\n",
"should not use blank lines between nodes when given phrasing"
);

assert_eq!(
to(&Node::Root(Root {
children: vec![
Node::Paragraph(Paragraph {
children: vec![Node::Text(Text {
value: String::from("a"),
position: None
})],
position: None
}),
Node::Definition(Definition {
position: None,
url: String::new(),
title: None,
identifier: String::from("b"),
label: None
}),
Node::Definition(Definition {
position: None,
url: String::new(),
title: None,
identifier: String::from("c"),
label: None
}),
Node::Paragraph(Paragraph {
children: vec![Node::Text(Text {
value: String::from("d"),
position: None
})],
position: None
}),
],
position: None
}))
.unwrap(),
"a\n\n[b]: <>\n\n[c]: <>\n\nd\n",
"should support adjacent definitions"
);

assert_eq!(
to_md_with_opts(
&Node::Root(Root {
Expand Down Expand Up @@ -61,10 +146,57 @@ fn core() {
})],
position: None
}),
Node::ThematicBreak(ThematicBreak { position: None }),
Node::List(List {
children: vec![Node::ListItem(ListItem {
children: vec![],
position: None,
spread: false,
checked: None
})],
position: None,
ordered: false,
start: None,
spread: false
}),
Node::List(List {
children: vec![Node::ListItem(ListItem {
children: vec![],
position: None,
spread: false,
checked: None
})],
position: None,
ordered: false,
start: None,
spread: false
}),
Node::List(List {
children: vec![Node::ListItem(ListItem {
children: vec![],
position: None,
spread: false,
checked: None
})],
position: None,
ordered: true,
start: None,
spread: false
}),
Node::List(List {
children: vec![Node::ListItem(ListItem {
children: vec![],
position: None,
spread: false,
checked: None
})],
position: None,
ordered: true,
start: None,
spread: false
}),
Node::Paragraph(Paragraph {
children: vec![Node::Text(Text {
value: String::from("b"),
value: String::from("d"),
position: None
})],
position: None
Expand All @@ -73,7 +205,193 @@ fn core() {
position: None
}))
.unwrap(),
"a\n\n***\n\nb\n",
"should support root"
"a\n\n*\n\n-\n\n1.\n\n1)\n\nd\n",
"should use a different marker for adjacent lists"
);

assert_eq!(
to_md_with_opts(
&Node::Root(Root {
children: vec![
Node::Code(Code {
value: String::from("a"),
position: None,
lang: None,
meta: None
}),
Node::List(List {
children: vec![Node::ListItem(ListItem {
children: vec![],
position: None,
spread: false,
checked: None
})],
position: None,
ordered: false,
start: None,
spread: false
}),
Node::Code(Code {
value: String::from("b"),
position: None,
lang: None,
meta: None
}),
],
position: None
}),
&Options {
fences: false,
..Default::default()
}
)
.unwrap(),
" a\n\n*\n\n<!---->\n\n b\n",
"should inject HTML comments between lists and an indented code"
);

assert_eq!(
to_md_with_opts(
&Node::Root(Root {
children: vec![
Node::Code(Code {
value: String::from("a"),
position: None,
lang: None,
meta: None
}),
Node::Code(Code {
value: String::from("b"),
position: None,
lang: None,
meta: None
}),
],
position: None
}),
&Options {
fences: false,
..Default::default()
}
)
.unwrap(),
" a\n\n<!---->\n\n b\n",
"should inject HTML comments between adjacent indented code"
);

assert_eq!(
to(&Node::ListItem(ListItem {
children: vec![
Node::Paragraph(Paragraph {
children: vec![Node::Text(Text {
value: String::from("a"),
position: None
})],
position: None
}),
Node::Paragraph(Paragraph {
children: vec![Node::Text(Text {
value: String::from("b"),
position: None
})],
position: None
}),
],
position: None,
spread: false,
checked: None
}))
.unwrap(),
"* a\n\n b\n",
"should not honour `spread: false` for two paragraphs"
);

assert_eq!(
to(&Node::ListItem(ListItem {
children: vec![
Node::Paragraph(Paragraph {
children: vec![Node::Text(Text {
value: String::from("a"),
position: None
})],
position: None
}),
Node::Definition(Definition {
position: None,
url: String::from("d"),
title: None,
identifier: String::from("b"),
label: Some(String::from("c"))
}),
],
position: None,
spread: false,
checked: None
}))
.unwrap(),
"* a\n\n [c]: d\n",
"should not honour `spread: false` for a paragraph and a definition"
);

assert_eq!(
to(&Node::ListItem(ListItem {
children: vec![
Node::Paragraph(Paragraph {
children: vec![Node::Text(Text {
value: String::from("a"),
position: None
})],
position: None
}),
Node::Heading(Heading {
children: vec![Node::Text(Text {
value: String::from("b"),
position: None
})],
position: None,
depth: 1
})
],
position: None,
spread: false,
checked: None
}))
.unwrap(),
"* a\n # b\n",
"should honour `spread: false` for a paragraph and a heading"
);

assert_eq!(
to_md_with_opts(
&Node::ListItem(ListItem {
children: vec![
Node::Paragraph(Paragraph {
children: vec![Node::Text(Text {
value: String::from("a"),
position: None
})],
position: None
}),
Node::Heading(Heading {
children: vec![Node::Text(Text {
value: String::from("b"),
position: None
})],
position: None,
depth: 1
})
],
position: None,
spread: false,
checked: None
}),
&Options {
setext: true,
..Default::default()
}
)
.unwrap(),
"* a\n\n b\n =\n",
"should not honour `spread: false` for a paragraph and a setext heading"
);
}

0 comments on commit 65751f8

Please sign in to comment.