-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
docValidator.test.ts
166 lines (148 loc) · 8.66 KB
/
docValidator.test.ts
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
import assert from 'assert';
import { promises as fs } from 'fs';
import * as path from 'path';
import { createTextDocument, TextDocument } from '../Models/TextDocument';
import { AutoCache } from '../util/simpleCache';
import { DocumentValidator } from './docValidator';
import { ValidationIssue } from '../Models/ValidationIssue';
const docCache = new AutoCache(_loadDoc, 100);
const fixturesDir = path.join(__dirname, '../../fixtures');
const oc = expect.objectContaining;
const ac = expect.arrayContaining;
describe('docValidator', () => {
test('DocumentValidator', () => {
const doc = td(__filename, '/** This is some code */');
const dVal = new DocumentValidator(doc, {}, {});
expect(dVal.document).toBe(doc);
// dVal.prepareSync();
// expect(dVal.checkText([0, 0], '', [])).toEqual([]);
});
test.each`
filename
${__filename}
${path.join(__dirname, '../../package.json')}
`('DocumentValidator prepare $filename', async ({ filename }) => {
const doc = await loadDoc(filename);
const dVal = new DocumentValidator(doc, {}, {});
expect(dVal.ready).toBe(false);
await expect(dVal.prepare()).resolves.toBeUndefined();
expect(dVal.ready).toBe(true);
});
// cspell:ignore Helllo grrrr dockblock
test.each`
filename | startText | endText | expected
${__filename} | ${'__filename'} | ${undefined} | ${[]}
${fix('sample-with-errors.ts')} | ${"'H"} | ${"'"} | ${[oc({ text: 'Helllo' })]}
${fix('sample-with-errors.ts')} | ${'/**'} | ${'*/'} | ${[oc({ text: 'dockblock' })]}
${fix('sample-with-errors.ts')} | ${'main'} | ${undefined} | ${[]}
${fix('sample-with-cspell-directives.ts')} | ${'grrrr'} | ${undefined} | ${[]}
${fixDict('remote/test.txt')} | ${'New'} | ${'Paris'} | ${[]}
`('checkText async $filename "$startText"', async ({ filename, startText, endText, expected }) => {
const doc = await loadDoc(filename);
const dVal = new DocumentValidator(doc, {}, {});
await dVal.prepare();
const startOffset = doc.text.indexOf(startText);
const endOffset = endText
? doc.text.indexOf(endText, startOffset + startText.length) + endText.length
: startOffset + startText.length;
assert(startOffset >= 0);
const range = [startOffset, endOffset] as const;
const text = doc.text.slice(startOffset, endOffset);
expect(dVal.checkText(range, text, [])).toEqual(expected);
expect(dVal.prepTime).toBeGreaterThan(0);
});
test.each`
filename | text | configFile | expected
${__filename} | ${'__filename'} | ${undefined} | ${[]}
${fix('sample-with-errors.ts')} | ${'Helllo'} | ${undefined} | ${[oc({ text: 'Helllo' })]}
${fix('sample-with-errors.ts')} | ${'Helllo'} | ${fix('cspell.json')} | ${[oc({ text: 'Helllo' })]}
${fix('sample-with-errors.ts')} | ${'main'} | ${undefined} | ${[]}
${fix('sample-with-cspell-directives.ts')} | ${'grrrr'} | ${undefined} | ${[]}
`('checkText sync $filename "$text"', async ({ filename, text, expected, configFile }) => {
const doc = await loadDoc(filename);
const dVal = new DocumentValidator(doc, { configFile }, {});
dVal.prepareSync();
const offset = doc.text.indexOf(text);
assert(offset >= 0);
const range = [offset, offset + text.length] as const;
expect(dVal.checkText(range, text, [])).toEqual(expected);
expect(dVal.prepTime).toBeGreaterThan(0);
});
test.each`
filename | text | expected
${fix('sample-with-errors.ts')} | ${'Helllo'} | ${[oc({ text: 'Helllo', suggestions: ac(['hello']) })]}
`('checkText suggestions $filename "$text"', async ({ filename, text, expected }) => {
const doc = await loadDoc(filename);
const dVal = new DocumentValidator(doc, { generateSuggestions: true }, {});
dVal.prepareSync();
const offset = doc.text.indexOf(text);
assert(offset >= 0);
const range = [offset, offset + text.length] as const;
const result = dVal.checkText(range, text, []);
expect(result).toEqual(expected);
for (const r of result) {
expect(r.suggestions).toBe(r.suggestions);
}
expect(dVal.prepTime).toBeGreaterThan(0);
});
// cspell:ignore kount naame colector Reciever reciever recievers serrors dockblock
test.each`
filename | maxDuplicateProblems | expectedIssues | expectedRawIssues
${fix('sample-with-errors.ts')} | ${undefined} | ${['dockblock', 'Helllo']} | ${undefined}
${fix('sample-with-many-errors.ts')} | ${undefined} | ${['reciever', 'naame', 'naame', 'naame', 'reciever', 'Reciever', 'naame', 'Reciever', 'naame', 'kount', 'Reciever', 'kount', 'colector', 'recievers', 'Reciever', 'recievers', 'recievers']} | ${undefined}
${fix('sample-with-many-errors.ts')} | ${1} | ${['reciever', 'naame', 'Reciever', 'kount', 'colector', 'recievers']} | ${undefined}
${fix('parser/sample.ts')} | ${1} | ${['serrors']} | ${['\\x73errors']}
${fix('sample-with-directives-errors.ts')} | ${1} | ${['disable-prev', 'ignored', 'world', 'enable-line']} | ${undefined}
`(
'checkDocument $filename $maxDuplicateProblems',
async ({ filename, maxDuplicateProblems, expectedIssues, expectedRawIssues }) => {
const doc = await loadDoc(filename);
const dVal = new DocumentValidator(doc, { generateSuggestions: false }, { maxDuplicateProblems });
await dVal.prepare();
const r = dVal.checkDocument();
expect(r.map((issue) => issue.text)).toEqual(expectedIssues);
expect(extractRawText(doc.text, r)).toEqual(expectedRawIssues ?? expectedIssues);
}
);
test('updateDocumentText', () => {
// cspell:ignore foor
const expectedIssues = [
oc({
text: 'foor',
isFound: false,
isFlagged: false,
line: { text: 'foor\n', offset: 14, position: { character: 0, line: 3 } },
}),
];
const doc = td('files://words.txt', 'one\ntwo\nthree\nfoor\n', 'plaintext');
const dVal = new DocumentValidator(doc, { generateSuggestions: false }, {});
dVal.prepareSync();
const r = dVal.checkDocument();
expect(r).toEqual(expectedIssues);
dVal.updateDocumentText(doc.text + '# cspell:ignore foor\n');
expect(dVal.checkDocument()).toEqual([]);
});
});
function extractRawText(text: string, issues: ValidationIssue[]): string[] {
return issues.map((issue) => {
const start = issue.offset;
const end = start + (issue.length ?? issue.text.length);
return text.slice(start, end);
});
}
function td(uri: string, content: string, languageId?: string, locale?: string, version = 1): TextDocument {
return createTextDocument({ uri, content, languageId, locale, version });
}
async function _loadDoc(filename: string): Promise<TextDocument> {
const content = await fs.readFile(filename, 'utf8');
return createTextDocument({ uri: filename, content });
}
function loadDoc(filename: string) {
return docCache.get(filename);
}
function fix(...fixtureFile: string[]): string {
return path.resolve(path.join(fixturesDir, 'docValidator'), ...fixtureFile);
}
function fixDict(...fixtureFile: string[]): string {
return fix('../dictionaries', ...fixtureFile);
}