Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 26, 2023
1 parent 223bf98 commit 4a1a05e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,22 @@
"uvu": "^0.5.0"
},
"devDependencies": {
"@types/power-assert": "^1.0.0",
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"commonmark.json": "^0.30.0",
"esbuild": "^0.17.0",
"gzip-size-cli": "^5.0.0",
"hast-util-from-html": "^1.0.0",
"hast-util-to-html": "^8.0.0",
"mdast-util-to-hast": "^12.0.0",
"micromark-build": "^1.0.0",
"prettier": "^2.0.0",
"rehype-parse": "^8.0.0",
"rehype-stringify": "^9.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"tape": "^5.0.0",
"terser": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"unified": "^10.0.0",
"xo": "^0.53.0"
},
"scripts": {
Expand All @@ -100,7 +97,15 @@
"unicorn/prefer-code-point": "off",
"unicorn/prefer-switch": "off",
"unicorn/prefer-node-protocol": "off"
}
},
"overrides": [
{
"files": "test/**/*.js",
"rules": {
"no-await-in-loop": "off"
}
}
]
},
"remarkConfig": {
"plugins": [
Expand Down
61 changes: 30 additions & 31 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@
* @typedef {import('mdast').Root} Root
*/

import assert from 'node:assert/strict'
import {Buffer} from 'node:buffer'
import fs from 'fs'
import path from 'path'
import assert from 'assert'
import fs from 'node:fs/promises'
import test from 'tape'
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {toHast} from 'mdast-util-to-hast'
import {toString} from 'mdast-util-to-string'
import {fromHtml} from 'hast-util-from-html'
import {toHtml} from 'hast-util-to-html'
import {commonmark} from 'commonmark.json'
import {fromMarkdown} from '../dev/index.js'

const join = path.join

test('mdast-util-from-markdown', (t) => {
t.equal(typeof fromMarkdown, 'function', 'should expose a function')

Expand Down Expand Up @@ -1040,26 +1035,32 @@ test('mdast-util-from-markdown', (t) => {
t.end()
})

test('fixtures', (t) => {
const base = join('test', 'fixtures')
const files = fs.readdirSync(base).filter((d) => path.extname(d) === '.md')
test('fixtures', async (t) => {
const base = new URL('fixtures/', import.meta.url)

const files = await fs.readdir(base)
let index = -1

while (++index < files.length) {
const file = files[index]
const stem = path.basename(file, path.extname(file))
const fp = join(base, stem + '.json')
const doc = fs.readFileSync(join(base, stem + '.md'))

if (!/\.md$/i.test(file)) {
continue
}

const stem = file.split('.').slice(0, -1).join('.')
const fp = new URL(stem + '.json', base)
const doc = await fs.readFile(new URL(file, base))
const actual = fromMarkdown(doc)
/** @type {Root} */
let expected

try {
expected = JSON.parse(String(fs.readFileSync(fp)))
expected = JSON.parse(String(await fs.readFile(fp)))
} catch {
// New fixture.
expected = actual
fs.writeFileSync(fp, JSON.stringify(actual, null, 2) + '\n')
await fs.writeFile(fp, JSON.stringify(actual, null, 2) + '\n')
}

t.deepEqual(actual, expected, stem)
Expand All @@ -1070,6 +1071,8 @@ test('fixtures', (t) => {

test('commonmark', (t) => {
let index = -1

// To do: update micromark.
// Changes in living version of CommonMark.
const skip = new Set([623, 624])

Expand All @@ -1079,23 +1082,19 @@ test('commonmark', (t) => {
}

const example = commonmark[index]
const root = fromMarkdown(example.markdown.slice(0, -1))
const hast = toHast(root, {allowDangerousHtml: true})
assert(hast && hast.type === 'root', 'expected `root`')
const html = toHtml(hast, {
allowDangerousHtml: true,
entities: {useNamedReferences: true},
closeSelfClosing: true
})
const input = example.markdown.slice(0, -1)
const output = example.html.slice(0, -1)

const reformat = unified()
.use(rehypeParse, {fragment: true})
.use(rehypeStringify)

const actual = reformat.processSync(html).toString()
const expected = reformat.processSync(example.html.slice(0, -1)).toString()
const mdast = fromMarkdown(input)
const hast = toHast(mdast, {allowDangerousHtml: true})
assert(hast && hast.type === 'root', 'expected `root`')
const actual = toHtml(hast, {allowDangerousHtml: true})

t.deepLooseEqual(actual, expected, example.section + ' (' + index + ')')
t.deepLooseEqual(
toHtml(fromHtml(actual, {fragment: true})),
toHtml(fromHtml(output, {fragment: true})),
example.section + ' (' + index + ')'
)
}

t.end()
Expand Down

0 comments on commit 4a1a05e

Please sign in to comment.