adds test for issue #2

This commit is contained in:
Rasmus Andersson 2020-10-15 13:37:03 -07:00
parent 7e7fcfd464
commit 352fd5008b
4 changed files with 102 additions and 59 deletions

11
test/issue2.js Normal file
View file

@ -0,0 +1,11 @@
// https://github.com/rsms/markdown-wasm/issues/2
const { checkHTMLResult, exit } = require("./testutil")
// When MD4C_USE_UTF8 is not defined for md4c, the example input here fail to parse as a valid
// reference link. In that case, instead of the expected output, we get "<p>[Á]</p>".
checkHTMLResult("MD4C_USE_UTF8", `
[á]: /url
[Á]
`, `<p><a href="/url">Á</a></p>\n`)
exit()

View file

@ -1,6 +1,5 @@
// https://github.com/rsms/markdown-wasm/issues/5
const libdir = process.argv.includes("-debug") ? "build/debug" : "dist"
const md = require(`../${libdir}/markdown.node.js`)
const { checkHTMLResult, exit } = require("./testutil")
// this triggers the bug
const input1 = Buffer.from(
@ -38,59 +37,7 @@ const expected3 = Buffer.from(
"</code></pre>\n"
, "utf8")
let fails = 0
check("test1", expected1, input1)
check("test2", expected2, input2)
check("test3", expected3, input3)
function check(name, expected, inputData) {
const actual = Buffer.from(md.parse(inputData, { asMemoryView: true }))
if (expected.compare(actual) == 0) {
console.log(`${name} OK`)
return
}
fails++
const line = "——————————————————————————————————————————————————"
const wave = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
console.error(`${name} FAIL.`)
console.error(`\n\nExpected output:\n${line}`)
inspectBuf(expected, actual)
console.error(`${line}\n\nActual output:\n${line}`)
inspectBuf(actual, expected)
console.error(line)
function inspectBuf(buf, otherbuf) {
process.stderr.write(buf)
if (buf[buf.length-1] != 0x0a) {
process.stderr.write("<no-ending-line-break>\n")
}
console.error(wave)
const styleReset = "\x1b[22;39m"
const styleNone = s=>s
const styleDiff = process.stderr.isTTY ? s => "\x1b[1;33m"+s+styleReset : styleNone
const styleErr = process.stderr.isTTY ? s => "\x1b[1;31m"+s+styleReset : styleNone
for (let i = 0; i < buf.length; i++) {
let b = buf[i]
let style = styleNone
if (b < 0x20 && b != 0x09 && b != 0x0A && b != 0x0D) {
// byte is unexpected control character (except TAB, CR, LF)
style = styleErr
} else if (otherbuf && otherbuf[i] != b) {
style = styleDiff
}
process.stderr.write(style(b.toString(16).padStart(2, '0')) + " ")
if (b == 0x0a) {
process.stderr.write("\n")
}
}
}
}
process.exit(fails > 0 ? 1 : 0)
checkHTMLResult("test1", input1, expected1)
checkHTMLResult("test2", input2, expected2)
checkHTMLResult("test3", input3, expected3)
exit()

View file

@ -2,4 +2,6 @@
set -euo pipefail
cd "$(dirname "$0")"
node issue5.js "$@"
for f in issue*.js; do
node "$f" "$@"
done

83
test/testutil.js Normal file
View file

@ -0,0 +1,83 @@
const Path = require("path")
const libdir = process.argv.includes("-debug") ? "build/debug" : "dist"
const md = require(`../${libdir}/markdown.node.js`)
const line = "——————————————————————————————————————————————————"
const wave = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
exports.numFailures = 0
exports.checkHTMLResult = function check(name, inputData, expectedOutputData) {
if (typeof inputData == "string") {
inputData = Buffer.from(inputData, "utf8")
}
if (typeof expectedOutputData == "string") {
expectedOutputData = Buffer.from(expectedOutputData, "utf8")
}
const actual = Buffer.from(md.parse(inputData, { asMemoryView: true }))
if (expectedOutputData.compare(actual) == 0) {
log(`${name} OK`)
return true
}
exports.numFailures++
logerr(`${name} FAIL`)
console.error(`\n\nExpected output:\n${line}`)
inspectBuf(expectedOutputData, actual)
console.error(`${line}\n\nActual output:\n${line}`)
inspectBuf(actual, expectedOutputData)
console.error(line)
}
exports.exit = function() {
process.exit(exports.numFailures > 0 ? 1 : 0)
}
exports.log = log
exports.logerr = logerr
const logprefix = Path.basename(process.argv[1])+":"
function log() {
console.log.apply(console, [logprefix].concat([].slice.call(arguments)))
}
function logerr() {
console.error.apply(console, [logprefix].concat([].slice.call(arguments)))
}
exports.inspectBuf = inspectBuf
function inspectBuf(buf, otherbuf) {
process.stderr.write(buf)
if (buf[buf.length-1] != 0x0a) {
process.stderr.write("<no-ending-line-break>\n")
}
console.error(wave)
const styleReset = "\x1b[22;39m"
const styleNone = s=>s
const styleDiff = process.stderr.isTTY ? s => "\x1b[1;33m"+s+styleReset : styleNone
const styleErr = process.stderr.isTTY ? s => "\x1b[1;31m"+s+styleReset : styleNone
for (let i = 0; i < buf.length; i++) {
let b = buf[i]
let style = styleNone
if (b < 0x20 && b != 0x09 && b != 0x0A && b != 0x0D) {
// byte is unexpected control character (except TAB, CR, LF)
style = styleErr
} else if (otherbuf && otherbuf[i] != b) {
style = styleDiff
}
process.stderr.write(style(b.toString(16).padStart(2, '0')) + " ")
if (b == 0x0a) {
process.stderr.write("\n")
}
}
}