/
InDocSettings.test.ts
162 lines (144 loc) · 7.32 KB
/
InDocSettings.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
import * as Text from '../util/text';
import * as TextRange from '../util/TextRange';
import { isDefined } from '../util/util';
import * as InDoc from './InDocSettings';
const oc = expect.objectContaining;
// cSpell:ignore faullts straange
// cSpell:ignoreRegExp \w+s{4}\w+
// cSpell:ignoreRegExp /\/\/\/.*/
// cSpell:ignoreRegExp weird
const sampleCode = `
// cSpell:enableCompoundWords
// cSpell:disableCompoundWords
// cSpell: enableCOMPOUNDWords
// cSpell:words whiteberry, redberry, lightbrown
// cSpell: ignoreRegExp /\\/\\/\\/.*/
// cSpell:ignoreRegexp w\\w+berry
// cSpell::ignoreRegExp /
/* cSpell:ignoreRegExp \\w+s{4}\\w+ */
/* cSpell:ignoreRegExp /faullts[/]?/ */
const berries = ['whiteberry', 'redberry', 'blueberry'];
/* cSpell:ignore tripe, comment */
// cSpell:: ignoreWords tooo faullts
/// ignore triple comment, with misssspellings and faullts
/// mooree prooobleems onn thisss line tooo with wordberry
// misssspellings faullts
// weirdberry can be straange.
// cSpell:language en-US
// cspell:local
// cspell:locale es-ES
// cspell:local en, nl
// cspell:dictionaries lorem-ipsum
// LocalWords: one two three
// LocalWords:four five six
// localwords: seven eight nine
`;
// cspell:ignore againxx
const sampleText = `
# cSpell:disableCompoundWords
# cSpell:enableCOMPOUNDWords
# happydays arehere againxx
`;
// cspell:ignore popoutlist
const sampleTextWithIncompleteInDocSetting = `
// spell:dictionaries php
// spell:words const
// cspell:
// cspell:ignore popoutlist
const x = imp.popoutlist;
// cspell:ignore again
`;
// cspell:disable
const sampleTextWithBadRegexp = `
# cspell\x3AignoreRegExp "(foobar|foo_baz)"');
`;
// cspell:enable
describe('Validate InDocSettings', () => {
test('tests matching settings', () => {
const matches = InDoc.internal
.getPossibleInDocSettings(sampleCode)
.map((a) => a.slice(1).filter((a) => !!a))
.toArray();
expect(matches.map((a) => a[0])).toEqual([
'enableCompoundWords',
'disableCompoundWords',
' enableCOMPOUNDWords',
'words whiteberry, redberry, lightbrown',
' ignoreRegExp /\\/\\/\\/.*/',
'ignoreRegexp w\\w+berry',
'ignoreRegExp /',
'ignoreRegExp \\w+s{4}\\w+ */',
'ignoreRegExp /faullts[/]?/ */',
'ignore tripe, comment */',
' ignoreWords tooo faullts',
'language en-US',
'local',
'locale es-ES',
'local en, nl',
'dictionaries lorem-ipsum',
'LocalWords: one two three',
'LocalWords:four five six',
]);
});
test.each`
test | text | expected
${'Empty Doc'} | ${''} | ${{ id: 'in-doc-settings' }}
${'cSpell:enableCompoundWords'} | ${'cSpell:enableCompoundWords'} | ${oc({ allowCompoundWords: true })}
${'cSpell:ENABLECompoundWords'} | ${'cSpell:ENABLECompoundWords'} | ${oc({ allowCompoundWords: true })}
${'cSpell:disableCompoundWords'} | ${'cSpell:disableCompoundWords'} | ${oc({ allowCompoundWords: false })}
${'cSpell:disableCompoundWORDS'} | ${'cSpell:disableCompoundWORDS'} | ${oc({ allowCompoundWords: false })}
${'cSpell:ENABLECompoundWords\ncSpell:disableCompoundWords'} | ${'cSpell:ENABLECompoundWords\ncSpell:disableCompoundWords'} | ${oc({ allowCompoundWords: false })}
${'cSpell:disableCompoundWords\ncSpell:enableCompoundWords'} | ${'cSpell:disableCompoundWords\ncSpell:enableCompoundWords'} | ${oc({ allowCompoundWords: true })}
${'sampleText'} | ${sampleText} | ${oc({ allowCompoundWords: true })}
${'sampleCode'} | ${sampleCode} | ${oc({ allowCompoundWords: true })}
`('detect compound words setting: $test', ({ text, expected }) => {
expect(InDoc.getInDocumentSettings(text)).toEqual(expected);
});
test.each`
test | text | expected
${'Empty Doc'} | ${''} | ${{ id: 'in-doc-settings' }}
${'sampleTextWithIncompleteInDocSetting'} | ${sampleTextWithIncompleteInDocSetting} | ${oc({ words: ['const'], ignoreWords: ['popoutlist', 'again'], dictionaries: ['php'] })}
${'enableCaseSensitive'} | ${'// cspell:enableCaseSensitive'} | ${oc({ caseSensitive: true })}
${'disableCaseSensitive'} | ${'// cspell:disableCaseSensitive'} | ${oc({ caseSensitive: false })}
`('extract setting: $test', ({ text, expected }) => {
expect(InDoc.getInDocumentSettings(text)).toEqual(expected);
});
test('tests finding words to add to dictionary', () => {
const words = InDoc.internal.getWordsFromDocument(sampleCode);
// we match to the end of the line, so the */ is included.
expect(words).toEqual(['whiteberry', 'redberry', 'lightbrown', 'one', 'two', 'three', 'four', 'five', 'six']);
expect(InDoc.getIgnoreWordsFromDocument('Hello')).toEqual([]);
});
test('tests finding words to ignore', () => {
const words = InDoc.getIgnoreWordsFromDocument(sampleCode);
// we match to the end of the line, so the */ is included.
expect(words).toEqual(['tripe', 'comment', '*/', 'tooo', 'faullts']);
expect(InDoc.getIgnoreWordsFromDocument('Hello')).toEqual([]);
});
test('tests finding ignoreRegExp', () => {
const matches = InDoc.getIgnoreRegExpFromDocument(sampleCode);
expect(matches).toEqual(['/\\/\\/\\/.*/', 'w\\w+berry', '/', '\\w+s{4}\\w+', '/faullts[/]?/ */']);
const regExpList = matches.map((s) => Text.stringToRegExp(s));
expect(regExpList).toEqual([/\/\/\/.*/g, /w\w+berry/gimu, /\//gimu, /\w+s{4}\w+/gimu, /faullts[/]?\/ */g]);
const ranges = TextRange.findMatchingRangesForPatterns(regExpList.filter(isDefined), sampleCode);
expect(ranges.length).toBe(39);
});
test('fetching the local for the text', () => {
const settings = InDoc.getInDocumentSettings(sampleCode);
expect(settings.language).toBe('en,nl');
});
test('setting dictionaries for file', () => {
const settings = InDoc.getInDocumentSettings(sampleCode);
expect(settings.dictionaries).toStrictEqual(['lorem-ipsum']);
});
test('bad ignoreRegExp', () => {
// I currently does not check the validity of the expressions.
expect(InDoc.getInDocumentSettings(sampleTextWithBadRegexp)).toEqual(
expect.objectContaining({
ignoreRegExpList: [`"(foobar|foo_baz)"');`],
})
);
});
});
// cspell:disableCompoundWords
// cspell:ignore localwords happydays arehere