-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathtraceWord.test.ts
83 lines (73 loc) · 3.57 KB
/
traceWord.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
import { describe, expect, test } from 'vitest';
import { pathPackageFixturesURL } from '../../test-util/test.locations.cjs';
import type { TextDocumentRef } from '../Models/TextDocument.js';
import { searchForConfig } from '../Settings/index.js';
import { getDictionaryInternal } from '../SpellingDictionary/index.js';
import { toUri } from '../util/Uri.js';
import { determineTextDocumentSettings } from './determineTextDocumentSettings.js';
import type { WordSplits } from './traceWord.js';
import { traceWord } from './traceWord.js';
const fixturesURL = new URL('traceWords/', pathPackageFixturesURL);
const urlReadme = new URL('README.md', fixturesURL);
const expectedConfigURL = new URL('cspell.config.yaml', fixturesURL);
const ac = <T>(a: Array<T>) => expect.arrayContaining(a);
const oc = <T>(obj: T) => expect.objectContaining(obj);
describe('traceWord', async () => {
const doc: TextDocumentRef = { uri: toUri(import.meta.url), languageId: 'typescript' };
const fixtureSettings = (await searchForConfig(urlReadme)) || {};
const baseSettings = await determineTextDocumentSettings(doc, fixtureSettings);
const dicts = await getDictionaryInternal(baseSettings);
test('traceWord', () => {
const r = traceWord('trace', dicts, baseSettings);
expect(r.filter((r) => r.found)).toEqual(
ac([
{
word: 'trace',
found: true,
foundWord: 'trace',
forbidden: false,
noSuggest: false,
dictName: 'en_us',
dictSource: expect.any(String),
configSource: undefined,
errors: undefined,
},
]),
);
});
test.each`
word | expected
${'word'} | ${[wft('word')]}
${'word_word'} | ${[wff('word_word'), wft('word'), wft('word')]}
${'word_nword'} | ${[wff('word_nword'), wft('word'), wff('nword')] /* cspell:ignore nword */}
${'ISpellResult'} | ${[wff('ISpellResult'), wft('I'), wft('Spell'), wft('Result')]}
${'ERRORcode'} | ${[wft('ERRORcode'), wft('ERROR'), wft('code')]}
`('traceWord splits $word', ({ word, expected }) => {
const r = traceWord(word, dicts, baseSettings);
expect(r.splits).toEqual(expected);
});
test.each`
word | expected
${'word_word'} | ${{ ...wft('word'), dictName: 'en_us' }}
${'ISpellResult'} | ${{ ...wft('Result'), foundWord: 'result', dictName: 'en_us' }}
${'ERRORcode'} | ${{ ...wft('ERRORcode'), foundWord: 'errorcode', dictName: 'node' }}
${'ERRORcode'} | ${{ ...wft('ERROR'), foundWord: 'error', dictName: 'en_us' }}
${'apple-pie'} | ${{ ...wft('pie'), dictName: 'en_us' }}
${"can't"} | ${{ ...wft("can't"), dictName: 'en_us' }}
${'canNOT'} | ${{ ...wft('canNOT'), foundWord: 'cannot', dictName: 'en_us' }}
${'baz'} | ${{ ...wft('baz'), foundWord: 'baz', dictName: '[words]', dictSource: expectedConfigURL.href }}
`('traceWord check found $word', ({ word, expected }) => {
const r = traceWord(word, dicts, baseSettings);
const matching = r.filter((r) => r.word === expected.word && r.found === expected.found);
expect(matching).toEqual(ac([oc(expected)]));
});
});
function wf(word: string, found: boolean): WordSplits {
return { word, found };
}
function wft(word: string): WordSplits {
return wf(word, true);
}
function wff(word: string): WordSplits {
return wf(word, false);
}