use anyhow::Result; use html_parser_tarasenko::{parse_html, Node}; #[test] fn test_empty_html() -> Result<()> { let input = ""; let result = parse_html(input)?; assert!(result.is_empty()); Ok(()) } #[test] fn test_simple_element() -> Result<()> { let input = "
"; let expected = vec![Node::Element(html_parser_tarasenko::Element { tag_name: "div".to_string(), attributes: vec![], children: vec![], })]; let result = parse_html(input)?; assert_eq!(result, expected); Ok(()) } #[test] fn test_self_closed_tag() -> Result<()> { let input = ""; let expected = vec![Node::Element(html_parser_tarasenko::Element { tag_name: "img".to_string(), attributes: vec![("src".to_string(), "image.png".to_string())], children: vec![], })]; let result = parse_html(input)?; assert_eq!(result, expected); Ok(()) } #[test] fn test_nested_elements() -> Result<()> { let input = "
Text
"; let expected = vec![Node::Element(html_parser_tarasenko::Element { tag_name: "div".to_string(), attributes: vec![], children: vec![Node::Element(html_parser_tarasenko::Element { tag_name: "span".to_string(), attributes: vec![], children: vec![Node::Text("Text".to_string())], })], })]; let result = parse_html(input)?; assert_eq!(result, expected); Ok(()) } #[test] fn test_attributes() -> Result<()> { let input = "Link"; let expected = vec![Node::Element(html_parser_tarasenko::Element { tag_name: "a".to_string(), attributes: vec![ ("href".to_string(), "https://example.com".to_string()), ("title".to_string(), "Example".to_string()), ], children: vec![Node::Text("Link".to_string())], })]; let result = parse_html(input)?; assert_eq!(result, expected); Ok(()) } #[test] fn test_empty_element() -> Result<()> { let input = "
"; let expected = vec![Node::Element(html_parser_tarasenko::Element { tag_name: "div".to_string(), attributes: vec![], children: vec![], })]; let result = parse_html(input)?; assert_eq!(result, expected); Ok(()) } #[test] fn test_invalid_html() { let input = "

Unclosed tag"; let result = parse_html(input); assert!(result.is_err()); } #[test] fn test_html() -> Result<()> { assert!( html_parser_tarasenko::parse_html("

Hello World!

").is_ok() ); assert!(html_parser_tarasenko::parse_html("
").is_ok()); assert!( html_parser_tarasenko::parse_html("

Hello World!").is_err() ); assert!(html_parser_tarasenko::parse_html("

Hello World!").is_err()); Ok(()) } #[test] fn test_elements() -> Result<()> { assert!( html_parser_tarasenko::parse("elements", "

Paragraph

").is_ok() ); assert!(html_parser_tarasenko::parse("elements", "
").is_ok()); Ok(()) } #[test] fn test_element() -> Result<()> { assert!(html_parser_tarasenko::parse("element", "

Text

").is_ok()); assert!(html_parser_tarasenko::parse("element", "
Nested
").is_ok()); assert!(html_parser_tarasenko::parse("element", "

Text").is_err()); assert!(html_parser_tarasenko::parse("element", "

Nested
").is_err()); Ok(()) } #[test] fn test_opening_tag() -> Result<()> { assert!(html_parser_tarasenko::parse("opening_tag", "
").is_ok()); assert!(html_parser_tarasenko::parse("opening_tag", "").is_ok()); assert!(html_parser_tarasenko::parse("opening_tag", "").is_err()); Ok(()) } #[test] fn test_closing_tag() -> Result<()> { assert!(html_parser_tarasenko::parse("closing_tag", "
").is_ok()); assert!(html_parser_tarasenko::parse("closing_tag", "").is_ok()); assert!(html_parser_tarasenko::parse("closing_tag", "").is_err()); Ok(()) } #[test] fn test_self_closed_tag_second_test() -> Result<()> { assert!(html_parser_tarasenko::parse("self_closed_tag", "").is_ok()); assert!(html_parser_tarasenko::parse("self_closed_tag", "
").is_ok()); assert!(html_parser_tarasenko::parse("self_closed_tag", "").is_err()); assert!(html_parser_tarasenko::parse("self_closed_tag", " Result<()> { assert!(html_parser_tarasenko::parse("tag_name", "div").is_ok()); assert!(html_parser_tarasenko::parse("tag_name", "img1").is_ok()); assert!(html_parser_tarasenko::parse("tag_name", "123").is_err()); Ok(()) } #[test] fn test_attribute_list() -> Result<()> { assert!(html_parser_tarasenko::parse( "attribute_list", "src=\"image.jpg\" alt=\"description\"" ) .is_ok()); assert!(html_parser_tarasenko::parse("attribute_list", "class=\"container\"").is_ok()); Ok(()) } #[test] fn test_attribute() -> Result<()> { assert!(html_parser_tarasenko::parse("attribute", "src=\"image.jpg\"").is_ok()); assert!(html_parser_tarasenko::parse("attribute", "alt=\"description\"").is_ok()); assert!(html_parser_tarasenko::parse("attribute", "src=image.jpg").is_err()); assert!(html_parser_tarasenko::parse("attribute", "alt='description'").is_err()); Ok(()) } #[test] fn test_identifier() -> Result<()> { assert!(html_parser_tarasenko::parse("identifier", "src").is_ok()); assert!(html_parser_tarasenko::parse("identifier", "class").is_ok()); assert!(html_parser_tarasenko::parse("identifier", "123class").is_err()); Ok(()) } #[test] fn test_quoted_string() -> Result<()> { assert!(html_parser_tarasenko::parse("quoted_string", "\"image.jpg\"").is_ok()); assert!(html_parser_tarasenko::parse("quoted_string", "\"description\"").is_ok()); assert!(html_parser_tarasenko::parse("quoted_string", "image.jpg").is_err()); assert!(html_parser_tarasenko::parse("quoted_string", "\"unclosed string").is_err()); Ok(()) } #[test] fn test_content() -> Result<()> { assert!(html_parser_tarasenko::parse("content", "Text").is_ok()); assert!(html_parser_tarasenko::parse("content", "

Paragraph

Nested text").is_ok()); Ok(()) } #[test] fn test_text() -> Result<()> { assert!(html_parser_tarasenko::parse("text", "Hello World!").is_ok()); assert!(html_parser_tarasenko::parse("text", "Plain text with spaces").is_ok()); assert!(html_parser_tarasenko::parse("text", "").is_err()); Ok(()) } #[test] fn test_whitespace() -> Result<()> { assert!(html_parser_tarasenko::parse("WHITESPACE", " ").is_ok()); assert!(html_parser_tarasenko::parse("WHITESPACE", "\t\n\r").is_ok()); assert!(html_parser_tarasenko::parse("WHITESPACE", "a").is_err()); Ok(()) }