use super::*;
use ox_content_ast::Node;
#[test]
fn ascii_contains_closing_tag_matches_case_insensitively() {
assert!(super::html::ascii_contains_closing_tag("end </SCRIPT> tail", b"script"));
assert!(super::html::ascii_contains_closing_tag("</style ", b"style"));
assert!(!super::html::ascii_contains_closing_tag("<scriptsource>", b"script"));
assert!(!super::html::ascii_contains_closing_tag("", b"pre"));
assert!(!super::html::ascii_contains_closing_tag("</pr", b"pre"));
}
#[test]
fn test_parse_image() {
let allocator = Allocator::new();
let doc = Parser::new(&allocator, "").parse().unwrap();
assert_eq!(doc.children.len(), 1);
match &doc.children[0] {
Node::Paragraph(p) => {
assert_eq!(p.children.len(), 1);
match &p.children[0] {
Node::Image(img) => {
assert_eq!(img.alt, "Alt text");
assert_eq!(img.url, "/path/to/image.png");
}
_ => panic!("expected image, got {:?}", &p.children[0]),
}
}
_ => panic!("expected paragraph"),
}
}
#[test]
fn test_parse_heading() {
let allocator = Allocator::new();
let doc = Parser::new(&allocator, "# Hello\n").parse().unwrap();
assert_eq!(doc.children.len(), 1);
match &doc.children[0] {
Node::Heading(h) => {
assert_eq!(h.depth, 1);
}
_ => panic!("expected heading"),
}
}
#[test]
fn indented_heading_like_text_does_not_loop() {
let allocator = Allocator::new();
let doc = Parser::new(&allocator, " # heading\n").parse().unwrap();
assert_eq!(doc.children.len(), 1);
assert!(
matches!(&doc.children[0], Node::Paragraph(_)),
"expected leading-indented `#` to parse as paragraph text, got {:?}",
&doc.children[0]
);
}
#[test]
fn test_parse_paragraph() {
let allocator = Allocator::new();
let doc = Parser::new(&allocator, "Hello world").parse().unwrap();
assert_eq!(doc.children.len(), 1);
assert!(matches!(&doc.children[0], Node::Paragraph(_)));
}
#[test]
fn test_parse_thematic_break() {
let allocator = Allocator::new();
let doc = Parser::new(&allocator, "---").parse().unwrap();
assert_eq!(doc.children.len(), 1);
assert!(matches!(&doc.children[0], Node::ThematicBreak(_)));
}
#[test]
fn test_parse_fenced_code() {
let allocator = Allocator::new();
let doc = Parser::new(
&allocator,
"```rust\nfn main() {}
```",
)
.parse()
.unwrap();
assert_eq!(doc.children.len(), 1);
match &doc.children[0] {
Node::CodeBlock(cb) => {
assert_eq!(cb.lang, Some("rust"));
}
_ => panic!("expected code block"),
}
}
#[test]
fn test_parse_inline_code() {
let allocator = Allocator::new();
let doc = Parser::new(&allocator, "Use `code` here").parse().unwrap();
assert_eq!(doc.children.len(), 1);
match &doc.children[0] {
Node::Paragraph(p) => {
assert!(p.children.iter().any(|n| matches!(n, Node::InlineCode(_))));
}
_ => panic!("expected paragraph"),
}
}
#[test]
fn test_parse_strikethrough() {
let allocator = Allocator::new();
let doc = Parser::with_options(&allocator, "~~done~~", ParserOptions::gfm()).parse().unwrap();
assert_eq!(doc.children.len(), 1);
match &doc.children[0] {
Node::Paragraph(p) => {
assert!(matches!(&p.children[0], Node::Delete(_)));
}
_ => panic!("expected paragraph"),
}
}
#[test]
fn test_parse_strikethrough_lone_tilde_not_matched() {
let allocator = Allocator::new();
for input in ["~~open ~ but no close", "~~trailing tilde~"] {
let doc = Parser::with_options(&allocator, input, ParserOptions::gfm()).parse().unwrap();
match &doc.children[0] {
Node::Paragraph(p) => {
assert!(
!p.children.iter().any(|n| matches!(n, Node::Delete(_))),
"{input:?} should not produce a Delete node"
);
}
_ => panic!("expected paragraph"),
}
}
}
#[test]
fn test_parse_hard_break() {
let allocator = Allocator::new();
let doc = Parser::new(&allocator, "line 1\\\nline 2").parse().unwrap();
assert_eq!(doc.children.len(), 1);
match &doc.children[0] {
Node::Paragraph(p) => {
assert!(p.children.iter().any(|n| matches!(n, Node::Break(_))));
}
_ => panic!("expected paragraph"),
}
}
#[test]
fn test_parse_table() {
let allocator = Allocator::new();
let table_md = "| Header 1 | Header 2 |\n|----------|----------|\n| Cell 1 | Cell 2 |";
let parser = Parser::with_options(&allocator, table_md, ParserOptions::gfm());
let doc = parser.parse().unwrap();
assert_eq!(doc.children.len(), 1);
match &doc.children[0] {
Node::Table(t) => {
assert_eq!(t.children.len(), 2); }
_ => panic!("expected table, got {:?}", &doc.children[0]),
}
}
#[test]
fn test_parse_unordered_list() {
let allocator = Allocator::new();
let list_md = "- Item 1\n- Item 2\n- Item 3";
let parser = Parser::new(&allocator, list_md);
let doc = parser.parse().unwrap();
assert_eq!(doc.children.len(), 1);
match &doc.children[0] {
Node::List(list) => {
assert!(!list.ordered);
assert_eq!(list.children.len(), 3);
}
_ => panic!("expected list, got {:?}", &doc.children[0]),
}
}
#[test]
fn test_parse_ordered_list() {
let allocator = Allocator::new();
let list_md = "1. First\n2. Second\n3. Third";
let parser = Parser::new(&allocator, list_md);
let doc = parser.parse().unwrap();
assert_eq!(doc.children.len(), 1);
match &doc.children[0] {
Node::List(list) => {
assert!(list.ordered);
assert_eq!(list.children.len(), 3);
}
_ => panic!("expected list, got {:?}", &doc.children[0]),
}
}
#[test]
fn test_parse_block_quote() {
let allocator = Allocator::new();
let doc = Parser::new(&allocator, "> Hello world").parse().unwrap();
assert_eq!(doc.children.len(), 1);
match &doc.children[0] {
Node::BlockQuote(bq) => {
assert_eq!(bq.children.len(), 1);
assert!(matches!(&bq.children[0], Node::Paragraph(_)));
}
_ => panic!("expected block quote, got {:?}", &doc.children[0]),
}
}
#[test]
fn test_parse_block_quote_multiline() {
let allocator = Allocator::new();
let doc = Parser::new(&allocator, "> line 1\n> line 2").parse().unwrap();
assert_eq!(doc.children.len(), 1);
match &doc.children[0] {
Node::BlockQuote(bq) => {
assert_eq!(bq.children.len(), 1);
}
_ => panic!("expected block quote, got {:?}", &doc.children[0]),
}
}
#[test]
fn test_parse_nested_block_quote() {
let allocator = Allocator::new();
let doc = Parser::new(&allocator, "> > nested").parse().unwrap();
assert_eq!(doc.children.len(), 1);
match &doc.children[0] {
Node::BlockQuote(bq) => {
assert_eq!(bq.children.len(), 1);
assert!(matches!(&bq.children[0], Node::BlockQuote(_)));
}
_ => panic!("expected block quote, got {:?}", &doc.children[0]),
}
}