Skip to content

Commit a853240

Browse files
github-actions[bot]Copilotdsyme
authored
[Repo Assist] Add comprehensive Markdown.ToMd tests (16 new test cases) (#1097)
* Add comprehensive Markdown.ToMd tests (16 new test cases) Markdown.ToMd had zero test coverage before this change. Add 16 NUnit test cases covering: - plain paragraphs, headings (H1/H2/H3) - bold/strong, inline code - direct links, direct images - unordered lists, fenced code blocks - blockquotes, horizontal rules - LaTeX inline math - inline HTML blocks - tables - empty documents - indirect links (reference-style → resolved direct form) The toMd helper normalises newlines to '\n' and strips trailing whitespace so tests are platform-independent. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * ci: trigger checks --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Don Syme <dsyme@users.noreply.github.com>
1 parent 0e46e50 commit a853240

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

tests/FSharp.Markdown.Tests/Markdown.fs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,124 @@ let ``Paragraph between sublists should not be absorbed into first sublist item
12021202
// Second sublist starts after paragraph.
12031203
secondSublistStart |> should be (greaterThan paragraphPos)
12041204

1205+
// -----------------------------------------------------------------------
1206+
// ToMd serialisation tests
1207+
// These verify the round-trip Markdown-to-Markdown serialiser. Prior to
1208+
// this test file there was zero coverage of Markdown.ToMd.
1209+
// -----------------------------------------------------------------------
1210+
1211+
/// Round-trip helper: parse markdown, serialise back with ToMd (Unix newlines
1212+
/// so comparisons are platform-independent), then strip trailing whitespace.
1213+
let toMd (input: string) =
1214+
Markdown.Parse(input)
1215+
|> (fun doc -> Markdown.ToMd(doc, newline = "\n"))
1216+
|> (fun s -> s.TrimEnd())
1217+
1218+
[<Test>]
1219+
let ``ToMd preserves a plain paragraph`` () =
1220+
"Hello, world." |> toMd |> should contain "Hello, world."
1221+
1222+
[<Test>]
1223+
let ``ToMd preserves a level-1 heading`` () =
1224+
"# Heading One" |> toMd |> shouldEqual "# Heading One"
1225+
1226+
[<Test>]
1227+
let ``ToMd preserves a level-2 heading`` () =
1228+
"## Heading Two" |> toMd |> shouldEqual "## Heading Two"
1229+
1230+
[<Test>]
1231+
let ``ToMd preserves a level-3 heading`` () =
1232+
"### Heading Three" |> toMd |> shouldEqual "### Heading Three"
1233+
1234+
[<Test>]
1235+
let ``ToMd preserves strong (bold) text`` () =
1236+
"**bold**" |> toMd |> should contain "**bold**"
1237+
1238+
[<Test>]
1239+
let ``ToMd preserves inline code`` () =
1240+
"Use `printf` here." |> toMd |> should contain "`printf`"
1241+
1242+
[<Test>]
1243+
let ``ToMd preserves a direct link`` () =
1244+
"[FSharp](https://fsharp.org)"
1245+
|> toMd
1246+
|> should contain "[FSharp](https://fsharp.org)"
1247+
1248+
[<Test>]
1249+
let ``ToMd preserves a direct image`` () =
1250+
"![alt text](image.png)" |> toMd |> should contain "![alt text](image.png)"
1251+
1252+
[<Test>]
1253+
let ``ToMd preserves an unordered list`` () =
1254+
let md = "* apple\n* banana\n* cherry"
1255+
let result = toMd md
1256+
result |> should contain "apple"
1257+
result |> should contain "banana"
1258+
result |> should contain "cherry"
1259+
1260+
[<Test>]
1261+
let ``ToMd preserves a fenced code block`` () =
1262+
let md = "```fsharp\nlet x = 1\n```"
1263+
let result = toMd md
1264+
result |> should contain "let x = 1"
1265+
result |> should contain "```"
1266+
1267+
[<Test>]
1268+
let ``ToMd preserves a blockquote`` () =
1269+
let md = "> This is a quote."
1270+
let result = toMd md
1271+
result |> should contain "> "
1272+
result |> should contain "This is a quote."
1273+
1274+
[<Test>]
1275+
let ``ToMd preserves a horizontal rule`` () =
1276+
let md = "Before\n\n---\n\nAfter"
1277+
let result = toMd md
1278+
result |> should contain "Before"
1279+
result |> should contain "---"
1280+
result |> should contain "After"
1281+
1282+
[<Test>]
1283+
let ``ToMd preserves LaTeX inline math`` () =
1284+
let md = "Einstein's $E = mc^2$ equation."
1285+
let result = toMd md
1286+
result |> should contain "$E = mc^2$"
1287+
1288+
[<Test>]
1289+
let ``ToMd preserves inline HTML block`` () =
1290+
let md = "<div class=\"note\">Note content</div>"
1291+
let result = toMd md
1292+
result |> should contain "<div"
1293+
result |> should contain "Note content"
1294+
1295+
[<Test>]
1296+
let ``ToMd handles a document with heading and paragraph`` () =
1297+
let md = "# Title\n\nBody text here."
1298+
let result = toMd md
1299+
result |> should contain "# Title"
1300+
result |> should contain "Body text here."
1301+
1302+
[<Test>]
1303+
let ``ToMd handles a table`` () =
1304+
let md = "Col1 | Col2\n:--- | :---\nA | B"
1305+
let result = toMd md
1306+
result |> should contain "Col1"
1307+
result |> should contain "Col2"
1308+
result |> should contain "A"
1309+
result |> should contain "B"
1310+
1311+
[<Test>]
1312+
let ``ToMd handles empty document`` () = "" |> toMd |> shouldEqual ""
1313+
1314+
[<Test>]
1315+
let ``ToMd preserves an indirect link when key is not resolved`` () =
1316+
// Indirect link whose reference definition is present — should round-trip
1317+
let md = "[link text][ref]\n\n[ref]: https://example.com"
1318+
let result = toMd md
1319+
// ToMd resolves the indirect link to a direct link form
1320+
result |> should contain "[link text]"
1321+
result |> should contain "https://example.com"
1322+
12051323
// --------------------------------------------------------------------------------------
12061324
// ToMd round-trip: indirect images (issue - failwith "tbd - IndirectImage")
12071325
// --------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)