-- Unit checks for custom/extensions/about-render.lua, run under a standalone -- Lua by t0501-about-render.sh with the script path as the first argument. -- The second argument may be nolpeg, which makes the lpeg module fail to -- load so the escaped plain text fallback is proven even on a machine where -- lpeg is installed. Without it the markdown and man checks run when lpeg is -- present and are skipped with a note when it is not, the same way the -- filter itself degrades. local test_directory = arg[0]:match("^(.*)/") or "." local h = dofile(test_directory .. "/harness.lua") local script = arg[1] local mode = arg[2] or "" local has_lpeg = false if mode == "nolpeg" then h.preload_failure("lpeg") else has_lpeg = pcall(require, "lpeg") if not has_lpeg then io.write("lpeg not installed, markdown and man checks skipped\n") end end h.load(script) local function render(filename, text) return h.run({ filename }, { text }) end -- A readme tracked in a repository comes with the url of its tree. local function render_tree(filename, query, text) return h.run({ filename, "/r/tree/", query }, { text }) end -- Dispatch and the plain text rendering need nothing but the script itself, -- so they run whatever became of lpeg. local out, ret = render("README", "plain & more") h.contains("no extension renders as plain text", out, "
plain <text> & more
") h.equals("close answers zero", ret, 0) out = render("notes.txt", "just text") h.contains("an unknown extension renders as plain text", out, "
just text
") out = render(nil, "no name at all") h.contains("a missing filename renders as plain text", out, "
no name at all
") -- cgit's C sink stops at the first NUL byte, which the script documents, so -- the text past one is lost while the wrapper written separately survives. out = render("README", "before\0after") h.contains("text before a nul byte survives", out, "before") h.excludes("text after a nul byte is dropped", out, "after") h.contains("the wrapper written after the text survives", out, "") out = h.run({ "README" }, { "two ", "writes" }) h.contains("writes are joined before rendering", out, "two writes") if mode == "nolpeg" then out = render("README.md", "# Title") h.contains("markdown without lpeg falls back to plain text", out, "
# Title
") out = render("page.1", ".SH NAME") h.contains("man without lpeg falls back to plain text", out, "
.SH NAME
") h.finish() end if not has_lpeg then h.finish() end -- Markdown. out = render("README.md", "# Title") h.contains("a heading renders inside the wrapper", out, "

Title

") out = render("README.md", "## Sub ##") h.contains("trailing hashes are stripped from a heading", out, "

Sub

") out = render("README.md", "# C#") h.contains("a hash ending a word stays in the heading", out, "

C#

") out = render("README.md", "## The `run` [Command](x), Again!") h.contains("an anchor is the heading's text in lower case", out, "

") out = render("README.md", "# Twice\n# Twice\n# twice") h.contains("a repeated anchor is counted", out, "

Twice

Twice

") out = render("README.md", "# About") h.contains("no heading takes an id of the page", out, "

") out = render("README.md", "# ???") h.contains("a heading of punctuation gets no anchor", out, "

???

") out = render("README.MD", "# Upper") h.contains("the extension matches whatever its case", out, "

Upper

") out = render("readme.markdown", "# Long") h.contains("the long markdown extension dispatches too", out, "

Long

") out = render("README.md", "line one\nline two") h.contains("the lines of a paragraph run together", out, "

line one\nline two

") out = render("README.md", "first para\r\nsecond line") h.contains("crlf line endings are normalised", out, "

first para\nsecond line

") out = render("README.md", "one \ntwo\\\nthree") h.contains("two trailing spaces break the line", out, "

one
\ntwo") h.contains("a trailing backslash breaks the line", out, "two
\nthree

") out = render("README.md", "spaces \n\nslash\\") h.contains("the last line takes no break", out, "

spaces

slash\\

") out = render("README.md", "a **bold** and *leaning* word") h.contains("double stars embolden", out, "bold") h.contains("single stars lean", out, "leaning") out = render("README.md", "an __up__ and _down_ word") h.contains("double underscores embolden", out, "up") h.contains("single underscores lean", out, "down") out = render("README.md", "snake_case_name and _lean_ and foo__bar__") h.contains("underscores inside words stay literal", out, "

snake_case_name and lean and foo__bar__

") out = render("README.md", "_a_b_ and __c_d__") h.contains("emphasis spans an underscore inside a word", out, "

a_b and c_d

") out = render("README.md", "\\*not leaning\\* and \\# and \\q") h.contains("a backslash makes punctuation literal", out, "

*not leaning* and # and \\q

") out = render("README.md", "\\# not a heading") h.contains("an escaped hash starts no heading", out, "

# not a heading

") out = render("README.md", "`a\\*b`") h.contains("code keeps its backslashes", out, "a\\*b") out = render("README.md", "© © © &") h.contains("character references pass through", out, "

© © © &

") out = render("README.md", "AT&T & &#xZZ; �") h.contains("a bare ampersand is escaped", out, "

AT&T & &#xZZ; &#12345678;

") out = render("README.md", "`&`") h.contains("code shows a reference as written", out, "&amp;") out = render("README.md", "run `x < y` here") h.contains("inline code is escaped", out, "x < y") out = render("README.md", "raw here") h.contains("markup in text reaches the page escaped", out, "<script>alert(1)</script>") h.excludes("no live tag slips through", out, "