-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
248 lines (215 loc) · 8.68 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import assert from 'node:assert/strict'
import test from 'node:test'
import {retext} from 'retext'
import retextSyntaxUrls from 'retext-syntax-urls'
import retextQuotes from 'retext-quotes'
const mixed = [
'“One ‘sentence’. Two sentences.”',
'"One \'sentence\'. Two sentences."'
].join('\n\n')
const apostrophes = [
"Mr. Jones' golf clubs.",
"'Mr. Jones' golf clubs.",
'Mr. Jones’ golf clubs.',
'‘Mr. Jones’ golf clubs.'
].join('\n\n')
const nesting = [
'“One ‘sentence’. Two sentences.”',
'‘One “sentence”. Two sentences.’',
'"One \'sentence\'. Two sentences."',
'\'One "sentence". Two sentences.\''
].join('\n\n')
const moreApostrophes = 'Isn’t it funny? It was acceptable in the ’80s'
const soManyOpenings = '“Open this, ‘Open that, “open here, ‘open there'
const thisAndThat = '"this and \'that\'"'
test('retextQuotes', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('retext-quotes')).sort(), [
'default'
])
})
await t.test('should emit a message w/ metadata', async function () {
const file = await retext().use(retextQuotes).process('Isn\'t it "funny"?')
assert.deepEqual(
JSON.parse(JSON.stringify({...file.messages[0], ancestors: []})),
{
ancestors: [],
column: 4,
fatal: false,
message: "Unexpected straight apostrophe `'`, expected `’`",
line: 1,
name: '1:4-1:5',
place: {
start: {line: 1, column: 4, offset: 3},
end: {line: 1, column: 5, offset: 4}
},
reason: "Unexpected straight apostrophe `'`, expected `’`",
ruleId: 'apostrophe',
source: 'retext-quotes',
actual: "'",
expected: ['’'],
url: 'https://github.com/retextjs/retext-quotes#readme'
}
)
})
await t.test(
'should catch straight quotes when preferring smart',
async function () {
const file = await retext().use(retextQuotes).process(mixed)
assert.deepEqual(file.messages.map(String), [
'3:1-3:2: Unexpected straight quote `"`, expected `“`',
"3:6-3:7: Unexpected straight quote `'`, expected `‘`",
"3:15-3:16: Unexpected straight quote `'`, expected `’`",
'3:32-3:33: Unexpected straight quote `"`, expected `”`'
])
}
)
await t.test(
'should catch smart quotes when preferring straight',
async function () {
const file = await retext()
.use(retextQuotes, {preferred: 'straight'})
.process(mixed)
assert.deepEqual(file.messages.map(String), [
'1:1-1:2: Unexpected smart quote `“`, expected `"`',
"1:6-1:7: Unexpected smart quote `‘`, expected `'`",
"1:15-1:16: Unexpected smart quote `’`, expected `'`",
'1:32-1:33: Unexpected smart quote `”`, expected `"`'
])
}
)
await t.test(
'should detect common hard cases of apostrophes (when smart)',
async function () {
const file = await retext().use(retextQuotes).process(moreApostrophes)
assert.deepEqual(file.messages.map(String), [])
}
)
await t.test(
'should detect common hard cases of apostrophes (when straight)',
async function () {
const file = await retext()
.use(retextQuotes, {preferred: 'straight'})
.process(moreApostrophes)
assert.deepEqual(file.messages.map(String), [
"1:4-1:5: Unexpected smart apostrophe `’`, expected `'`",
"1:42-1:43: Unexpected smart apostrophe `’`, expected `'`"
])
}
)
await t.test(
'should detect apostrophes correctly (when preferring smart)',
async function () {
const file = await retext()
.use(retextQuotes, {preferred: 'smart'})
.process(apostrophes)
assert.deepEqual(file.messages.map(String), [
"1:10-1:11: Unexpected straight apostrophe `'`, expected `’`",
"3:1-3:2: Unexpected straight quote `'`, expected `“`",
"3:11-3:12: Unexpected straight quote `'`, expected `”`",
'7:1-7:2: Unexpected `‘` at this level of nesting, expected `“`',
'7:11-7:12: Unexpected `’` at this level of nesting, expected `”`'
])
}
)
await t.test(
'should detect apostrophes correctly (when preferring straight)',
async function () {
const file = await retext()
.use(retextQuotes, {preferred: 'straight'})
.process(apostrophes)
assert.deepEqual(file.messages.map(String), [
'3:1-3:2: Unexpected `\'` at this level of nesting, expected `"`',
'3:11-3:12: Unexpected `\'` at this level of nesting, expected `"`',
"5:10-5:11: Unexpected smart apostrophe `’`, expected `'`",
'7:1-7:2: Unexpected smart quote `‘`, expected `"`',
'7:11-7:12: Unexpected smart quote `’`, expected `"`'
])
}
)
await t.test(
'should detect nesting correctly (when preferring smart)',
async function () {
const file = await retext().use(retextQuotes).process(nesting)
assert.deepEqual(file.messages.map(String), [
'3:1-3:2: Unexpected `‘` at this level of nesting, expected `“`',
'3:6-3:7: Unexpected `“` at this level of nesting, expected `‘`',
'3:15-3:16: Unexpected `”` at this level of nesting, expected `’`',
'3:32-3:33: Unexpected `’` at this level of nesting, expected `”`',
'5:1-5:2: Unexpected straight quote `"`, expected `“`',
"5:6-5:7: Unexpected straight quote `'`, expected `‘`",
"5:15-5:16: Unexpected straight quote `'`, expected `’`",
'5:32-5:33: Unexpected straight quote `"`, expected `”`',
"7:1-7:2: Unexpected straight quote `'`, expected `“`",
'7:6-7:7: Unexpected straight quote `"`, expected `‘`',
'7:15-7:16: Unexpected straight quote `"`, expected `’`',
"7:32-7:33: Unexpected straight quote `'`, expected `”`"
])
}
)
await t.test(
'should detect nesting correctly (when preferring straight)',
async function () {
const file = await retext()
.use(retextQuotes, {preferred: 'straight'})
.process(nesting)
assert.deepEqual(file.messages.map(String), [
'1:1-1:2: Unexpected smart quote `“`, expected `"`',
"1:6-1:7: Unexpected smart quote `‘`, expected `'`",
"1:15-1:16: Unexpected smart quote `’`, expected `'`",
'1:32-1:33: Unexpected smart quote `”`, expected `"`',
'3:1-3:2: Unexpected smart quote `‘`, expected `"`',
"3:6-3:7: Unexpected smart quote `“`, expected `'`",
"3:15-3:16: Unexpected smart quote `”`, expected `'`",
'3:32-3:33: Unexpected smart quote `’`, expected `"`',
'7:1-7:2: Unexpected `\'` at this level of nesting, expected `"`',
'7:6-7:7: Unexpected `"` at this level of nesting, expected `\'`',
'7:15-7:16: Unexpected `"` at this level of nesting, expected `\'`',
'7:32-7:33: Unexpected `\'` at this level of nesting, expected `"`'
])
}
)
await t.test('should deal with funky nesting', async function () {
const file = await retext().use(retextQuotes).process(soManyOpenings)
assert.deepEqual(file.messages.map(String), [])
})
await t.test(
'should suggest based on the order of given straight quotes',
async function () {
const file = await retext()
.use(retextQuotes, {preferred: 'straight', straight: ["'", '"']})
.process(thisAndThat)
assert.deepEqual(file.messages.map(String), [
'1:1-1:2: Unexpected `"` at this level of nesting, expected `\'`',
'1:11-1:12: Unexpected `\'` at this level of nesting, expected `"`',
'1:16-1:17: Unexpected `\'` at this level of nesting, expected `"`',
'1:17-1:18: Unexpected `"` at this level of nesting, expected `\'`'
])
}
)
await t.test(
'should suggest based on the order (and markers) of given smart quotes',
async function () {
const file = await retext()
.use(retextQuotes, {smart: ['«»', '‹›']})
.process(thisAndThat)
assert.deepEqual(file.messages.map(String), [
'1:1-1:2: Unexpected straight quote `"`, expected `«`',
"1:11-1:12: Unexpected straight quote `'`, expected `‹`",
"1:16-1:17: Unexpected straight quote `'`, expected `›`",
'1:17-1:18: Unexpected straight quote `"`, expected `»`'
])
}
)
await t.test(
'should integrate with `retext-syntax-urls` and check source nodes',
async function () {
// GH-7.
const file = await retext()
.use(retextSyntaxUrls)
.use(retextQuotes, {preferred: 'straight'})
.process(thisAndThat)
assert.deepEqual(file.messages.map(String), [])
}
)
})