use anyhow::{anyhow, Result}; use html_simple_parser::{parse_html, HtmlElem, HtmlParseError}; #[test] fn thow_mismatched_closing_tag_error() -> Result<()> { let input = " "; let result = parse_html(input); match result { Ok(_) => Err(anyhow!( "Test failed! There should be an error here due to tag mismatch." )), Err(HtmlParseError::MismatchedClosingTag(expected, found)) => { assert_eq!(expected, "html"); assert_eq!(found, "div"); Ok(()) } Err(err) => Err(anyhow!(err).context("Test failed! Not the error that was expected.")), } } #[test] fn invalid_html_structure() -> Result<()> { let input = " "; let result = parse_html(input); match result { Ok(_) => Err(anyhow!( "Test failed! There should be an error here due to invalid structure." )), Err(HtmlParseError::ErrorHtmlStructure) => Ok(()), Err(err) => Err(anyhow!(err).context("Test failed! Not the error that was expected.")), } } #[test] fn defining_right_tags() -> Result<()> { let input = "

Text


"; let result = parse_html(input); match result { Ok(elements) => { assert_eq!(elements.len(), 2); match &elements[0] { HtmlElem::Documentation(doctype) => { assert_eq!("", doctype); } _ => { return Err(anyhow!( "Test failed! First Element of html dom must be documentatation tag." )) } } match &elements[1] { HtmlElem::Tag { tag_name, children } => { assert_eq!("html", tag_name); assert_eq!(children.len(), 1); match &children[0] { HtmlElem::Tag { tag_name, children } => { assert_eq!("head", tag_name); assert_eq!(children.len(), 2); match &children[0] { HtmlElem::Tag { tag_name, children } => { assert_eq!(tag_name, "p"); assert_eq!(children.len(), 1); if let HtmlElem::Text(text) = &children[0] { assert_eq!(text, "Text"); } else { return Err(anyhow!( "Test failed!

should contain text." )); } } _ => { return Err(anyhow!( "Test failed! The first child of should be a

tag." )) } } match &children[1] { HtmlElem::Tag { tag_name, children } => { assert_eq!(tag_name, "
"); assert!(children.is_empty()); } _ => { return Err(anyhow!( "Test failed! The second child of should be a
tag." )) } } } _ => { return Err(anyhow!( "Test failed! The child of should be a tag." )) } } } _ => return Err(anyhow!("Test failed! Second element must be tag.")), } Ok(()) } Err(_) => Err(anyhow!("Test failed. HTML parsing failed.")), } }