rechiko/test/test_frontmatter.ml
2025-05-31 21:02:41 +05:30

94 lines
2.1 KiB
OCaml

open Rechiko.Frontmatter
let simple_markdown =
{|
---
Title: "Test Document"
Draft: true
Date: "2023-10-01"
---
# Test Document
This is a test document with frontmatter.
|}
let simple_markdown_yaml_section =
{|
Title: "Test Document"
Draft: true
Date: "2023-10-01"
|}
let simple_markdown_content =
{|
# Test Document
This is a test document with frontmatter.
|}
let markdown_with_empty_frontmatter =
{|
---
---
# Empty Frontmatter Document
|}
let markdown_when_frontmatter_is_invalid_yaml =
{|
---
John: "Doe
---
" # bad yaml
|}
let invalid_yaml_frontmatter = {|John: "Doe|}
let%test "extract yaml from simplie markdown 1" =
let yaml_section, _ = extract_frontmatter simple_markdown in
String.trim yaml_section = String.trim simple_markdown_yaml_section
let%test "extract content from simple markdown" =
let _, content = extract_frontmatter simple_markdown in
String.trim content = String.trim simple_markdown_content
let%test "extract yaml from md with empty frontmatter" =
let yaml_section, _ = extract_frontmatter markdown_with_empty_frontmatter in
String.trim yaml_section = ""
let%test "extract content from md with empty frontmatter" =
let _, content = extract_frontmatter markdown_with_empty_frontmatter in
String.trim content = "# Empty Frontmatter Document"
let%test "extract yaml from md with invalid frontmatter" =
let yaml_section, _ =
extract_frontmatter markdown_when_frontmatter_is_invalid_yaml
in
String.trim yaml_section = invalid_yaml_frontmatter
let%test "extract content from md with invalid frontmatter" =
let _, content =
extract_frontmatter markdown_when_frontmatter_is_invalid_yaml
in
String.trim content = {|" # bad yaml|}
let%test "parse valid yaml" =
let fm = parse_frontmatter simple_markdown_yaml_section in
let expected =
StringMap.of_list
[
("Title", `String "Test Document");
("Draft", `Bool true);
("Date", `String "2023-10-01");
]
in
StringMap.equal Yojson.Safe.equal fm expected
let%test "parse invalid yaml" =
try
ignore (parse_frontmatter invalid_yaml_frontmatter);
false
with Frontmatter_parse_error _ -> true