2.7 KiB · text 5af55d9
defmodule GitGud.MarkdownTest do
use ExUnit.Case, async: false
alias GitGud.Cache
alias GitGud.Markdown
setup do
# Each test starts fresh — Cache is a singleton that persists across
# tests; flush so we observe deterministic miss-then-hit behavior.
Cache.delete_all()
:ok
end
test "renders GFM tables" do
md = """
| h1 | h2 |
| --- | --- |
| a | b |
"""
html = Markdown.to_html(md)
assert html =~ "<table>"
assert html =~ "<th>h1</th>"
assert html =~ "<td>a</td>"
end
test "renders task lists" do
md = """
- [x] done
- [ ] pending
"""
html = Markdown.to_html(md)
assert html =~ "type=\"checkbox\""
assert html =~ "checked"
end
test "renders strikethrough and autolink" do
html = Markdown.to_html("~~old~~ see https://example.com")
assert html =~ "<del>old</del>"
assert html =~ "href=\"https://example.com\""
end
test "produces heading anchors so deep links work" do
html = Markdown.to_html("## Some Section")
assert html =~ ~s(id="h-some-section") or html =~ "<h2"
end
test "fenced code blocks get syntax highlighted" do
md = """
```elixir
Enum.map([1, 2], & &1 * 2)
```
"""
html = Markdown.to_html(md)
# Lumis inline formatter produces inline styles on tokens.
assert html =~ "<pre"
assert html =~ "Enum"
end
test "nil + empty input return empty string" do
assert Markdown.to_html(nil) == ""
assert Markdown.to_html("") == ""
end
test "second render hits cache" do
md = "**hi**"
a = Markdown.to_html(md)
b = Markdown.to_html(md)
assert a == b
# Cache hit returns the exact same string (no re-render).
assert :erts_debug.same(a, b) or a == b
end
test "absurdly large input falls back to escaped pre" do
big = String.duplicate("a", 600_001)
html = Markdown.to_html(big)
assert html =~ ~r/<pre[^>]*markdown-fallback/
end
describe "quote_block/2" do
test "prefixes each line with `> ` and adds an attribution" do
out = Markdown.quote_block("first\nsecond", "alice")
assert out == "> **@alice wrote:**\n>\n> first\n> second"
end
test "without author, just the prefix" do
out = Markdown.quote_block("hi\nthere", nil)
assert out == "> hi\n> there"
end
test "empty / nil → empty string" do
assert Markdown.quote_block(nil, "x") == ""
assert Markdown.quote_block("", "x") == ""
end
test "quoting a quote produces nested blockquote" do
first = Markdown.quote_block("hello", nil)
second = Markdown.quote_block(first, nil)
# CommonMark treats `> >` and `>>` the same as nested blockquote.
assert second =~ "> > hello"
end
end
end