#[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 **world**\n".to_string(); assert_eq!(safe_from_html_to_md(input).unwrap(), expected); } #[test] fn strong_header() { 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 = "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 = "".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(); 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
world
from
blockquote
abc
abc
123
hello
world
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 = "
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 = "this should be rendered