#[cfg(test)] mod to_md_tests { use html2md_rs::{ parser::safe_parse_html, structs::{Node, NodeType, ToMdConfig}, to_md::{safe_from_html_to_md, safe_from_html_to_md_with_config}, }; pub trait PrintNode { fn print_node(&self); } impl PrintNode for Node { fn print_node(&self) { println!("{:#?}", self); } } trait StringPrintNode { fn print_node(&self); } impl StringPrintNode for String { fn print_node(&self) { match safe_parse_html(self.clone()) { Ok(node) => node.print_node(), Err(e) => println!("Error: {}", e), } } } #[test] fn simple_paragraph_with_text() { let input = "

hello

".to_string(); let expected = "hello\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn multiple_headers() { let input = "

hello

world

".to_string(); let expected = "# hello\n## world\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn paragraph_with_strong() { let input = "

hello world

".to_string(); let expected = "hello **world**\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn strong_header() { let input = "

hello

".to_string(); let expected = "# **hello**\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn unordered_list() { let input = "".to_string(); let expected = "- hello\n- world\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn ordered_list() { let input = "
  1. hello
  2. world
".to_string(); let expected = "1. hello\n2. world\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn multiple_paragraphs() { let input = "

hello

world

".to_string(); let expected = "hello\nworld\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn multiple_paragraphs_with_empty_paragraph() { let input = "

hello

world

".to_string(); let expected = "hello\nworld\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn header_and_paragraph() { let input = "

hello

world

".to_string(); let expected = "# hello\nworld\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn paragraph_with_link() { let input = "

hello

".to_string(); let expected = "[hello](https://example.com)\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn code_block() { let input = "

let x: i32 = 123;
let y: i32 = 456;
let z = x + y;
println!(\"{}\", z);
" .to_string(); let expected = "```rust let x: i32 = 123; let y: i32 = 456; let z = x + y; println!(\"{}\", z); ```\n" .to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn line_break() { let input = "

hello
world

".to_string(); let expected = "hello \nworld\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn blockquote() { let input = "

hello

world

from

blockquote

" .to_string(); input.print_node(); let expected = "> hello\n> world\n> from\n> blockquote\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn unknown_tag() { let input = "hello".to_string(); let expected = "hello".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn list_in_list() { let input = " " .to_string(); let expected = "- abc\n - abc\n 1. 123\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn ol_start_attribute() { let input = "
  1. hello

  2. world

".to_string(); let expected = "3. hello\n4. world\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn comment() { let input = "".to_string(); let expected = "".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn comments_in_p() { let input = "

".to_string(); let expected = "".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn unclosed_tag() { let input = "

hello".to_string(); let expected = "hello\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn unclosed_tag_2() { let input = "Test

hello

".to_string(); input.print_node(); let expected = "hello\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn ignore_rendering() { let input = "
don't render this

this should be rendered

render this
".to_string(); let config = ToMdConfig { ignore_rendering: vec![NodeType::Unknown("span".to_string())], }; let expected = "this should be rendered\nrender this".to_string(); assert_eq!( safe_from_html_to_md_with_config(input, &config).unwrap(), expected ); } }