Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Feb 3, 2025
1 parent 5c3b277 commit ff5d9df
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 52 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
"@types/node": "^22.0.0",
"c8": "^10.0.0",
"prettier": "^3.0.0",
"rehype": "^13.0.0",
"rehype-parse": "^9.0.0",
"rehype-stringify": "^10.0.0",
"remark-cli": "^12.0.0",
"remark-preset-wooorm": "^11.0.0",
"type-coverage": "^2.0.0",
"typescript": "^5.0.0",
"unified": "^11.0.0",
"xo": "^0.60.0"
},
"exports": "./index.js",
Expand Down
125 changes: 74 additions & 51 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import {common} from 'lowlight'
import {rehype} from 'rehype'
import rehypeHighlight from 'rehype-highlight'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'

test('rehypeHighlight', async function (t) {
await t.test('should expose the public api', async function () {
Expand All @@ -16,9 +18,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should work on empty code', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight, {detect: true})
.use(rehypeStringify)
.process(
['<h1>Hello World!</h1>', '', '<pre><code></code></pre>'].join('\n')
)
Expand All @@ -34,9 +37,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should not highlight (no class)', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight)
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -56,9 +60,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should highlight (`detect`, no class)', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight, {detect: true})
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -80,9 +85,10 @@ test('rehypeHighlight', async function (t) {
await t.test(
'should highlight (detect, no class, subset)',
async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight, {detect: true, subset: ['arduino']})
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -105,9 +111,10 @@ test('rehypeHighlight', async function (t) {
await t.test(
'should not highlight (`detect: false`, no class)',
async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight, {detect: false})
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -128,9 +135,10 @@ test('rehypeHighlight', async function (t) {
)

await t.test('should highlight (prefix without dash)', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight, {detect: true, prefix: 'foo'})
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -150,9 +158,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should highlight (prefix with dash)', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight, {detect: true, prefix: 'foo-'})
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -172,9 +181,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should highlight (lang class)', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight)
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -196,9 +206,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should highlight (language class)', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight)
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -220,9 +231,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should highlight (long name)', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight)
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -244,9 +256,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should not highlight (`no-highlight`)', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight)
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -270,9 +283,10 @@ test('rehypeHighlight', async function (t) {
await t.test(
'should prefer `no-highlight` over a `language-*` class',
async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight)
.use(rehypeStringify)
.process(
'<h1>Hello World!</h1>\n<pre><code class="lang-js no-highlight">alert(1)</code></pre>'
)
Expand All @@ -285,9 +299,10 @@ test('rehypeHighlight', async function (t) {
)

await t.test('should not highlight (`nohighlight`)', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight)
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -309,9 +324,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should warn on missing languages', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight)
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -329,9 +345,10 @@ test('rehypeHighlight', async function (t) {
await t.test(
'should not highlight plainText-ed languages',
async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight, {plainText: ['js']})
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -355,9 +372,10 @@ test('rehypeHighlight', async function (t) {

await t.test('should not remove contents', async function () {
// For some reason this isn’t detected as c++.
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight, {detect: true, subset: ['cpp']})
.use(rehypeStringify)
.process(`<pre><code>def add(a, b):\n return a + b</code></pre>`)

assert.equal(
Expand All @@ -367,9 +385,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should support multiple `code`s in a `pre`', async function () {
const file = await rehype()
.data('settings', {fragment: true})
.use(rehypeHighlight).process(`<pre>
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight)
.use(rehypeStringify).process(`<pre>
<code class="language-javascript">const a = 1;</code>
<code class="language-python">printf("x")</code>
</pre>`)
Expand All @@ -381,9 +400,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should reprocess exact', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight)
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -405,11 +425,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should parse custom language', async function () {
const file = await rehype()
.data('settings', {fragment: true})
.use(rehypeHighlight, {
aliases: {javascript: ['funkyscript']}
})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight, {aliases: {javascript: ['funkyscript']}})
.use(rehypeStringify)
.process(
'<pre><code class="lang-funkyscript">console.log(1)</code></pre>'
)
Expand All @@ -421,9 +440,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should reprocess exact', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight)
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -445,9 +465,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should ignore comments', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight, {detect: true})
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -467,9 +488,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should support `<br>` elements', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight)
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -490,9 +512,10 @@ test('rehypeHighlight', async function (t) {
})

await t.test('should register languages', async function () {
const file = await rehype()
.data('settings', {fragment: true})
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeHighlight, {languages: {...common, test: testLang}})
.use(rehypeStringify)
.process(
[
'<h1>Hello World!</h1>',
Expand Down

0 comments on commit ff5d9df

Please sign in to comment.