use super::*; // A series of tests ensuring the default HTML formatter doesn't panic on // unusual but still believable ASTs. We also assert expected panics give at // least helpful messages. #[test] fn test_paragraph_at_root_crash() { let options = Options { ..Default::default() }; let arena = Arena::new(); let para = parse_document(&arena, "para", &options) .first_child() .unwrap(); // This is the bit we don't expect: what if the paragraph doesn't have a // parent at all? Normally it should have at least a Document. para.detach(); let mut output = String::new(); html::format_document(para, &options, &mut output).unwrap(); } #[test] fn test_empty_table_crash() { let options = Options { extension: options::Extension { table: true, ..Default::default() }, ..Default::default() }; let arena = Arena::new(); let table = parse_document(&arena, "| x |\n| - |\n| z |", &options) .first_child() .unwrap(); // What if the table has been emptied of *all* children? while let Some(child) = table.first_child() { child.detach(); } let mut output = String::new(); html::format_document(table, &options, &mut output).unwrap(); } #[test] #[should_panic(expected = "rendered a table cell without a containing table")] fn test_table_cell_out_of_water_crash() { let options = Options { extension: options::Extension { table: true, ..Default::default() }, ..Default::default() }; let arena = Arena::new(); let doc = parse_document(&arena, "| x |\n| - |\n| z |", &options); let table_row = doc .first_child() // table .unwrap() .last_child() // table row .unwrap(); let table_cell = table_row .first_child() // table cell .unwrap(); // What if the table cell has no owning table? table_row.detach(); let mut output = String::new(); html::format_document(table_cell, &options, &mut output).unwrap(); } #[test] #[should_panic(expected = "rendered a table cell without a containing table row")] fn test_table_cell_out_of_school_crash() { let options = Options { extension: options::Extension { table: true, ..Default::default() }, ..Default::default() }; let arena = Arena::new(); let doc = parse_document(&arena, "| x |\n| - |\n| z |", &options); let table_row = doc .first_child() // table .unwrap() .last_child() // table row .unwrap(); let table_cell = table_row .first_child() // table cell .unwrap(); // What if the table cell has no owning table row? table_cell.detach(); let mut output = String::new(); html::format_document(table_cell, &options, &mut output).unwrap(); } // List of HTML block kinds: https://spec.commonmark.org/0.31.2/#html-blocks #[test] fn sourcepos_kind_1() { assert_ast_match!( [], "", (document (1:1-1:17) [ (html_block (1:1-1:17) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:18) [ (html_block (1:1-1:18) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:29) [ (html_block (1:1-1:29) "") ]) ); assert_ast_match!( [], "\n\n**parsed**", (document (1:1-5:10) [ (html_block (1:1-3:19) "\n") (paragraph (5:1-5:10) [ (strong (5:1-5:10) [ (text (5:3-5:8) "parsed") ]) ]) ]) ); assert_ast_match!( [], "", (document (1:1-1:27) [ (html_block (1:1-1:27) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:33) [ (html_block (1:1-1:33) "") ]) ); assert_ast_match!( [], "
A html block", (document (1:1-1:23) [ (html_block (1:1-1:23) "
A html block") ]) ); assert_ast_match!( [], "
\nA html block\n", (document (1:1-3:6) [ (html_block (1:1-3:6) "
\nA html block\n") ]) ); assert_ast_match!( [], "
\n\nA html block\n\n", (document (1:1-5:6) [ (html_block (1:1-5:6) "
\n\nA html block\n\n") ]) ); assert_ast_match!( [], "Test\n\n
\nA html block\n\n\nMore text", (document (1:1-7:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-5:6) "
\nA html block\n\n") (paragraph (7:1-7:9) [ (text (7:1-7:9) "More text") ]) ]) ); } #[test] fn sourcepos_kind_2() { assert_ast_match!( [], "", (document (1:1-1:7) [ (html_block (1:1-1:7) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:8) [ (html_block (1:1-1:8) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:21) [ (html_block (1:1-1:21) "") ]) ); assert_ast_match!( [], "", (document (1:1-3:9) [ (html_block (1:1-3:9) "") ]) ); assert_ast_match!( [], "", (document (1:1-5:9) [ (html_block (1:1-5:9) "") ]) ); assert_ast_match!( [], "Test\n\n\n\nMore text", (document (1:1-7:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-5:9) "\n") (paragraph (7:1-7:9) [ (text (7:1-7:9) "More text") ]) ]) ); } #[test] fn sourcepos_kind_3() { assert_ast_match!( [], "?>", (document (1:1-1:4) [ (html_block (1:1-1:4) "?>") ]) ); assert_ast_match!( [], " ?>", (document (1:1-1:5) [ (html_block (1:1-1:5) " ?>") ]) ); assert_ast_match!( [], "", (document (1:1-1:22) [ (html_block (1:1-1:22) "") ]) ); assert_ast_match!( [], "", (document (1:1-3:8) [ (html_block (1:1-3:8) "") ]) ); assert_ast_match!( [], "", (document (1:1-5:8) [ (html_block (1:1-5:8) "") ]) ); assert_ast_match!( [], "Test\n\n\n\nMore text", (document (1:1-7:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-5:8) "\n") (paragraph (7:1-7:9) [ (text (7:1-7:9) "More text") ]) ]) ); } #[test] fn sourcepos_kind_4() { assert_ast_match!( [], "", (document (1:1-1:4) [ (html_block (1:1-1:4) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:4) [ (html_block (1:1-1:4) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:15) [ (html_block (1:1-1:15) "") ]) ); assert_ast_match!( [], "", (document (1:1-2:5) [ (html_block (1:1-2:5) "") ]) ); assert_ast_match!( [], "", (document (1:1-3:5) [ (html_block (1:1-3:5) "") ]) ); assert_ast_match!( [], "Test\n\n\n\nMore text", (document (1:1-6:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-4:5) "\n") (paragraph (6:1-6:9) [ (text (6:1-6:9) "More text") ]) ]) ); } #[test] fn sourcepos_kind_5() { assert_ast_match!( [], "", (document (1:1-1:12) [ (html_block (1:1-1:12) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:24) [ (html_block (1:1-1:24) "") ]) ); assert_ast_match!( [], "", (document (1:1-3:8) [ (html_block (1:1-3:8) "") ]) ); assert_ast_match!( [], "", (document (1:1-5:8) [ (html_block (1:1-5:8) "") ]) ); assert_ast_match!( [], "Test\n\n\n\nMore text", (document (1:1-7:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-5:8) "\n") (paragraph (7:1-7:9) [ (text (7:1-7:9) "More text") ]) ]) ); } #[test] fn sourcepos_kind_6() { assert_ast_match!( [], "", (document (1:1-1:11) [ (html_block (1:1-1:11) "") ]) ); assert_ast_match!( [], "