diff --git a/src/tree.rs b/src/tree.rs index 1e864fd..a05c7f8 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -692,6 +692,9 @@ impl Lookups { #[cfg(test)] mod tests { use super::*; + use crate::mdq_inline; + use crate::mdq_node; + use crate::mdq_nodes; /// tests of each mdast node type /// @@ -753,7 +756,7 @@ mod tests { let (root, lookups) = parse("> hello"); let child = &root.children[0]; check!(child, Node::BlockQuote(_), lookups => MdqNode::BlockQuote(bq)= { - assert_eq!(&bq.body, &vec![text_paragraph("hello")]); + assert_eq!(bq.body, mdq_nodes!["hello"]); }); } @@ -772,9 +775,7 @@ mod tests { check!(&p.children[1], Node::FootnoteReference(_), lookups => MdqNode::Inline(footnote) = { assert_eq!(footnote, Inline::Footnote(Footnote{ label: "a".to_string(), - text: vec![ - text_paragraph("My footnote\nwith two lines.") - ], + text: mdq_nodes!["My footnote\nwith two lines."], })) }); check!(no_node: &root.children[1], Node::FootnoteDefinition(_), lookups => NoNode::Skipped); @@ -798,7 +799,7 @@ mod tests { items: vec![ ListItem{ checked: None, - item: vec![text_paragraph("footnote is a list")], + item: mdq_nodes!["footnote is a list"], } ], }), @@ -835,15 +836,15 @@ mod tests { assert_eq!(items, vec![ ListItem { checked: None, - item: vec![text_paragraph("First")], + item: mdq_nodes!["First"], }, ListItem { checked: Some(false), - item: vec![text_paragraph("Second")], + item: mdq_nodes!["Second"], }, ListItem { checked: Some(true), - item: vec![text_paragraph("Third\nWith a line break")], + item: mdq_nodes!["Third\nWith a line break"], }, ]); }); @@ -855,17 +856,17 @@ mod tests { assert_eq!(items, vec![ ListItem { checked: None, - item: vec![text_paragraph("Fourth")], + item: mdq_nodes!["Fourth"], }, ListItem { checked: Some(false), - item: vec![text_paragraph("Fifth")], + item: mdq_nodes!["Fifth"], }, ListItem { checked: Some(true), - item: vec![ - text_paragraph("Sixth"), - text_paragraph("With a paragraph"), + item: mdq_nodes![ + "Sixth", + "With a paragraph", ], }, ]); @@ -987,11 +988,11 @@ mod tests { check!(&root.children[0], Node::Paragraph(_), lookups => MdqNode::Paragraph(Paragraph{body}) = { assert_eq!(body.len(), 4); assert_eq!(body, vec![ - inline_text("In "), + mdq_inline!("In "), Inline::Text { variant: InlineVariant::Html, value: "".to_string()}, - inline_text("a paragraph."), + mdq_inline!("a paragraph."), Inline::Text { variant: InlineVariant::Html, value: "".to_string()}, @@ -1007,11 +1008,11 @@ mod tests { check!(&root.children[0], Node::Paragraph(_), lookups => MdqNode::Paragraph(Paragraph{body}) = { assert_eq!(body.len(), 4); assert_eq!(body, vec![ - inline_text("In "), + mdq_inline!("In "), Inline::Text { variant: InlineVariant::Html, value: "".to_string()}, - inline_text("a paragraph."), + mdq_inline!("a paragraph."), Inline::Text { variant: InlineVariant::Html, value: "".to_string()}, @@ -1082,7 +1083,7 @@ mod tests { // This isn't an image, though it almost looks like one let (root, lookups) = parse(r#"![]("only a tooltip")"#); check!(&root.children[0], Node::Paragraph(_), lookups => p @ MdqNode::Paragraph{ .. } = { - assert_eq!(p, text_paragraph(r#"![]("only a tooltip")"#)); + assert_eq!(p, mdq_node!(r#"![]("only a tooltip")"#)); }); } } @@ -1097,10 +1098,10 @@ mod tests { check!(&p.children[0], Node::Link(_), lookups => MdqNode::Inline(link) = { assert_eq!(link, Inline::Link { text: vec![ - inline_text("hello "), + mdq_inline!("hello "), Inline::Span { variant: SpanVariant::Emphasis, - children: vec![inline_text("world")], + children: vec![mdq_inline!("world")], } ], link: Link{ @@ -1119,10 +1120,10 @@ mod tests { check!(&p.children[0], Node::Link(_), lookups => MdqNode::Inline(link) = { assert_eq!(link, Inline::Link { text: vec![ - inline_text("hello "), + mdq_inline!("hello "), Inline::Span { variant: SpanVariant::Emphasis, - children: vec![inline_text("world")], + children: vec![mdq_inline!("world")], } ], link: Link{ @@ -1148,10 +1149,10 @@ mod tests { check!(&p.children[0], Node::LinkReference(_), lookups => MdqNode::Inline(link) = { assert_eq!(link, Inline::Link { text: vec![ - inline_text("hello "), + mdq_inline!("hello "), Inline::Span { variant: SpanVariant::Emphasis, - children: vec![inline_text("world")], + children: vec![mdq_inline!("world")], }, ], link: Link{ @@ -1178,10 +1179,10 @@ mod tests { check!(&p.children[0], Node::LinkReference(_), lookups => MdqNode::Inline(link) = { assert_eq!(link, Inline::Link { text: vec![ - inline_text("hello "), + mdq_inline!("hello "), Inline::Span { variant: SpanVariant::Emphasis, - children: vec![inline_text("world")], + children: vec![mdq_inline!("world")], }, ], link: Link{ @@ -1208,10 +1209,10 @@ mod tests { check!(&p.children[0], Node::LinkReference(_), lookups => MdqNode::Inline(link) = { assert_eq!(link, Inline::Link { text: vec![ - inline_text("hello "), + mdq_inline!("hello "), Inline::Span { variant: SpanVariant::Emphasis, - children: vec![inline_text("world")], + children: vec![mdq_inline!("world")], }, ], link: Link{ @@ -1562,9 +1563,7 @@ mod tests { MdqNode::Header(Header{ depth: header_depth, title: header_title, - body: vec![ - text_paragraph("And some text below it.") - ] + body: mdq_nodes!["And some text below it."], }), ]) }); @@ -1608,16 +1607,16 @@ mod tests { assert_eq!(rows, vec![ // rows vec![// Header row - vec![inline_text("Header A")], // cells, each being a spans of inline - vec![inline_text("Header B")], - vec![inline_text("Header C")], - vec![inline_text("Header D")], + vec![mdq_inline!("Header A")], // cells, each being a spans of inline + vec![mdq_inline!("Header B")], + vec![mdq_inline!("Header C")], + vec![mdq_inline!("Header D")], ], vec![// first (and only) data row - vec![inline_text("1")], // cells, each being a spans of inline - vec![inline_text("2")], - vec![inline_text("3")], - vec![inline_text("4")], + vec![mdq_inline!("1")], // cells, each being a spans of inline + vec![mdq_inline!("2")], + vec![mdq_inline!("3")], + vec![mdq_inline!("4")], ], ], ); @@ -1857,25 +1856,25 @@ mod tests { let linear = vec![ MdqNode::Header(Header { depth: 1, - title: vec![inline_text("first")], + title: vec![mdq_inline!("first")], body: vec![], }), MdqNode::Paragraph(Paragraph { - body: vec![inline_text("aaa")], + body: vec![mdq_inline!("aaa")], }), MdqNode::Paragraph(Paragraph { - body: vec![inline_text("bbb")], + body: vec![mdq_inline!("bbb")], }), ]; let expect = vec![MdqNode::Header(Header { depth: 1, - title: vec![inline_text("first")], + title: vec![mdq_inline!("first")], body: vec![ MdqNode::Paragraph(Paragraph { - body: vec![inline_text("aaa")], + body: vec![mdq_inline!("aaa")], }), MdqNode::Paragraph(Paragraph { - body: vec![inline_text("bbb")], + body: vec![mdq_inline!("bbb")], }), ], })]; @@ -1889,26 +1888,26 @@ mod tests { let linear = vec![ MdqNode::Header(Header { depth: 1, - title: vec![inline_text("first")], + title: vec![mdq_inline!("first")], body: vec![], }), MdqNode::Header(Header { depth: 2, - title: vec![inline_text("aaa")], + title: vec![mdq_inline!("aaa")], body: vec![], }), MdqNode::Paragraph(Paragraph { - body: vec![inline_text("bbb")], + body: vec![mdq_inline!("bbb")], }), ]; let expect = vec![MdqNode::Header(Header { depth: 1, - title: vec![inline_text("first")], + title: vec![mdq_inline!("first")], body: vec![MdqNode::Header(Header { depth: 2, - title: vec![inline_text("aaa")], + title: vec![mdq_inline!("aaa")], body: vec![MdqNode::Paragraph(Paragraph { - body: vec![inline_text("bbb")], + body: vec![mdq_inline!("bbb")], })], })], })]; @@ -1922,53 +1921,53 @@ mod tests { let linear = vec![ MdqNode::Header(Header { depth: 1, - title: vec![inline_text("first")], + title: vec![mdq_inline!("first")], body: vec![], }), MdqNode::Header(Header { depth: 2, - title: vec![inline_text("second")], + title: vec![mdq_inline!("second")], body: vec![], }), MdqNode::Header(Header { depth: 3, - title: vec![inline_text("third")], + title: vec![mdq_inline!("third")], body: vec![], }), MdqNode::Header(Header { depth: 3, - title: vec![inline_text("fourth")], + title: vec![mdq_inline!("fourth")], body: vec![], }), MdqNode::Header(Header { depth: 2, - title: vec![inline_text("fifth")], + title: vec![mdq_inline!("fifth")], body: vec![], }), ]; let expect = vec![MdqNode::Header(Header { depth: 1, - title: vec![inline_text("first")], + title: vec![mdq_inline!("first")], body: vec![ MdqNode::Header(Header { depth: 2, - title: vec![inline_text("second")], + title: vec![mdq_inline!("second")], body: vec![ MdqNode::Header(Header { depth: 3, - title: vec![inline_text("third")], + title: vec![mdq_inline!("third")], body: vec![], }), MdqNode::Header(Header { depth: 3, - title: vec![inline_text("fourth")], + title: vec![mdq_inline!("fourth")], body: vec![], }), ], }), MdqNode::Header(Header { depth: 2, - title: vec![inline_text("fifth")], + title: vec![mdq_inline!("fifth")], body: vec![], }), ], @@ -1982,18 +1981,18 @@ mod tests { fn no_headers() -> Result<(), InvalidMd> { let linear = vec![ MdqNode::Paragraph(Paragraph { - body: vec![inline_text("one")], + body: vec![mdq_inline!("one")], }), MdqNode::Paragraph(Paragraph { - body: vec![inline_text("two")], + body: vec![mdq_inline!("two")], }), ]; let expect = vec![ MdqNode::Paragraph(Paragraph { - body: vec![inline_text("one")], + body: vec![mdq_inline!("one")], }), MdqNode::Paragraph(Paragraph { - body: vec![inline_text("two")], + body: vec![mdq_inline!("two")], }), ]; let actual = MdqNode::all_from_iter(linear.into_iter().map(|n| Ok(n)))?; @@ -2006,40 +2005,40 @@ mod tests { let linear = vec![ MdqNode::Header(Header { depth: 1, - title: vec![inline_text("one")], + title: vec![mdq_inline!("one")], body: vec![], }), MdqNode::Header(Header { depth: 5, - title: vec![inline_text("five")], + title: vec![mdq_inline!("five")], body: vec![], }), MdqNode::Header(Header { depth: 2, - title: vec![inline_text("two")], + title: vec![mdq_inline!("two")], body: vec![], }), MdqNode::Header(Header { depth: 3, - title: vec![inline_text("three")], + title: vec![mdq_inline!("three")], body: vec![], }), ]; let expect = vec![MdqNode::Header(Header { depth: 1, - title: vec![inline_text("one")], + title: vec![mdq_inline!("one")], body: vec![ MdqNode::Header(Header { depth: 5, - title: vec![inline_text("five")], + title: vec![mdq_inline!("five")], body: vec![], }), MdqNode::Header(Header { depth: 2, - title: vec![inline_text("two")], + title: vec![mdq_inline!("two")], body: vec![MdqNode::Header(Header { depth: 3, - title: vec![inline_text("three")], + title: vec![mdq_inline!("three")], body: vec![], })], }), @@ -2055,34 +2054,34 @@ mod tests { let linear = vec![ MdqNode::Header(Header { depth: 3, - title: vec![inline_text("three")], + title: vec![mdq_inline!("three")], body: vec![], }), MdqNode::Header(Header { depth: 2, - title: vec![inline_text("two")], + title: vec![mdq_inline!("two")], body: vec![], }), MdqNode::Header(Header { depth: 1, - title: vec![inline_text("one")], + title: vec![mdq_inline!("one")], body: vec![], }), ]; let expect = vec![ MdqNode::Header(Header { depth: 3, - title: vec![inline_text("three")], + title: vec![mdq_inline!("three")], body: vec![], }), MdqNode::Header(Header { depth: 2, - title: vec![inline_text("two")], + title: vec![mdq_inline!("two")], body: vec![], }), MdqNode::Header(Header { depth: 1, - title: vec![inline_text("one")], + title: vec![mdq_inline!("one")], body: vec![], }), ]; @@ -2095,26 +2094,26 @@ mod tests { fn paragraph_before_and_after_header() -> Result<(), InvalidMd> { let linear = vec![ MdqNode::Paragraph(Paragraph { - body: vec![inline_text("before")], + body: vec![mdq_inline!("before")], }), MdqNode::Header(Header { depth: 3, - title: vec![inline_text("the header")], + title: vec![mdq_inline!("the header")], body: vec![], }), MdqNode::Paragraph(Paragraph { - body: vec![inline_text("after")], + body: vec![mdq_inline!("after")], }), ]; let expect = vec![ MdqNode::Paragraph(Paragraph { - body: vec![inline_text("before")], + body: vec![mdq_inline!("before")], }), MdqNode::Header(Header { depth: 3, - title: vec![inline_text("the header")], + title: vec![mdq_inline!("the header")], body: vec![MdqNode::Paragraph(Paragraph { - body: vec![inline_text("after")], + body: vec![mdq_inline!("after")], })], }), ]; @@ -2124,23 +2123,6 @@ mod tests { } } - fn inline_text(text: &str) -> Inline { - Inline::Text { - value: text.to_string(), - variant: InlineVariant::Text, - } - } - - /// Helper for creating a [MdqNode::Paragraph] with plain text. - fn text_paragraph(text: &str) -> MdqNode { - MdqNode::Paragraph(Paragraph { - body: vec![Inline::Text { - variant: InlineVariant::Text, - value: text.to_string(), - }], - }) - } - /// A simple representation of some nodes. Very non-exhaustive, just for testing. fn simple_to_string(nodes: &Vec) -> String { fn build(out: &mut String, node: &mdast::Node) {