From e0ca78397ea34cde06ab8257a8481afc812d344f Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 13 Oct 2022 16:52:44 +0200 Subject: Add a lot of tests on how mdast, unist work --- tests/test_utils/hast.rs | 238 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 233 insertions(+), 5 deletions(-) (limited to 'tests/test_utils') diff --git a/tests/test_utils/hast.rs b/tests/test_utils/hast.rs index 09a180a..2861898 100644 --- a/tests/test_utils/hast.rs +++ b/tests/test_utils/hast.rs @@ -230,11 +230,6 @@ pub struct Text { /// ``` #[derive(Clone, Debug, PartialEq, Eq)] pub struct MdxJsxElement { - // Parent. - /// Content model. - pub children: Vec, - /// Positional info. - pub position: Option, // JSX element. /// Name. /// @@ -242,6 +237,11 @@ pub struct MdxJsxElement { pub name: Option, /// Attributes. pub attributes: Vec, + // Parent. + /// Content model. + pub children: Vec, + /// Positional info. + pub position: Option, } /// MDX: expression. @@ -279,3 +279,231 @@ pub struct MdxjsEsm { // Custom data on where each slice of `value` came from. pub stops: Vec, } + +#[cfg(test)] +mod tests { + use super::*; + use alloc::{format, string::ToString, vec}; + use markdown::unist::Position; + + // Literals. + + #[test] + fn text() { + let mut node = Node::Text(Text { + value: "a".into(), + position: None, + }); + + assert_eq!( + format!("{:?}", node), + "Text { value: \"a\", position: None }", + "should support `Debug`" + ); + assert_eq!(node.to_string(), "a", "should support `ToString`"); + assert_eq!(node.children_mut(), None, "should support `children_mut`"); + assert_eq!(node.children(), None, "should support `children`"); + assert_eq!(node.position(), None, "should support `position`"); + assert_eq!(node.position_mut(), None, "should support `position`"); + node.position_set(Some(Position::new(1, 1, 0, 1, 2, 1))); + assert_eq!( + format!("{:?}", node), + "Text { value: \"a\", position: Some(1:1-1:2 (0-1)) }", + "should support `position_set`" + ); + } + + #[test] + fn comment() { + let mut node = Node::Comment(Comment { + value: "a".into(), + position: None, + }); + + assert_eq!( + format!("{:?}", node), + "Comment { value: \"a\", position: None }", + "should support `Debug`" + ); + assert_eq!(node.to_string(), "a", "should support `ToString`"); + assert_eq!(node.children_mut(), None, "should support `children_mut`"); + assert_eq!(node.children(), None, "should support `children`"); + assert_eq!(node.position(), None, "should support `position`"); + assert_eq!(node.position_mut(), None, "should support `position`"); + node.position_set(Some(Position::new(1, 1, 0, 1, 2, 1))); + assert_eq!( + format!("{:?}", node), + "Comment { value: \"a\", position: Some(1:1-1:2 (0-1)) }", + "should support `position_set`" + ); + } + + #[test] + fn mdx_expression() { + let mut node = Node::MdxExpression(MdxExpression { + value: "a".into(), + stops: vec![], + position: None, + }); + + assert_eq!( + format!("{:?}", node), + "MdxExpression { value: \"a\", position: None, stops: [] }", + "should support `Debug`" + ); + assert_eq!(node.to_string(), "a", "should support `ToString`"); + assert_eq!(node.children_mut(), None, "should support `children_mut`"); + assert_eq!(node.children(), None, "should support `children`"); + assert_eq!(node.position(), None, "should support `position`"); + assert_eq!(node.position_mut(), None, "should support `position`"); + node.position_set(Some(Position::new(1, 1, 0, 1, 2, 1))); + assert_eq!( + format!("{:?}", node), + "MdxExpression { value: \"a\", position: Some(1:1-1:2 (0-1)), stops: [] }", + "should support `position_set`" + ); + } + + #[test] + fn mdxjs_esm() { + let mut node = Node::MdxjsEsm(MdxjsEsm { + value: "a".into(), + stops: vec![], + position: None, + }); + + assert_eq!( + format!("{:?}", node), + "MdxjsEsm { value: \"a\", position: None, stops: [] }", + "should support `Debug`" + ); + assert_eq!(node.to_string(), "a", "should support `ToString`"); + assert_eq!(node.children_mut(), None, "should support `children_mut`"); + assert_eq!(node.children(), None, "should support `children`"); + assert_eq!(node.position(), None, "should support `position`"); + assert_eq!(node.position_mut(), None, "should support `position`"); + node.position_set(Some(Position::new(1, 1, 0, 1, 2, 1))); + assert_eq!( + format!("{:?}", node), + "MdxjsEsm { value: \"a\", position: Some(1:1-1:2 (0-1)), stops: [] }", + "should support `position_set`" + ); + } + + // Voids. + + #[test] + fn doctype() { + let mut node = Node::Doctype(Doctype { position: None }); + + assert_eq!( + format!("{:?}", node), + "Doctype { position: None }", + "should support `Debug`" + ); + assert_eq!(node.to_string(), "", "should support `ToString`"); + assert_eq!(node.children_mut(), None, "should support `children_mut`"); + assert_eq!(node.children(), None, "should support `children`"); + assert_eq!(node.position(), None, "should support `position`"); + assert_eq!(node.position_mut(), None, "should support `position`"); + node.position_set(Some(Position::new(1, 1, 0, 1, 2, 1))); + assert_eq!( + format!("{:?}", node), + "Doctype { position: Some(1:1-1:2 (0-1)) }", + "should support `position_set`" + ); + } + + // Parents. + + #[test] + fn root() { + let mut node = Node::Root(Root { + position: None, + children: vec![], + }); + + assert_eq!( + format!("{:?}", node), + "Root { children: [], position: None }", + "should support `Debug`" + ); + assert_eq!(node.to_string(), "", "should support `ToString`"); + assert_eq!( + node.children_mut(), + Some(&mut vec![]), + "should support `children_mut`" + ); + assert_eq!(node.children(), Some(&vec![]), "should support `children`"); + assert_eq!(node.position(), None, "should support `position`"); + assert_eq!(node.position_mut(), None, "should support `position`"); + node.position_set(Some(Position::new(1, 1, 0, 1, 2, 1))); + assert_eq!( + format!("{:?}", node), + "Root { children: [], position: Some(1:1-1:2 (0-1)) }", + "should support `position_set`" + ); + } + + #[test] + fn element() { + let mut node = Node::Element(Element { + tag_name: "a".into(), + properties: vec![], + position: None, + children: vec![], + }); + + assert_eq!( + format!("{:?}", node), + "Element { tag_name: \"a\", properties: [], children: [], position: None }", + "should support `Debug`" + ); + assert_eq!(node.to_string(), "", "should support `ToString`"); + assert_eq!( + node.children_mut(), + Some(&mut vec![]), + "should support `children_mut`" + ); + assert_eq!(node.children(), Some(&vec![]), "should support `children`"); + assert_eq!(node.position(), None, "should support `position`"); + assert_eq!(node.position_mut(), None, "should support `position`"); + node.position_set(Some(Position::new(1, 1, 0, 1, 2, 1))); + assert_eq!( + format!("{:?}", node), + "Element { tag_name: \"a\", properties: [], children: [], position: Some(1:1-1:2 (0-1)) }", + "should support `position_set`" + ); + } + + #[test] + fn mdx_jsx_element() { + let mut node = Node::MdxJsxElement(MdxJsxElement { + name: None, + attributes: vec![], + position: None, + children: vec![], + }); + + assert_eq!( + format!("{:?}", node), + "MdxJsxElement { name: None, attributes: [], children: [], position: None }", + "should support `Debug`" + ); + assert_eq!(node.to_string(), "", "should support `ToString`"); + assert_eq!( + node.children_mut(), + Some(&mut vec![]), + "should support `children_mut`" + ); + assert_eq!(node.children(), Some(&vec![]), "should support `children`"); + assert_eq!(node.position(), None, "should support `position`"); + assert_eq!(node.position_mut(), None, "should support `position`"); + node.position_set(Some(Position::new(1, 1, 0, 1, 2, 1))); + assert_eq!( + format!("{:?}", node), + "MdxJsxElement { name: None, attributes: [], children: [], position: Some(1:1-1:2 (0-1)) }", + "should support `position_set`" + ); + } +} -- cgit