-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
suggest-en.test.ts
191 lines (175 loc) · 7.61 KB
/
suggest-en.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
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
import { readTrie } from './dictionaries.test.helper';
import { genCompoundableSuggestions, suggest } from './suggest';
import { suggestionCollector, SuggestionCollectorOptions, SuggestionResult } from './suggestCollector';
import { createTimer } from './timer';
import { CompoundWordsMethod } from './walker';
function getTrie() {
return readTrie('@cspell/dict-en_us/cspell-ext.json');
}
const timeout = 10000;
interface ExpectedSuggestion extends Partial<SuggestionResult> {
word: string;
}
describe('Validate English Suggestions', () => {
interface WordSuggestionsTest {
word: string;
expected: ExpectedSuggestion[];
}
const SEPARATE_WORDS = { compoundMethod: CompoundWordsMethod.SEPARATE_WORDS };
const JOIN_WORDS = { compoundMethod: CompoundWordsMethod.JOIN_WORDS };
const NONE = { compoundMethod: CompoundWordsMethod.NONE };
// cspell:ignore emplode ballence catagory
test.each`
word | expected
${'hello'} | ${sr({ word: 'hello', cost: 0 })}
${'apple'} | ${sr({ word: 'apple', cost: 0 }, { word: 'apples', cost: 94 })}
${'emplode'} | ${sr('implode')}
${'dont'} | ${sr("don't")}
${'ballence'} | ${sr('balance')}
${'catagory'} | ${sr('category')}
`('suggestions for $word', async ({ word, expected }: WordSuggestionsTest) => {
const trie = await getTrie();
const x = suggest(trie.root, word);
expect(x).toEqual(expect.arrayContaining(expected.map((e) => expect.objectContaining(e))));
});
test(
'Tests suggestions "joyful"',
async () => {
const trie = await getTrie();
const collector = suggestionCollector('joyful', opts(8, undefined, 1));
collector.collect(genCompoundableSuggestions(trie.root, collector.word, NONE), timeout);
const results = collector.suggestions;
const suggestions = results.map((s) => s.word);
expect(suggestions).toEqual(expect.arrayContaining(['joyful']));
expect(suggestions[0]).toBe('joyful');
},
timeout
);
test(
'Tests suggestions "joyfull"',
async () => {
const trie = await getTrie();
// cspell:ignore joyfull
const collector = suggestionCollector('joyfull', opts(8));
collector.collect(genCompoundableSuggestions(trie.root, collector.word, SEPARATE_WORDS), timeout);
const results = collector.suggestions;
const suggestions = results.map((s) => s.word);
expect(suggestions).toEqual(expect.arrayContaining(['joyful']));
expect(suggestions[0]).toBe('joyfully');
expect(suggestions[1]).toBe('joyful');
expect(suggestions).toHaveLength(collector.maxNumSuggestions);
},
timeout
);
test(
'Tests compound SEPARATE_WORDS suggestions',
async () => {
const trie = await getTrie();
// cspell:ignore onetwothreefour
const collector = suggestionCollector('onetwothreefour', opts(8, undefined, 3.3));
collector.collect(genCompoundableSuggestions(trie.root, collector.word, SEPARATE_WORDS), timeout);
const results = collector.suggestions;
const suggestions = results.map((s) => s.word);
expect(suggestions).toEqual(expect.arrayContaining(['one two three four']));
expect(suggestions[0]).toBe('one two three four');
expect(suggestions).toHaveLength(collector.maxNumSuggestions);
},
timeout
);
test(
'Tests compound JOIN_WORDS suggestions',
async () => {
const trie = await getTrie();
// cspell:ignore onetwothrefour
const collector = suggestionCollector('onetwothreefour', opts(8, undefined, 3));
collector.collect(genCompoundableSuggestions(trie.root, collector.word, JOIN_WORDS), timeout);
const results = collector.suggestions;
const suggestions = results.map((s) => s.word);
expect(suggestions).toEqual(expect.arrayContaining(['one+two+three+four']));
expect(suggestions).toHaveLength(collector.maxNumSuggestions);
},
timeout
);
test(
'Tests compound suggestions',
async () => {
const trie = await getTrie();
// cspell:ignore onetwothrefour
const collector = suggestionCollector('onetwothreefour', opts(8, undefined, 3));
collector.collect(genCompoundableSuggestions(trie.root, collector.word, JOIN_WORDS), timeout);
const results = collector.suggestions;
const suggestions = results.map((s) => s.word);
expect(suggestions).toEqual(expect.arrayContaining(['one+two+three+four']));
expect(suggestions).toHaveLength(collector.maxNumSuggestions);
},
timeout
);
// Takes a long time.
test(
'Tests long compound suggestions `testscomputesuggestions`',
async () => {
const trie = await getTrie();
// cspell:ignore testscomputesuggestions
const collector = suggestionCollector('testscomputesuggestions', opts(2, undefined, 3, true));
collector.collect(genCompoundableSuggestions(trie.root, collector.word, SEPARATE_WORDS), timeout);
const results = collector.suggestions;
const suggestions = results.map((s) => s.word);
expect(suggestions).toHaveLength(collector.maxNumSuggestions);
expect(suggestions).toEqual(['tests compute suggestions', 'test compute suggestions']);
expect(suggestions[0]).toBe('tests compute suggestions');
},
timeout
);
// Takes a long time.
test(
'Tests long compound suggestions `testscompundsuggestions`',
async () => {
const trie = await getTrie();
// cspell:ignore testscompundsuggestions
const collector = suggestionCollector('testscompundsuggestions', opts(1, undefined, 3));
collector.collect(genCompoundableSuggestions(trie.root, collector.word, SEPARATE_WORDS), timeout);
const results = collector.suggestions;
const suggestions = results.map((s) => s.word);
expect(suggestions).toHaveLength(collector.maxNumSuggestions);
expect(suggestions).toEqual(expect.arrayContaining(['tests compound suggestions']));
expect(suggestions[0]).toBe('tests compound suggestions');
},
timeout
);
test(
'Expensive suggestion `testscompundsuggestions`',
async () => {
const suggestionTimeout = 100;
const trie = await getTrie();
// cspell:ignore testscompundsuggestions
const collector = suggestionCollector('testscompundsuggestions', opts(1, undefined, 3));
const timer = createTimer();
collector.collect(genCompoundableSuggestions(trie.root, collector.word, SEPARATE_WORDS), suggestionTimeout);
const elapsed = timer.elapsed();
expect(elapsed).toBeLessThan(suggestionTimeout * 2);
},
timeout
);
});
function opts(
numSuggestions: number,
filter?: SuggestionCollectorOptions['filter'],
changeLimit?: number,
includeTies?: boolean,
ignoreCase?: boolean
): SuggestionCollectorOptions {
return {
numSuggestions,
filter,
changeLimit,
includeTies,
ignoreCase,
timeout,
};
}
function sr(...sugs: (string | ExpectedSuggestion)[]): ExpectedSuggestion[] {
return sugs.map((s) => {
if (typeof s === 'string') return { word: s };
return s;
});
}