-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
126 lines (111 loc) · 3.59 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import assert from 'node:assert/strict'
import test from 'node:test'
import {retext} from 'retext'
import retextSentenceSpacing from 'retext-sentence-spacing'
const mixed = [
'One sentence. Two sentences.',
'',
'One sentence. Two sentences.',
'',
'One sentence.\nTwo sentences.'
].join('\n')
test('retextSentenceSpacing', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(
Object.keys(await import('retext-sentence-spacing')).sort(),
['default']
)
})
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: 'Unexpected 2 spaces between sentence, expected 1 space',
line: 3,
name: '3:14-3:16',
place: {
start: {line: 3, column: 14, offset: 43},
end: {line: 3, column: 16, offset: 45}
},
reason: 'Unexpected 2 spaces between sentence, expected 1 space',
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: Unexpected 2 spaces between sentence, expected 1 space'
])
}
)
})
)
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: Unexpected 1 space between sentence, expected 2 spaces'
])
}
)
})
)
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: Unexpected spaces between sentence, expected a line ending',
'3:14-3:16: Unexpected spaces between sentence, expected a line ending'
])
}
)
})
)
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: Unexpected 3 spaces between sentence, expected 1 space'
])
})
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), [])
}
)
})