test_parse, test_block

This commit is contained in:
Noah Hellman 2022-11-27 22:19:15 +01:00
parent d32519009e
commit b2bc575e27

View file

@ -285,134 +285,136 @@ mod test {
use super::Container::*; use super::Container::*;
use super::Leaf::*; use super::Leaf::*;
macro_rules! test_parse {
($src:expr $(,$($event:expr),* $(,)?)?) => {
let t = super::Parser::new($src).parse();
let actual = t.iter().collect::<Vec<_>>();
let expected = &[$($($event),*,)?];
assert_eq!(actual, expected, "\n\n{}\n\n", $src);
};
}
#[test] #[test]
fn parse_elem_oneline() { fn parse_elem_oneline() {
let src = "para\n"; test_parse!(
"para\n",
assert_eq!( Event::Enter(&Leaf(Paragraph), Span::new(0, 0)),
super::Parser::new(src).parse().iter().collect::<Vec<_>>(), Event::Element(&Inline, Span::new(0, 5)),
&[ Event::Exit,
Event::Enter(&Leaf(Paragraph), Span::new(0, 0)),
Event::Element(&Inline, Span::new(0, 5)),
Event::Exit,
],
); );
} }
#[test] #[test]
fn parse_elem_multiline() { fn parse_elem_multiline() {
let src = "para\npara\n"; test_parse!(
"para\npara\n",
assert_eq!( Event::Enter(&Leaf(Paragraph), Span::new(0, 0)),
super::Parser::new(src).parse().iter().collect::<Vec<_>>(), Event::Element(&Inline, Span::new(0, 5)),
&[ Event::Element(&Inline, Span::new(5, 10)),
Event::Enter(&Leaf(Paragraph), Span::new(0, 0)), Event::Exit,
Event::Element(&Inline, Span::new(0, 5)),
Event::Element(&Inline, Span::new(5, 10)),
Event::Exit,
],
); );
} }
#[test] #[test]
fn parse_elem_multi() { fn parse_elem_multi() {
let src = concat!( test_parse!(
"# 2\n", concat!(
"\n", "# 2\n",
" # 8\n", "\n",
" 12\n", " # 8\n",
"15\n", // " 12\n",
); "15\n", //
),
assert_eq!( Event::Enter(&Leaf(Heading { level: 1 }), Span::new(0, 1)),
super::Parser::new(src).parse().iter().collect::<Vec<_>>(), Event::Element(&Inline, Span::new(1, 4)),
&[ Event::Exit,
Event::Enter(&Leaf(Heading { level: 1 }), Span::new(0, 1)), Event::Element(&Blankline, Span::new(4, 5)),
Event::Element(&Inline, Span::new(1, 4)), Event::Enter(&Leaf(Heading { level: 1 }), Span::new(6, 7)),
Event::Exit, Event::Element(&Inline, Span::new(7, 10)),
Event::Element(&Blankline, Span::new(4, 5)), Event::Element(&Inline, Span::new(10, 15)),
Event::Enter(&Leaf(Heading { level: 1 }), Span::new(6, 7)), Event::Element(&Inline, Span::new(15, 18)),
Event::Element(&Inline, Span::new(7, 10)), Event::Exit,
Event::Element(&Inline, Span::new(10, 15)),
Event::Element(&Inline, Span::new(15, 18)),
Event::Exit,
],
); );
} }
#[test] #[test]
fn parse_container() { fn parse_container() {
let src = concat!( test_parse!(
"> a\n", concat!(
">\n", "> a\n",
"> ## hl\n", ">\n",
">\n", "> ## hl\n",
"> para\n", // ">\n",
); "> para\n", //
),
assert_eq!( Event::Enter(&Container(Blockquote), Span::new(0, 1)),
super::Parser::new(src).parse().iter().collect::<Vec<_>>(), Event::Enter(&Leaf(Paragraph), Span::new(1, 1)),
&[ Event::Element(&Inline, Span::new(1, 4)),
Event::Enter(&Container(Blockquote), Span::new(0, 1)), Event::Exit,
Event::Enter(&Leaf(Paragraph), Span::new(1, 1)), Event::Element(&Blankline, Span::new(5, 6)),
Event::Element(&Inline, Span::new(1, 4)), Event::Enter(&Leaf(Heading { level: 2 }), Span::new(8, 10)),
Event::Exit, Event::Element(&Inline, Span::new(10, 14)),
Event::Element(&Blankline, Span::new(5, 6)), Event::Exit,
Event::Enter(&Leaf(Heading { level: 2 }), Span::new(8, 10)), Event::Element(&Blankline, Span::new(15, 16)),
Event::Element(&Inline, Span::new(10, 14)), Event::Enter(&Leaf(Paragraph), Span::new(17, 17)),
Event::Exit, Event::Element(&Inline, Span::new(17, 23)),
Event::Element(&Blankline, Span::new(15, 16)), Event::Exit,
Event::Enter(&Leaf(Paragraph), Span::new(17, 17)), Event::Exit,
Event::Element(&Inline, Span::new(17, 23)),
Event::Exit,
Event::Exit,
]
); );
} }
#[test] #[test]
fn parse_code_block() { fn parse_code_block() {
let src = concat!( test_parse!(
"```lang\n", concat!(
"l0\n", "```lang\n",
"l1\n", "l0\n",
"```", // "l1\n",
"```", //
),
Event::Enter(&Leaf(CodeBlock { fence_length: 3 }), Span::new(0, 8)),
Event::Element(&Inline, Span::new(8, 11)),
Event::Element(&Inline, Span::new(11, 14)),
Event::Exit
); );
}
assert_eq!( macro_rules! test_block {
super::Parser::new(src).parse().iter().collect::<Vec<_>>(), ($src:expr, $kind:expr, $str:expr, $len:expr $(,)?) => {
&[ let lines = super::lines($src).map(|sp| sp.of($src));
Event::Enter(&Leaf(CodeBlock { fence_length: 3 }), Span::new(0, 8)), let (kind, sp, len) = Block::parse(lines).unwrap();
Event::Element(&Inline, Span::new(8, 11)), assert_eq!(
Event::Element(&Inline, Span::new(11, 14)), (kind, sp.of($src), len),
Event::Exit ($kind, $str, $len),
] "\n\n{}\n\n",
); $src
);
};
} }
#[test] #[test]
fn block_multiline() { fn block_multiline() {
let src = "# heading\n spanning two lines\n"; test_block!(
let lines = super::lines(src).map(|sp| sp.of(src)); "# heading\n spanning two lines\n",
let (kind, sp, len) = Block::parse(lines).unwrap(); Block::Leaf(Heading { level: 1 }),
assert_eq!(kind, Block::Leaf(Heading { level: 1 })); "#",
assert_eq!(sp.of(src), "#"); 2
assert_eq!(len, 2); );
} }
#[test] #[test]
fn block_container() { fn block_container() {
let src = concat!( test_block!(
"> a\n", concat!(
">\n", "> a\n", //
" > b\n", ">\n", //
">\n", " > b\n", //
"> c\n", // ">\n", //
); "> c\n", //
let lines = super::lines(src).map(|sp| sp.of(src)); ),
let (kind, sp, len) = Block::parse(lines).unwrap(); Block::Container(Blockquote),
assert_eq!(kind, Block::Container(Blockquote)); ">",
assert_eq!(sp.of(src), ">"); 5,
assert_eq!(len, 5); );
} }
} }