-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
suggestCollector.test.ts
74 lines (67 loc) · 2.7 KB
/
suggestCollector.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
import { suggestionCollector, SuggestionCollectorOptions, SuggestionGenerator } from './suggestCollector';
const defaultOptions: SuggestionCollectorOptions = {
numSuggestions: 10,
ignoreCase: undefined,
changeLimit: undefined,
timeout: undefined,
};
describe('Validate suggestCollector', () => {
test('Tests the collector with filter', () => {
const collector = suggestionCollector(
'joyfully',
sugOpts({ numSuggestions: 3, filter: (word) => word !== 'joyfully' })
);
collector.add({ word: 'joyfully', cost: 100 }).add({ word: 'joyful', cost: 100 });
expect(collector.suggestions).toHaveLength(1);
});
test('Tests the collector with duplicate words of different costs', () => {
const collector = suggestionCollector(
'joyfully',
sugOpts({ numSuggestions: 3, filter: (word) => word !== 'joyfully' })
);
collector.add({ word: 'joyful', cost: 100 });
expect(collector.suggestions.length).toBe(1);
collector.add({ word: 'joyful', cost: 75 });
expect(collector.suggestions.length).toBe(1);
expect(collector.suggestions[0].cost).toBe(75);
collector
.add({ word: 'joyfuller', cost: 200 })
.add({ word: 'joyfullest', cost: 300 })
.add({ word: 'joyfulness', cost: 340 })
.add({ word: 'joyful', cost: 85 });
expect(collector.suggestions.length).toBe(3);
expect(collector.suggestions[0].cost).toBe(75);
});
test('collect', () => {
const spellResults = [
{ word: 'joyfuller', cost: 200 },
{ word: 'joyfullest', cost: 300 },
{ word: 'joy', cost: 500 },
{ word: 'joyfulness', cost: 340 },
{ word: 'joyfuller', cost: 200 },
{ word: 'joy full', cost: 285 },
{ word: 'joyful', cost: 85 },
];
const rValues: (number | undefined | symbol)[] = [];
function* emit(): SuggestionGenerator {
for (const r of spellResults) {
rValues.push(yield r);
}
}
const collector = suggestionCollector(
'joyfully',
sugOpts({ numSuggestions: 3, filter: (word) => word !== 'joyfully' })
);
collector.collect(emit());
expect(collector.suggestions.length).toBe(3);
expect(collector.suggestions[0].cost).toBe(85);
// 285 -> 295 because the weight was adjusted because of the space.
expect(rValues).toEqual([412, 412, 412, 412, 412, 300, 295]);
});
});
function sugOpts(opts: Partial<SuggestionCollectorOptions>): SuggestionCollectorOptions {
return {
...defaultOptions,
...opts,
};
}