Skip to content

Commit

Permalink
Refactor to use node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 10, 2023
1 parent bfa7aab commit e2fe800
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 121 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"@types/tape": "^5.0.0",
"@types/node": "^20.0.0",
"c8": "^8.0.0",
"prettier": "^3.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"retext": "^9.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^5.0.0",
"xo": "^0.56.0"
Expand Down
233 changes: 114 additions & 119 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* @typedef {import('./lib/index.js').Preferred} Preferred
*/

import test from 'tape'
import assert from 'node:assert/strict'
import test from 'node:test'
import {retext} from 'retext'
import retextSentenceSpacing from './index.js'

Expand All @@ -14,136 +15,130 @@ const mixed = [
'One sentence.\nTwo sentences.'
].join('\n')

test('retext-sentence-spacing', (t) => {
t.plan(13)

retext()
.use(retextSentenceSpacing, {preferred: 1})
.process(mixed)
.then((file) => {
t.deepEqual(
JSON.parse(JSON.stringify({...file.messages[0], ancestors: []})),
{
ancestors: [],
column: 14,
fatal: false,
message: 'Expected `1` space between sentences, not `2`',
line: 3,
name: '3:14-3:16',
place: {
start: {line: 3, column: 14, offset: 43},
end: {line: 3, column: 16, offset: 45}
},
reason: 'Expected `1` space between sentences, not `2`',
ruleId: 'space',
source: 'retext-sentence-spacing',
actual: ' ',
expected: [' '],
url: 'https://github.com/retextjs/retext-sentence-spacing#readme'
test('retextSentenceSpacing', async function (t) {
await t.test('should emit a message w/ metadata', async function () {
const file = await retext()
.use(retextSentenceSpacing, {preferred: 1})
.process(mixed)

assert.deepEqual(
JSON.parse(JSON.stringify({...file.messages[0], ancestors: []})),
{
ancestors: [],
column: 14,
fatal: false,
message: 'Expected `1` space between sentences, not `2`',
line: 3,
name: '3:14-3:16',
place: {
start: {line: 3, column: 14, offset: 43},
end: {line: 3, column: 16, offset: 45}
},
'should emit messages'
reason: 'Expected `1` space between sentences, not `2`',
ruleId: 'space',
source: 'retext-sentence-spacing',
actual: ' ',
expected: [' '],
url: 'https://github.com/retextjs/retext-sentence-spacing#readme'
}
)
})

const ones = /** @type {const} */ ([null, 'space', 1])
const twos = /** @type {const} */ ([2, 'double-space'])
const zeros = /** @type {const} */ ([0, 'newline'])

await Promise.all(
ones.map(function (preferred) {
return t.test(
'should catch double spaces w/ `' + preferred + '`',
async function () {
const file = await retext()
.use(retextSentenceSpacing, {preferred})
.process(mixed)

assert.deepEqual(file.messages.map(String), [
'3:14-3:16: Expected `1` space between sentences, not `2`'
])
}
)
}, t.ifErr)

one(null)
one('space')
one(1)
two(2)
two('double-space')
zero(0)
zero('newline')

/** @param {Preferred} pref */
function one(pref) {
retext()
.use(retextSentenceSpacing, {preferred: pref})
.process(mixed)
.then((file) => {
t.deepEqual(
file.messages.map(String),
['3:14-3:16: Expected `1` space between sentences, not `2`'],
'should catch double spaces when preferred == ' + pref
)
}, t.ifErr)
}

/** @param {Preferred} pref */
function two(pref) {
retext()
.use(retextSentenceSpacing, {preferred: pref})
.process(mixed)
.then((file) => {
t.deepEqual(
file.messages.map(String),
['1:14-1:15: Expected `2` spaces between sentences, not `1`'],
'should catch single spaces when preferred == ' + pref
)
}, t.ifErr)
}

/** @param {Preferred} pref */
function zero(pref) {
retext()
.use(retextSentenceSpacing, {preferred: pref})
.process(mixed)
.then((file) => {
t.deepEqual(
file.messages.map(String),
[
})
)

await Promise.all(
twos.map(function (preferred) {
return t.test(
'should catch single spaces w/ `' + preferred + '`',
async function () {
const file = await retext()
.use(retextSentenceSpacing, {preferred})
.process(mixed)

assert.deepEqual(file.messages.map(String), [
'1:14-1:15: Expected `2` spaces between sentences, not `1`'
])
}
)
})
)

await Promise.all(
zeros.map(function (preferred) {
return t.test(
'should catch spaces w/ `' + preferred + '`',
async function () {
const file = await retext()
.use(retextSentenceSpacing, {preferred})
.process(mixed)

assert.deepEqual(file.messages.map(String), [
'1:14-1:15: Expected a newline between sentences, not `1` space',
'3:14-3:16: Expected a newline between sentences, not `2` spaces'
],
'should catch spaces when preferred == ' + pref
)
}, t.ifErr)
}

retext()
.use(retextSentenceSpacing)
.process('One sentence. Three sentences.')
.then((file) => {
t.deepEqual(
file.messages.map(String),
['1:14-1:17: Expected `1` space between sentences, not `3`'],
'should catch more than two spaces'
)
}, t.ifErr)

retext()
.use(retextSentenceSpacing)
.process('One sentence.\tFour sentences.')
.then((file) => {
t.deepEqual(
file.messages.map(String),
[],
'should not emit messages for non-space white-space'
])
}
)
}, t.ifErr)
})
)

await t.test('should catch more than two spaces', async function () {
const file = await retext()
.use(retextSentenceSpacing)
.process('One sentence. Three sentences.')

assert.deepEqual(file.messages.map(String), [
'1:14-1:17: Expected `1` space between sentences, not `3`'
])
})

t.throws(
() => {
await t.test(
'should not emit messages for non-space whitespace',
async function () {
const file = await retext()
.use(retextSentenceSpacing)
.process('One sentence.\tFour sentences.')

assert.deepEqual(file.messages.map(String), [])
}
)

await t.test('should throw for preferred lower than 1', async function () {
assert.throws(() => {
// @ts-expect-error: runtime.
retext().use(retextSentenceSpacing, {preferred: -1}).freeze()
},
/Error: Expected `options.preferred` to be `'space'`, `'newline'`, or a `number` between \(including\) `0` and `2`/,
'should throw for preferred lower than 1'
)
}, /Error: Expected `options.preferred` to be `'space'`, `'newline'`, or a `number` between \(including\) `0` and `2`/)
})

t.throws(
() => {
await t.test('should throw for preferred higher than 2', async function () {
assert.throws(() => {
// @ts-expect-error: runtime.
retext().use(retextSentenceSpacing, {preferred: 3}).freeze()
},
/Error: Expected `options.preferred` to be `'space'`, `'newline'`, or a `number` between \(including\) `0` and `2`/,
'should throw for preferred higher than 2'
)
}, /Error: Expected `options.preferred` to be `'space'`, `'newline'`, or a `number` between \(including\) `0` and `2`/)
})

t.throws(
() => {
await t.test('should throw for non-numbers', async function () {
assert.throws(() => {
// @ts-expect-error: runtime.
retext().use(retextSentenceSpacing, {preferred: 'foo'}).freeze()
},
/Error: Expected `options.preferred` to be `'space'`, `'newline'`, or a `number`/,
'should throw for non-numbers'
)
}, /Error: Expected `options.preferred` to be `'space'`, `'newline'`, or a `number`/)
})
})

0 comments on commit e2fe800

Please sign in to comment.