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,48 +285,46 @@ 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!(
super::Parser::new(src).parse().iter().collect::<Vec<_>>(),
&[
Event::Enter(&Leaf(Paragraph), Span::new(0, 0)), Event::Enter(&Leaf(Paragraph), Span::new(0, 0)),
Event::Element(&Inline, Span::new(0, 5)), Event::Element(&Inline, Span::new(0, 5)),
Event::Exit, Event::Exit,
],
); );
} }
#[test] #[test]
fn parse_elem_multiline() { fn parse_elem_multiline() {
let src = "para\npara\n"; test_parse!(
"para\npara\n",
assert_eq!(
super::Parser::new(src).parse().iter().collect::<Vec<_>>(),
&[
Event::Enter(&Leaf(Paragraph), Span::new(0, 0)), Event::Enter(&Leaf(Paragraph), Span::new(0, 0)),
Event::Element(&Inline, Span::new(0, 5)), Event::Element(&Inline, Span::new(0, 5)),
Event::Element(&Inline, Span::new(5, 10)), Event::Element(&Inline, Span::new(5, 10)),
Event::Exit, Event::Exit,
],
); );
} }
#[test] #[test]
fn parse_elem_multi() { fn parse_elem_multi() {
let src = concat!( test_parse!(
concat!(
"# 2\n", "# 2\n",
"\n", "\n",
" # 8\n", " # 8\n",
" 12\n", " 12\n",
"15\n", // "15\n", //
); ),
assert_eq!(
super::Parser::new(src).parse().iter().collect::<Vec<_>>(),
&[
Event::Enter(&Leaf(Heading { level: 1 }), Span::new(0, 1)), Event::Enter(&Leaf(Heading { level: 1 }), Span::new(0, 1)),
Event::Element(&Inline, Span::new(1, 4)), Event::Element(&Inline, Span::new(1, 4)),
Event::Exit, Event::Exit,
@ -336,23 +334,19 @@ mod test {
Event::Element(&Inline, Span::new(10, 15)), Event::Element(&Inline, Span::new(10, 15)),
Event::Element(&Inline, Span::new(15, 18)), Event::Element(&Inline, Span::new(15, 18)),
Event::Exit, Event::Exit,
],
); );
} }
#[test] #[test]
fn parse_container() { fn parse_container() {
let src = concat!( test_parse!(
concat!(
"> a\n", "> a\n",
">\n", ">\n",
"> ## hl\n", "> ## hl\n",
">\n", ">\n",
"> para\n", // "> para\n", //
); ),
assert_eq!(
super::Parser::new(src).parse().iter().collect::<Vec<_>>(),
&[
Event::Enter(&Container(Blockquote), Span::new(0, 1)), Event::Enter(&Container(Blockquote), Span::new(0, 1)),
Event::Enter(&Leaf(Paragraph), Span::new(1, 1)), Event::Enter(&Leaf(Paragraph), Span::new(1, 1)),
Event::Element(&Inline, Span::new(1, 4)), Event::Element(&Inline, Span::new(1, 4)),
@ -366,53 +360,61 @@ mod test {
Event::Element(&Inline, Span::new(17, 23)), Event::Element(&Inline, Span::new(17, 23)),
Event::Exit, Event::Exit,
Event::Exit, Event::Exit,
]
); );
} }
#[test] #[test]
fn parse_code_block() { fn parse_code_block() {
let src = concat!( test_parse!(
concat!(
"```lang\n", "```lang\n",
"l0\n", "l0\n",
"l1\n", "l1\n",
"```", // "```", //
); ),
assert_eq!(
super::Parser::new(src).parse().iter().collect::<Vec<_>>(),
&[
Event::Enter(&Leaf(CodeBlock { fence_length: 3 }), Span::new(0, 8)), Event::Enter(&Leaf(CodeBlock { fence_length: 3 }), Span::new(0, 8)),
Event::Element(&Inline, Span::new(8, 11)), Event::Element(&Inline, Span::new(8, 11)),
Event::Element(&Inline, Span::new(11, 14)), Event::Element(&Inline, Span::new(11, 14)),
Event::Exit Event::Exit
]
); );
} }
macro_rules! test_block {
($src:expr, $kind:expr, $str:expr, $len:expr $(,)?) => {
let lines = super::lines($src).map(|sp| sp.of($src));
let (kind, sp, len) = Block::parse(lines).unwrap();
assert_eq!(
(kind, sp.of($src), len),
($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", //
">\n", //
"> c\n", // "> c\n", //
),
Block::Container(Blockquote),
">",
5,
); );
let lines = super::lines(src).map(|sp| sp.of(src));
let (kind, sp, len) = Block::parse(lines).unwrap();
assert_eq!(kind, Block::Container(Blockquote));
assert_eq!(sp.of(src), ">");
assert_eq!(len, 5);
} }
} }