diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-10-13 16:52:44 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-10-13 16:53:10 +0200 |
commit | e0ca78397ea34cde06ab8257a8481afc812d344f (patch) | |
tree | 5dc85ca4f9e18c2b93a3f86f106f2cd8535c828b /tests | |
parent | 5ad84c1670733843f677b2c5bee8c50d78b8e24a (diff) | |
download | markdown-rs-e0ca78397ea34cde06ab8257a8481afc812d344f.tar.gz markdown-rs-e0ca78397ea34cde06ab8257a8481afc812d344f.tar.bz2 markdown-rs-e0ca78397ea34cde06ab8257a8481afc812d344f.zip |
Add a lot of tests on how mdast, unist work
Diffstat (limited to '')
-rw-r--r-- | tests/test_utils/hast.rs | 238 |
1 files changed, 233 insertions, 5 deletions
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<Node>, - /// Positional info. - pub position: Option<Position>, // JSX element. /// Name. /// @@ -242,6 +237,11 @@ pub struct MdxJsxElement { pub name: Option<String>, /// Attributes. pub attributes: Vec<AttributeContent>, + // Parent. + /// Content model. + pub children: Vec<Node>, + /// Positional info. + pub position: Option<Position>, } /// MDX: expression. @@ -279,3 +279,231 @@ pub struct MdxjsEsm { // Custom data on where each slice of `value` came from. pub stops: Vec<Stop>, } + +#[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`" + ); + } +} |